karenina.utils.mcp¶
mcp
¶
Shared MCP utilities for Karenina.
This module provides adapter-agnostic MCP utilities using the core mcp package.
For adapter-specific MCP functionality:
- LangChain tools: karenina.adapters.langchain.mcp
- Claude SDK tools: karenina.adapters.claude_tool.mcp
The multi-server helpers (connect_all_mcp_servers, get_all_mcp_tools) that used to live here were unused duplicates of the live copies in karenina.adapters.claude_tool.mcp and were removed. Use those instead.
Classes¶
ManagedMcpServer
dataclass
¶
A running managed MCP server.
Source code in src/karenina/utils/mcp/server.py
McpServerSpec
dataclass
¶
Configuration for one managed local MCP server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Human-readable server name used in logs and errors. |
required |
|
tuple[str, ...]
|
Executable and arguments used to start the server. |
required |
|
str
|
Host used for the readiness check and returned URL. |
'127.0.0.1'
|
|
int
|
TCP port used for the readiness check and returned URL. |
8000
|
|
str
|
HTTP path appended to the returned URL. |
'/mcp'
|
|
Mapping[str, str]
|
Environment additions passed to the child process. |
dict()
|
|
Path | None
|
Optional child working directory. |
None
|
|
Path | None
|
Optional file receiving combined standard output and error. |
None
|
|
float
|
Seconds to wait for the TCP port to accept connections. |
60.0
|
|
float
|
Seconds to wait after termination before forcing kill. |
10.0
|
|
float
|
Seconds between readiness checks. |
0.1
|
Source code in src/karenina/utils/mcp/server.py
Functions¶
afetch_tool_descriptions
async
¶
afetch_tool_descriptions(
mcp_urls_dict: dict[str, str],
tool_filter: list[str] | None = None,
) -> dict[str, str]
Fetch tool descriptions from MCP servers using the core mcp package.
This function connects to MCP servers and retrieves the descriptions for all available tools. Useful for getting seed descriptions for GEPA optimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict[str, str]
|
Dictionary mapping server names to MCP server URLs |
required |
|
list[str] | None
|
Optional list of tool names to include |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dict mapping tool names to their descriptions |
Example
descriptions = await fetch_tool_descriptions( ... {"biocontext": "https://mcp.biocontext.ai/mcp/"} ... ) print(descriptions) {'search_proteins': 'Search for protein information...', ...}
Source code in src/karenina/utils/mcp/tools.py
apply_tool_description_overrides
¶
Apply description overrides to tools.
Used by GEPA optimization to test different tool descriptions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
list[Any]
|
List of tool objects (LangChain Tool or MCP Tool) |
required |
|
dict[str, str]
|
Dict mapping tool names to new descriptions |
required |
Returns:
| Type | Description |
|---|---|
list[Any]
|
Modified tools list with updated descriptions |
Source code in src/karenina/utils/mcp/tools.py
connect_mcp_session
async
¶
connect_mcp_session(
exit_stack: AsyncExitStack, config: MCPServerConfig
) -> ClientSession
Connect to an MCP server via HTTP/SSE transport.
Creates a new MCP client session and registers it with the exit stack for automatic cleanup when the stack closes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
AsyncExitStack
|
AsyncExitStack for managing session lifecycle. |
required |
|
MCPServerConfig
|
MCP server configuration. Must include 'url' for HTTP/SSE transport. Optional 'headers' dict for authentication. Optional 'timeout' and 'sse_read_timeout' floats (seconds) override the MCP SDK defaults. |
required |
Returns:
| Type | Description |
|---|---|
ClientSession
|
Initialized ClientSession ready for tool calls. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If config doesn't include required 'url' field. |
ImportError
|
If mcp package is not installed. |
Example
async with AsyncExitStack() as stack: ... config = {"url": "https://mcp.example.com/mcp", "type": "http"} ... session = await connect_mcp_session(stack, config) ... tools = await session.list_tools()
Source code in src/karenina/utils/mcp/client.py
fetch_tool_descriptions
¶
fetch_tool_descriptions(
mcp_urls_dict: dict[str, str],
tool_filter: list[str] | None = None,
) -> dict[str, str]
Synchronous wrapper for afetch_tool_descriptions.
Uses the same async handling pattern as other sync wrappers in karenina, supporting both the shared BlockingPortal (when available from parallel verification) and fallback to asyncio.run().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict[str, str]
|
Dictionary mapping server names to MCP server URLs |
required |
|
list[str] | None
|
Optional list of tool names to include |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dict mapping tool names to their descriptions |
Source code in src/karenina/utils/mcp/tools.py
managed_mcp_server
¶
managed_mcp_server(
spec: McpServerSpec,
) -> Iterator[ManagedMcpServer]
Start one local MCP server and tear it down on context exit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
McpServerSpec
|
Server command, address, timing, and logging configuration. |
required |
Yields:
| Type | Description |
|---|---|
ManagedMcpServer
|
The running server and its URL. |
Raises:
| Type | Description |
|---|---|
McpServerError
|
If the process cannot be started, become ready, or stop. |
Source code in src/karenina/utils/mcp/server.py
managed_mcp_servers
¶
managed_mcp_servers(
specs: Sequence[McpServerSpec],
) -> Iterator[list[ManagedMcpServer]]
Start multiple local MCP servers and tear them down in reverse order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Sequence[McpServerSpec]
|
Server specifications in startup order. |
required |
Yields:
| Type | Description |
|---|---|
list[ManagedMcpServer]
|
Running servers in the same order as |