Expand description
§Model Context Protocol (MCP) for Rust
This crate provides a Rust implementation of Anthropic’s Model Context Protocol (MCP), an open standard for connecting AI assistants to data sources and tools.
The implementation includes:
- Schema definitions for MCP messages
- Transport layer for communication
- High-level client and server implementations
- CLI tools for generating server and client stubs
- Generator for creating MCP server and client stubs
§High-Level Client
The high-level client provides a simple interface for communicating with MCP servers:
use mcpr::{
client::Client,
transport::stdio::StdioTransport,
};
// Create a client with stdio transport
let transport = StdioTransport::new();
let mut client = Client::new(transport);
// Initialize the client
client.initialize()?;
// Call a tool (example with serde_json::Value)
let request = serde_json::json!({
"param1": "value1",
"param2": "value2"
});
let response: serde_json::Value = client.call_tool("my_tool", &request)?;
// Shutdown the client
client.shutdown()?;
§High-Level Server
The high-level server makes it easy to create MCP-compatible servers:
use mcpr::{
error::MCPError,
server::{Server, ServerConfig},
transport::stdio::StdioTransport,
Tool,
};
use serde_json::Value;
// Configure the server
let server_config = ServerConfig::new()
.with_name("My MCP Server")
.with_version("1.0.0")
.with_tool(Tool {
name: "my_tool".to_string(),
description: Some("My awesome tool".to_string()),
input_schema: mcpr::schema::common::ToolInputSchema {
r#type: "object".to_string(),
properties: Some([
("param1".to_string(), serde_json::json!({
"type": "string",
"description": "First parameter"
})),
("param2".to_string(), serde_json::json!({
"type": "string",
"description": "Second parameter"
}))
].into_iter().collect()),
required: Some(vec!["param1".to_string(), "param2".to_string()]),
},
});
// Create the server
let mut server: Server<StdioTransport> = Server::new(server_config);
// Register tool handlers
server.register_tool_handler("my_tool", |params: Value| {
// Parse parameters and handle the tool call
let param1 = params.get("param1")
.and_then(|v| v.as_str())
.ok_or_else(|| MCPError::Protocol("Missing param1".to_string()))?;
let param2 = params.get("param2")
.and_then(|v| v.as_str())
.ok_or_else(|| MCPError::Protocol("Missing param2".to_string()))?;
// Process the parameters and generate a response
let response = serde_json::json!({
"result": format!("Processed {} and {}", param1, param2)
});
Ok(response)
})?;
// In a real application, you would start the server with:
// let transport = StdioTransport::new();
// server.start(transport)?;
Re-exports§
pub use schema::common::Cursor;
pub use schema::common::LoggingLevel;
pub use schema::common::ProgressToken;
pub use schema::common::Tool;
pub use schema::json_rpc::JSONRPCMessage;
pub use schema::json_rpc::RequestId;
Modules§
- cli
- CLI tools for working with MCP
- client
- High-level client implementation for MCP
- constants
- Protocol version constants
- error
- Error types for the MCP implementation
- generator
- Module for generating MCP server and client stubs
- schema
- MCP schema definitions
- server
- High-level server implementation for MCP
- transport
- Transport layer for MCP communication
Constants§
- VERSION
- Current version of the MCPR crate