JSON-RPC Handler API
The Mojentic MCP library provides a JSON-RPC 2.0 handler for processing MCP requests and responses.
mojentic_mcp.rpc.JsonRpcErrorCode
mojentic_mcp.rpc.JsonRpcRequest
Bases: BaseModel
JSON-RPC 2.0 request model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
jsonrpc
|
str
|
JSON-RPC version |
'2.0'
|
id
|
Any | None
|
Request ID |
None
|
method
|
str
|
Method name |
required |
params
|
Dict[str, Any] | None
|
Method parameters |
None
|
Source code in src/mojentic_mcp/rpc.py
mojentic_mcp.rpc.JsonRpcError
Bases: Exception
Exception raised for JSON-RPC errors.
Source code in src/mojentic_mcp/rpc.py
__init__(code, message, data=None)
Initialize a JSON-RPC error.
Args: code (int): The error code message (str): The error message data (Any, optional): Additional error data. Defaults to None.
Source code in src/mojentic_mcp/rpc.py
mojentic_mcp.rpc.JsonRpcHandler
Base class for handling JSON-RPC 2.0 requests.
Source code in src/mojentic_mcp/rpc.py
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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | |
__init__(tools)
Initialize the JSON-RPC handler.
Args: tools (List[LLMTool]): List of tools to serve.
Source code in src/mojentic_mcp/rpc.py
handle_request(request)
Handle a JSON-RPC 2.0 request.
Args: request (JsonRpcRequest): The JSON-RPC request
Returns: Dict[str, Any]: The JSON-RPC response
Source code in src/mojentic_mcp/rpc.py
JSON-RPC 2.0 Protocol
The JSON-RPC 2.0 protocol is a stateless, light-weight remote procedure call (RPC) protocol. It defines several data structures and the rules around their processing.
Request Object
A request object has the following members:
jsonrpc: A string specifying the version of the JSON-RPC protocol. Must be exactly "2.0".method: A string containing the name of the method to be invoked.params: An object or array that holds the parameter values to be used during the invocation of the method. This member may be omitted.id: A string or number that identifies this request. If omitted, the request is considered a notification.
Example:
Response Object
A response object has the following members:
jsonrpc: A string specifying the version of the JSON-RPC protocol. Must be exactly "2.0".result: The result of the method invocation. This member is required on success.error: An error object if there was an error invoking the method. This member is required on error.id: The id of the request this response is for. If there was an error in detecting the id in the request, it must be null.
Example (success):
{
"jsonrpc": "2.0",
"result": {
"tools": [
{
"name": "get_time",
"description": "Get the current time",
"inputSchema": {
"type": "object",
"properties": {}
}
}
],
"nextCursor": null
},
"id": "request-id"
}
Example (error):
{
"jsonrpc": "2.0",
"error": {
"code": -32601,
"message": "Method not found"
},
"id": "request-id"
}
Error Object
An error object has the following members:
code: A number that indicates the error type that occurred.message: A string providing a short description of the error.data: A primitive or structured value that contains additional information about the error. This may be omitted.
Standard Error Codes
The JSON-RPC 2.0 specification defines several standard error codes:
-32700: Parse error - Invalid JSON was received by the server.-32600: Invalid Request - The JSON sent is not a valid Request object.-32601: Method not found - The method does not exist / is not available.-32602: Invalid params - Invalid method parameter(s).-32603: Internal error - Internal JSON-RPC error.-32000to-32099: Server error - Reserved for implementation-defined server-errors.
See Also
- HTTP Server API: Documentation for the HTTP server implementation.
- STDIO Server API: Documentation for the STDIO server implementation.
- Transports API: Documentation for the transport classes.