2.2 KiB
2.2 KiB
Model Context Protocol (MCP) Reference
Protocol Overview
MCP is JSON-RPC 2.0 based protocol for AI-tool integration.
Version: 2025-03-26 Foundation: JSON-RPC 2.0 Architecture: Client-Host-Server
Connection Lifecycle
- Initialize: Client sends
initializerequest with capabilities - Response: Server responds with its capabilities
- Handshake: Client sends
notifications/initialized - Active: Bidirectional messaging
- Shutdown: Close connections, cleanup
Core Capabilities
Tools (Executable Functions)
Tools are functions that servers expose for execution.
List Tools:
{"method": "tools/list"}
Call Tool:
{
"method": "tools/call",
"params": {
"name": "tool_name",
"arguments": {}
}
}
Prompts (Interaction Templates)
Prompts are reusable templates for LLM interactions.
List Prompts:
{"method": "prompts/list"}
Get Prompt:
{
"method": "prompts/get",
"params": {
"name": "prompt_name",
"arguments": {}
}
}
Resources (Data Sources)
Resources expose read-only data to clients.
List Resources:
{"method": "resources/list"}
Read Resource:
{
"method": "resources/read",
"params": {"uri": "resource://path"}
}
Transport Types
stdio (Local)
Server runs as subprocess. Messages via stdin/stdout.
const transport = new StdioClientTransport({
command: 'node',
args: ['server.js']
});
HTTP+SSE (Remote)
POST for requests, GET for server events.
const transport = new StreamableHTTPClientTransport({
url: 'http://localhost:3000/mcp'
});
Error Codes
- -32700: Parse error
- -32600: Invalid request
- -32601: Method not found
- -32602: Invalid params
- -32603: Internal error
- -32002: Resource not found (MCP-specific)
Best Practices
- Progressive Disclosure: Load tool definitions on-demand
- Context Efficiency: Filter data before returning
- Security: Validate inputs, sanitize outputs
- Resource Management: Cleanup connections properly
- Error Handling: Handle all error cases gracefully