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
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_all_mcp_servers
async
¶
connect_all_mcp_servers(
exit_stack: AsyncExitStack,
mcp_servers: dict[str, MCPServerConfig],
) -> dict[str, ClientSession]
Connect to all configured MCP servers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
AsyncExitStack
|
AsyncExitStack for managing session lifecycles. |
required |
|
dict[str, MCPServerConfig]
|
Dict mapping server names to their configurations. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, ClientSession]
|
Dict mapping server names to initialized ClientSession objects. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any server config is invalid. |
ImportError
|
If mcp package is not installed. |
Source code in src/karenina/utils/mcp/client.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. |
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
get_all_mcp_tools
async
¶
get_all_mcp_tools(
sessions: dict[str, Any],
) -> list[tuple[str, Any, Any]]
Get all tools from connected MCP sessions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict[str, Any]
|
Dict mapping server names to ClientSession objects. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[str, Any, Any]]
|
List of tuples (server_name, session, mcp_tool) for each tool. |