This directory contains Model Context Protocol (MCP) server examples implemented in Rust. These examples demonstrate how to create MCP servers using different transport methods and how to implement various server capabilities including tools, resources, prompts, and authentication.
A basic MCP server that communicates using standard input/output transport.
- Provides a simple counter tool with increment, decrement, and get_value operations
- Demonstrates basic tool implementation and stdio transport
A minimal server example using stdio transport.
- Lightweight server implementation
- Demonstrates basic server setup patterns
- Good starting point for custom server development
A server using streamable HTTP transport for MCP communication, with axum.
- Runs on HTTP with streaming capabilities
- Provides counter tools via HTTP streaming
- Demonstrates streamable HTTP transport configuration
A server using streamable HTTP transport for MCP communication, with hyper.
- Runs on HTTP with streaming capabilities
- Provides counter tools via HTTP streaming
- Demonstrates streamable HTTP transport configuration
A working MCP server demonstrating elicitation for user name collection.
- Real MCP server using rmcp library
context.peer.elicit::<T>()API usage- Type-safe elicitation with
elicit_safe!macro - JSON Schema validation with schemars
- Tools:
greet_user(collects name),reset_name(clears stored name)
A demonstration of elicitation using enum inference configuration for MCP server.
- Runs on HTTP with streaming capabilities
- Uses schemars for elicitation schema generation
- Shows how to configure enum inference for elicitation prompts
A server demonstrating the prompt framework capabilities.
- Shows how to implement prompts in MCP servers
- Provides code review and debugging prompts
- Demonstrates prompt argument handling with JSON schema
- Uses standard I/O transport
- Good example of prompt implementation patterns
A server that demonstrates progress notifications during long-running operations.
- Provides a stream_processor tool that generates progress notifications
- Demonstrates progress notifications during long-running operations
- Can be run with
cargo run -p mcp-server-examples --example servers_progress_demo -- {stdio|http|all}
A server demonstrating simple token-based authentication with streamable HTTP transport.
- Uses bearer token authentication via Authorization header
- Provides
/api/token/{id}endpoint to get demo tokens - Protected MCP endpoint at
/mcp - Shows how to add auth middleware to streamable HTTP services
A full OAuth 2.0 authorization server implementation with streamable HTTP MCP transport.
- Complete OAuth 2.0 authorization code flow
- Client registration endpoint
- Authorization server metadata discovery
- Protected MCP endpoint with token validation
- Demonstrates building a production-like auth server
Each example can be run using Cargo:
# Run the counter standard I/O server
cargo run -p mcp-server-examples --example servers_counter_stdio
# Run the memory standard I/O server
cargo run -p mcp-server-examples --example servers_memory_stdio
# Run the counter streamable HTTP server
cargo run -p mcp-server-examples --example servers_counter_streamhttp
# Run the elicitation standard I/O server
cargo run -p mcp-server-examples --example servers_elicitation_stdio
# Run the prompt standard I/O server
cargo run -p mcp-server-examples --example servers_prompt_stdio
# Run the simple auth streamable HTTP server
cargo run -p mcp-server-examples --example servers_simple_auth_streamhttp
# Run the complex auth streamable HTTP server
cargo run -p mcp-server-examples --example servers_complex_auth_streamhttpMany of these servers can be tested using the MCP Inspector tool: See inspector
These examples use the following main dependencies:
rmcp: Rust implementation of the MCP server librarytokio: Asynchronous runtimeserdeandserde_json: For JSON serialization and deserializationtracingandtracing-subscriber: For logginganyhow: Error handlingaxum: Web framework for HTTP-based transportstokio-util: Utilities for async programmingschemars: JSON Schema generation (used in elicitation examples)
The common/ directory contains shared code used across examples:
counter.rs: Counter tool implementation with MCP server traitscalculator.rs: Calculator tool examplesgeneric_service.rs: Generic service implementations
This modular approach allows for code reuse and demonstrates how to structure larger MCP server applications.