model_context_protocol/lib.rs
1//! # MCP - Model Context Protocol
2//!
3//! A Rust implementation of the Model Context Protocol (MCP) for AI tool integration.
4//!
5//! This crate provides the infrastructure for communicating with MCP servers
6//! via stdio/HTTP transports.
7//!
8//! ## Features
9//!
10//! - **McpHub**: Central hub for managing multiple MCP server connections
11//! - **Multiple Transports**: Support for stdio and HTTP-based MCP servers
12//! - **Tool Routing**: Automatic routing of tool calls to the correct server
13//! - **Macros**: `#[mcp_tool]` for easy tool definitions from functions
14//!
15//! ## Quick Start - Creating a Server
16//!
17//! ```rust,ignore
18//! use mcp::macros::mcp_tool;
19//! use mcp::{McpServerConfig, McpServer};
20//!
21//! // Define tools as simple functions
22//! #[mcp_tool(description = "Add two numbers")]
23//! fn add(a: f64, b: f64) -> f64 { a + b }
24//!
25//! #[mcp_tool(description = "Subtract two numbers")]
26//! fn subtract(a: f64, b: f64) -> f64 { a - b }
27//!
28//! #[tokio::main]
29//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
30//! let config = McpServerConfig::builder()
31//! .name("calculator")
32//! .version("1.0.0")
33//! .with_stdio_transport()
34//! .with_tools(tools![AddTool, SubtractTool])
35//! .build();
36//!
37//! McpServer::run(config).await?;
38//! Ok(())
39//! }
40//! ```
41//!
42//! ## Connecting to Servers
43//!
44//! ```rust,ignore
45//! use mcp::{McpHub, McpServerConnectionConfig};
46//!
47//! let hub = McpHub::new();
48//!
49//! // Connect to an external server
50//! let config = McpServerConnectionConfig::stdio("my-server", "node", vec!["server.js".into()]);
51//! hub.connect(config).await?;
52//!
53//! // List available tools
54//! let tools = hub.list_all_tools().await?;
55//! ```
56//!
57//! ## Feature Flags
58//!
59//! - `default` - Enables stdio, http, and macros features
60//! - `stdio` - Stdio transport for spawning server processes
61//! - `http` - HTTP transport for connecting to HTTP servers
62//! - `http-server` - HTTP server with actix-web (for hosting MCP servers)
63//! - `macros` - Procedural macros for defining tools
64
65#![cfg_attr(docsrs, feature(doc_cfg))]
66
67pub mod protocol;
68pub mod result;
69pub mod server;
70pub mod tool;
71pub mod transport;
72
73#[cfg(feature = "macros")]
74#[cfg_attr(docsrs, doc(cfg(feature = "macros")))]
75pub mod macro_adapter;
76
77#[cfg(feature = "stdio")]
78#[cfg_attr(docsrs, doc(cfg(feature = "stdio")))]
79pub mod stdio;
80
81#[cfg(feature = "http")]
82#[cfg_attr(docsrs, doc(cfg(feature = "http")))]
83pub mod http;
84
85pub mod hub;
86
87// =============================================================================
88// Re-exports
89// =============================================================================
90
91// Protocol types
92pub use protocol::{
93 CallToolParams, CallToolResult, JsonRpcError, JsonRpcId, JsonRpcPayload, JsonRpcRequest,
94 JsonRpcResponse, ListToolsParams, ListToolsResult, McpCapabilities, McpServerInfo, McpToolDef,
95 ToolContent, ToolDefinition, ToolInputSchema, MCP_PROTOCOL_VERSION,
96};
97
98// Transport types
99pub use transport::{
100 ClientInfo, InitializeCapabilities, InitializeParams, InitializeResult,
101 McpServerConnectionConfig, McpTransport, McpTransportError, ServerCapabilities, ServerInfo,
102 TransportTypeId,
103};
104
105// Result types
106pub use result::{error_result, success_result, tool_err, tool_ok, IntoCallToolResult, ToolResult};
107
108// Tool types
109pub use tool::{
110 all_tools, tools_in_group, BoxFuture, DynTool, FnTool, McpTool, ToolCallResult, ToolEntry,
111 ToolFactory, ToolProvider, ToolRegistry,
112};
113
114// Re-export inventory for use in macro-generated code
115#[doc(hidden)]
116pub use inventory;
117
118// Server
119pub use server::{
120 McpServer, McpServerConfig, McpServerConfigBuilder, ServerError, ServerTransport,
121};
122
123// Hub
124pub use hub::McpHub;
125
126// Stdio transport (when enabled)
127#[cfg(feature = "stdio")]
128#[cfg_attr(docsrs, doc(cfg(feature = "stdio")))]
129pub use stdio::{AsyncStdioTransport, StdioTransport, StdioTransportAdapter};
130
131// HTTP transport (when enabled)
132#[cfg(feature = "http")]
133#[cfg_attr(docsrs, doc(cfg(feature = "http")))]
134pub use http::{HttpTransport, HttpTransportAdapter};
135
136// Macros (when enabled)
137#[cfg(feature = "macros")]
138#[cfg_attr(docsrs, doc(cfg(feature = "macros")))]
139pub mod macros {
140 //! Procedural macros for defining MCP servers and tools.
141 //!
142 //! This module re-exports the macros from `model-context-protocol-macros`.
143 pub use model_context_protocol_macros::{mcp_server, mcp_tool};
144}
145
146// Macro adapter (when enabled)
147#[cfg(feature = "macros")]
148#[cfg_attr(docsrs, doc(cfg(feature = "macros")))]
149pub use macro_adapter::{MacroServer, MacroServerAdapter};
150
151/// Current MCP protocol version supported by this crate.
152pub const PROTOCOL_VERSION: &str = "2024-11-05";
153
154/// Crate version.
155pub const VERSION: &str = env!("CARGO_PKG_VERSION");
156
157#[cfg(test)]
158mod tests {
159 use super::*;
160
161 #[test]
162 fn test_protocol_version() {
163 assert_eq!(PROTOCOL_VERSION, "2024-11-05");
164 }
165
166 #[test]
167 fn test_version() {
168 assert!(!VERSION.is_empty());
169 }
170
171 #[tokio::test]
172 async fn test_hub_basic() {
173 let hub = McpHub::new();
174 assert!(!hub.list_servers().is_empty() || hub.list_servers().is_empty());
175 }
176}