|
| 1 | +# MCP Server Examples |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +## Example List |
| 6 | + |
| 7 | +### Counter Standard I/O Server (`counter_stdio.rs`) |
| 8 | + |
| 9 | +A basic MCP server that communicates using standard input/output transport. |
| 10 | + |
| 11 | +- Provides a simple counter tool with increment, decrement, and get_value operations |
| 12 | +- Demonstrates basic tool implementation and stdio transport |
| 13 | + |
| 14 | +### Counter SSE Server (`counter_sse.rs`) |
| 15 | + |
| 16 | +A server that provides counter functionality using Server-Sent Events (SSE) transport. |
| 17 | + |
| 18 | +- Runs on `http://127.0.0.1:8000/sse` by default |
| 19 | +- Provides the same counter tools as the stdio version |
| 20 | +- Demonstrates SSE transport setup with graceful shutdown |
| 21 | +- Can be accessed via web browsers or SSE-compatible clients |
| 22 | + |
| 23 | +### Counter SSE Direct Server (`counter_sse_directly.rs`) |
| 24 | + |
| 25 | +A minimal SSE server implementation showing direct SSE server usage. |
| 26 | + |
| 27 | +- Simplified version of the SSE server |
| 28 | +- Demonstrates basic SSE server configuration |
| 29 | +- Provides counter functionality with minimal setup |
| 30 | + |
| 31 | +### Memory Standard I/O Server (`memory_stdio.rs`) |
| 32 | + |
| 33 | +A minimal server example using stdio transport. |
| 34 | + |
| 35 | +- Lightweight server implementation |
| 36 | +- Demonstrates basic server setup patterns |
| 37 | +- Good starting point for custom server development |
| 38 | + |
| 39 | +### Counter Streamable HTTP Server (`counter_streamhttp.rs`) |
| 40 | + |
| 41 | +A server using streamable HTTP transport for MCP communication. |
| 42 | + |
| 43 | +- Runs on HTTP with streaming capabilities |
| 44 | +- Provides counter tools via HTTP streaming |
| 45 | +- Demonstrates streamable HTTP transport configuration |
| 46 | + |
| 47 | +### Complex OAuth SSE Server (`complex_auth_sse.rs`) |
| 48 | + |
| 49 | +A comprehensive example demonstrating OAuth 2.0 integration with MCP servers. |
| 50 | + |
| 51 | +- Full OAuth 2.0 authorization server implementation |
| 52 | +- Client registration and token management |
| 53 | +- User authorization flow with web interface |
| 54 | +- Token validation middleware |
| 55 | +- Integrated with MCP SSE transport |
| 56 | +- Demonstrates enterprise-grade authentication patterns |
| 57 | + |
| 58 | +### Simple OAuth SSE Server (`simple_auth_sse.rs`) |
| 59 | + |
| 60 | +A simplified OAuth example showing basic token-based authentication. |
| 61 | + |
| 62 | +- Basic token store and validation |
| 63 | +- Authorization middleware for SSE endpoints |
| 64 | +- Token generation API |
| 65 | +- Simplified authentication flow |
| 66 | +- Good starting point for adding authentication to MCP servers |
| 67 | + |
| 68 | +## How to Run |
| 69 | + |
| 70 | +Each example can be run using Cargo: |
| 71 | + |
| 72 | +```bash |
| 73 | +# Run the counter standard I/O server |
| 74 | +cargo run --example servers_counter_stdio |
| 75 | + |
| 76 | +# Run the counter SSE server |
| 77 | +cargo run --example servers_counter_sse |
| 78 | + |
| 79 | +# Run the counter SSE direct server |
| 80 | +cargo run --example servers_counter_sse_directly |
| 81 | + |
| 82 | +# Run the memory standard I/O server |
| 83 | +cargo run --example servers_memory_stdio |
| 84 | + |
| 85 | +# Run the counter streamable HTTP server |
| 86 | +cargo run --example servers_counter_streamhttp |
| 87 | + |
| 88 | +# Run the complex OAuth SSE server |
| 89 | +cargo run --example servers_complex_auth_sse |
| 90 | + |
| 91 | +# Run the simple OAuth SSE server |
| 92 | +cargo run --example servers_simple_auth_sse |
| 93 | +``` |
| 94 | + |
| 95 | +## Testing with MCP Inspector |
| 96 | + |
| 97 | +Many of these servers can be tested using the MCP Inspector tool: |
| 98 | +See [inspector](https://github.com/modelcontextprotocol/inspector) |
| 99 | + |
| 100 | +## Dependencies |
| 101 | + |
| 102 | +These examples use the following main dependencies: |
| 103 | + |
| 104 | +- `rmcp`: Rust implementation of the MCP server library |
| 105 | +- `tokio`: Asynchronous runtime |
| 106 | +- `serde` and `serde_json`: For JSON serialization and deserialization |
| 107 | +- `tracing` and `tracing-subscriber`: For logging |
| 108 | +- `anyhow`: Error handling |
| 109 | +- `axum`: Web framework for HTTP-based transports |
| 110 | +- `tokio-util`: Utilities for async programming |
| 111 | +- `askama`: Template engine (used in OAuth examples) |
| 112 | +- `tower-http`: HTTP middleware (used for CORS in OAuth examples) |
| 113 | +- `uuid`: UUID generation (used in OAuth examples) |
| 114 | +- `chrono`: Date and time handling (used in OAuth examples) |
| 115 | +- `rand`: Random number generation (used in OAuth examples) |
| 116 | + |
| 117 | +## Common Module |
| 118 | + |
| 119 | +The `common/` directory contains shared code used across examples: |
| 120 | + |
| 121 | +- `counter.rs`: Counter tool implementation with MCP server traits |
| 122 | +- `calculator.rs`: Calculator tool examples |
| 123 | +- `generic_service.rs`: Generic service implementations |
| 124 | + |
| 125 | +This modular approach allows for code reuse and demonstrates how to structure larger MCP server applications. |
0 commit comments