| Crates.io | mcp-protocol-types |
| lib.rs | mcp-protocol-types |
| version | 0.2.0 |
| created_at | 2025-06-12 16:01:13.783829+00 |
| updated_at | 2025-07-31 01:24:33.647917+00 |
| description | โ DEPRECATED: Use mcp-protocol-sdk v0.4.0+ instead. This crate is consolidated into the main SDK. |
| homepage | https://modelcontextprotocol.io |
| repository | https://github.com/mcp-rust/mcp-protocol-types |
| max_upload_size | |
| id | 1709956 |
| size | 103,648 |
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.
Add to your Cargo.toml:
[dependencies]
mcp-protocol-types = "0.1.0"
# With optional features:
mcp-protocol-types = { version = "0.1.0", features = ["validation", "timestamps"] }
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()),
};
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 | Description | Default |
|---|---|---|
validation |
JSON schema validation support | โ |
timestamps |
Timestamp handling with chrono | โ |
JsonRpcRequest / JsonRpcResponse - JSON-RPC 2.0 structuresRequestId - Request identificationMcpError - Error definitions and codesServerCapabilities / ClientCapabilities - Capability negotiationTool - Tool definitions and metadataToolInputSchema - Input parameter schemasCallToolRequest / CallToolResult - Tool executionResource - Resource definitions and metadataResourceTemplate - Templated resources with URI patternsReadResourceRequest / ResourceContents - Resource accessPrompt - Prompt templates and definitionsPromptArgument - Prompt parametersGetPromptRequest / GetPromptResult - Prompt retrievalLoggingLevel - Log level enumerationLogEntry - Structured log entriesSetLoggingLevelRequest - Logging configurationSamplingMessage - LLM sampling requestsCreateMessageRequest / CreateMessageResult - Message creationThis 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 โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
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)?;
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)?;
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);
}
# Build the crate
cargo build
# Run tests
cargo test
# Check with all features
cargo check --all-features
# Generate documentation
cargo doc --all-features --open
This crate is part of the MCP Rust ecosystem. Contributions are welcome!
โ MCP 2024-11-05 Specification
This crate implements all types defined in the official MCP specification:
Licensed under the MIT License.
Foundation types for the MCP Rust ecosystem ๐ฆ