karenina.adapters.claude_agent_sdk.mcp¶
mcp
¶
MCP configuration converter for Claude Agent SDK.
Converts karenina's MCP config format to Claude Agent SDK format.
Karenina uses a simple dict mapping server names to URLs or commands
{"biocontext": "https://mcp.biocontext.ai/mcp/"}
The Claude Agent SDK uses a more structured format with explicit types
{ "biocontext": { "type": "http", "url": "https://mcp.biocontext.ai/mcp/", "headers": {...} } }
IMPORTANT: Karenina's streamable_http transport maps to SDK's type="http",
NOT "sse". Both are URL-based but use different protocols.
Transport Mapping
| Karenina | SDK Type | Notes |
|---|---|---|
| HTTP URL | type="http" | Streamable HTTP transport |
| stdio | command + args | Standard subprocess |
Example
from karenina.adapters.claude_agent_sdk.mcp import convert_mcp_config
Convert karenina MCP config¶
karenina_config = { ... "biocontext": "https://mcp.biocontext.ai/mcp/", ... "github": "npx -y @modelcontextprotocol/server-github", ... "local": "/path/to/mcp-server" ... } sdk_config = convert_mcp_config(karenina_config) print(sdk_config) { "biocontext": {"type": "http", "url": "https://mcp.biocontext.ai/mcp/"}, "github": {"command": "npx", "args": ["-y", "@modelcontextprotocol/server-github"]}, "local": {"command": "/path/to/mcp-server"} }
Classes¶
McpConfigValidationError
¶
Bases: McpError
Raised when MCP configuration is invalid.
Source code in src/karenina/adapters/claude_agent_sdk/mcp.py
McpHttpServerConfig
¶
Bases: TypedDict
SDK configuration for HTTP/Streamable HTTP MCP servers.
Source code in src/karenina/adapters/claude_agent_sdk/mcp.py
McpStdioServerConfig
¶
Functions¶
convert_and_validate_mcp_config
¶
convert_and_validate_mcp_config(
mcp_urls_dict: dict[str, str],
auth_headers: dict[str, str] | None = None,
) -> dict[str, McpServerConfig]
Convert and validate karenina MCP config to SDK format.
Combines convert_mcp_config() and validate_mcp_config() for convenience. Raises McpConfigValidationError if validation fails.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict[str, str]
|
Dictionary mapping server names to URLs or commands. |
required |
|
dict[str, str] | None
|
Optional headers to include with HTTP requests. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, McpServerConfig]
|
Validated SDK-formatted MCP server configuration. |
Raises:
| Type | Description |
|---|---|
McpConfigValidationError
|
If the converted config is invalid. |
Examples:
>>> config = convert_and_validate_mcp_config({
... "api": "https://mcp.example.com/mcp/"
... })
>>> # Returns validated config
Source code in src/karenina/adapters/claude_agent_sdk/mcp.py
convert_mcp_config
¶
convert_mcp_config(
mcp_urls_dict: dict[str, str],
auth_headers: dict[str, str] | None = None,
) -> dict[str, McpServerConfig]
Convert karenina MCP URLs to Claude Agent SDK format.
Karenina uses a simplified format where servers are specified as either: - HTTP URLs (e.g., "https://mcp.example.com/mcp/") - Commands with args (e.g., "npx -y @modelcontextprotocol/server-github") - Simple paths/commands (e.g., "/path/to/mcp-server")
The Claude Agent SDK requires explicit type configuration: - HTTP URLs -> {"type": "http", "url": ..., "headers": ...} - Commands -> {"command": ..., "args": [...]}
IMPORTANT: Karenina's streamable_http transport maps to SDK's type="http", NOT "sse". This is a critical distinction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict[str, str]
|
Dictionary mapping server names to URLs or commands. Keys are server names (e.g., "biocontext"). Values are either: - HTTP URLs: "https://mcp.example.com/mcp/" - Commands with args: "npx -y @mcp/server" - Simple commands/paths: "/path/to/server" |
required |
|
dict[str, str] | None
|
Optional headers to include with HTTP requests. Applied to all HTTP servers in the config. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, McpServerConfig]
|
Dictionary mapping server names to SDK-formatted configs. |
dict[str, McpServerConfig]
|
Each config is one of: |
dict[str, McpServerConfig]
|
|
dict[str, McpServerConfig]
|
|
Examples:
>>> # HTTP URL
>>> convert_mcp_config({"api": "https://mcp.example.com/mcp/"})
{"api": {"type": "http", "url": "https://mcp.example.com/mcp/"}}
>>> # HTTP URL with auth
>>> convert_mcp_config(
... {"api": "https://mcp.example.com/mcp/"},
... auth_headers={"Authorization": "Bearer token123"}
... )
{"api": {"type": "http", "url": "https://mcp.example.com/mcp/", "headers": {"Authorization": "Bearer token123"}}}
>>> # Command with args
>>> convert_mcp_config({"github": "npx -y @mcp/server-github"})
{"github": {"command": "npx", "args": ["-y", "@mcp/server-github"]}}
>>> # Simple path
>>> convert_mcp_config({"local": "/usr/local/bin/mcp-server"})
{"local": {"command": "/usr/local/bin/mcp-server"}}
Source code in src/karenina/adapters/claude_agent_sdk/mcp.py
validate_mcp_config
¶
validate_mcp_config(
config: dict[str, McpServerConfig],
) -> list[str]
Validate an SDK MCP configuration for correctness.
Checks that each server config has the required fields based on its type: - HTTP configs: Must have "type" == "http" and "url" - stdio configs: Must have "command"
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict[str, McpServerConfig]
|
SDK-formatted MCP server configuration dict. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of validation error messages. Empty list if config is valid. |
Examples:
>>> errors = validate_mcp_config({
... "api": {"type": "http", "url": "https://example.com/mcp/"},
... "local": {"command": "/path/to/server"}
... })
>>> print(errors)
[]
>>> errors = validate_mcp_config({
... "bad": {"type": "http"} # Missing url
... })
>>> print(errors)
["Server 'bad': HTTP config missing 'url' field"]
Source code in src/karenina/adapters/claude_agent_sdk/mcp.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | |