tmcp
A Rust implementation of the Model Context Protocol for building AI-integrated applications.
Community
Want to contribute? Have ideas or feature requests? Come tell us about it on Discord.
Overview
A Rust implementation of the Model Context Protocol (MCP) - a JSON-RPC 2.0 based protocol for AI models to interact with external tools and services. Supports both client and server roles with async/await APIs.
Features
- Full MCP Protocol Support: Implements the latest MCP specification (2025-11-25)
- Client & Server: Build both MCP clients and servers with ergonomic APIs
- Multiple Transports: TCP/IP and stdio transport layers
- OAuth 2.0 Authentication: Complete OAuth 2.0 support including:
- Authorization Code Flow with PKCE
- Dynamic client registration (RFC7591)
- Automatic token refresh
- MCP-specific
resourceparameter support - Built-in callback server for browser flows
- Protected resource metadata discovery (RFC 9728)
- Authorization server discovery (RFC 8414 / OpenID Connect)
- Client ID metadata documents for HTTPS client IDs
- Async/Await: Built on Tokio for high-performance async operations
Note: Batch operations in the previous protocol version are not supported.
OAuth discovery flow
When a protected resource challenges a request, inspect any WWW-Authenticate header for a
resource_metadata value. Fetch protected resource metadata from that URL, or fall back to
/.well-known/oauth-protected-resource (with optional path suffix) when no challenge is provided.
Use the advertised authorization server issuers to resolve RFC 8414 or OpenID Connect discovery
documents, then use those endpoints for authorization and registration. If the client identifier
is an HTTPS URL, fetch the client ID metadata document at that URL to obtain redirect URIs and
additional client settings.
Example
From ./examples/weather_server.rs
//! Minimal weather server example.
use json;
use ;
/// Example server.
;
/// Parameters for the weather tool.
// Tool input schema is automatically derived from the struct using serde and schemars.
/// Structured response for the weather tool.
/// Structured response for the ping tool.
/// Parameters for emitting a log message.
/// Result of emitting a log message.
// The `mcp_server` macro generates the necessary boilerplate to expose methods as MCP tools.
async
When you serve MCP over stdio, stdout is reserved for JSON-RPC messages. Do not attach a
tracing/logging subscriber that writes human-readable logs to stdout, and do not print with
println!. Route diagnostics to stderr, a file, or another sink instead.
Flat tool arguments can be declared directly in the tool signature for multi-argument tools:
async
Single-argument tools remain struct-based by default; opt into flat handling explicitly:
async