Skip to content

feat(mcp): caller sign-in seam for connect-time RFC 9728 challenges and shared Agent 365 OBO exchange - #41163

Closed
devin-ai-integration[bot] wants to merge 114 commits into
mainfrom
litellm_mcp_gateway_sign_in_provider
Closed

devin-ai-integration[bot] wants to merge 114 commits into
mainfrom
litellm_mcp_gateway_sign_in_provider

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem

  • Agent 365 only refused at tools/call, where JSON-RPC drops WWW-Authenticate, and discovery named the gateway as issuer — the client never got a sign-in prompt
  • The guardrail carried its own Entra OBO exchange next to the shared one

Fix

  • One caller_sign_in contract shared by OBO servers and sign-in guardrails: a connect with no usable subject answers 401 with the RFC 9728 challenge, and the protected-resource metadata advertises the real issuer
  • The sign-in subject is a Bearer that is not a LiteLLM key; anything else is challenged at connect and never sent to Entra
  • Agent 365 exchanges through the shared OboTokenExchanger; Entra invalid_client with an AADSTS 50027xx code is a caller fault (401, never 503 or fail_open)
  • Connect resolves the server the way the router does: granted server or access group first, then the registry pick
  • The sign-in advertises the server's scopes when set, api://<client_id>/access_as_user otherwise

Intentional behavior change: a server gated by a default_on Agent 365 guardrail answers initialize with 401 + RFC 9728 metadata instead of 200 and failing tool calls; its discovery lists the Entra issuer instead of the gateway. A tag-only Mode with no default keeps the old bytes.

Built on #41162 (merged); main merged back at 6608e49 — reviewers diffing against an older merge base will also see main's own changes in that window, e.g. #44066's stdio MCP servers now default-disabled (LITELLM_ENABLE_MCP_STDIO=true to enable).

User Flow

Before: claude mcp add --transport http catalog https://litellm-domain/mcp/catalog --header "x-litellm-api-key: Bearer <key>" — initialize returns 200, the server shows as connected, then every tools/call returns isError Agent 365 guardrail rejected the tool call with no sign-in prompt.

After: the same connect gets 401 with WWW-Authenticate: Bearer resource_metadata="…/.well-known/oauth-protected-resource/mcp/catalog"; that document lists the Entra tenant issuer and scopes. Claude Code opens the Microsoft sign-in page, retries initialize with the Entra bearer, gets 200, and tool calls run as the user. Ungranted keys still get 403; opted-out keys and /mcp still get 200; a token Entra rejects gets the same 401, not 503.

Linear ticket

Resolves LIT-8934

Pre-Submission checklist

  • I have added meaningful tests
  • The handful of test files covering my change pass locally
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review

Screenshots / Proof of Fix

Independently re-verified at merge commit 6608e4924b on a live rig (real proxy on real Postgres, 2 workers; scripted MCP upstream; a wire-faithful Entra/Defender double behind owned TLS — no real tenant creds available on that box, so the Microsoft side is a disclosed emulator):

  • Challenge at initialize → RFC 9728 doc naming the Entra issuer → sign-in → retry initialize 200 → tools/call runs as the user (upstream saw the call, Entra saw the OBO exchange, Defender scored it)
  • Defender block surfaces as isError; tampered JWS → 401 challenge; non-JWS credential → 401 with zero Entra calls; ungranted key → 403; opted-out key → 200 with zero Entra calls; AADSTS 50027xx invalid_client → caller-fault 401 — 13/13 cells
  • Merge base 8ffd7c107a shows the old dead end: initialize 200, tool call isError rejected, discovery naming the gateway as issuer — 3/3 cells
  • Unit suites touched by the main merge: 3,454 passed

Earlier legs (including a real-tenant Entra rig) are in the Devin session history linked below.

Type

🆕 New Feature

Caveats (if any)

Medium

  • Entra invalid_client without an AADSTS 50027xx code stays a gateway-owned 503/fail_open on Agent 365 and a 500 misconfigured on an OBO server, as on main
  • Entra 408/429 on an oauth2_token_exchange server's exchange now follow the unavailable path (previously 401); Agent 365 keeps its throttled 503
  • A server gated by a default_on Agent 365 guardrail whose own OBO subject issuer differs from the guardrail's Entra tenant cannot complete the sign-in: RFC 9728 lists authorization_servers as alternatives, a client signs in with one, and the connect preflight requires the single bearer to satisfy every gating provider. Align the issuers (same tenant) or opt the key/server out of the guardrail
  • Scoped calls naming a server hidden from the caller's IP (available_on_public_internet: false) are denied — the 403 at connect and the isError denial at tools/call — where the merge base's tools path ignored client_ip and could run them; connect and tools/call now agree
  • A signed-in connect to a gated server under an Entra outage now fails closed at initialize (503, the preflight's design) where the merge base opened the session (200, tools/list 200) and only tools/call failed
  • A gated server named by x-mcp-servers on /mcp (a scoped single-server connect) now answers initialize with the 401 sign-in challenge, where the merge base opened the session; a true multi-server aggregate on /mcp still connects 200 and rejects at tools/call, as on main
  • /mcp-rest/tools/call keeps the structured 401 without WWW-Authenticate, as on main

Low

  • The challenge origin honors forwarded headers in two stages: the ASGI server's own forwarded-header trust first (uvicorn's default --forwarded-allow-ips trusts loopback peers, so a local reverse proxy's X-Forwarded-Proto applies even when LiteLLM's mcp_trusted_proxy_ranges + use_x_forwarded_for gate would reject that peer), then LiteLLM's gate
  • Discovery for a gated server hides the gateway's own issuer; gateway-OAuth-only clients need an ungated server
  • An Entra 400 whose body has error: null now reads rejected (invalid_grant) where main printed rejected (None)

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

REVIEWER MUST KNOW BEFORE APPROVING

  1. The headline change: every route shape of a default_on Agent 365-gated server (/mcp/<name>, /mcp/<server_id>, case variants, /<name>/mcp) answers initialize with the 401 challenge for a missing bearer, a LiteLLM key as bearer, a non-Bearer scheme, an opaque bearer, or a JWS Entra rejects — even under fail_open. Discovery lists the Entra issuer (server scopes or api://<client_id>/access_as_user).
  2. Name resolution now follows the router for connect and the OAuth discovery/relay endpoints: the key's granted server or access group wins, then the registry pass order (exact alias → server_name → name → server_id → case-folded). Router-resolved shapes of OBO, oauth_delegate, gateway-oauth2, passthru and true_passthrough servers that previously answered 200 now answer the 401 challenge (ungranted keys get 401 instead of 403 there), and some case-variant discovery documents/relays move from 404 to the exact name's answer. Root GET /authorize and POST /token also resolve client_id with the router's lookup: a case variant of a plain server's alias (e.g. client_id=DOCS) answers 400 server_not_oauth2 where the merge base fell through to a lone gateway OAuth server; exact client_ids are unchanged. Routing and server selection on exact-name routes, plain servers and scoped routes are unchanged — except that IP-visibility now also governs scoped selection (available_on_public_internet: false servers are denied from public IPs), but every OBO connect challenge's resource_metadata moved from the merge base's relative alias path (<alias>/mcp/<name> forms) to the absolute metadata URL of the route used — plain OBO servers included.
  3. Alias collision: with one name owned by both a config server and a DB server (only reachable via config.yaml), the key's granted server now wins — /mcp/github can serve a different upstream than before. Rejecting the collision in the loader is a follow-up.
  4. Subject-token handling: a LiteLLM virtual key or the master key offered as the bearer is withheld — the master key is no longer sent to Entra as an assertion; Basic/Digest/scheme-less values are challenged, never sent to Entra. Every pre_mcp_call hook now receives incoming_subject_token (the caller's non-LiteLLM bearer, or null); hooks that forward the whole request dict (Noma v2, Conduct) pass it to their vendor.
  5. Error classification follows the shared exchanger's gateway-fault set: invalid_resource is now a caller fault (401 under both fallbacks — under fail_open this is stricter than the merge base, which allowed the call unscanned), invalid_target/unsupported_grant_type are now gateway faults (under fail_open, looser than base); invalid_grant, invalid_scope and plain invalid_client keep their status.
  6. Sign-in challenge runs on every method of a single-server connect route, not only initialize: a sessionless DELETE, SSE GET or tools/* that used to answer 200 terminated or an isError rejection now gets the 401 challenge with no Logs row. A bearer-carrying intruder naming another caller's mcp-session-id still gets 403.
  7. Cache: Agent 365's exchanged tokens moved to the shared exchanger cache — 500-entry FIFO (was its own 1000-entry LRU), 3600s reuse when expires_in is absent, and one INFO log per successful exchange. Duplicate scopes entries are listed once in discovery.

Link to Devin session: https://app.devin.ai/sessions/0c5e46d406144373b8b0daebb4049fc4
Open in Devin Desktop: https://app.devin.ai/desktop/session/0c5e46d406144373b8b0daebb4049fc4?variant=devin
Requested by: @yucheng-berri


Note

High Risk
Changes MCP authentication, OAuth discovery, and connect-time challenges across many entry points; misconfiguration or resolver differences can alter who gets 401 vs 403 and which IdP metadata clients see.

Overview
Introduces a caller_sign_in seam so MCP connect and OAuth protected-resource metadata share one contract: OBO token-exchange servers and guardrails that implement CallerSignInProvider merge required issuers/scopes, and a connect without a usable caller Bearer (not a LiteLLM virtual/master key) returns RFC 9728 401 with WWW-Authenticate before JSON-RPC swallows the header.

Agent 365 adopts that provider: connect-time challenge + preflight Entra OBO via the shared OboTokenExchanger, discovery advertises the Entra issuer (and server scopes or gateway scope), and tool hooks use incoming_subject_token instead of treating the admission key as the assertion. Token-exchange HTTP handling now treats 408/429 as upstream outage, distinguishes Entra invalid_client with AADSTS 50027xx as caller fault, and challenges can carry the absolute metadata URL for the route the client used.

Routing/authorization is aligned through get_mcp_server_answering_to and scoped selection that prefers the key’s granted server, respects client IP visibility, and fails closed on unknown scoped names; connect can peek the body only when a gating provider needs to distinguish initialize.

Reviewed by Cursor Bugbot for commit 9d74643. Bugbot is set up for automated code reviews on this repo. Configure here.

@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

@greptile-apps

greptile-apps Bot commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[High risk] Adds authentication and authorization logic to MCP server connections.

The PR appears safe to merge based on the current review; no new actionable issue or outstanding previous finding was identified.

Summary

The PR adds connect-time RFC 9728 sign-in challenges for gated MCP servers, shares Agent 365 OBO exchange handling, and aligns scoped server selection with discovery. The latest author change restores the legacy OBO behavior for an empty-valued Authorization header.

Reviews (43) · Last reviewed commit: "merge main into gateway sign-in provider..."

@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@yucheng-berri
yucheng-berri requested a review from a team September 16, 2026 01:21
@yucheng-berri
yucheng-berri force-pushed the litellm_mcp_gateway_sign_in_provider branch from 4f23e86 to d0cb350 Compare September 16, 2026 01:21
@codspeed

codspeed Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_mcp_gateway_sign_in_provider (9d74643) with main (c2c0bb5)

Open in CodSpeed

yucheng-berri and others added 6 commits September 26, 2026 08:34
…r catalog identity

Track the tools each MCP server listed per caller identity so pre_mcp_call and during_mcp_call hooks receive the tool description and input schema the client saw. Servers with no caller-dependent inputs share one slot; user identity, forwarded headers, stdio env, relayed bearers, and server-specific auth get their own. Local registry and OpenAPI paths pass the registered metadata and admin description overrides. The Agent 365 guardrail reads the new fields into its evaluate payload.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…docstrings

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
token_exchange servers list upstream with the caller's own Entra bearer, so two callers on one
LiteLLM key with different subjects were sharing a catalog slot

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…slot

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…nges

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration
devin-ai-integration Bot force-pushed the litellm_mcp_gateway_sign_in_provider branch from d0cb350 to e3bee03 Compare September 26, 2026 09:32
@devin-ai-integration
devin-ai-integration Bot force-pushed the litellm_mcp_gateway_sign_in_provider branch from e3bee03 to 17daa55 Compare September 26, 2026 09:42
@devin-ai-integration devin-ai-integration Bot changed the title feat(mcp): gateway sign-in provider for connect-time RFC 9728 challenges feat(mcp): caller sign-in seam for connect-time RFC 9728 challenges and shared Agent 365 OBO exchange Sep 26, 2026
The pre-listing resolver ran the tool-call byok_auth_required check even
when the caller already supplied x-mcp-auth, and it ran outside the
per-server error boundary, so a single deprecated-header caller dropped
the server from the aggregate list. Listing now returns a supplied header
unchanged and falls back to the stored credential without raising

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

…; true_passthrough moved-shape regression tests

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

…bearer

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@mateo-berri

Copy link
Copy Markdown
Contributor

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

# Conflicts:
#	litellm/proxy/_experimental/mcp_server/mcp_server_manager.py
#	litellm/proxy/_experimental/mcp_server/operations.py
#	litellm/proxy/_experimental/mcp_server/rest_endpoints.py
#	litellm/proxy/guardrails/guardrail_hooks/agent_365/agent_365.py
#	litellm/proxy/utils.py
#	tests/integration/mcp/test_mcp_access_matrix.py
#	tests/integration/mcp/test_mcp_credentials.py
#	tests/integration/mcp/test_mcp_listed_tool_metadata.py
#	tests/integration/mcp/test_mcp_oauth_flows.py
#	tests/unit/proxy/_experimental/mcp_server/test_mcp_server.py
#	tests/unit/proxy/_experimental/mcp_server/test_mcp_server_manager.py
#	tests/unit/proxy/_experimental/mcp_server/test_mcp_server_tool_calls_and_headers.py
#	tests/unit/proxy/_experimental/mcp_server/test_operations.py
#	tests/unit/proxy/guardrails/guardrail_hooks/test_agent_365.py
#	tests/unit/responses/mcp/test_litellm_proxy_mcp_handler.py
@yucheng-berri

Copy link
Copy Markdown
Contributor

@greptileai review latest head

@yucheng-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

…rule gates only sign-in providers

The r12 regression: a scheme-less Authorization value or a tab-separated
Bearer used to be exchanged with the IdP (the legacy lenient parse) and the
sign-in contract's single-space Bearer rule challenged them at connect.
The gate now decomposes the requirement: guardrail providers keep the strict
subject rule, the OBO server's own requirement challenges only when there is
no Authorization at all or the bearer is a withheld LiteLLM key, and any
other bearer the legacy parser accepts flows to the exchange untouched.
@yucheng-berri

Copy link
Copy Markdown
Contributor

@greptileai review latest head

@yucheng-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

…sted slot only

The merge resolution let request_obj.tool_description feed the synthetic
message the rate limiter counts, so a listed tool's gateway metadata
inflated the caller's projected input reservation. Restore main's exact
source: kwargs['mcp_tool_description'] only; request_obj.tool_description
still reaches guardrails via mcp_tool_description.
@yucheng-berri

Copy link
Copy Markdown
Contributor

@greptileai review latest head

@yucheng-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stale Bugbot comment from a previous run.

…, not parsability

Gauntlet round: an empty-valued Authorization header opened the session on the
merge base (the old gate tested oauth2_headers presence) and the restored gate
challenged it (the parse yields None). Presence now decides the no-header leg,
and the withheld-LiteLLM-key leg fires only when a parseable bearer exists.
# Conflicts:
#	litellm/proxy/_experimental/mcp_server/operations.py
#	litellm/proxy/guardrails/guardrail_hooks/agent_365/agent_365.py
@yucheng-berri

Copy link
Copy Markdown
Contributor

@greptileai review latest head

@yucheng-berri

Copy link
Copy Markdown
Contributor

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 9d74643. Configure here.

This branch had an error being deployed

1 failed deployment
e2e-changed — 9d74643e Deployed Oct 6, 2026 by yucheng-berri via oauth #3306
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants