Expand description
§MCP SDK for Rust
A high-quality Rust implementation of the Model Context Protocol (MCP) SDK.
This crate provides both client and server implementations of MCP with:
- Full protocol compatibility with the TypeScript SDK
- Zero-copy parsing where possible
- Comprehensive type safety
- Multiple transport options (stdio, HTTP/SSE, WebSocket)
- Built-in authentication support
§Quick Start
§Client Example
use pmcp::{Client, StdioTransport, ClientCapabilities};
// Create a client with stdio transport
let transport = StdioTransport::new();
let mut client = Client::new(transport);
// Initialize the connection
let server_info = client.initialize(ClientCapabilities::default()).await?;
// List available tools
let tools = client.list_tools(None).await?;§Server Example
use pmcp::{Server, ServerCapabilities, ToolHandler};
use async_trait::async_trait;
use serde_json::Value;
struct MyTool;
#[async_trait]
impl ToolHandler for MyTool {
async fn handle(&self, args: Value, _extra: pmcp::RequestHandlerExtra) -> Result<Value, pmcp::Error> {
Ok(serde_json::json!({"result": "success"}))
}
}
let server = Server::builder()
.name("my-server")
.version("1.0.0")
.capabilities(ServerCapabilities::default())
.tool("my-tool", MyTool)
.build()?;
// Run with stdio transport
server.run_stdio().await?;Re-exports§
pub use client::Client;pub use client::ClientBuilder;pub use error::Error;pub use error::ErrorCode;pub use error::Result;pub use server::cancellation::RequestHandlerExtra;Non-WebAssembly pub use server::auth;Non-WebAssembly pub use server::simple_prompt::SimplePrompt;Non-WebAssembly pub use server::simple_prompt::SyncPrompt;Non-WebAssembly pub use server::simple_resources::DynamicResourceHandler;Non-WebAssembly pub use server::simple_resources::ResourceCollection;Non-WebAssembly pub use server::simple_resources::StaticResource;Non-WebAssembly pub use server::simple_tool::SimpleTool;Non-WebAssembly pub use server::simple_tool::SyncTool;Non-WebAssembly pub use server::typed_tool::SimpleToolExt;Non-WebAssembly pub use server::typed_tool::SyncToolExt;Non-WebAssembly pub use server::typed_tool::TypedSyncTool;Non-WebAssembly pub use server::typed_tool::TypedTool;Non-WebAssembly pub use server::typed_tool::TypedToolWithOutput;Non-WebAssembly pub use server::PromptHandler;Non-WebAssembly pub use server::ResourceHandler;Non-WebAssembly pub use server::SamplingHandler;Non-WebAssembly pub use server::Server;Non-WebAssembly pub use server::ServerBuilder;Non-WebAssembly pub use server::ToolHandler;Non-WebAssembly pub use shared::StdioTransport;Non-WebAssembly pub use shared::batch::BatchRequest;pub use shared::batch::BatchResponse;pub use shared::uri_template::UriTemplate;pub use shared::AuthMiddleware;pub use shared::LoggingMiddleware;pub use shared::Middleware;pub use shared::MiddlewareChain;pub use shared::RetryMiddleware;pub use shared::Transport;pub use shared::WebSocketConfig;websocketand non-WebAssemblypub use shared::WebSocketTransport;websocketand non-WebAssemblypub use shared::HttpConfig;httpand non-WebAssemblypub use shared::HttpTransport;httpand non-WebAssemblypub use types::AuthInfo;pub use types::AuthScheme;pub use types::CallToolRequest;pub use types::CallToolResult;pub use types::ClientCapabilities;pub use types::ClientNotification;pub use types::ClientRequest;pub use types::CompleteRequest;pub use types::CompleteResult;pub use types::CompletionArgument;pub use types::CompletionReference;pub use types::Content;pub use types::CreateMessageParams;pub use types::CreateMessageRequest;pub use types::CreateMessageResult;pub use types::GetPromptResult;pub use types::Implementation;pub use types::IncludeContext;pub use types::ListResourcesResult;pub use types::ListToolsResult;pub use types::LoggingLevel;pub use types::MessageContent;pub use types::ModelPreferences;pub use types::ProgressNotification;pub use types::ProgressToken;pub use types::PromptMessage;pub use types::ProtocolVersion;pub use types::ReadResourceResult;pub use types::RequestId;pub use types::ResourceInfo;pub use types::Role;pub use types::RootsCapabilities;pub use types::SamplingCapabilities;pub use types::SamplingMessage;pub use types::ServerCapabilities;pub use types::ServerNotification;pub use types::ServerRequest;pub use types::TokenUsage;pub use types::ToolCapabilities;pub use types::ToolInfo;pub use types::CallToolResult as ToolResult;pub use utils::BatchingConfig;Non-WebAssembly pub use utils::DebouncingConfig;Non-WebAssembly pub use utils::MessageBatcher;Non-WebAssembly pub use utils::MessageDebouncer;Non-WebAssembly
Modules§
- client
- MCP client implementation.
- error
- Error types for the MCP SDK.
- runtime
- Runtime abstraction layer for platform-independent async execution
- server
- MCP server implementation.
- shared
- Shared components used by both client and server.
- simd
simd - SIMD optimizations for PMCP performance-critical operations Provides vectorized implementations of JSON parsing and string operations
- types
- Core protocol types for MCP.
- utils
- Utility modules for the MCP SDK.
Macros§
- context_
or_ new - Macro to get current context or create new one.
- debug_
correlated - Log debug with correlation.
- error_
correlated - Log error with correlation.
- info_
correlated - Log info with correlation.
- log_
correlated - Helper macros for correlated logging.
- warn_
correlated - Log warning with correlation.
- with_
context - Macro to run code with request context.
Constants§
- DEFAULT_
PROTOCOL_ VERSION - Default protocol version to use for negotiation
- DEFAULT_
REQUEST_ TIMEOUT_ MS - Default request timeout in milliseconds
- LATEST_
PROTOCOL_ VERSION - Protocol version constants
- SUPPORTED_
PROTOCOL_ VERSIONS - List of all protocol versions supported by this SDK
Functions§
- log
- Server-side logging function (placeholder for examples).