# MCP Protocol Types
[](https://crates.io/crates/mcp-protocol-types)
[](https://docs.rs/mcp-protocol-types)
[](https://opensource.org/licenses/MIT)
**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`:
```toml
[dependencies]
mcp-protocol-types = "0.1.0"
# With optional features:
mcp-protocol-types = { version = "0.1.0", features = ["validation", "timestamps"] }
```
## ๐ Core Types
### Protocol Structures
```rust
use mcp_protocol_types::*;
// JSON-RPC request/response
let request = JsonRpcRequest {
jsonrpc: "2.0".to_string(),
id: RequestId::Number(1),
method: "tools/list".to_string(),
params: None,
};
// Tool definition
let tool = Tool {
name: "calculate".to_string(),
description: Some("Perform calculations".to_string()),
input_schema: ToolInputSchema {
type_: "object".to_string(),
properties: Some(json!({
"expression": {
"type": "string",
"description": "Mathematical expression to evaluate"
}
})),
required: Some(vec!["expression".to_string()]),
},
};
// Resource definition
let resource = Resource {
uri: "file:///path/to/file.txt".to_string(),
name: Some("Configuration File".to_string()),
description: Some("Application configuration".to_string()),
mime_type: Some("text/plain".to_string()),
};
```
### Error Handling
```rust
use mcp_protocol_types::{McpError, ErrorCode};
// Create custom errors
let error = McpError {
code: ErrorCode::InvalidRequest,
message: "Missing required parameter".to_string(),
data: Some(json!({"parameter": "expression"})),
};
// Use predefined error types
let parse_error = McpError::parse_error("Invalid JSON");
let method_not_found = McpError::method_not_found("unknown/method");
```
## ๐ง Feature Flags
| `validation` | JSON schema validation support | โ |
| `timestamps` | Timestamp handling with chrono | โ |
## ๐ Type Categories
### ๐ Core Protocol
- `JsonRpcRequest` / `JsonRpcResponse` - JSON-RPC 2.0 structures
- `RequestId` - Request identification
- `McpError` - Error definitions and codes
- `ServerCapabilities` / `ClientCapabilities` - Capability negotiation
### ๐ ๏ธ Tools
- `Tool` - Tool definitions and metadata
- `ToolInputSchema` - Input parameter schemas
- `CallToolRequest` / `CallToolResult` - Tool execution
### ๐ Resources
- `Resource` - Resource definitions and metadata
- `ResourceTemplate` - Templated resources with URI patterns
- `ReadResourceRequest` / `ResourceContents` - Resource access
### ๐ฌ Prompts
- `Prompt` - Prompt templates and definitions
- `PromptArgument` - Prompt parameters
- `GetPromptRequest` / `GetPromptResult` - Prompt retrieval
### ๐ Logging
- `LoggingLevel` - Log level enumeration
- `LogEntry` - Structured log entries
- `SetLoggingLevelRequest` - Logging configuration
### ๐ฒ Sampling
- `SamplingMessage` - LLM sampling requests
- `CreateMessageRequest` / `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
```rust
use mcp_protocol_types::*;
use serde_json;
// Serialize a request
let request = JsonRpcRequest {
jsonrpc: "2.0".to_string(),
id: RequestId::String("req-1".to_string()),
method: "tools/call".to_string(),
params: Some(json!({
"name": "calculator",
"arguments": {"expression": "2 + 2"}
})),
};
let json = serde_json::to_string(&request)?;
println!("{}", json);
// Deserialize a response
let json = r#"{
"jsonrpc": "2.0",
"id": "req-1",
"result": {
"content": [
{
"type": "text",
"text": "4"
}
]
}
}"#;
let response: JsonRpcResponse = serde_json::from_str(json)?;
```
### Tool Definitions
```rust
use mcp_protocol_types::*;
let tool = Tool {
name: "weather".to_string(),
description: Some("Get weather information".to_string()),
input_schema: ToolInputSchema {
type_: "object".to_string(),
properties: Some(json!({
"location": {
"type": "string",
"description": "City name or coordinates"
},
"units": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"default": "celsius"
}
})),
required: Some(vec!["location".to_string()]),
},
};
// Serialize for transmission
let tool_json = serde_json::to_string_pretty(&tool)?;
```
## ๐งช Testing
```rust
use mcp_protocol_types::*;
use serde_json;
#[test]
fn test_request_serialization() {
let request = JsonRpcRequest {
jsonrpc: "2.0".to_string(),
id: RequestId::Number(42),
method: "test/method".to_string(),
params: Some(json!({"key": "value"})),
};
let json = serde_json::to_string(&request).unwrap();
let deserialized: JsonRpcRequest = serde_json::from_str(&json).unwrap();
assert_eq!(request.id, deserialized.id);
assert_eq!(request.method, deserialized.method);
}
```
## ๐ ๏ธ Development
```bash
# Build the crate
cargo build
# Run tests
cargo test
# Check with all features
cargo check --all-features
# Generate documentation
cargo doc --all-features --open
```
## ๐ค Contributing
This crate is part of the [MCP Rust ecosystem](https://github.com/mcp-rust). 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](./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 ๐ฆ*