5 releases
0.1.4 | Jan 7, 2025 |
---|---|
0.1.3 | Jan 7, 2025 |
0.1.2 | Jan 7, 2025 |
0.1.1 | Jan 7, 2025 |
0.1.0 | Jan 7, 2025 |
#146 in Machine learning
264 downloads per month
27KB
541 lines
Model Context Protocol (MCP) Rust Schema
A Rust implementation of the Model Context Protocol (MCP) schema, providing type definitions and validation for MCP messages.
Features
- 🔍 Complete type definitions for MCP messages and data structures
- 🚀 JSON-RPC 2.0 message schema support
- ✅ Schema validation against the MCP specification
- 📦 Serialization/deserialization support via serde
Installation
Add this to your Cargo.toml
:
[dependencies]
mcp_rust_schema = "0.1.4"
Usage
Basic Message Creation
use mcp_rust_schema::protocol::{Request, RequestId};
use mcp_rust_schema::types::RequestMeta;
use serde_json::json;
// Create a new request
let request = Request::new(
"test/method",
Some(json!({"key": "value"})),
RequestId::String("test-1".to_string()),
Some(RequestMeta {
progress_token: None,
}),
);
Schema Validation
use mcp_rust_schema::types::Resource;
use serde_json::json;
use jsonschema::Validator;
// Create a resource
let resource = Resource {
uri: "file:///test.txt".to_string(),
title: "Test File".to_string(),
description: Some("A test file".to_string()),
contents: ResourceContents::Text {
text: "Hello World".to_string(),
uri: "file:///test.txt".to_string(),
mime_type: Some("text/plain".to_string()),
},
mime_type: Some("text/plain".to_string()),
annotations: None,
};
// Validate against schema
let resource_json = serde_json::to_value(resource)?;
let validator = jsonschema::validator_for(&schema)?;
assert!(validator.is_valid(&resource_json));
Error Handling
The library provides comprehensive error handling:
use mcp_rust_schema::error::Error;
match result {
Ok(value) => println!("Success: {:?}", value),
Err(Error::Protocol { code, message, .. }) => println!("Protocol error {}: {}", code, message),
Err(Error::Serialization(e)) => println!("Serialization error: {}", e),
Err(e) => println!("Other error: {}", e),
}
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
Dependencies
~0.7–1.6MB
~35K SLoC