|
| 1 | +# MCP JSON-RPC API Specification |
| 2 | + |
| 3 | +This document describes the Model Context Protocol (MCP) JSON-RPC API implementation for the Solana MCP Server. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The Solana MCP Server implements the full MCP JSON-RPC 2.0 specification, providing a standards-compliant interface for AI clients to interact with the Solana blockchain. |
| 8 | + |
| 9 | +## API Endpoints |
| 10 | + |
| 11 | +### HTTP Web Service Mode |
| 12 | + |
| 13 | +When running in web service mode, the server exposes the following endpoints: |
| 14 | + |
| 15 | +- `POST /api/mcp` - MCP JSON-RPC 2.0 API endpoint |
| 16 | +- `GET /health` - Health check and capability information |
| 17 | +- `GET /metrics` - Prometheus metrics (Prometheus format) |
| 18 | + |
| 19 | +## MCP JSON-RPC 2.0 Specification |
| 20 | + |
| 21 | +All MCP requests and responses follow the JSON-RPC 2.0 specification with MCP-specific extensions. |
| 22 | + |
| 23 | +### Request Format |
| 24 | + |
| 25 | +```json |
| 26 | +{ |
| 27 | + "jsonrpc": "2.0", |
| 28 | + "id": 1, |
| 29 | + "method": "methodName", |
| 30 | + "params": { |
| 31 | + // Method-specific parameters |
| 32 | + } |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +### Response Format |
| 37 | + |
| 38 | +#### Success Response |
| 39 | +```json |
| 40 | +{ |
| 41 | + "jsonrpc": "2.0", |
| 42 | + "id": 1, |
| 43 | + "result": { |
| 44 | + // Method-specific result data |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +#### Error Response |
| 50 | +```json |
| 51 | +{ |
| 52 | + "jsonrpc": "2.0", |
| 53 | + "id": 1, |
| 54 | + "error": { |
| 55 | + "code": -32603, |
| 56 | + "message": "Internal error", |
| 57 | + "data": { |
| 58 | + "protocolVersion": "2024-11-05" |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +## Content Types |
| 65 | + |
| 66 | +The MCP specification supports multiple content types with optional annotations: |
| 67 | + |
| 68 | +### Text Content |
| 69 | +```json |
| 70 | +{ |
| 71 | + "type": "text", |
| 72 | + "text": "Content text here", |
| 73 | + "annotations": { |
| 74 | + "audience": ["user", "assistant"], |
| 75 | + "priority": 0.8, |
| 76 | + "lastModified": "2024-01-15T10:00:00Z" |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +### Image Content |
| 82 | +```json |
| 83 | +{ |
| 84 | + "type": "image", |
| 85 | + "data": "base64-encoded-image-data", |
| 86 | + "mimeType": "image/png", |
| 87 | + "annotations": { |
| 88 | + "audience": ["user"], |
| 89 | + "priority": 1.0 |
| 90 | + } |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +### Resource Content |
| 95 | +```json |
| 96 | +{ |
| 97 | + "type": "resource", |
| 98 | + "resource": { |
| 99 | + "uri": "https://example.com/resource", |
| 100 | + "mimeType": "application/json" |
| 101 | + }, |
| 102 | + "annotations": { |
| 103 | + "priority": 0.5 |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +## Annotations |
| 109 | + |
| 110 | +Annotations provide metadata about content objects: |
| 111 | + |
| 112 | +- `audience`: Array of intended recipients (`["user", "assistant"]`) |
| 113 | +- `priority`: Importance level (0.0 = least important, 1.0 = most important) |
| 114 | +- `lastModified`: ISO 8601 timestamp of last modification |
| 115 | + |
| 116 | +## Error Codes |
| 117 | + |
| 118 | +Standard JSON-RPC 2.0 error codes are used: |
| 119 | + |
| 120 | +- `-32700`: Parse error |
| 121 | +- `-32600`: Invalid Request |
| 122 | +- `-32601`: Method not found |
| 123 | +- `-32602`: Invalid params |
| 124 | +- `-32603`: Internal error |
| 125 | +- `-32002`: Server not initialized (MCP-specific) |
| 126 | + |
| 127 | +## Health Check Response |
| 128 | + |
| 129 | +The `/health` endpoint returns detailed server information: |
| 130 | + |
| 131 | +```json |
| 132 | +{ |
| 133 | + "status": "ok", |
| 134 | + "service": "solana-mcp-server", |
| 135 | + "version": "1.0.2", |
| 136 | + "protocol": "2024-11-05", |
| 137 | + "capabilities": { |
| 138 | + "tools": true, |
| 139 | + "resources": true, |
| 140 | + "prompts": false, |
| 141 | + "sampling": false |
| 142 | + } |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +## Headers |
| 147 | + |
| 148 | +### Request Headers |
| 149 | +- `Content-Type: application/json` (required) |
| 150 | +- `Accept: application/json` (recommended) |
| 151 | + |
| 152 | +### Response Headers |
| 153 | +- `Content-Type: application/json` |
| 154 | +- `X-MCP-Version: 2024-11-05` (protocol version) |
| 155 | +- `Cache-Control: no-cache` |
| 156 | + |
| 157 | +## Client Usage Examples |
| 158 | + |
| 159 | +### Python Example |
| 160 | +```python |
| 161 | +import requests |
| 162 | +import json |
| 163 | + |
| 164 | +# Initialize MCP session |
| 165 | +init_request = { |
| 166 | + "jsonrpc": "2.0", |
| 167 | + "id": 1, |
| 168 | + "method": "initialize", |
| 169 | + "params": { |
| 170 | + "protocolVersion": "2024-11-05", |
| 171 | + "capabilities": {}, |
| 172 | + "clientInfo": { |
| 173 | + "name": "my-client", |
| 174 | + "version": "1.0.0" |
| 175 | + } |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +response = requests.post( |
| 180 | + "http://localhost:3000/api/mcp", |
| 181 | + headers={"Content-Type": "application/json"}, |
| 182 | + json=init_request |
| 183 | +) |
| 184 | + |
| 185 | +print(response.json()) |
| 186 | +``` |
| 187 | + |
| 188 | +### JavaScript Example |
| 189 | +```javascript |
| 190 | +const mcpRequest = { |
| 191 | + jsonrpc: "2.0", |
| 192 | + id: 2, |
| 193 | + method: "tools/call", |
| 194 | + params: { |
| 195 | + name: "getBalance", |
| 196 | + arguments: { |
| 197 | + pubkey: "11111111111111111111111111111112" |
| 198 | + } |
| 199 | + } |
| 200 | +}; |
| 201 | + |
| 202 | +fetch("http://localhost:3000/api/mcp", { |
| 203 | + method: "POST", |
| 204 | + headers: { |
| 205 | + "Content-Type": "application/json" |
| 206 | + }, |
| 207 | + body: JSON.stringify(mcpRequest) |
| 208 | +}) |
| 209 | +.then(response => response.json()) |
| 210 | +.then(data => console.log(data)); |
| 211 | +``` |
| 212 | + |
| 213 | +## Validation |
| 214 | + |
| 215 | +The server performs strict validation on all requests: |
| 216 | + |
| 217 | +1. **JSON-RPC 2.0 compliance**: Validates `jsonrpc`, `method`, and `id` fields |
| 218 | +2. **Content-Type validation**: Ensures `application/json` content type |
| 219 | +3. **Parameter validation**: Validates method-specific parameters |
| 220 | +4. **Protocol version compatibility**: Checks MCP protocol version |
| 221 | + |
| 222 | +## Security Considerations |
| 223 | + |
| 224 | +- All HTTP responses include appropriate caching headers |
| 225 | +- Request validation prevents malformed JSON-RPC requests |
| 226 | +- Parameter sanitization prevents injection attacks |
| 227 | +- Network detection for proper metrics labeling |
| 228 | +- Rate limiting should be implemented at the reverse proxy level |
| 229 | + |
| 230 | +## Compatibility |
| 231 | + |
| 232 | +This implementation follows: |
| 233 | +- JSON-RPC 2.0 specification |
| 234 | +- MCP Protocol version 2024-11-05 |
| 235 | +- HTTP/1.1 and HTTP/2 standards |
| 236 | +- OpenAPI 3.0 compatible (documentation available separately) |
| 237 | + |
| 238 | +The server maintains full backward compatibility with existing stdio transport clients while providing modern HTTP JSON-RPC capabilities for web-based integrations. |
0 commit comments