haive.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.
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 haive.mcp.client.protocol.MCPCapability[source]¶
-
Standard MCP capabilities.
Initialize self. See help(type(self)) for accurate signature.
- class haive.mcp.client.protocol.MCPMessage(/, **data)[source]¶
Bases:
pydantic.BaseModelBase MCP message.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- Parameters:
data (Any)
- class haive.mcp.client.protocol.MCPMessageType[source]¶
-
MCP message types.
Initialize self. See help(type(self)) for accurate signature.
- class haive.mcp.client.protocol.MCPMethod[source]¶
-
Standard MCP methods.
Initialize self. See help(type(self)) for accurate signature.
- class haive.mcp.client.protocol.MCPPrompt(/, **data)[source]¶
Bases:
pydantic.BaseModelMCP prompt definition.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- Parameters:
data (Any)
- class haive.mcp.client.protocol.MCPProtocol(transport, timeout=30.0, client_info=None)[source]¶
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)
Initialize MCP protocol.
- Parameters:
- async call_tool(name, arguments=None)[source]¶
Call a tool on the server.
- Parameters:
- Returns:
Tool execution result
- Raises:
MCPCapabilityError – If tools capability not supported
MCPToolError – If tool execution fails
MCPProtocolError – If request fails
- Return type:
Any
- async initialize()[source]¶
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
- Return type:
Dict[str, Any]
- async list_prompts()[source]¶
List available prompts from the server.
- Returns:
List of available prompts
- Raises:
MCPCapabilityError – If prompts capability not supported
- Return type:
List[MCPPrompt]
- async list_resources()[source]¶
List available resources from the server.
- Returns:
List of available resources
- Return type:
List[MCPResource]
- async list_tools()[source]¶
List available tools from the server.
- Returns:
List of available tools
- Raises:
MCPCapabilityError – If tools capability not supported
MCPProtocolError – If request fails
- Return type:
List[MCPTool]
- class haive.mcp.client.protocol.MCPProtocolVersion[source]¶
-
Supported MCP protocol versions.
Initialize self. See help(type(self)) for accurate signature.
- class haive.mcp.client.protocol.MCPResource(/, **data)[source]¶
Bases:
pydantic.BaseModelMCP resource definition.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- Parameters:
data (Any)
- class haive.mcp.client.protocol.MCPTool(/, **data)[source]¶
Bases:
pydantic.BaseModelMCP tool definition.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- Parameters:
data (Any)