Transports API
The Mojentic MCP library provides transport classes for communicating with MCP servers using different protocols. These transport classes implement the McpTransport interface.
mojentic_mcp.transports.McpTransport
Bases: ABC
Abstract base class for MCP transports.
Source code in src/mojentic_mcp/transports.py
initialize()
send_request(rpc_request)
abstractmethod
Sends a JSON-RPC request and returns the response.
Args: rpc_request: The JSON-RPC request object.
Returns: The JSON-RPC response as a dictionary.
Raises: McpTransportError: If a transport-level error occurs. JsonRpcError: If the server returns a JSON-RPC error.
Source code in src/mojentic_mcp/transports.py
mojentic_mcp.transports.HttpTransport
Bases: McpTransport
MCP Transport using HTTP.
Source code in src/mojentic_mcp/transports.py
__init__(url=None, host=None, port=None, path='/jsonrpc', timeout=30.0, http_gateway=None)
Initialize the HTTP transport.
Args: url (str, optional): The full URL to the JSON-RPC endpoint. If provided, this takes precedence over host, port, and path. host (str, optional): The host to connect to. Required if url is not provided. port (int, optional): The port to connect to. Required if url is not provided. path (str, optional): The path to the JSON-RPC endpoint. Defaults to "/jsonrpc". timeout (float, optional): The timeout for HTTP requests in seconds. Defaults to 30.0. http_gateway (HttpGateway, optional): The HTTP gateway to use. If not provided, a new one will be created.
Raises: ValueError: If neither url nor both host and port are provided.
Source code in src/mojentic_mcp/transports.py
mojentic_mcp.transports.StdioTransport
Bases: McpTransport
MCP Transport using STDIO with a subprocess.
Source code in src/mojentic_mcp/transports.py
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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | |
__init__(command, stdio_gateway=None)
Initialize the STDIO transport.
Args: command (List[str]): The command to run. stdio_gateway (StdioGateway, optional): The STDIO gateway to use. If not provided, a new one will be created.
Source code in src/mojentic_mcp/transports.py
mojentic_mcp.transports.McpTransportError
Usage Examples
HTTP Transport
from mojentic_mcp.transports import HttpTransport
from mojentic_mcp.rpc import JsonRpcRequest
# Create an HTTP transport with a full URL
http_transport = HttpTransport(url="http://localhost:8000/jsonrpc")
# Create an HTTP transport with host and port (using default path "/jsonrpc")
http_transport_host_port = HttpTransport(host="localhost", port=8000)
# Create an HTTP transport with host, port, and custom path
http_transport_custom_path = HttpTransport(host="localhost", port=8000, path="/custom-path")
# Create an HTTP transport with a custom timeout
http_transport_with_timeout = HttpTransport(url="http://localhost:8000/jsonrpc", timeout=60.0)
# Use the transport as a context manager
with HttpTransport(url="http://localhost:8000/jsonrpc") as transport:
# Create a JSON-RPC request
rpc_request = JsonRpcRequest(method="tools/list", params={})
# Send the request and get the response
response = transport.send_request(rpc_request)
STDIO Transport
import sys
from mojentic_mcp.transports import StdioTransport
from mojentic_mcp.rpc import JsonRpcRequest
# Create a STDIO transport that runs a Python script
stdio_transport = StdioTransport(command=[sys.executable, "stdio_server.py"])
# Create a STDIO transport that runs a compiled executable
stdio_transport_executable = StdioTransport(command=["./mcp_server"])
# Use the transport as a context manager
with StdioTransport(command=[sys.executable, "stdio_server.py"]) as transport:
# Create a JSON-RPC request
rpc_request = JsonRpcRequest(method="tools/list", params={})
# Send the request and get the response
response = transport.send_request(rpc_request)
Best Practices
-
Use Context Managers: Always use transports with a context manager (
withstatement) to ensure proper initialization and cleanup. -
Handle Errors: Always handle
McpTransportErrorexceptions when sending requests. -
Choose the Right Transport: Use
HttpTransportfor communicating with HTTP servers andStdioTransportfor communicating with STDIO servers. -
Set Appropriate Timeouts: For
HttpTransport, set an appropriate timeout based on the expected response time of the server.
See Also
- McpClient API: Documentation for the client class that uses transports.
- Client Usage Tutorial: Step-by-step guide to using the client with different transports.
- Multi-Transport Client Tutorial: Guide to using multiple transports with a single client.