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 server that provides counter functionality using Server-Sent Events (SSE) transport.
- Runs on
http://127.0.0.1:8000/sseby default - Provides the same counter tools as the stdio version
- Demonstrates SSE transport setup with graceful shutdown
- Can be accessed via web browsers or SSE-compatible clients
A minimal SSE server implementation showing direct SSE server usage.
- Simplified version of the SSE server
- Demonstrates basic SSE server configuration
- Provides counter functionality with minimal setup
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 comprehensive example demonstrating OAuth 2.0 integration with MCP servers.
- Full OAuth 2.0 authorization server implementation
- Client registration and token management
- User authorization flow with web interface
- Token validation middleware
- Integrated with MCP SSE transport
- Demonstrates enterprise-grade authentication patterns
A simplified OAuth example showing basic token-based authentication.
- Basic token store and validation
- Authorization middleware for SSE endpoints
- Token generation API
- Simplified authentication flow
- Good starting point for adding authentication to MCP servers
Each example can be run using Cargo:
# Run the counter standard I/O server
cargo run --example servers_counter_stdio
# Run the counter SSE server
cargo run --example servers_counter_sse
# Run the counter SSE direct server
cargo run --example servers_counter_sse_directly
# Run the memory standard I/O server
cargo run --example servers_memory_stdio
# Run the counter streamable HTTP server
cargo run --example servers_counter_streamhttp
# Run the complex OAuth SSE server
cargo run --example servers_complex_auth_sse
# Run the simple OAuth SSE server
cargo run --example servers_simple_auth_sseMany 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 programmingaskama: Template engine (used in OAuth examples)tower-http: HTTP middleware (used for CORS in OAuth examples)uuid: UUID generation (used in OAuth examples)chrono: Date and time handling (used in OAuth examples)rand: Random number generation (used in OAuth 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.