Skip to main content

mcp.client

MCP (Model Context Protocol) client and tool manager.

This module provides an HTTP client for communicating with MCP servers and a tool manager for managing multiple MCP servers and their tools.

MCPClient Objects

class MCPClient()

Client for communicating with MCP servers via HTTP.

__init__

def __init__(server_url: str) -> None

Initialize MCP client.

Arguments:

  • server_url - URL of the MCP server

list_tools

async def list_tools() -> list[dict[str, Any]]

List available tools from MCP server.

Returns:

List of tool definitions

call_tool

async def call_tool(tool_name: str, arguments: dict[str, Any]) -> Any

Call a tool on the MCP server.

Arguments:

  • tool_name - Name of the tool to call
  • arguments - Tool arguments

Returns:

Tool execution result

close

async def close() -> None

Close the HTTP client.

MCPToolManager Objects

class MCPToolManager()

Manage MCP tools and servers.

This manager maintains connections to multiple MCP servers and provides unified access to their tools.

__init__

def __init__() -> None

Initialize MCP tool manager.

add_server

def add_server(server: MCPServer) -> None

Add an MCP server.

Arguments:

  • server - MCP server configuration

remove_server

def remove_server(server_id: str) -> None

Remove an MCP server.

Arguments:

  • server_id - ID of the server to remove

update_server

def update_server(server_id: str, server: MCPServer) -> None

Update an MCP server configuration.

Arguments:

  • server_id - ID of the server to update
  • server - New server configuration

get_servers

def get_servers() -> list[MCPServer]

Get all MCP servers.

Returns:

List of MCP server configurations

get_server

def get_server(server_id: str) -> MCPServer | None

Get a specific MCP server by ID.

Arguments:

  • server_id - The server identifier

Returns:

MCPServer if found, None otherwise

get_available_tools

async def get_available_tools() -> list[dict[str, Any]]

Get all available tools from enabled MCP servers.

Returns:

List of tool definitions with server information

register_with_agent

def register_with_agent(agent: Any) -> None

Register MCP tools with a Pydantic AI agent.

Arguments:

  • agent - The Pydantic AI agent

close_all

async def close_all() -> None

Close all MCP clients.