Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(mcp): cors headers, de-auth preflight (#2644)

Eric Hayesb34436a0b3c6
diff --git a/rust/cloud-storage/Cargo.lock b/rust/cloud-storage/Cargo.lock
index 29b44f7311..72c632a2b8 100644
--- a/rust/cloud-storage/Cargo.lock
+++ b/rust/cloud-storage/Cargo.lock
@@ -6457,6 +6457,7 @@ dependencies = [
"sha2",
"thiserror 2.0.17",
"tower 0.5.2",
+ "tower-http",
"tracing",
"url",
"urlencoding",
diff --git a/rust/cloud-storage/mcp_auth_proxy/Cargo.toml b/rust/cloud-storage/mcp_auth_proxy/Cargo.toml
index 5220dba4f9..4e44a56876 100644
--- a/rust/cloud-storage/mcp_auth_proxy/Cargo.toml
+++ b/rust/cloud-storage/mcp_auth_proxy/Cargo.toml
@@ -12,6 +12,7 @@ inbound = [
"dep:macro_auth",
"dep:macro_user_id",
"dep:tower",
+ "dep:tower-http",
]
outbound = ["dep:fusionauth"]
@@ -28,6 +29,7 @@ serde_json = { workspace = true }
sha2 = { workspace = true }
thiserror = { workspace = true }
tower = { workspace = true, features = ["util"], optional = true }
+tower-http = { workspace = true, optional = true }
tracing = { workspace = true }
url = { workspace = true }
urlencoding = { workspace = true }
diff --git a/rust/cloud-storage/mcp_auth_proxy/src/domain/service.rs b/rust/cloud-storage/mcp_auth_proxy/src/domain/service.rs
index 0bb08eca02..c6b7b2301f 100644
--- a/rust/cloud-storage/mcp_auth_proxy/src/domain/service.rs
+++ b/rust/cloud-storage/mcp_auth_proxy/src/domain/service.rs
@@ -334,7 +334,15 @@ fn is_allowed_redirect_uri(uri: &str) -> bool {
return false;
};
- matches!(parsed.host_str(), Some("localhost" | "127.0.0.1" | "[::1]"))
+ if parsed.scheme() == "https" {
+ return true;
+ }
+
+ if parsed.scheme() == "http" {
+ return matches!(parsed.host_str(), Some("localhost" | "127.0.0.1" | "[::1]"));
+ }
+
+ false
}
/// Errors returned when starting authorization.
@@ -346,8 +354,8 @@ pub enum StartAuthorizationError {
/// Only S256 PKCE is supported.
#[error("unsupported code_challenge_method")]
UnsupportedCodeChallengeMethod,
- /// Only loopback redirect URIs are allowed.
- #[error("redirect_uri must be a loopback address")]
+ /// Only https or loopback http redirect URIs are allowed.
+ #[error("redirect_uri must be https or a loopback address")]
InvalidRedirectUri,
/// Inflight auth state could not be persisted.
#[error("failed to persist inflight auth state")]
diff --git a/rust/cloud-storage/mcp_auth_proxy/src/inbound/axum_router.rs b/rust/cloud-storage/mcp_auth_proxy/src/inbound/axum_router.rs
index da86f0bb13..34fb7dbb42 100644
--- a/rust/cloud-storage/mcp_auth_proxy/src/inbound/axum_router.rs
+++ b/rust/cloud-storage/mcp_auth_proxy/src/inbound/axum_router.rs
@@ -1,13 +1,17 @@
//! Axum router for the MCP OAuth broker.
+use std::time::Duration;
+
use axum::{
Router,
extract::{Query, State},
+ http::{HeaderName, Method, header},
middleware as axum_mw,
response::{IntoResponse, Json, Redirect, Response},
routing,
};
use macro_auth::middleware::decode_jwt::JwtValidationArgs;
+use tower_http::cors::{AllowOrigin, CorsLayer};
use crate::domain::{
models::{AuthorizeRequest, CallbackRequest, TokenRequest},
@@ -59,7 +63,7 @@ async fn authorize<I: InflightAuthStore + 'static>(
.into_response(),
Err(StartAuthorizationError::InvalidRedirectUri) => (
axum::http::StatusCode::BAD_REQUEST,
- "redirect_uri must be a loopback address",
+ "redirect_uri must be https or a loopback address",
)
.into_response(),
Err(StartAuthorizationError::InflightStore(error)) => {
@@ -235,5 +239,29 @@ where
super::middleware::validate_bearer,
));
- oauth_routes.merge(mcp_route)
+ oauth_routes.merge(mcp_route).layer(mcp_cors_layer())
+}
+
+/// CORS layer for the MCP router.
+///
+/// Applied outside the bearer middleware so OPTIONS preflights short-circuit
+/// to 204 (never hitting auth) and 401 challenges still carry CORS headers —
+/// both required for browser clients like claude.ai to complete the OAuth
+/// dance over the MCP streamable HTTP transport.
+fn mcp_cors_layer() -> CorsLayer {
+ CorsLayer::new()
+ .allow_origin(AllowOrigin::mirror_request())
+ .allow_credentials(true)
+ .allow_methods([Method::GET, Method::POST, Method::DELETE, Method::OPTIONS])
+ .allow_headers([
+ header::CONTENT_TYPE,
+ header::AUTHORIZATION,
+ HeaderName::from_static("mcp-protocol-version"),
+ HeaderName::from_static("mcp-session-id"),
+ ])
+ .expose_headers([
+ HeaderName::from_static("mcp-session-id"),
+ header::WWW_AUTHENTICATE,
+ ])
+ .max_age(Duration::from_secs(3600))
}