TurboMCP Protocol
Model Context Protocol (MCP) specification implementation with JSON-RPC 2.0 and runtime schema validation.
Overview
turbomcp-protocol
provides a specification-compliant implementation of the Model Context Protocol (MCP). This crate handles protocol-level concerns including message formatting, capability negotiation, and runtime validation.
Key Features
📋 MCP Specification Support
- MCP specification implementation with current message types
- Tools, resources, prompts, and capabilities support
- Capability negotiation with feature detection and handshake
- Version compatibility support
🔧 JSON-RPC 2.0 Implementation
- Compliant message format with request, response, and notification handling
- ID correlation for automatic request/response matching
- Standard JSON-RPC error codes and extensions
- Support for batch request/response operations
✅ Runtime Schema Validation
- JSON Schema validation using
jsonschema
crate - Rust type definitions for MCP message types
- Tool and resource parameter validation
- Schema generation from Rust types
🤝 Capability Management
- Type-State Capability Builders - Compile-time validated capability configuration (New in v1.1.0)
- Server capabilities for tools, resources, prompts declarations
- Client capabilities including sampling, roots, progress reporting
- Feature negotiation with capability matching
- Support for custom capability extensions
🔄 MCP Enhanced Features
- Bidirectional communication for server-initiated requests to clients
- Elicitation support for server-requested structured input from users
- Completion context with references and metadata
- Resource templates for dynamic resource generation with parameters
- Ping/keepalive for 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
use ;
MCP Enhanced Types
use ;
JSON-RPC Infrastructure
use ;
Usage
Basic Protocol Handling
use ;
// 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.1.0"}
}
}"#;
let request: JsonRpcRequest = from_str?;
// Handle specific message types
match request.method.as_str
Schema Validation
use ;
// Define tool parameters with schema validation
// Validate tool call parameters
let params = json!;
let result: ValidationResult = validate_tool_call?;
match result
Type-State Capability Builders (New in v1.1.0)
use ;
// Compile-time validated server capabilities
let server_caps = new
.enable_tools // Enable tools capability
.enable_resources // Enable resources capability
.enable_prompts // Enable prompts capability
.enable_tool_list_changed // ✅ Only available when tools enabled
.enable_resources_subscribe // ✅ Only available when resources enabled
.enable_resources_list_changed // ✅ Only available when resources enabled
.build;
// Compile-time validated client capabilities
let client_caps = new
.enable_roots // Enable filesystem roots
.enable_sampling // Enable LLM sampling
.enable_elicitation // Enable interactive forms
.enable_roots_list_changed // ✅ Only available when roots enabled
.build;
// Convenience builders for common patterns
let full_server = full_featured.build;
let minimal_server = minimal.build;
let sampling_client = sampling_focused.build;
Traditional Capability Negotiation
use ;
// Traditional approach (still supported)
let server_caps = ServerCapabilities ;
// Define client capabilities
let client_caps = ClientCapabilities ;
// Negotiate capabilities
let negotiated = negotiate_capabilities?;
Error Handling
use ;
// Create protocol-specific errors
// Convert to MCP error
let mcp_error = Protocol;
Custom Message Types
use ;
use ;
// Define custom message types
// Create custom JSON-RPC messages
Message Flow
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 negotiationinitialized
- Initialization completion notification
Tool Methods
tools/list
- List available toolstools/call
- Execute a tool with parameters
Resource Methods
resources/list
- List available resourcesresources/read
- Read resource contentresources/updated
- Resource change notification
Prompt Methods
prompts/list
- List available promptsprompts/get
- Get prompt content
Capability Methods
capabilities/changed
- Capability change notification
Integration
With TurboMCP Framework
Protocol handling is automatic when using the main framework:
use *;
Direct Protocol Usage
For custom implementations or integrations:
use ;
;
Development
Building
# Build with all features
# Build minimal (no validation)
Testing
# Run protocol compliance tests
# Test with all message types
# Validate against MCP specification
Schema Validation
# Generate JSON schemas from Rust types
# Validate example messages
Related Crates
- turbomcp - Main framework (uses this crate)
- turbomcp-core - Core types and utilities
- turbomcp-transport - Transport layer
- turbomcp-server - Server framework
External Resources
- MCP Specification - Official protocol specification
- JSON-RPC 2.0 - JSON-RPC specification
- JSON Schema - Schema validation specification
License
Licensed under the MIT License.
Part of the TurboMCP high-performance Rust SDK for the Model Context Protocol.