# Claude Agent SDK Compatibility

K2 is compatible with Claude Agent SDK through MCP. The SDK owns the agent loop,
permissions, sessions, hooks, and tool execution. K2 exposes a single read-only
MCP tool that returns a cited evaluation plan before any adversarial generator
creates prompts or runs tests.

## What Runs Where

| Layer | Owner |
| --- | --- |
| Claude Agent SDK agent loop | Customer or partner application |
| MCP client configuration | Customer or partner application |
| `get_evaluation_plan` MCP tool | K2 context layer |
| Adversarial generation, execution, scoring | Customer's existing red-team stack |

## Stdio MCP Configuration

From the repo root:

```json
{
  "mcpServers": {
    "k2-adversarial-context": {
      "command": "python",
      "args": ["scripts/k2_adversarial_mcp_server.py"],
      "env": {
        "K2_ADV_BUNDLE_DIR": "docs/customer-demos/demo-adversarial-context/k2-assets"
      }
    }
  }
}
```

For a live K2-backed server, keep the same MCP tool name and pass credentials
through environment variables or remote MCP headers. Do not put tokens in the
repository.

## TypeScript Agent SDK Example

```ts
import { query } from "@anthropic-ai/claude-agent-sdk";

for await (const message of query({
  prompt:
    "Get a cited adversarial evaluation plan for SupportBot v2.3. Do not generate adversarial inputs.",
  options: {
    mcpServers: {
      "k2-adversarial-context": {
        command: "python",
        args: ["scripts/k2_adversarial_mcp_server.py"],
        env: {
          K2_ADV_BUNDLE_DIR:
            "docs/customer-demos/demo-adversarial-context/k2-assets"
        }
      }
    },
    allowedTools: [
      "mcp__k2-adversarial-context__get_evaluation_plan"
    ]
  }
})) {
  if (message.type === "system" && message.subtype === "init") {
    console.log(message.mcp_servers);
  }
  if (message.type === "result" && message.subtype === "success") {
    console.log(message.result);
  }
}
```

## Python Agent SDK Example

```python
import asyncio
from claude_agent_sdk import ClaudeAgentOptions, query


async def main() -> None:
    async for message in query(
        prompt=(
            "Get a cited adversarial evaluation plan for SupportBot v2.3. "
            "Do not generate adversarial inputs."
        ),
        options=ClaudeAgentOptions(
            mcp_servers={
                "k2-adversarial-context": {
                    "command": "python",
                    "args": ["scripts/k2_adversarial_mcp_server.py"],
                    "env": {
                        "K2_ADV_BUNDLE_DIR": (
                            "docs/customer-demos/demo-adversarial-context/k2-assets"
                        )
                    },
                }
            },
            allowed_tools=["mcp__k2-adversarial-context__get_evaluation_plan"],
        ),
    ):
        print(message)


asyncio.run(main())
```

## Direct MCP Smoke Test

The server is a JSON-RPC stdio MCP server. This direct smoke test verifies the
tool without requiring a Claude API key:

```bash
printf '%s\n' \
  '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"smoke","version":"0"}}}' \
  '{"jsonrpc":"2.0","method":"notifications/initialized"}' \
  '{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}' \
  '{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"get_evaluation_plan","arguments":{"target_id":"target-supportbot-v2.3","modalities":["text","image"],"environment":"staging"}}}' \
  | python scripts/k2_adversarial_mcp_server.py
```

Expected result:

- `tools/list` includes `get_evaluation_plan`.
- `tools/call` returns `structuredContent.schema =
  k2.adversarial_context.evaluation_plan.v1`.
- Each plan entry includes threat, policy, and target lineage.

## Compatibility Boundary

Claude Agent SDK can call K2 over MCP. K2 does not become the Claude agent, does
not control Claude permissions or sessions, and does not generate adversarial
inputs. It supplies scoped, current, traceable context to the agent or generator
before evaluation begins.
