-
-
Notifications
You must be signed in to change notification settings - Fork 11.7k
feat(mcp): migrate the token_exchange (OBO) arm to the v2 resolver #31526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tin-berri
merged 9 commits into
litellm_internal_staging
from
litellm_mcp_v2_token_exchange
Jul 3, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5ece8df
feat(mcp): v2-native RFC 8693 token exchanger for the token_exchange …
tin-berri b990c21
feat(mcp): migrate the token_exchange arm to the v2 resolve_credentials
tin-berri b33b47d
fix(mcp): bind the token-exchange cache key to the exchange config
tin-berri 3b980ae
refactor(mcp): build the token exchanger eagerly, dropping the lazy w…
tin-berri cdc6086
fix(mcp): map non-object token-exchange JSON to a miss instead of a 500
tin-berri 80c3d95
fix(mcp): fail closed on a non-Bearer token_type in the OBO exchange
tin-berri 5bcf4aa
feat(mcp): honor token_endpoint_auth_method (client_secret_basic) in …
tin-berri 346088a
feat(mcp): reject a non-access issued_token_type in the OBO exchange
tin-berri eaf9fd7
fix: include token endpoint auth method in exchange cache key
cursoragent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
litellm/proxy/_experimental/mcp_server/outbound_credentials/token_exchange_provider.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| """Composition root for the v2-native token_exchange (OBO) exchanger. | ||
|
|
||
| Wires the pure ``Rfc8693TokenExchanger`` to its runtime edges: the real httpx POST against the IdP and | ||
| the configured cache sizing/TTL constants. ``build_token_exchanger`` is built once at egress | ||
| construction and reused, so the in-process exchanged-token cache survives across requests. Unlike the | ||
| per-user store, nothing here reads a runtime global at build time (the httpx client is acquired per | ||
| call), so it needs no lazy wrapper. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from litellm._logging import verbose_logger | ||
| from litellm.constants import ( | ||
| MCP_OAUTH2_TOKEN_CACHE_DEFAULT_TTL, | ||
| MCP_OAUTH2_TOKEN_CACHE_MIN_TTL, | ||
| MCP_OAUTH2_TOKEN_EXPIRY_BUFFER_SECONDS, | ||
| MCP_TOKEN_EXCHANGE_CACHE_MAX_SIZE, | ||
| ) | ||
| from litellm.proxy._experimental.mcp_server.outbound_credentials.oauth_token_store import ( | ||
| InMemoryTokenCacheBackend, | ||
| ) | ||
| from litellm.proxy._experimental.mcp_server.outbound_credentials.token_exchanger import ( | ||
| Rfc8693TokenExchanger, | ||
| ) | ||
|
|
||
|
|
||
| async def _post_exchange_endpoint( | ||
| url: str, form: dict[str, str], client_auth_headers: dict[str, str] | ||
| ) -> dict[str, object] | None: | ||
| from litellm.llms.custom_httpx.http_handler import ( # noqa: PLC0415 | ||
| get_async_httpx_client, # pyright: ignore | ||
| ) | ||
| from litellm.types.llms.custom_http import httpxSpecialProvider # noqa: PLC0415 | ||
|
|
||
| # litellm's httpx handler and httpx.Response are only partially typed; the IdP returns a JSON | ||
| # object and the exchanger validates each field, so the untyped boundary is contained here. | ||
| # A failed exchange is a miss, not a 500 (matches v1), so any error becomes None. | ||
| headers = {"Accept": "application/json", **client_auth_headers} | ||
| try: | ||
| client = get_async_httpx_client(llm_provider=httpxSpecialProvider.MCP) # pyright: ignore | ||
| response = await client.post(url, headers=headers, data=form) # pyright: ignore | ||
| response.raise_for_status() # pyright: ignore | ||
| parsed: object = response.json() # pyright: ignore | ||
| except Exception as exc: # noqa: BLE001 | ||
| verbose_logger.warning("MCP token exchange request failed: %s", exc) | ||
| return None | ||
| if not isinstance(parsed, dict): | ||
| # A valid-but-non-object JSON body (list/string/number) would crash the field parsing; map it | ||
| # to a miss so it surfaces as a typed upstream_unavailable, not a 500. | ||
| verbose_logger.warning("MCP token exchange returned non-object JSON (%s)", type(parsed).__name__) | ||
| return None | ||
| return parsed # pyright: ignore | ||
|
|
||
|
|
||
| def build_token_exchanger() -> Rfc8693TokenExchanger: | ||
| return Rfc8693TokenExchanger( | ||
| _post_exchange_endpoint, | ||
| cache=InMemoryTokenCacheBackend(max_size=MCP_TOKEN_EXCHANGE_CACHE_MAX_SIZE), | ||
| default_ttl_seconds=MCP_OAUTH2_TOKEN_CACHE_DEFAULT_TTL, | ||
| min_ttl_seconds=MCP_OAUTH2_TOKEN_CACHE_MIN_TTL, | ||
| expiry_buffer_seconds=MCP_OAUTH2_TOKEN_EXPIRY_BUFFER_SECONDS, | ||
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.