Repository navigation
fix(mcp): return 401 challenge for REST token-exchange tool calls without a subject token - #42782
Conversation
…hout a subject token Co-Authored-By: bot_apk <apk@cognition.ai>
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
|
|
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
… tool prefix Co-Authored-By: bot_apk <apk@cognition.ai>
|
@greptileai please re-review the tip, the server_id mismatch finding is addressed in 8353712 |
Co-Authored-By: bot_apk <apk@cognition.ai>
|
@greptileai please re-review the tip, the test helper typing finding is addressed in 1d42560 |
TLDR
Problem this solves:
POST /mcp-rest/tools/callon a token-exchange server with no subject bearer returned 500How it solves it:
WWW-Authenticatechallenge and subject-token extractionUser Flow
Before: a client that authenticates to LiteLLM with a virtual key and has not yet obtained an IdP token gets a 500 instead of being told how to authenticate
x-litellm-api-key: sk-...and body{"server_id": "<id>", "name": "<alias>-add", "arguments": {"a": 2, "b": 3}}500 {"error":"internal_server_error","message":"An unexpected error occurred: Tool add not found"}with noWWW-Authenticateheader, so the client cannot discover the IdP401with theWWW-AuthenticatechallengeAfter: the same request gets the 401 challenge on the first call, so the client can discover the IdP, sign in, and retry
x-litellm-api-key: sk-...and the same body401 {"detail":"Unauthorized"}withWWW-Authenticate: Bearer resource_metadata="/.well-known/oauth-protected-resource/mcp/<alias>", error="invalid_token", error_description="Missing or invalid subject token; authenticate with the IdP and retry"Authorization: Bearer <user token>200with the tool result; LiteLLM exchanged their token for one scoped to the upstream server and never forwarded their own tokenRelevant issues
Follow-up to the integration report in #42711
Affected release
Linear ticket
Resolves LIT-8471
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/test_litellm/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more@greptileaito re-request a review after pushing changes)Delays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
Setup: the integration stack from
.circleci/scripts/run_integration.sh(real proxy on Postgres and Redis, the harness's scripted MCP peer and OAuth token endpoint as the protocol-edge upstreams), a freshoauth2_token_exchangeserver registered throughPOST /v1/mcp/server, and a key scoped to it viaPOST /key/generate. The same three curls run in order on each side. Request and response headers unrelated to the fix are trimmedBefore (e0af991)
Cold catalog, no subject token
curl -s -i -X POST http://127.0.0.1:4010/mcp-rest/tools/call -H 'x-litellm-api-key: sk-<key>' -H 'Content-Type: application/json' -d '{"server_id": "bd6409b9-1ed8-4b9c-98d5-fa612f2c632b", "name": "te01b7a945-add", "arguments": {"a": 2, "b": 3}}'HTTP/1.1 500 Internal Server Errorand body{"detail":{"error":"internal_server_error","message":"An unexpected error occurred: Tool add not found"}}; peer tools/call 0, token requests 0Control, valid subject token
-H 'Authorization: Bearer subject-abc'HTTP/1.1 200 OKand body{"content":[{"type":"text","text":"5"}],"structuredContent":{"result":5},"isError":false,...}; peer tools/call 1, token requests 1Warm catalog, no subject token
HTTP/1.1 401 Unauthorizedwithwww-authenticate: Bearer resource_metadata="/.well-known/oauth-protected-resource/mcp/te01b7a945", error="invalid_token", error_description="Missing or invalid subject token; authenticate with the IdP and retry"; peer tools/call 0, token requests 0After (8353712, product code unchanged at tip 1d42560)
Cold catalog, no subject token
curl -s -i -X POST http://127.0.0.1:4010/mcp-rest/tools/call -H 'x-litellm-api-key: sk-<key>' -H 'Content-Type: application/json' -d '{"server_id": "<id>", "name": "tee579b8a9-add", "arguments": {"a": 2, "b": 3}}'HTTP/1.1 401 Unauthorizedwithwww-authenticate: Bearer resource_metadata="/.well-known/oauth-protected-resource/mcp/tee579b8a9", error="invalid_token", error_description="Missing or invalid subject token; authenticate with the IdP and retry"and body{"detail":"Unauthorized"}; peer tools/call 0, token requests 0Control, valid subject token
-H 'Authorization: Bearer subject-abc'HTTP/1.1 200 OKand body{"content":[{"type":"text","text":"5"}],"structuredContent":{"result":5},"isError":false,...}; peer tools/call 1, token requests 1Warm catalog, no subject token
HTTP/1.1 401 Unauthorizedwith the samewww-authenticatechallenge as the cold case; peer tools/call 0, token requests 0Tests:
tests/integration/mcp/test_mcp_oauth_flows.py::test_token_exchange_without_a_subject_token_is_rejected_before_any_upstream_requesthas itsBUGskip removed and now asserts cold 401 + challenge, a valid-subject warm-up (200, one exchange, one peer call), warm 401 + challenge, and a virtual key placed inAuthorizationstill challenging, with zero upstream requests on every no-subject leg. The wholetests/integration/mcpgroup passes against the fixed proxy (213 passed, 4 pre-existing skips). Unit coverage intests/test_litellm/proxy/_experimental/mcp_server/test_operations.pywas mutation-checked: dropping the auth-type guard, dropping the allowlist guard, swapping subject extraction for plain bearer extraction, dropping the server_id agreement guard, or removing the call each turn at least one test redType
🐛 Bug Fix
Caveats (if any)
Low
server_idnames a different server than the tool prefix, the challenge is skipped so the existing 403tool_server_mismatchanswer is kept/mcpJSON-RPC route with no server pinned, a no-subject tools/call on a token-exchange server now maps to an unauthorized JSON-RPC error instead of "tool not found"Final Attestation
Link to Devin session: https://app.devin.ai/sessions/877d4ff0585540b88fb91e28de42fcb2
Open in Devin Desktop: https://app.devin.ai/desktop/session/877d4ff0585540b88fb91e28de42fcb2?variant=devin