MCP is built on JSON-RPC 2.0 for bidirectional communication between Claude and servers...
Keywords: mcp, model context protocol, json-rpc, protocol, specification
File Patterns: **/mcp_server.py, **/protocol.py
Modes: backend_python, api_development
MCP is built on JSON-RPC 2.0 for bidirectional communication between Claude and servers.
Request Structure:
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "search",
"arguments": {"query": "test"}
},
"id": 1
}
Response Structure:
{
"jsonrpc": "2.0",
"result": {"data": "..."},
"id": 1
}
Error Response:
{
"jsonrpc": "2.0",
"error": {
"code": -32601,
"message": "Method not found"
},
"id": 1
}
Tools (Model-Controlled):
Resources (App-Controlled):
Prompts (Optional):
Server ā Client (Server exposes):
tools/list ā List available tools
tools/call ā Execute a tool
resources/list ā List available resources
resources/read ā Read resource content
prompts/list ā List prompt templates
prompts/get ā Get prompt with arguments
Client ā Server (Server can request):
sampling/createMessage ā Request Claude to generate text
roots/list ā Get workspace roots
Claude Desktop MCP Server
| |
|---tools/list-------->|
|<--[tool schemas]-----|
| |
|---tools/call-------->|
| {name, arguments} |
|<--result/error-------|
CRITICAL: Use Pydantic strict mode for all schemas.
from pydantic import BaseModel, ConfigDict
class ToolInput(BaseModel):
model_config = ConfigDict(strict=True)
param: str
# Auto-generates inputSchema:
# {
# "type": "object",
# "properties": {"param": {"type": "string"}},
# "required": ["param"]
# }
MCP supports two transports:
stdio (Standard I/O):
async def main():
async with stdio_server() as (read, write):
await server.run(read, write)
HTTP/SSE (Server-Sent Events):
@app.post("/mcp")
async def handle_mcp(request: JsonRpcRequest):
return await server.handle_request(request)
-32700 ā Parse error (invalid JSON)
-32600 ā Invalid request
-32601 ā Method not found
-32602 ā Invalid params
-32603 ā Internal error
-32000 to -32099 ā Server-defined errors
ā Manual JSON schema writing (use Pydantic) ā Blocking operations in async handlers ā Missing error handling ā Invalid JSON-RPC 2.0 structure
Official Specification: https://spec.modelcontextprotocol.io/ JSON-RPC 2.0 Spec: https://www.jsonrpc.org/specification Python SDK: https://github.com/modelcontextprotocol/python-sdk
Example Servers: