From b1288078ac5bb562555c9692b580975caa188e52 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Sat, 31 Oct 2026 03:02:02 -0700 Subject: [PATCH] fix(gateway): accept x-api-key on the MCP REST routes The Python proxy guards /mcp-rest/tools/list and /mcp-rest/tools/call with user_api_key_auth, which reads x-api-key, so the gateway now authenticates those two routes with BearerOrXApiKey. MCP protocol routes keep Bearer. litellm_gateway_mcp::routes returns the protocol and REST routes apart so the host can give each group its own extractor, and the session transport is returned as a service so a stateless protocol router can mount it. The 401 text now says "missing or invalid API key", since the key may come from x-api-key. --- litellm-rust/crates/gateway-auth/src/error.rs | 2 +- litellm-rust/crates/gateway-mcp/AGENTS.md | 2 +- litellm-rust/crates/gateway-mcp/src/lib.rs | 2 +- litellm-rust/crates/gateway-mcp/src/routes.rs | 18 +++++++-- .../crates/gateway-mcp/src/sessions.rs | 20 ++++++---- litellm-rust/crates/gateway/AGENTS.md | 2 +- litellm-rust/crates/gateway/src/lib.rs | 11 ++++-- litellm-rust/crates/gateway/src/main.rs | 6 +-- litellm-rust/crates/gateway/tests/mcp.rs | 39 +++++++++++++++---- 9 files changed, 75 insertions(+), 27 deletions(-) diff --git a/litellm-rust/crates/gateway-auth/src/error.rs b/litellm-rust/crates/gateway-auth/src/error.rs index 7bb6292b18c..a7f7ac42144 100644 --- a/litellm-rust/crates/gateway-auth/src/error.rs +++ b/litellm-rust/crates/gateway-auth/src/error.rs @@ -6,7 +6,7 @@ use litellm_error::Source; #[derive(Debug, thiserror::Error)] pub enum Error { - #[error("missing or invalid bearer token")] + #[error("missing or invalid API key")] InvalidToken, #[error("invalid UI credentials or session")] InvalidSession, diff --git a/litellm-rust/crates/gateway-mcp/AGENTS.md b/litellm-rust/crates/gateway-mcp/AGENTS.md index c4b00e3acbf..f462e44b9da 100644 --- a/litellm-rust/crates/gateway-mcp/AGENTS.md +++ b/litellm-rust/crates/gateway-mcp/AGENTS.md @@ -2,7 +2,7 @@ Native MCP routing using the official [Rust SDK](https://github.com/modelcontextprotocol/rust-sdk), pinned to `rmcp 3.4.1`. The crate exposes a mountable Axum router and an SDK `ServerHandler` -The HTTP router serves `/mcp`, `/{server}/mcp`, `/mcp/{server}`, `/mcp/{server}/mcp`, legacy `/mcp/sse` and `/mcp/sse/messages`, `/mcp/enabled`, and `/mcp-rest/tools/list` and `/mcp-rest/tools/call`. Streamable HTTP supports stateless requests and legacy initialize/session flows. The caller supplies the shutdown token, allowed hosts, allowed origins, and server identity through `HttpConfig` +The HTTP router serves `/mcp`, `/{server}/mcp`, `/mcp/{server}`, `/mcp/{server}/mcp`, legacy `/mcp/sse` and `/mcp/sse/messages`, `/mcp/enabled`, and `/mcp-rest/tools/list` and `/mcp-rest/tools/call`. `routes` returns the protocol routes and the `/mcp-rest` routes apart so a host can authenticate each group its own way, and `router` merges them. Streamable HTTP supports stateless requests and legacy initialize/session flows. The caller supplies the shutdown token, allowed hosts, allowed origins, and server identity through `HttpConfig` `NativeGateway` aggregates upstream tools, prompts, resources, and resource templates. Tool and prompt calls resolve registered server prefixes, including prefixes containing hyphens. Resource names receive prefixes while their URIs remain unchanged, and reads require exactly one selected server. Tool allowlists apply to discovery and execution. Upstream pagination is drained with cycle detection. Aggregate tool listings retain healthy servers and expose sanitized outcomes under `_meta["litellm.ai/server_outcomes"]`, while scoped REST listings preserve upstream failures diff --git a/litellm-rust/crates/gateway-mcp/src/lib.rs b/litellm-rust/crates/gateway-mcp/src/lib.rs index fcb7fd98383..a9624a7d36c 100644 --- a/litellm-rust/crates/gateway-mcp/src/lib.rs +++ b/litellm-rust/crates/gateway-mcp/src/lib.rs @@ -18,7 +18,7 @@ pub use error::{ConfigFault, ConnectError, Error}; pub use native::{Limits, NativeGateway, Registry, Server, ServerResolver}; pub use relay::RelayClient; pub use rmcp; -pub use routes::{HttpConfig, router}; +pub use routes::{HttpConfig, Routes, router, routes}; pub use server::McpServer; pub use sessions::SessionOwner; diff --git a/litellm-rust/crates/gateway-mcp/src/routes.rs b/litellm-rust/crates/gateway-mcp/src/routes.rs index 56d67a5bc77..30dd7cd9120 100644 --- a/litellm-rust/crates/gateway-mcp/src/routes.rs +++ b/litellm-rust/crates/gateway-mcp/src/routes.rs @@ -43,7 +43,17 @@ impl Default for HttpConfig { } } +pub struct Routes { + pub protocol: Router, + pub rest: Router, +} + pub fn router(operations: Arc, config: HttpConfig) -> Router { + let Routes { protocol, rest } = routes(operations, config); + protocol.merge(rest) +} + +pub fn routes(operations: Arc, config: HttpConfig) -> Routes { let server = McpServer::new(operations.clone()).with_info(config.server_info.clone()); let legacy = crate::legacy::router(operations.clone(), server.clone(), &config); let transport_config = StreamableHttpServerConfig::default() @@ -59,7 +69,7 @@ pub fn router(operations: Arc, config: HttpConfig) -> Router { .route_service("/{server}/mcp", service.clone()) .route_service("/{server}/mcp/", service.clone()) .route_layer(middleware::from_fn(scope_server)); - Router::new() + let protocol = Router::new() .route_service("/mcp", service.clone()) .route_service("/mcp/", service) .merge(scoped) @@ -67,10 +77,12 @@ pub fn router(operations: Arc, config: HttpConfig) -> Router { "/mcp/enabled", get(|| async { Json(Enabled { enabled: true }) }), ) + .merge(legacy); + let rest = Router::new() .route("/mcp-rest/tools/list", get(rest_list)) .route("/mcp-rest/tools/call", post(rest_call)) - .with_state(operations) - .merge(legacy) + .with_state(operations); + Routes { protocol, rest } } async fn scope_server(Path(server): Path, mut request: Request, next: Next) -> Response { diff --git a/litellm-rust/crates/gateway-mcp/src/sessions.rs b/litellm-rust/crates/gateway-mcp/src/sessions.rs index 583a8b86341..67656f354db 100644 --- a/litellm-rust/crates/gateway-mcp/src/sessions.rs +++ b/litellm-rust/crates/gateway-mcp/src/sessions.rs @@ -6,7 +6,7 @@ use axum::{ extract::{Request, State}, http::Method, response::IntoResponse, - routing::any, + routing::{RouterIntoService, any}, }; use moka::future::Cache; use rmcp::{ @@ -31,7 +31,10 @@ struct Transport { owners: Cache, } -pub(crate) fn transport(server: McpServer, config: StreamableHttpServerConfig) -> Router { +pub(crate) fn transport( + server: McpServer, + config: StreamableHttpServerConfig, +) -> RouterIntoService { let manager = Arc::new(LocalSessionManager::default()); let cleanup = manager.clone(); let owners = Cache::builder() @@ -55,11 +58,14 @@ pub(crate) fn transport(server: McpServer, config: StreamableHttpServerConfig) - Arc::new(LocalSessionManager::default()), config.with_legacy_session_mode(false), ); - Router::new().fallback(any(dispatch)).with_state(Transport { - stateful: Router::new().fallback_service(stateful), - stateless: Router::new().fallback_service(stateless), - owners, - }) + Router::new() + .fallback(any(dispatch)) + .with_state(Transport { + stateful: Router::new().fallback_service(stateful), + stateless: Router::new().fallback_service(stateless), + owners, + }) + .into_service() } async fn dispatch( diff --git a/litellm-rust/crates/gateway/AGENTS.md b/litellm-rust/crates/gateway/AGENTS.md index 5cbe448ab19..4b7b80afcdc 100644 --- a/litellm-rust/crates/gateway/AGENTS.md +++ b/litellm-rust/crates/gateway/AGENTS.md @@ -1,6 +1,6 @@ - Keep this crate a thin composition layer: mount endpoint routers and serve the supplied listener - Server lifecycle and shared inbound middleware belong here, including client authentication, rate limiting, and request logging -- Inference routes authenticate with `BearerOrXApiKey`, since the proxy accepts an Anthropic `x-api-key` there. MCP routes keep `Bearer`, since the proxy's MCP admission never reads `x-api-key` +- Inference and `/mcp-rest` routes authenticate with `BearerOrXApiKey`, since the proxy accepts an Anthropic `x-api-key` there. MCP protocol routes keep `Bearer`, since the proxy's MCP admission never reads `x-api-key` - Endpoint paths, request handling, model resolution, and response encoding belong to the mounted crates. Provider execution belongs to the `inference-` crates and `llms` - Inject shared state and infrastructure. Avoid global runtimes, duplicate client pools, and abstractions for hypothetical endpoint groups - Test mounting and server lifecycle through public HTTP behavior. Test endpoint semantics in the owning crate diff --git a/litellm-rust/crates/gateway/src/lib.rs b/litellm-rust/crates/gateway/src/lib.rs index 985d9d844b8..9d358ddb7fa 100644 --- a/litellm-rust/crates/gateway/src/lib.rs +++ b/litellm-rust/crates/gateway/src/lib.rs @@ -126,14 +126,19 @@ pub fn router( inference: Arc, config: &Config, ui: Option, - mcp: Option, + mcp: Option, ) -> Router { let auth = Auth::from_config(config, inference.secrets.clone()); + let (protocol, rest) = mcp.map(|mcp| (mcp.protocol, mcp.rest)).unzip(); let inference = authenticated( - litellm_gateway_inference::router(inference), + litellm_gateway_inference::router(inference).merge(rest.unwrap_or_default()), auth.clone().with_extractor(Arc::new(BearerOrXApiKey)), ) - .merge(mcp.map(|mcp| authenticated(mcp, auth)).unwrap_or_default()) + .merge( + protocol + .map(|protocol| authenticated(protocol, auth)) + .unwrap_or_default(), + ) .layer(axum::middleware::from_fn(log_request)); let health = Router::new().route("/health/readiness", axum::routing::get(readiness)); match ui { diff --git a/litellm-rust/crates/gateway/src/main.rs b/litellm-rust/crates/gateway/src/main.rs index 47c1fe83f50..3dbb5113e3a 100644 --- a/litellm-rust/crates/gateway/src/main.rs +++ b/litellm-rust/crates/gateway/src/main.rs @@ -79,9 +79,9 @@ async fn main() -> Result<(), Box> { &inference.http, ) .await?; - let mcp_router = mcp.as_ref().map(|gateway| { + let mcp_routes = mcp.as_ref().map(|gateway| { let defaults = litellm_gateway_mcp::HttpConfig::default(); - litellm_gateway_mcp::router( + litellm_gateway_mcp::routes( gateway.operations.clone(), litellm_gateway_mcp::HttpConfig { allowed_hosts: config @@ -102,7 +102,7 @@ async fn main() -> Result<(), Box> { let result = axum::serve( listener, - litellm_gateway::router(inference, &config, ui, mcp_router), + litellm_gateway::router(inference, &config, ui, mcp_routes), ) .with_graceful_shutdown(shutdown.clone().cancelled_owned()) .await; diff --git a/litellm-rust/crates/gateway/tests/mcp.rs b/litellm-rust/crates/gateway/tests/mcp.rs index 06b4e0b439c..2278f8f7305 100644 --- a/litellm-rust/crates/gateway/tests/mcp.rs +++ b/litellm-rust/crates/gateway/tests/mcp.rs @@ -158,14 +158,14 @@ mcp_servers: .await .unwrap() .unwrap(); - let mcp_router = litellm_gateway_mcp::router( + let mcp_routes = litellm_gateway_mcp::routes( mcp.operations.clone(), HttpConfig { cancellation_token: shutdown.clone(), ..Default::default() }, ); - let app = litellm_gateway::router(inference, &config, None, Some(mcp_router)); + let app = litellm_gateway::router(inference, &config, None, Some(mcp_routes)); Served { app, mcp, @@ -180,10 +180,9 @@ mcp_servers: #[case::streamable_delete("/mcp", "DELETE")] #[case::legacy_sse("/mcp/sse", "GET")] #[case::legacy_messages("/mcp/sse/messages", "POST")] -#[case::rest_list("/mcp-rest/tools/list", "GET")] #[case::enabled("/mcp/enabled", "GET")] #[tokio::test] -async fn every_mcp_route_requires_admission_credentials( +async fn mcp_protocol_routes_require_bearer_admission( #[future] served: Served, #[case] path: &str, #[case] method: &str, @@ -207,10 +206,36 @@ async fn every_mcp_route_requires_admission_credentials( served.close().await; } +#[rstest] +#[case::missing(&[])] +#[case::wrong_x_api_key(&[("x-api-key", "wrong")])] +#[case::non_bearer_authorization(&[("authorization", "Basic admission-secret"), ("x-api-key", "admission-secret")])] +#[tokio::test] +async fn mcp_rest_routes_reject_a_missing_or_invalid_key( + #[future] served: Served, + #[case] headers: &[(&str, &str)], +) { + let served = served.await; + let request = headers.iter().fold( + Request::get("/mcp-rest/tools/list?server_id=configured-id"), + |request, (name, value)| request.header(*name, *value), + ); + let response = served + .app + .clone() + .oneshot(request.body(Body::empty()).unwrap()) + .await + .unwrap(); + assert_eq!(response.status(), 401); + served.close().await; +} + #[rstest] #[tokio::test] async fn serves_configured_upstream_with_separate_admission_and_outbound_credentials( #[future] served: Served, + #[values(("authorization", "Bearer admission-secret"), ("x-api-key", "admission-secret"))] + admission: (&str, &str), ) { let served = served.await; let app = served.app.clone(); @@ -219,7 +244,7 @@ async fn serves_configured_upstream_with_separate_admission_and_outbound_credent .oneshot( Request::builder() .uri("/mcp-rest/tools/list?server_id=configured-id") - .header("authorization", "Bearer admission-secret") + .header(admission.0, admission.1) .body(Body::empty()) .unwrap(), ) @@ -232,7 +257,7 @@ async fn serves_configured_upstream_with_separate_admission_and_outbound_credent assert_eq!(body["tools"][0]["name"], "echo"); assert_eq!(body["tools"][0]["mcp_info"]["alias"], "knowledge"); - let response = app.clone().oneshot(Request::post("/mcp-rest/tools/call").header("authorization", "Bearer admission-secret").header("content-type", "application/json").body(Body::from(json!({"server_id":"configured-id", "name":"echo", "arguments":{"value":"hello"}}).to_string())).unwrap()).await.unwrap(); + let response = app.clone().oneshot(Request::post("/mcp-rest/tools/call").header(admission.0, admission.1).header("content-type", "application/json").body(Body::from(json!({"server_id":"configured-id", "name":"echo", "arguments":{"value":"hello"}}).to_string())).unwrap()).await.unwrap(); assert_eq!(response.status(), 200); let body: Value = serde_json::from_slice(&to_bytes(response.into_body(), 65536).await.unwrap()).unwrap(); @@ -244,7 +269,7 @@ async fn serves_configured_upstream_with_separate_admission_and_outbound_credent let blocked = app .oneshot( Request::post("/mcp-rest/tools/call") - .header("authorization", "Bearer admission-secret") + .header(admission.0, admission.1) .header("content-type", "application/json") .body(Body::from( json!({"server_id":"configured-id", "name":"hidden"}).to_string(),