Skip to content

Commit 23e8200

Browse files
authored
fix(examples): Make mcp_oauth_server example worked in MCP inspector OAuth (#219)
1 parent 22134eb commit 23e8200

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

examples/servers/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ chrono = "0.4"
2727
uuid = { version = "1.6", features = ["v4", "serde"] }
2828
serde_urlencoded = "0.7"
2929
askama = { version = "0.14"}
30+
tower-http = { version = "0.6", features = ["cors"] }
3031

3132
# [dev-dependencies.'cfg(target_arch="linux")'.dependencies]
3233

examples/servers/src/mcp_oauth_server.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ use rmcp::transport::{
2121
sse_server::SseServerConfig,
2222
};
2323
use serde::{Deserialize, Serialize};
24+
use serde_json::Value;
2425
use tokio::sync::RwLock;
2526
use tokio_util::sync::CancellationToken;
27+
use tower_http::cors::{Any, CorsLayer};
2628
use tracing::{debug, error, info, warn};
2729
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
2830
use uuid::Uuid;
@@ -518,14 +520,23 @@ async fn validate_token_middleware(
518520

519521
// handle oauth server metadata request
520522
async fn oauth_authorization_server() -> impl IntoResponse {
523+
let mut additional_fields = HashMap::new();
524+
additional_fields.insert(
525+
"response_types_supported".into(),
526+
Value::Array(vec![Value::String("code".into())]),
527+
);
528+
additional_fields.insert(
529+
"code_challenge_methods_supported".into(),
530+
Value::Array(vec![Value::String("S256".into())]),
531+
);
521532
let metadata = AuthorizationMetadata {
522533
authorization_endpoint: format!("http://{}/oauth/authorize", BIND_ADDRESS),
523534
token_endpoint: format!("http://{}/oauth/token", BIND_ADDRESS),
524535
scopes_supported: Some(vec!["profile".to_string(), "email".to_string()]),
525536
registration_endpoint: format!("http://{}/oauth/register", BIND_ADDRESS),
526537
issuer: Some(BIND_ADDRESS.to_string()),
527538
jwks_uri: Some(format!("http://{}/oauth/jwks", BIND_ADDRESS)),
528-
additional_fields: HashMap::new(),
539+
additional_fields,
529540
};
530541
debug!("metadata: {:?}", metadata);
531542
(StatusCode::OK, Json(metadata))
@@ -655,18 +666,33 @@ async fn main() -> Result<()> {
655666
validate_token_middleware,
656667
));
657668

669+
// Create CORS layer for the oauth authorization server endpoint
670+
let cors_layer = CorsLayer::new()
671+
.allow_origin(Any)
672+
.allow_methods(Any)
673+
.allow_headers(Any);
674+
675+
// Create a sub-router for the oauth authorization server endpoint with CORS
676+
let oauth_server_router = Router::new()
677+
.route(
678+
"/.well-known/oauth-authorization-server",
679+
get(oauth_authorization_server).options(oauth_authorization_server),
680+
)
681+
.route("/oauth/token", post(oauth_token).options(oauth_token))
682+
.route(
683+
"/oauth/register",
684+
post(oauth_register).options(oauth_register),
685+
)
686+
.layer(cors_layer)
687+
.with_state(oauth_store.clone());
688+
658689
// Create HTTP router with request logging middleware
659690
let app = Router::new()
660691
.route("/", get(index))
661692
.route("/mcp", get(index))
662-
.route(
663-
"/.well-known/oauth-authorization-server",
664-
get(oauth_authorization_server),
665-
)
666693
.route("/oauth/authorize", get(oauth_authorize))
667694
.route("/oauth/approve", post(oauth_approve))
668-
.route("/oauth/token", post(oauth_token))
669-
.route("/oauth/register", post(oauth_register))
695+
.merge(oauth_server_router) // Merge the CORS-enabled oauth server router
670696
// .merge(protected_sse_router)
671697
.with_state(oauth_store.clone())
672698
.layer(middleware::from_fn(log_request));

0 commit comments

Comments
 (0)