1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//! # 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:
//!
//! ```rust,no_run
//! use mcpr::{
//! client::Client,
//! transport::stdio::StdioTransport,
//! };
//!
//! # fn main() -> Result<(), mcpr::error::MCPError> {
//! // 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()?;
//! # Ok(())
//! # }
//! ```
//!
//! ## High-Level Server
//!
//! The high-level server makes it easy to create MCP-compatible servers:
//!
//! ```rust
//! use mcpr::{
//! error::MCPError,
//! server::{Server, ServerConfig},
//! transport::stdio::StdioTransport,
//! Tool,
//! };
//! use serde_json::Value;
//!
//! # fn main() -> Result<(), mcpr::error::MCPError> {
//! // 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)?;
//! # Ok(())
//! # }
//! ```
/// Current version of the MCPR crate
pub const VERSION: &str = env!;
// Re-export commonly used types
pub use ;
pub use ;
/// Protocol version constants
/// Error types for the MCP implementation