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ΒΆ

MCPCapability

Standard MCP capabilities.

MCPMessage

Base MCP message.

MCPMessageType

MCP message types.

MCPMethod

Standard MCP methods.

MCPPrompt

MCP prompt definition.

MCPProtocol

MCP protocol implementation.

MCPProtocolVersion

Supported MCP protocol versions.

MCPResource

MCP resource definition.

MCPTool

MCP tool definition.

Module ContentsΒΆ

class mcp.client.protocol.MCPCapabilityΒΆ

Bases: str, enum.Enum

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.

error: Dict[str, Any] | None = NoneΒΆ
id: str | int | None = NoneΒΆ
jsonrpc: str = NoneΒΆ
method: str | None = NoneΒΆ
params: Dict[str, Any] | None = NoneΒΆ
result: Any | None = NoneΒΆ
class mcp.client.protocol.MCPMessageTypeΒΆ

Bases: str, enum.Enum

MCP message types.

NOTIFICATION = 'notification'ΒΆ
REQUEST = 'request'ΒΆ
RESPONSE = 'response'ΒΆ
class mcp.client.protocol.MCPMethodΒΆ

Bases: str, enum.Enum

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.

arguments: List[Dict[str, Any]] | None = NoneΒΆ
description: str = NoneΒΆ
name: str = NoneΒΆ
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:
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:
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:
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

async shutdown() NoneΒΆ

Shutdown the MCP connection gracefully.

client_infoΒΆ
initialized = FalseΒΆ
protocol_version: MCPProtocolVersion | None = NoneΒΆ
server_capabilities: Set[MCPCapability]ΒΆ
server_info: Dict[str, Any] | None = NoneΒΆ
timeout = 30.0ΒΆ
transportΒΆ
class mcp.client.protocol.MCPProtocolVersionΒΆ

Bases: str, enum.Enum

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.

description: str | None = NoneΒΆ
mimeType: str | None = NoneΒΆ
name: str = NoneΒΆ
uri: str = NoneΒΆ
class mcp.client.protocol.MCPTool(/, **data: Any)ΒΆ

Bases: pydantic.BaseModel

MCP tool definition.

description: str = NoneΒΆ
inputSchema: Dict[str, Any] = NoneΒΆ
name: str = NoneΒΆ
mcp.client.protocol.loggerΒΆ