turbomcp-protocol 1.0.10

Model Context Protocol (MCP) specification implementation with JSON-RPC and schema validation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# TurboMCP Protocol

[![Crates.io](https://img.shields.io/crates/v/turbomcp-protocol.svg)](https://crates.io/crates/turbomcp-protocol)
[![Documentation](https://docs.rs/turbomcp-protocol/badge.svg)](https://docs.rs/turbomcp-protocol)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

**Complete Model Context Protocol (MCP) specification implementation with JSON-RPC 2.0 and runtime schema validation.**

## Overview

`turbomcp-protocol` provides a complete, specification-compliant implementation of the Model Context Protocol (MCP) version 2025-06-18. This crate handles all protocol-level concerns including message formatting, capability negotiation, and runtime validation.

## Key Features

### 📋 **Complete MCP Specification Support**
- **MCP 2025-06-18** - Full implementation of the latest protocol specification
- **All message types** - Tools, resources, prompts, and capabilities
- **Capability negotiation** - Automatic feature detection and handshake
- **Version compatibility** - Forward and backward compatibility support

### 🔧 **JSON-RPC 2.0 Implementation**
- **Compliant message format** - Request, response, and notification handling
- **ID correlation** - Automatic request/response correlation
- **Error handling** - Standard JSON-RPC error codes and extensions
- **Batch processing** - Support for batch request/response operations

### **Runtime Schema Validation**
- **JSON Schema validation** - Runtime validation using `jsonschema` crate
- **Type safety** - Rust type definitions for all MCP message types
- **Parameter validation** - Tool and resource parameter validation
- **Schema generation** - Automatic schema generation from Rust types

### 🤝 **Capability Management**
- **Server capabilities** - Tools, resources, prompts declarations
- **Client capabilities** - Sampling, roots, progress reporting
- **Feature negotiation** - Automatic capability matching
- **Extension support** - Custom capability extensions

### 🔄 **MCP 2025-06-18 Enhanced Features**
- **Bidirectional communication** - Server-initiated requests to clients
- **Elicitation support** - Server can request structured input from users
- **Completion context** - Enhanced completion with references and metadata
- **Resource templates** - Dynamic resource generation with parameters
- **Ping/keepalive** - Connection health monitoring

## Architecture

```
┌─────────────────────────────────────────────┐
│            TurboMCP Protocol                │
├─────────────────────────────────────────────┤
│ MCP Message Types                          │
│ ├── InitializeRequest/Response             │
│ ├── Tool/Resource/Prompt messages          │
│ ├── Capability negotiation               │
│ └── Notification handling                 │
├─────────────────────────────────────────────┤
│ JSON-RPC 2.0 Layer                        │
│ ├── Request/Response correlation          │
│ ├── ID generation and tracking           │
│ ├── Error code standardization           │
│ └── Batch message processing             │
├─────────────────────────────────────────────┤
│ Schema Validation                          │
│ ├── Runtime JSON schema validation       │
│ ├── Parameter type checking              │
│ ├── Response format validation           │
│ └── Custom schema extension support      │
└─────────────────────────────────────────────┘
```

## MCP Message Types

### Core Message Types

```rust
use turbomcp_protocol::{
    InitializeRequest, InitializeResponse,
    ToolsListRequest, ToolsListResponse,
    ToolsCallRequest, ToolsCallResponse,
    ResourcesListRequest, ResourcesListResponse,
    ResourcesReadRequest, ResourcesReadResponse,
    PromptsListRequest, PromptsListResponse,
    PromptsGetRequest, PromptsGetResponse,
};
```

### MCP 2025-06-18 Enhanced Types (v1.0.3)

```rust
use turbomcp_protocol::{
    // Elicitation - Server requests user input
    ElicitRequest, ElicitResult, ElicitationAction, ElicitationSchema,
    
    // Completion - Intelligent autocompletion
    CompleteRequest, CompleteRequestParams, CompletionResponse,
    CompletionReference, CompletionValue,
    
    // Resource Templates - Dynamic resources
    ListResourceTemplatesRequest, ListResourceTemplatesResult,
    ResourceTemplate, ResourceTemplateParameter,
    
    // Ping - Bidirectional health monitoring
    PingRequest, PingParams, PingResult,
    
    // Bidirectional communication support
    ServerRequest, ClientRequest,
};
```

### JSON-RPC Infrastructure

```rust
use turbomcp_protocol::{
    JsonRpcRequest, JsonRpcResponse, JsonRpcNotification,
    JsonRpcError, ErrorCode, RequestId,
};
```

## Usage

### Basic Protocol Handling

```rust
use turbomcp_protocol::{
    JsonRpcRequest, JsonRpcResponse, InitializeRequest, 
    ToolsListRequest, McpError
};

// Parse incoming JSON-RPC request
let json_data = r#"{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "initialize",
    "params": {
        "protocolVersion": "2025-06-18",
        "capabilities": {},
        "clientInfo": {"name": "test-client", "version": "1.0.0"}
    }
}"#;

let request: JsonRpcRequest = serde_json::from_str(json_data)?;

// Handle specific message types
match request.method.as_str() {
    "initialize" => {
        let init_req: InitializeRequest = serde_json::from_value(request.params)?;
        // Process initialization
    },
    "tools/list" => {
        let tools_req: ToolsListRequest = serde_json::from_value(request.params)?;
        // Process tools list request
    },
    _ => {
        // Handle unknown method
    }
}
```

### Schema Validation

```rust
use turbomcp_protocol::{validate_tool_call, ToolCallParams, ValidationResult};

// Define tool parameters with schema validation
#[derive(serde::Serialize, serde::Deserialize)]
struct AddParams {
    a: f64,
    b: f64,
}

// Validate tool call parameters
let params = serde_json::json!({"a": 5.0, "b": 3.0});
let result: ValidationResult = validate_tool_call("add", &params)?;

match result {
    ValidationResult::Valid => {
        // Parameters are valid, proceed with tool call
        let add_params: AddParams = serde_json::from_value(params)?;
    },
    ValidationResult::Invalid(errors) => {
        // Handle validation errors
        for error in errors {
            eprintln!("Validation error: {}", error);
        }
    }
}
```

### Capability Negotiation

```rust
use turbomcp_protocol::{
    ServerCapabilities, ClientCapabilities, CapabilitySet,
    ToolCapability, ResourceCapability, PromptCapability
};

// Define server capabilities
let server_caps = ServerCapabilities {
    tools: Some(ToolCapability {}),
    resources: Some(ResourceCapability { 
        subscribe: true,
        list_changed: true 
    }),
    prompts: Some(PromptCapability {}),
    experimental: None,
};

// Define client capabilities  
let client_caps = ClientCapabilities {
    sampling: None,
    roots: Some(RootCapability { 
        list_changed: true 
    }),
    experimental: None,
};

// Negotiate capabilities
let negotiated = negotiate_capabilities(&server_caps, &client_caps)?;
```

### Error Handling

```rust
use turbomcp_protocol::{JsonRpcError, ErrorCode, McpError};

// Create protocol-specific errors
fn handle_tool_error(error: &str) -> JsonRpcError {
    JsonRpcError {
        code: ErrorCode::InvalidParams,
        message: format!("Tool validation failed: {}", error),
        data: None,
    }
}

// Convert to MCP error
let mcp_error = McpError::Protocol(handle_tool_error("Missing parameter 'name'"));
```

### Custom Message Types

```rust
use turbomcp_protocol::{JsonRpcRequest, JsonRpcResponse, RequestId};
use serde::{Serialize, Deserialize};

// Define custom message types
#[derive(Serialize, Deserialize)]
struct CustomRequest {
    custom_field: String,
    optional_data: Option<serde_json::Value>,
}

#[derive(Serialize, Deserialize)]
struct CustomResponse {
    result: String,
    metadata: serde_json::Value,
}

// Create custom JSON-RPC messages
fn create_custom_request(id: RequestId, params: CustomRequest) -> JsonRpcRequest {
    JsonRpcRequest {
        jsonrpc: "2.0".to_string(),
        id,
        method: "custom/method".to_string(),
        params: serde_json::to_value(params).unwrap(),
    }
}
```

## Message Flow

```mermaid
sequenceDiagram
    participant Client
    participant Protocol as turbomcp-protocol
    participant Server
    
    Client->>Protocol: Raw JSON message
    Protocol->>Protocol: Parse JSON-RPC
    Protocol->>Protocol: Validate message format
    Protocol->>Protocol: Extract MCP message
    Protocol->>Protocol: Validate against schema
    Protocol->>Server: Typed MCP message
    Server->>Protocol: Typed MCP response
    Protocol->>Protocol: Serialize response
    Protocol->>Protocol: Wrap in JSON-RPC
    Protocol->>Client: JSON response
```

## Feature Flags

| Feature | Description | Default |
|---------|-------------|---------|
| `validation` | Enable runtime schema validation ||
| `extensions` | Enable MCP extension message types ||
| `batch` | Enable JSON-RPC batch processing ||
| `async-validation` | Enable async schema validation ||

## Supported MCP Methods

### Core Methods

- `initialize` - Protocol initialization and capability negotiation
- `initialized` - Initialization completion notification

### Tool Methods

- `tools/list` - List available tools
- `tools/call` - Execute a tool with parameters

### Resource Methods

- `resources/list` - List available resources
- `resources/read` - Read resource content
- `resources/updated` - Resource change notification

### Prompt Methods

- `prompts/list` - List available prompts
- `prompts/get` - Get prompt content

### Capability Methods

- `capabilities/changed` - Capability change notification

## Integration

### With TurboMCP Framework

Protocol handling is automatic when using the main framework:

```rust
use turbomcp::prelude::*;

#[server]
impl MyServer {
    #[tool("Add numbers")]
    async fn add(&self, a: f64, b: f64) -> McpResult<f64> {
        // Protocol parsing and validation handled automatically
        Ok(a + b)
    }
}
```

### Direct Protocol Usage

For custom implementations or integrations:

```rust
use turbomcp_protocol::{McpServer, JsonRpcRequest, JsonRpcResponse};

struct CustomProtocolHandler;

impl CustomProtocolHandler {
    async fn handle_message(&self, raw_json: &str) -> Result<String, Box<dyn std::error::Error>> {
        // Parse JSON-RPC message
        let request: JsonRpcRequest = serde_json::from_str(raw_json)?;
        
        // Handle based on method
        let response = match request.method.as_str() {
            "tools/list" => self.handle_tools_list(request).await?,
            "tools/call" => self.handle_tools_call(request).await?,
            _ => return Err("Unknown method".into()),
        };
        
        // Serialize response
        Ok(serde_json::to_string(&response)?)
    }
}
```

## Development

### Building

```bash
# Build with all features
cargo build --features validation,extensions,batch

# Build minimal (no validation)
cargo build --no-default-features
```

### Testing

```bash
# Run protocol compliance tests
cargo test

# Test with all message types
cargo test --features extensions

# Validate against MCP specification
cargo test test_mcp_compliance
```

### Schema Validation

```bash
# Generate JSON schemas from Rust types
cargo run --example generate_schemas

# Validate example messages
cargo test test_message_validation
```

## Related Crates

- **[turbomcp]../turbomcp/** - Main framework (uses this crate)
- **[turbomcp-core]../turbomcp-core/** - Core types and utilities
- **[turbomcp-transport]../turbomcp-transport/** - Transport layer
- **[turbomcp-server]../turbomcp-server/** - Server framework

## External Resources

- **[MCP Specification]https://modelcontextprotocol.io/** - Official protocol specification
- **[JSON-RPC 2.0]https://www.jsonrpc.org/specification** - JSON-RPC specification
- **[JSON Schema]https://json-schema.org/** - Schema validation specification

## License

Licensed under the [MIT License](../../LICENSE).

---

*Part of the [TurboMCP](../../) high-performance Rust SDK for the Model Context Protocol.*