1- # Rust Auth MCP Server
1+ # Gopher Auth MCP Server - Rust Example
22
3- An OAuth-protected MCP (Model Context Protocol) server example demonstrating JWT token validation and scope-based access control for MCP tools .
3+ This example demonstrates an MCP (Model Context Protocol) server with OAuth 2.0 authentication using the gopher-mcp-rust SDK .
44
5- ## Features
5+ ## Overview
66
7- - ** OAuth 2.0 Protected Resources** : Implements RFC 9728 protected resource metadata
8- - ** OpenID Connect Discovery** : Supports OIDC discovery endpoints
9- - ** JWT Token Validation** : Validates tokens using gopher-auth native library
10- - ** Scope-Based Access Control** : Tools require specific scopes (e.g., ` mcp:read ` , ` mcp:admin ` )
11- - ** MCP Protocol Support** : Full JSON-RPC 2.0 implementation for MCP tools
12- - ** Weather Tools Example** : Three tools demonstrating different access levels
7+ The auth example server provides:
8+ - OAuth 2.0 / OIDC discovery endpoints (RFC 8414, RFC 9728)
9+ - JWT token validation via native library
10+ - Scope-based authorization for MCP tools
11+ - Example weather tools with different scope requirements
1312
14- ## Requirements
13+ ## Prerequisites
1514
1615- Rust 1.70 or later
17- - (Optional) gopher-auth native library for JWT validation
16+ - GitHub CLI ( ` gh ` ) for downloading native libraries
1817
19- ## Quick Start
20-
21- ### Run Without Authentication
18+ ## Installation
2219
23- The fastest way to try the server:
20+ ### 1. Clone or Copy This Example
2421
2522``` bash
26- ./run_example.sh --no-auth
23+ # Option A: Clone the repository
24+ git clone https://github.com/GopherSecurity/gopher-mcp-rust.git
25+ cd gopher-mcp-rust/examples/auth
26+
27+ # Option B: Copy the example files to your project
28+ # Copy the examples/auth directory contents
29+ ```
30+
31+ ### 2. Install the Rust SDK
32+
33+ The SDK is specified in ` Cargo.toml ` as a git dependency:
34+
35+ ``` toml
36+ [dependencies ]
37+ gopher-orch = { git = " https://github.com/GopherSecurity/gopher-mcp-rust.git" , features = [" auth" ] }
2738```
2839
29- Or manually:
40+ ### 3. Download Native Libraries
41+
42+ The SDK requires native libraries for OAuth token validation. The ` run_example.sh ` script downloads these automatically, or you can install them manually:
3043
3144``` bash
32- cargo run
45+ # Using the run script (downloads automatically)
46+ ./run_example.sh --no-auth
47+
48+ # Or download manually using the install script
49+ curl -sSL https://raw.githubusercontent.com/GopherSecurity/gopher-mcp-rust/main/install-native.sh | bash -s -- latest ./native
3350```
3451
35- ### Run With Configuration
52+ ## Quick Start
53+
54+ ### Development Mode (No Auth)
3655
3756``` bash
38- ./run_example.sh --config server.config
57+ # Run with auth disabled (all requests bypass authentication)
58+ ./run_example.sh --no-auth
59+
60+ # Or build and run manually
61+ cargo build --release
62+ ./target/release/auth-mcp-server
3963```
4064
41- ### Build Release
65+ ### With Full OAuth Support
4266
4367``` bash
68+ # Run with OAuth authentication enabled
69+ ./run_example.sh
70+
71+ # Or build manually with environment set
72+ export DYLD_LIBRARY_PATH=" ./native/lib:$DYLD_LIBRARY_PATH "
4473cargo build --release
4574./target/release/auth-mcp-server server.config
4675```
4776
77+ ### Using Environment Variables
78+
79+ ``` bash
80+ # Use a specific SDK version
81+ SDK_VERSION=v0.1.3 ./run_example.sh
82+
83+ # Use custom native library location
84+ NATIVE_LIB_DIR=/usr/local/lib ./run_example.sh --skip-download
85+ ```
86+
4887## Configuration
4988
5089Create a ` server.config ` file with INI-style key-value pairs:
@@ -88,7 +127,7 @@ auth_disabled=true
88127| ` request_timeout ` | HTTP request timeout in seconds | ` 30 ` |
89128| ` auth_disabled ` | Disable authentication | ` false ` |
90129
91- ## API Endpoints
130+ ## Available Endpoints
92131
93132### Health Check
94133
@@ -122,9 +161,10 @@ curl -X POST http://localhost:3001/mcp \
122161 -H " Content-Type: application/json" \
123162 -d ' {"jsonrpc":"2.0","id":2,"method":"tools/list"}'
124163
125- # Call Tool
164+ # Call Tool (with auth)
126165curl -X POST http://localhost:3001/mcp \
127166 -H " Content-Type: application/json" \
167+ -H " Authorization: Bearer YOUR_TOKEN" \
128168 -d ' {"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"get-weather","arguments":{"city":"NYC"}}}'
129169```
130170
@@ -136,14 +176,83 @@ curl -X POST http://localhost:3001/mcp \
136176| ` get-forecast ` | ` mcp:read ` | Get 5-day weather forecast |
137177| ` get-weather-alerts ` | ` mcp:admin ` | Get weather alerts for a region |
138178
179+ ### Tool Examples
180+
181+ ``` bash
182+ # get-weather (no auth required)
183+ curl -X POST http://localhost:3001/mcp \
184+ -H " Content-Type: application/json" \
185+ -d ' {"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"get-weather","arguments":{"city":"Tokyo"}}}'
186+
187+ # get-forecast (requires mcp:read scope)
188+ curl -X POST http://localhost:3001/mcp \
189+ -H " Content-Type: application/json" \
190+ -H " Authorization: Bearer TOKEN_WITH_MCP_READ" \
191+ -d ' {"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"get-forecast","arguments":{"city":"Paris"}}}'
192+
193+ # get-weather-alerts (requires mcp:admin scope)
194+ curl -X POST http://localhost:3001/mcp \
195+ -H " Content-Type: application/json" \
196+ -H " Authorization: Bearer TOKEN_WITH_MCP_ADMIN" \
197+ -d ' {"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"get-weather-alerts","arguments":{"region":"California"}}}'
198+ ```
199+
200+ ## Troubleshooting
201+
202+ ### "Native library not found" at runtime
203+
204+ The native gopher-orch library is required for JWT validation:
205+
206+ ``` bash
207+ # Download using the run script
208+ ./run_example.sh
209+
210+ # Or manually download
211+ curl -sSL https://raw.githubusercontent.com/GopherSecurity/gopher-mcp-rust/main/install-native.sh | bash -s -- latest ./native
212+ ```
213+
214+ Verify the library is installed:
215+ ``` bash
216+ ls -la ./native/lib/libgopher-orch*
217+ ```
218+
219+ ### Library path not set
220+
221+ ``` bash
222+ # macOS
223+ export DYLD_LIBRARY_PATH=" ./native/lib:$DYLD_LIBRARY_PATH "
224+
225+ # Linux
226+ export LD_LIBRARY_PATH=" ./native/lib:$LD_LIBRARY_PATH "
227+ ```
228+
229+ ### "Auth client creation failed"
230+
231+ Check that:
232+ 1 . ` jwks_uri ` points to a valid JWKS endpoint
233+ 2 . ` issuer ` matches the token issuer
234+ 3 . Network can reach the auth server
235+
236+ ### "Token validation failed"
237+
238+ Ensure:
239+ 1 . Token is not expired
240+ 2 . Token issuer matches config
241+ 3 . Token was signed by a key in JWKS
242+ 4 . Required scopes are present in token
243+
139244## Project Structure
140245
141246```
142- examples/auth/
143- ├── Cargo.toml # Dependencies and metadata
144- ├── server.config # Default configuration
145- ├── run_example.sh # Launcher script
247+ auth/
248+ ├── Cargo.toml # Dependencies (uses gopher-orch SDK)
249+ ├── Cargo.lock # Dependency lock file
250+ ├── server.config # Example configuration
251+ ├── run_example.sh # Build and run script
146252├── README.md # This file
253+ ├── native/ # Downloaded native libraries
254+ │ ├── lib/ # .dylib/.so files
255+ │ └── include/ # Header files
147256└── src/
148257 ├── main.rs # Entry point and router setup
149258 ├── config.rs # Configuration parsing
@@ -184,6 +293,17 @@ cargo test -- --nocapture
184293| Variable | Description |
185294| ----------| -------------|
186295| ` RUST_LOG ` | Log level (e.g., ` info ` , ` debug ` , ` trace ` ) |
296+ | ` SDK_VERSION ` | Version of gopher-mcp-rust SDK (default: v0.1.2) |
297+ | ` NATIVE_LIB_DIR ` | Directory for native libraries (default: ./native/lib) |
298+ | ` DYLD_LIBRARY_PATH ` | macOS library search path |
299+ | ` LD_LIBRARY_PATH ` | Linux library search path |
300+
301+ ## SDK Documentation
302+
303+ For more information about the gopher-mcp-rust SDK:
304+
305+ - Repository: https://github.com/GopherSecurity/gopher-mcp-rust
306+ - Documentation: https://docs.rs/gopher-orch (after crates.io publish)
187307
188308## License
189309
0 commit comments