mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
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.
This commit is contained in:
parent
3f6f066e07
commit
b1288078ac
9 changed files with 75 additions and 27 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
|
|
@ -43,7 +43,17 @@ impl Default for HttpConfig {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct Routes {
|
||||
pub protocol: Router,
|
||||
pub rest: Router,
|
||||
}
|
||||
|
||||
pub fn router(operations: Arc<dyn Operations>, config: HttpConfig) -> Router {
|
||||
let Routes { protocol, rest } = routes(operations, config);
|
||||
protocol.merge(rest)
|
||||
}
|
||||
|
||||
pub fn routes(operations: Arc<dyn Operations>, 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<dyn Operations>, 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<dyn Operations>, 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<String>, mut request: Request, next: Next) -> Response {
|
||||
|
|
|
|||
|
|
@ -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<String, [u8; 32]>,
|
||||
}
|
||||
|
||||
pub(crate) fn transport(server: McpServer, config: StreamableHttpServerConfig) -> Router {
|
||||
pub(crate) fn transport(
|
||||
server: McpServer,
|
||||
config: StreamableHttpServerConfig,
|
||||
) -> RouterIntoService<Body> {
|
||||
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(
|
||||
|
|
|
|||
|
|
@ -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-<fmt>` 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
|
||||
|
|
|
|||
|
|
@ -126,14 +126,19 @@ pub fn router(
|
|||
inference: Arc<Gateway>,
|
||||
config: &Config,
|
||||
ui: Option<Router>,
|
||||
mcp: Option<Router>,
|
||||
mcp: Option<litellm_gateway_mcp::Routes>,
|
||||
) -> 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 {
|
||||
|
|
|
|||
|
|
@ -79,9 +79,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||
&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<dyn Error>> {
|
|||
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -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(),
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue