MCPClientManager¶
Manage MCP server connections.
Quick Example¶
from mamba_agents.mcp import MCPClientManager, MCPServerConfig
manager = MCPClientManager()
# Add server
manager.add_server(MCPServerConfig(
name="server1",
transport="stdio",
command="my-server",
))
# Get toolsets for agent
toolsets = manager.as_toolsets()
With Agent¶
from mamba_agents import Agent
from mamba_agents.mcp import MCPClientManager, MCPServerConfig
configs = [
MCPServerConfig(
name="filesystem",
transport="stdio",
command="npx",
args=["-y", "@modelcontextprotocol/server-filesystem", "/project"],
),
]
manager = MCPClientManager(configs)
agent = Agent("gpt-4o", toolsets=manager.as_toolsets())
result = await agent.run("List project files")
Loading from .mcp.json¶
Load configurations from .mcp.json files (Claude Desktop compatible format):
from mamba_agents.mcp import MCPClientManager
# Create manager from file
manager = MCPClientManager.from_mcp_json(".mcp.json")
agent = Agent("gpt-4o", toolsets=manager.as_toolsets())
Merging Multiple Files¶
# Start with project config
manager = MCPClientManager.from_mcp_json("project/.mcp.json")
# Add user defaults
manager.add_from_file("~/.mcp.json")
Combining File and Programmatic Config¶
from mamba_agents.mcp import MCPClientManager, MCPServerConfig
# Load from file
manager = MCPClientManager.from_mcp_json(".mcp.json")
# Add additional server
manager.add_server(MCPServerConfig(
name="custom",
transport="stdio",
command="custom-server",
))
Error Handling¶
from mamba_agents.mcp import (
MCPClientManager,
MCPFileNotFoundError,
MCPFileParseError,
MCPServerValidationError,
)
try:
manager = MCPClientManager.from_mcp_json(".mcp.json")
except MCPFileNotFoundError:
print("Config file not found")
except MCPFileParseError as e:
print(f"Invalid JSON: {e}")
except MCPServerValidationError as e:
print(f"Invalid config: {e}")
API Reference¶
MCPClientManager
¶
MCPClientManager(
configs: list[MCPServerConfig] | None = None,
)
Manages MCP server configurations and creates toolsets for pydantic-ai Agent.
The recommended pattern is to use as_toolsets() to get MCP servers and pass
them directly to the Agent via the toolsets parameter. pydantic-ai handles
the server lifecycle (connection/disconnection) automatically.
Example
configs = [ ... MCPServerConfig( ... name="filesystem", ... transport="stdio", ... command="npx", ... args=["-y", "@modelcontextprotocol/server-filesystem", "/project"], ... tool_prefix="fs", ... ), ... ] manager = MCPClientManager(configs) agent = Agent("gpt-4o", toolsets=manager.as_toolsets()) result = await agent.run("List project files")
Initialize the MCP client manager.
| PARAMETER | DESCRIPTION |
|---|---|
configs
|
Optional list of server configurations.
TYPE:
|
Source code in src/mamba_agents/mcp/client.py
add_server
¶
add_server(config: MCPServerConfig) -> None
Add a server configuration.
| PARAMETER | DESCRIPTION |
|---|---|
config
|
Server configuration to add.
TYPE:
|
as_toolsets
¶
Get MCP servers as toolsets for pydantic-ai Agent.
Creates MCPServer instances from configurations. These servers should
be passed to Agent via the toolsets parameter. pydantic-ai handles
server lifecycle automatically (connection on first use, cleanup on exit).
| RETURNS | DESCRIPTION |
|---|---|
list[MCPServer]
|
List of MCPServer instances to pass to Agent(toolsets=...). |
Example
manager = MCPClientManager(configs) agent = Agent("gpt-4o", toolsets=manager.as_toolsets())
Source code in src/mamba_agents/mcp/client.py
from_mcp_json
classmethod
¶
from_mcp_json(path: str | Path) -> MCPClientManager
Create an MCPClientManager from a .mcp.json file.
Parses the .mcp.json file format (compatible with Claude Desktop) and creates a manager with the loaded configurations.
| PARAMETER | DESCRIPTION |
|---|---|
path
|
Path to the .mcp.json file. Can be a string or Path object. Supports ~ expansion for user home directory.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
MCPClientManager
|
MCPClientManager instance with loaded configurations. |
| RAISES | DESCRIPTION |
|---|---|
MCPFileNotFoundError
|
If the file does not exist. |
MCPFileParseError
|
If the file is not valid JSON. |
MCPServerValidationError
|
If a server entry is invalid. |
Example
manager = MCPClientManager.from_mcp_json(".mcp.json") agent = Agent("gpt-4o", toolsets=manager.as_toolsets())
Source code in src/mamba_agents/mcp/client.py
add_from_file
¶
Add server configurations from a .mcp.json file.
Parses the .mcp.json file and appends configurations to the existing list. Useful for merging multiple configuration sources.
| PARAMETER | DESCRIPTION |
|---|---|
path
|
Path to the .mcp.json file. Can be a string or Path object. Supports ~ expansion for user home directory.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
MCPFileNotFoundError
|
If the file does not exist. |
MCPFileParseError
|
If the file is not valid JSON. |
MCPServerValidationError
|
If a server entry is invalid. |
Example
manager = MCPClientManager(existing_configs) manager.add_from_file("project/.mcp.json") manager.add_from_file("~/.mcp.json") # User defaults
Source code in src/mamba_agents/mcp/client.py
get_server
¶
Get a single MCP server instance by name.
Creates an MCPServer instance from the configuration with the given name.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Name of the server to retrieve.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
MCPServer
|
MCPServer instance. |
| RAISES | DESCRIPTION |
|---|---|
MCPServerNotFoundError
|
If no server with the given name exists. |
Example
manager = MCPClientManager(configs) server = manager.get_server("filesystem")
Source code in src/mamba_agents/mcp/client.py
test_connection
async
¶
Test connection to an MCP server.
Creates a server instance, connects to it, and retrieves the list of available tools to verify the connection is working.
| PARAMETER | DESCRIPTION |
|---|---|
server_name
|
Name of the server to test.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
MCPConnectionResult
|
MCPConnectionResult with connection status and tool information. |
Example
manager = MCPClientManager(configs) result = await manager.test_connection("filesystem") if result.success: ... print(f"Connected! {result.tool_count} tools available")
Source code in src/mamba_agents/mcp/client.py
test_all_connections
async
¶
Test connections to all configured MCP servers.
Tests each server concurrently and returns results for all.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, MCPConnectionResult]
|
Dictionary mapping server names to their connection results. |
Example
manager = MCPClientManager(configs) results = await manager.test_all_connections() for name, result in results.items(): ... status = "OK" if result.success else "FAILED" ... print(f"{name}: {status}")
Source code in src/mamba_agents/mcp/client.py
test_connection_sync
¶
Synchronous wrapper for test_connection.
| PARAMETER | DESCRIPTION |
|---|---|
server_name
|
Name of the server to test.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
MCPConnectionResult
|
MCPConnectionResult with connection status and tool information. |
Example
manager = MCPClientManager(configs) result = manager.test_connection_sync("filesystem") print(f"Success: {result.success}")
Source code in src/mamba_agents/mcp/client.py
test_all_connections_sync
¶
Synchronous wrapper for test_all_connections.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, MCPConnectionResult]
|
Dictionary mapping server names to their connection results. |
Example
manager = MCPClientManager(configs) results = manager.test_all_connections_sync()