MCP Protocol Types
Shared types and protocol definitions for the Model Context Protocol (MCP)
This crate provides the core type definitions, request/response structures, and error types used throughout the MCP Rust ecosystem. It serves as the foundation for both client and server implementations.
โจ Features
- ๐ฏ Complete Type Coverage - All MCP protocol types and structures
- ๐ Type Safety - Compile-time guarantees for protocol correctness
- ๐ฆ Zero Dependencies - Minimal dependency footprint (only serde + std types)
- ๐ Serialization Ready - Full serde support for JSON-RPC communication
- ๐ก๏ธ Validation Support - Optional JSON schema validation
- ๐ Excellent Documentation - Comprehensive docs with examples
๐ Quick Start
Add to your Cargo.toml:
[]
= "0.1.0"
# With optional features:
= { = "0.1.0", = ["validation", "timestamps"] }
๐ Core Types
Protocol Structures
use *;
// JSON-RPC request/response
let request = JsonRpcRequest ;
// Tool definition
let tool = Tool ;
// Resource definition
let resource = Resource ;
Error Handling
use ;
// Create custom errors
let error = McpError ;
// Use predefined error types
let parse_error = parse_error;
let method_not_found = method_not_found;
๐ง Feature Flags
| Feature | Description | Default |
|---|---|---|
validation |
JSON schema validation support | โ |
timestamps |
Timestamp handling with chrono | โ |
๐ Type Categories
๐ Core Protocol
JsonRpcRequest/JsonRpcResponse- JSON-RPC 2.0 structuresRequestId- Request identificationMcpError- Error definitions and codesServerCapabilities/ClientCapabilities- Capability negotiation
๐ ๏ธ Tools
Tool- Tool definitions and metadataToolInputSchema- Input parameter schemasCallToolRequest/CallToolResult- Tool execution
๐ Resources
Resource- Resource definitions and metadataResourceTemplate- Templated resources with URI patternsReadResourceRequest/ResourceContents- Resource access
๐ฌ Prompts
Prompt- Prompt templates and definitionsPromptArgument- Prompt parametersGetPromptRequest/GetPromptResult- Prompt retrieval
๐ Logging
LoggingLevel- Log level enumerationLogEntry- Structured log entriesSetLoggingLevelRequest- Logging configuration
๐ฒ Sampling
SamplingMessage- LLM sampling requestsCreateMessageRequest/CreateMessageResult- Message creation
๐๏ธ Architecture
This crate serves as the foundation for the MCP Rust ecosystem:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ MCP Applications โ
โโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ MCP Client โ MCP Server โ
โโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ MCP Protocol Types โ โ This crate
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ JSON-RPC Transport Layer โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ Serialization Examples
JSON-RPC Messages
use *;
use serde_json;
// Serialize a request
let request = JsonRpcRequest ;
let json = to_string?;
println!;
// Deserialize a response
let json = r#"{
"jsonrpc": "2.0",
"id": "req-1",
"result": {
"content": [
{
"type": "text",
"text": "4"
}
]
}
}"#;
let response: JsonRpcResponse = from_str?;
Tool Definitions
use *;
let tool = Tool ;
// Serialize for transmission
let tool_json = to_string_pretty?;
๐งช Testing
use *;
use serde_json;
๐ ๏ธ Development
# Build the crate
# Run tests
# Check with all features
# Generate documentation
๐ค Contributing
This crate is part of the MCP Rust ecosystem. Contributions are welcome!
Guidelines
- Breaking Changes - Require RFC process
- New Types - Must follow MCP specification
- Documentation - Required for all public APIs
- Testing - All types must have serialization tests
๐ Protocol Compliance
โ MCP 2024-11-05 Specification
This crate implements all types defined in the official MCP specification:
- Core JSON-RPC 2.0 structures
- Tool calling and parameter schemas
- Resource access and templates
- Prompt templates and arguments
- Logging and debugging
- LLM sampling integration
- Error codes and handling
๐ License
Licensed under the MIT License.
๐ Acknowledgments
- Anthropic - For creating the MCP specification
- Serde Team - For excellent serialization support
- Rust Community - For the amazing type system
Foundation types for the MCP Rust ecosystem ๐ฆ