mcp.client.protocolΒΆ
MCP Protocol Implementation.
This module implements the Model Context Protocol (MCP) JSON-RPC based communication protocol. It handles the protocol-level details including message framing, request/response matching, and capability negotiation.
The protocol implementation is transport-agnostic and works with any transport that implements the MCPTransport interface.
AttributesΒΆ
ClassesΒΆ
Standard MCP capabilities. |
|
Base MCP message. |
|
MCP message types. |
|
Standard MCP methods. |
|
MCP prompt definition. |
|
MCP protocol implementation. |
|
Supported MCP protocol versions. |
|
MCP resource definition. |
|
MCP tool definition. |
Module ContentsΒΆ
- class mcp.client.protocol.MCPCapabilityΒΆ
-
Standard MCP capabilities.
- LOGGING = 'logging'ΒΆ
- PROMPTS = 'prompts'ΒΆ
- RESOURCES = 'resources'ΒΆ
- SAMPLING = 'sampling'ΒΆ
- TOOLS = 'tools'ΒΆ
- class mcp.client.protocol.MCPMessage(/, **data: Any)ΒΆ
Bases:
pydantic.BaseModel
Base MCP message.
- class mcp.client.protocol.MCPMessageTypeΒΆ
-
MCP message types.
- NOTIFICATION = 'notification'ΒΆ
- REQUEST = 'request'ΒΆ
- RESPONSE = 'response'ΒΆ
- class mcp.client.protocol.MCPMethodΒΆ
-
Standard MCP methods.
- CANCELLED = 'cancelled'ΒΆ
- INITIALIZE = 'initialize'ΒΆ
- INITIALIZED = 'initialized'ΒΆ
- LOGGING_SET_LEVEL = 'logging/setLevel'ΒΆ
- PROGRESS = 'progress'ΒΆ
- PROMPTS_GET = 'prompts/get'ΒΆ
- PROMPTS_LIST = 'prompts/list'ΒΆ
- PROMPT_LIST_CHANGED = 'prompts/list_changed'ΒΆ
- RESOURCES_LIST = 'resources/list'ΒΆ
- RESOURCES_READ = 'resources/read'ΒΆ
- RESOURCES_SUBSCRIBE = 'resources/subscribe'ΒΆ
- RESOURCES_UNSUBSCRIBE = 'resources/unsubscribe'ΒΆ
- RESOURCE_LIST_CHANGED = 'resources/list_changed'ΒΆ
- RESOURCE_UPDATED = 'resources/updated'ΒΆ
- TOOLS_CALL = 'tools/call'ΒΆ
- TOOLS_LIST = 'tools/list'ΒΆ
- TOOL_LIST_CHANGED = 'tools/list_changed'ΒΆ
- class mcp.client.protocol.MCPPrompt(/, **data: Any)ΒΆ
Bases:
pydantic.BaseModel
MCP prompt definition.
- class mcp.client.protocol.MCPProtocol(transport, timeout: float = 30.0, client_info: Dict[str, Any] | None = None)ΒΆ
MCP protocol implementation.
- This class handles the MCP protocol layer, including:
Message serialization/deserialization
Request/response matching
Capability negotiation
Protocol state management
Error handling
The protocol is transport-agnostic and works with any MCPTransport implementation. It provides a clean async API for MCP operations.
Examples
Basic protocol usage:
from haive.mcp.client.transport import StdioTransport from haive.mcp.client.protocol import MCPProtocol transport = StdioTransport("npx", ["-y", "@modelcontextprotocol/server-filesystem"]) protocol = MCPProtocol(transport) await protocol.initialize() tools = await protocol.list_tools() result = await protocol.call_tool("read_file", {"path": "/etc/hosts"}) await protocol.shutdown()
With context manager:
async with MCPProtocol(transport) as protocol: tools = await protocol.list_tools() result = await protocol.call_tool("tool_name", args)
- async __aenter__()ΒΆ
Async context manager entry.
- async __aexit__(exc_type, exc_val, exc_tb)ΒΆ
Async context manager exit.
- add_notification_handler(method: str, handler: Callable[[Dict[str, Any]], Awaitable[None]]) None ΒΆ
Add a handler for notifications.
- Parameters:
method β Notification method name
handler β Async handler function
- async call_tool(name: str, arguments: Dict[str, Any] | None = None) Any ΒΆ
Call a tool on the server.
- Parameters:
name β Tool name to call
arguments β Tool arguments
- Returns:
Tool execution result
- Raises:
MCPCapabilityError β If tools capability not supported
MCPToolError β If tool execution fails
MCPProtocolError β If request fails
- async get_prompt(name: str, arguments: Dict[str, Any] | None = None) Dict[str, Any] ΒΆ
Get a prompt from the server.
- Parameters:
name β Prompt name
arguments β Prompt arguments
- Returns:
Prompt content and metadata
- async initialize() Dict[str, Any] ΒΆ
Initialize the MCP connection.
This performs the MCP initialization handshake, including capability negotiation and protocol version agreement.
- Returns:
Server information and capabilities
- Raises:
MCPProtocolError β If initialization fails
MCPCapabilityError β If capabilities are incompatible
- async list_prompts() List[MCPPrompt] ΒΆ
List available prompts from the server.
- Returns:
List of available prompts
- Raises:
MCPCapabilityError β If prompts capability not supported
- async list_resources() List[MCPResource] ΒΆ
List available resources from the server.
- Returns:
List of available resources
- async list_tools() List[MCPTool] ΒΆ
List available tools from the server.
- Returns:
List of available tools
- Raises:
MCPCapabilityError β If tools capability not supported
MCPProtocolError β If request fails
- async read_resource(uri: str) Dict[str, Any] ΒΆ
Read a resource from the server.
- Parameters:
uri β Resource URI to read
- Returns:
Resource content and metadata
- remove_notification_handler(method: str, handler: Callable[[Dict[str, Any]], Awaitable[None]]) None ΒΆ
Remove a notification handler.
- Parameters:
method β Notification method name
handler β Handler function to remove
- client_infoΒΆ
- initialized = FalseΒΆ
- protocol_version: MCPProtocolVersion | None = NoneΒΆ
- server_capabilities: Set[MCPCapability]ΒΆ
- timeout = 30.0ΒΆ
- transportΒΆ
- class mcp.client.protocol.MCPProtocolVersionΒΆ
-
Supported MCP protocol versions.
- V0_9 = '0.9'ΒΆ
- V1_0 = '1.0'ΒΆ
- class mcp.client.protocol.MCPResource(/, **data: Any)ΒΆ
Bases:
pydantic.BaseModel
MCP resource definition.
- class mcp.client.protocol.MCPTool(/, **data: Any)ΒΆ
Bases:
pydantic.BaseModel
MCP tool definition.
- mcp.client.protocol.loggerΒΆ