乐闻世界logo
搜索文章和话题

What is the message format of MCP? What are the common message types?

2月19日 21:35

MCP's message format is based on the JSON-RPC 2.0 protocol, with extensions to support interactions between AI models and external systems. Here's a detailed explanation of the message format:

Basic Message Structure

All MCP messages follow the basic JSON-RPC 2.0 format:

json
{ "jsonrpc": "2.0", "id": "unique-request-id", "method": "method-name", "params": { ... } }

1. Request Message

Used for clients to send tool invocation requests to the server:

json
{ "jsonrpc": "2.0", "id": "req-123", "method": "tools/call", "params": { "name": "calculate", "arguments": { "expression": "2 + 2" } } }

2. Response Message

Server returns execution results:

json
{ "jsonrpc": "2.0", "id": "req-123", "result": { "content": [ { "type": "text", "text": "Result: 4" } ] } }

3. Error Response

Returned when a request fails:

json
{ "jsonrpc": "2.0", "id": "req-123", "error": { "code": -32602, "message": "Invalid params", "data": { "details": "Parameter 'expression' cannot be empty" } } }

4. Notification Message

Server-initiated messages (no response required):

json
{ "jsonrpc": "2.0", "method": "notifications/progress", "params": { "progress": 0.5, "message": "Processing..." } }

Common Method Types

tools/list - Get list of available tools

json
{ "jsonrpc": "2.0", "id": "req-001", "method": "tools/list" }

resources/list - Get list of available resources

json
{ "jsonrpc": "2.0", "id": "req-002", "method": "resources/list" }

resources/read - Read resource content

json
{ "jsonrpc": "2.0", "id": "req-003", "method": "resources/read", "params": { "uri": "file:///data/config.json" } }

prompts/list - Get list of prompts

json
{ "jsonrpc": "2.0", "id": "req-004", "method": "prompts/list" }

Error Codes

MCP defines standard error codes:

CodeNameDescription
-32700Parse errorJSON parse error
-32600Invalid RequestInvalid request
-32601Method not foundMethod does not exist
-32602Invalid paramsInvalid parameters
-32603Internal errorInternal error
-32000Server errorServer error

Content Types

MCP supports multiple content types:

json
{ "type": "text", "text": "Plain text content" }
json
{ "type": "image", "data": "base64-encoded-image-data", "mimeType": "image/png" }
json
{ "type": "resource", "uri": "file:///data/report.pdf", "mimeType": "application/pdf" }

Message Streaming

For long-running operations, streaming responses are supported:

json
{ "jsonrpc": "2.0", "id": "req-005", "method": "tools/call", "params": { "name": "generate_report", "arguments": { "stream": true } } }

Best Practices:

  1. Unique ID: Each request must have a unique ID
  2. Type Validation: Strictly validate parameter types and formats
  3. Error Handling: Provide detailed error information and data
  4. Timeout Handling: Implement request timeout mechanisms
  5. Logging: Log all messages for debugging and auditing

Understanding MCP's message format is crucial for implementing compatible servers and clients.

标签:MCP