McpClient API
The McpClient class is the main client interface for interacting with MCP servers. It provides methods for discovering and calling tools exposed by MCP servers.
Usage Examples
from mojentic_mcp.client import McpClient
from mojentic_mcp.transports import HttpTransport, StdioTransport
# Create transports
http_transport = HttpTransport(url="http://localhost:8000/jsonrpc")
stdio_transport = StdioTransport(command=["python", "stdio_server.py"])
# Create client with multiple transports
with McpClient(transports=[http_transport, stdio_transport]) as client:
# List all tools from all transports
tools = client.list_tools()
for tool in tools:
print(f"Tool: {tool['name']} - {tool.get('description', 'No description')}")
# Call a tool with parameters
result = client.call_tool("some_tool", param="value")
# Using the dynamic accessor
current_time = client.tools.current_datetime()
resolved_date = client.tools.resolve_date(date_string="next Friday")
mojentic_mcp.client.McpClient
A client for interacting with MCP servers via one or more transports.
Source code in src/mojentic_mcp/client.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | |
call_tool(tool_name, **kwargs)
Calls a tool on the appropriate MCP server.
Args: tool_name: The name of the tool to call. **kwargs: Arguments to pass to the tool.
Returns:
The 'result' part of the JSON-RPC response from the tool execution.
Typically this is a dict like {"content": [...], "isError": ...}.
Raises: ValueError: If the tool is not found. McpClientError: For client-specific errors or if the tool execution itself reports an error. McpTransportError: If a transport layer error occurs. JsonRpcError: If the server returns a JSON-RPC protocol error.
Source code in src/mojentic_mcp/client.py
get_tool_schema(tool_name)
Gets the schema for a specific tool based on the 'first-wins' discovery.
Args: tool_name: The name of the tool.
Returns: The tool descriptor (schema) if found, else None.
Source code in src/mojentic_mcp/client.py
list_tools()
Lists all unique tools available from the configured transports.
Returns: A list of tool descriptors (schemas).
shutdown()
Shuts down all managed transports.
Source code in src/mojentic_mcp/client.py
mojentic_mcp.client.ToolAccessor
Provides dynamic attribute access for calling tools via an McpClient.
Source code in src/mojentic_mcp/client.py
mojentic_mcp.client.McpClientError
mojentic_mcp.client.McpToolExecutionError
Bases: McpClientError
Exception raised when a tool call results in an error reported by the tool itself (isError: true).
Source code in src/mojentic_mcp/client.py
Best Practices
-
Use Context Managers: Always use the client with a context manager (
withstatement) to ensure proper cleanup. -
Handle Errors: Always handle errors when calling tools, as they might fail for various reasons.
-
Check Tool Availability: Before calling a tool, check if it's available using
list_toolsorget_tool_schema. -
Prefer Dynamic Accessor: Use the dynamic accessor (
client.tools.<tool_name>()) for a more Pythonic interface.
See Also
- Transports API: Documentation for the transport classes used by the client.
- Client Usage Tutorial: Step-by-step guide to using the client.
- Multi-Transport Client Tutorial: Guide to using the client with multiple transports.