Skip to content

Anthropic tool-format adapter & middleware #50

@dgenio

Description

@dgenio

Parent: #40 | Split: Anthropic adapter

Problem

This is part 2 of #40. Developers using Anthropic's API must manually translate between Capability objects and Anthropic's {name, description, input_schema} tool schema with cache_control support.

Proposed Change

1. Schema adapter (src/agent_kernel/adapters/anthropic.py)

def capabilities_to_tools(caps: list[Capability]) -> list[dict]:
    """Convert Capabilities to Anthropic tool definitions."""

def tool_use_to_request(tool_use_block: dict) -> CapabilityRequest:
    """Convert an Anthropic tool_use block to a CapabilityRequest."""

def format_result(result: Any, tool_use_id: str) -> dict:
    """Format kernel invoke() result as Anthropic tool_result."""

2. Middleware class

class AnthropicMiddleware:
    def __init__(self, kernel: Kernel, principal: Principal): ...
    def get_tools(self) -> list[dict]: ...
    async def handle_tool_uses(self, content_blocks: list[dict]) -> list[dict]: ...
    def intercept_tool_call(self, callback: Callable) -> None: ...
    def intercept_tool_result(self, callback: Callable) -> None: ...

3. No SDK dependency

Adapters work with plain dicts — no dependency on anthropic package.

Acceptance Criteria

  • Round-trip: Capability → Anthropic schema → tool_useCapabilityRequest preserves all fields
  • AnthropicMiddleware.handle_tool_uses() processes through full kernel pipeline
  • cache_control support in tool definitions
  • Hooks execute in correct order
  • Schema matches current Anthropic API spec
  • No runtime dependency on anthropic package

Affected Files

  • src/agent_kernel/adapters/anthropic.py (new)
  • tests/test_adapters.py (extend)
  • docs/integrations.md (usage examples)

Dependencies

  • Can be developed independently or in parallel with the OpenAI adapter

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions