An OAuth-protected MCP (Model Context Protocol) server demonstrating JWT token validation and scope-based access control.
- Java 17 or later
- Maven 3.8 or later
mvn package./run_example.sh --no-auth./run_example.sh --config server.configjava -jar target/auth-mcp-server-1.0.0.jar server.configCreate a server.config file with the following options:
# Server settings
host=0.0.0.0
port=3001
# OAuth/IDP settings
client_id=my-client
client_secret=my-secret
auth_server_url=https://keycloak.example.com/realms/mcp
# Scopes
allowed_scopes=openid profile email mcp:read mcp:admin
# Cache settings
jwks_cache_duration=3600
jwks_auto_refresh=true
request_timeout=30
# Auth bypass mode (for development)
auth_disabled=truecurl http://localhost:3001/health# Protected Resource Metadata (RFC 9728)
curl http://localhost:3001/.well-known/oauth-protected-resource
# Authorization Server Metadata (RFC 8414)
curl http://localhost:3001/.well-known/oauth-authorization-server
# OpenID Configuration
curl http://localhost:3001/.well-known/openid-configurationcurl -X POST http://localhost:3001/mcp \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","id":1,"method":"initialize"}'curl -X POST http://localhost:3001/mcp \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","id":2,"method":"tools/list"}'curl -X POST http://localhost:3001/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc":"2.0",
"id":3,
"method":"tools/call",
"params":{
"name":"get-weather",
"arguments":{"city":"London"}
}
}'curl -X POST http://localhost:3001/mcp \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_TOKEN" \
-d '{
"jsonrpc":"2.0",
"id":4,
"method":"tools/call",
"params":{
"name":"get-forecast",
"arguments":{"city":"Tokyo"}
}
}'curl -X POST http://localhost:3001/mcp \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_TOKEN" \
-d '{
"jsonrpc":"2.0",
"id":5,
"method":"tools/call",
"params":{
"name":"get-weather-alerts",
"arguments":{"region":"California"}
}
}'| Tool | Description | Required Scope |
|---|---|---|
| get-weather | Get current weather for a city | None |
| get-forecast | Get 5-day weather forecast | mcp:read |
| get-weather-alerts | Get weather alerts for a region | mcp:admin |
When authentication is enabled, protected endpoints require a valid JWT bearer token:
curl -X POST http://localhost:3001/mcp \
-H "Authorization: Bearer eyJhbGciOiJSUzI1NiJ9..." \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","id":1,"method":"tools/list"}'The token can also be passed as a query parameter:
curl -X POST "http://localhost:3001/mcp?access_token=eyJhbGciOiJSUzI1NiJ9..." \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","id":1,"method":"tools/list"}'MIT License