Skip to content

feat(proxy): grant MCP server and tool access from JWT scope mappings - #41896

Open
devin-ai-integration[bot] wants to merge 9 commits into
mainfrom
litellm_jwt_scope_mcp_authz
Open

devin-ai-integration[bot] wants to merge 9 commits into
mainfrom
litellm_jwt_scope_mcp_authz

Conversation

@devin-ai-integration

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

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • JWT scope mappings could only grant models and routes
  • MCP servers and tools still needed a key or team row in the DB
  • Pure OIDC callers saw no MCP servers even with the right scope
  • Scopes had to live in the literal scope claim; nested IdP claims like realm_access.roles were unusable

How it solves it:

  • scope_mappings entries accept mcp_servers, mcp_access_groups, mcp_tool_permissions
  • JWT admission resolves the matching mappings into grants once and stores them on UserAPIKeyAuth.jwt_scope_mcp_grants
  • MCP admission unions those grants into the caller's servers and tools without reading any proxy global
  • New scope_jwt_field (default scope, dot notation) picks the claim that feeds scope_mappings, model scope checks, and admin_jwt_scope
  • Key grants and scope grants are additive: a whole-server grant on either side keeps every tool, two tool allowlists union
  • Team, org, and other higher-level grants stay ceilings on the additive result
  • Unmatched, missing, or malformed scope claims grant nothing

User Flow

Before: a platform team fronts LiteLLM with their IdP and wants a JWT scope to decide which MCP servers a caller sees, but the proxy refuses the config and, without it, the caller sees nothing

  1. The admin adds mcp_servers: ["math_alpha"] under a litellm_jwtauth.scope_mappings entry and starts the proxy
  2. The proxy exits during startup with ValidationError ... Extra inputs are not permitted
  3. The admin removes the two MCP lines so the proxy boots, and a developer whose token carries scope: litellm.mcp.alpha sends GET http://litellm-domain/v1/mcp/server
  4. They get [], and POST http://litellm-domain/mcp/ tools/list returns {"tools": []}
  5. POST http://litellm-domain/mcp/ tools/call for math_alpha-add returns Error: User not allowed to call this tool.
  6. The only way forward is to create a team or virtual key in the DB for every IdP group

After: the same scope now grants the servers and tools the admin mapped, with no team or key row involved

  1. The admin adds mcp_servers: ["math_alpha"] under the litellm.mcp.alpha entry and mcp_tool_permissions: {math_beta: ["add"]} under litellm.mcp.beta_add, and the proxy starts normally
  2. A developer whose token carries scope: litellm.mcp.alpha sends GET http://litellm-domain/v1/mcp/server and gets back the math_alpha server
  3. POST http://litellm-domain/mcp/ tools/list returns math_alpha-add and math_alpha-multiply, and tools/call on math_alpha-add with {"a": 2, "b": 3} returns 5
  4. A developer whose token carries scope: litellm.mcp.beta_add sees only math_beta-add, can call it, and gets Tool 'multiply' is not allowed for your key/team on server 'math_beta' when they try math_beta-multiply
  5. A developer whose token carries an unrelated scope still gets [], {"tools": []}, and Error: User not allowed to call this tool.
  6. An admin whose IdP puts entitlements under realm_access.roles sets scope_jwt_field: realm_access.roles; a token with scope: openid and realm_access: {roles: [litellm.mcp.alpha, litellm.mcp.beta_add]} gets math_alpha plus math_beta-add, and a chat completion on gpt-4.1-mini returns pong
  7. Nobody gains access to a server their scope does not name, and a team member's scope cannot reach a server outside the team's own MCP allowlist

Relevant issues

Affected release

Linear ticket

Resolves LIT-4505

Docs: BerriAI/litellm-docs#1565 (scope_jwt_field and the MCP permission precedence section)

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. 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
  • 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 (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Screenshots / Proof of Fix

Shared setup for both sides: one Postgres database with two teams and zero virtual keys, two real MCP upstreams on 127.0.0.1:25505 (math_alpha) and 127.0.0.1:25506 (math_beta), a local JWKS endpoint, and RS256 tokens minted per run with fresh subjects that belong to no team. The proxy runs on 127.0.0.1:20505 with --use_prisma_db_push. The chat control hits the real OpenAI API. The scope_mappings section under general_settings.litellm_jwtauth is

enable_jwt_auth: true
litellm_jwtauth:
  user_id_jwt_field: sub
  user_id_upsert: true
  enforce_scope_based_access: true
  scope_mappings:
    - scope: litellm.mcp.alpha
      models: ["gpt-4.1-mini"]
      mcp_servers: ["math_alpha"]
    - scope: litellm.mcp.beta_add
      mcp_tool_permissions:
        math_beta: ["add"]

The MCP calls below are all the same shape, so mcp stands for

mcp() { curl -s -w '\nHTTP %{http_code}\n' -X POST http://127.0.0.1:20505/mcp/ \
  -H "Authorization: Bearer $1" -H "Content-Type: application/json" \
  -H "Accept: application/json, text/event-stream" -d "$2" | sed -n 's/^data: //p; /^HTTP/p'; }
TL='{"jsonrpc":"2.0","id":1,"method":"tools/list","params":{}}'
call() { printf '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"%s","arguments":{"a":2,"b":3}}}' "$1"; }

The base tree refuses the two MCP lines (case 1), so cases 2 to 5 on the Before side ran with the same config minus mcp_servers and mcp_tool_permissions. Before is the current merge base eda1faba14 (main after the MCP SDK 2 merge, #41718) checked out in a clean worktree; After is the current tip 9ed18c3cb0, the merge of that main into this branch (proxy build marker sha=9ed18c3cb0 dirty=0). Both sides ran on mcp==2.2.0, the SDK 2 lock, and the rig's upstreams were ported to the SDK 2 MCPServer API for this run. Since the previous run at 766fd564b3 the only change is that merge; no production line of this PR changed

Before (eda1fab)

Case 1: proxy boots with MCP fields in scope_mappings

  1. python litellm/proxy/proxy_cli.py --config config.yaml --port 20505 --use_prisma_db_push
  2. Observed: the proxy exits before listening
pydantic_core._pydantic_core.ValidationError: 2 validation errors for LiteLLM_JWTAuth
  Extra inputs are not permitted [type=extra_forbidden, input_value=['math_alpha'], input_type=list]
  Extra inputs are not permitted [type=extra_forbidden, input_value={'math_beta': ['add']}, input_type=dict]

Case 2: alice, scope litellm.mcp.alpha, no key or team row

  1. curl -s http://127.0.0.1:20505/v1/mcp/server -H "Authorization: Bearer $ALICE"
  2. Observed: []
  3. mcp "$ALICE" "$TL"
  4. Observed: {"jsonrpc":"2.0","id":1,"result":{"tools":[]}} HTTP 200
  5. mcp "$ALICE" "$(call math_alpha-add)"
  6. Observed: {"content":[{"type":"text","text":"Error: User not allowed to call this tool."}],"isError":true} HTTP 200
  7. mcp "$ALICE" "$(call math_beta-add)"
  8. Observed: Error: User not allowed to call this tool. HTTP 200

Case 3: bob, scope litellm.mcp.beta_add, no key or team row

  1. mcp "$BOB" "$TL"
  2. Observed: {"jsonrpc":"2.0","id":1,"result":{"tools":[]}} HTTP 200
  3. mcp "$BOB" "$(call math_beta-add)"
  4. Observed: Error: User not allowed to call this tool. HTTP 200
  5. mcp "$BOB" "$(call math_beta-multiply)"
  6. Observed: Error: User not allowed to call this tool. HTTP 200

Case 4: carol, unrelated scope

  1. curl -s http://127.0.0.1:20505/v1/mcp/server -H "Authorization: Bearer $CAROL" then mcp "$CAROL" "$TL"
  2. Observed: [] and {"tools":[]} HTTP 200
  3. mcp "$CAROL" "$(call math_alpha-add)"
  4. Observed: Error: User not allowed to call this tool. HTTP 200

Case 5: controls

  1. mcp sk-1234 "$TL"
  2. Observed: math_alpha-add, math_alpha-multiply, math_beta-add, math_beta-multiply HTTP 200
  3. curl -s http://127.0.0.1:20505/v1/chat/completions -H "Authorization: Bearer $ALICE" -H 'Content-Type: application/json' -d '{"model":"gpt-4.1-mini","messages":[{"role":"user","content":"Reply with the single word pong"}],"max_tokens":5}'
  4. Observed: "model":"gpt-4.1-mini" ... "content":"pong" HTTP 200
  5. psql ... -c "select count(*) from \"LiteLLM_VerificationToken\"" and a membership query for the three subjects
  6. Observed: keys_total|0, teams_with_these_members|0, user_rows_with_teams|0

After (9ed18c3)

Case 1: proxy boots with MCP fields in scope_mappings

  1. python litellm/proxy/proxy_cli.py --config config.yaml --port 20505 --use_prisma_db_push
  2. Observed: /health/readiness returns 200, serving tree sha=9ed18c3cb0 dirty=0 config=config.yaml

Case 2: alice, scope litellm.mcp.alpha, no key or team row

  1. curl -s http://127.0.0.1:20505/v1/mcp/server -H "Authorization: Bearer $ALICE"
  2. Observed: one server, "server_name":"math_alpha"
  3. mcp "$ALICE" "$TL"
  4. Observed: "tools":[{"name":"math_alpha-add",...},{"name":"math_alpha-multiply",...}] HTTP 200
  5. mcp "$ALICE" "$(call math_alpha-add)"
  6. Observed: {"content":[{"type":"text","text":"5"}],"isError":false} HTTP 200
  7. mcp "$ALICE" "$(call math_beta-add)"
  8. Observed: Error: User not allowed to call this tool. HTTP 200

Case 3: bob, scope litellm.mcp.beta_add, no key or team row

  1. mcp "$BOB" "$TL"
  2. Observed: {"jsonrpc":"2.0","id":1,"result":{"_meta":{"litellm.ai/server_outcomes":{"math_beta":{"status":"ok","tool_count":1}}},"tools":[{"name":"math_beta-add",...}]}} HTTP 200
  3. mcp "$BOB" "$(call math_beta-add)"
  4. Observed: {"content":[{"type":"text","text":"5"}],"isError":false} HTTP 200
  5. mcp "$BOB" "$(call math_beta-multiply)"
  6. Observed: Error: Tool 'multiply' is not allowed for your key/team on server 'math_beta'. Contact proxy admin for access. HTTP 200

Case 4: carol, unrelated scope

  1. curl -s http://127.0.0.1:20505/v1/mcp/server -H "Authorization: Bearer $CAROL" then mcp "$CAROL" "$TL"
  2. Observed: [] and {"tools":[]} HTTP 200
  3. mcp "$CAROL" "$(call math_alpha-add)"
  4. Observed: Error: User not allowed to call this tool. HTTP 200

Case 5: controls

  1. mcp sk-1234 "$TL"
  2. Observed: math_alpha-add, math_alpha-multiply, math_beta-add, math_beta-multiply HTTP 200
  3. Same chat completion curl as Before with $ALICE
  4. Observed: "model":"gpt-4.1-mini" ... "content":"pong" HTTP 200
  5. Same row queries as Before
  6. Observed: keys_total|0, teams_with_these_members|0, user_rows_with_teams|0

Case 6: custom claim path, scope_jwt_field: realm_access.roles, no key or team row (9ed18c3)

Same config plus scope_jwt_field: realm_access.roles under litellm_jwtauth. Tokens carry a decoy top-level scope and put the real entitlements under realm_access.roles

  1. erin, scope: openid, realm_access.roles: [offline_access, litellm.mcp.alpha, litellm.mcp.beta_add]: curl -s http://127.0.0.1:20505/v1/mcp/server -H "Authorization: Bearer $ERIN"
  2. Observed: ['math_alpha', 'math_beta']
  3. mcp "$ERIN" "$TL"
  4. Observed: math_alpha-add, math_alpha-multiply, math_beta-add HTTP 200
  5. mcp "$ERIN" "$(call math_alpha-multiply)" then mcp "$ERIN" "$(call math_beta-multiply)"
  6. Observed: 6 with isError: false, then Error: Tool 'multiply' is not allowed for your key/team on server 'math_beta'. Contact proxy admin for access.
  7. curl -s http://127.0.0.1:20505/v1/chat/completions -H "Authorization: Bearer $ERIN" ... -d '{"model":"gpt-4.1-mini",...}'
  8. Observed: {'model': 'gpt-4.1-mini', 'content': 'pong'} HTTP 200 (the model scope check reads the same nested claim)
  9. fred, realm_access.roles: [offline_access] but literal scope: litellm.mcp.alpha: server list [], tools: [], tools/call math_alpha-add returns Error: User not allowed to call this tool. (the literal claim is ignored once a path is configured)
  10. gina, no realm_access claim at all, literal scope: litellm.mcp.alpha: tools: [], and the chat completion returns HTTP 403 The requested model 'gpt-4.1-mini' is not available for this API key
  11. hank, realm_access.roles is an object: /mcp/ returns HTTP 401 authentication_required
  12. ivy, realm_access.roles: "openid litellm.mcp.beta_add" (space separated string): tools: ['math_beta-add'], tools/call math_beta-add returns 5
  13. Rows: keys_total|0, user_rows_with_teams|0, user_rows_with_object_permission|0 for erin, fred, gina, ivy

Case 7: key plus scope on the same server, both directions, driven over HTTP with a mapped key (9ed18c3)

Same config plus virtual_key_claim_field: sub and a litellm.mcp.beta mapping with mcp_servers: ["math_beta"]. Two keys are issued and mapped to JWT subjects, so a JWT caller carries a real key object permission

  1. curl -s http://127.0.0.1:20505/key/generate -H "Authorization: Bearer sk-1234" -d '{"object_permission":{"mcp_servers":["math_beta"]}}' (K1, whole server) and the same with "mcp_tool_permissions":{"math_beta":["multiply"]} (K2), then POST /jwt/key/mapping/new with {"jwt_claim_name":"sub","jwt_claim_value":"kim...","key":K1} and leo... to K2
  2. Observed: two mappings with is_active: True, and GET /jwt/key/mapping/list returns both subjects
  3. kim (K1 whole server) with scope: litellm.mcp.beta_add: mcp "$KIM" "$TL" returns math_beta-add, math_beta-multiply; tools/call math_beta-multiply returns 6 (key whole-server grant is not narrowed by the scope allowlist)
  4. kim with no MCP scope: tools/list still returns both beta tools
  5. leo (K2 [multiply]) with scope: litellm.mcp.beta: tools/list returns both beta tools and tools/call math_beta-add returns 5 (scope whole-server grant is not narrowed by the key allowlist)
  6. leo with scope: litellm.mcp.beta_add: tools/list returns both beta tools (two allowlists union)
  7. leo with no MCP scope: tools/list returns only math_beta-multiply, and tools/call math_beta-add returns Error: Tool 'add' is not allowed for your key/team on server 'math_beta'. Contact proxy admin for access.
  8. leo with scope: litellm.mcp.beta_add calling math_alpha-add: Error: User not allowed to call this tool.
  9. At the merge base the same config is refused at boot (3 validation errors for LiteLLM_JWTAuth ... Extra inputs are not permitted), so there is no Before for this case beyond case 1

Extra legs

Scope mappings are config only, with no Admin UI page, so the HTTP transcript above is the complete user-facing proof. These legs run on the After tree only because they need the new fields. Legs 3 and 5 were rerun at 9ed18c3cb0 with the same outcomes; legs 1, 2 and 4 were last run at 766fd564b3 and the merge did not touch the code they exercise, but they were not rerun on this tip

  1. Access group: mcp_access_groups: ["ops-tools"] on a litellm.mcp.ops mapping with math_beta tagged ops-tools. tools/list returns math_beta-add, math_beta-multiply; tools/call math_beta-multiply returns 6; tools/call math_alpha-add returns Error: User not allowed to call this tool.
  2. Hostile claims: {"scope":5} returns HTTP 401 at the auth layer; "", a 5000 char scope, litellm.mcp.alpha%20litellm.mcp.beta_add, and LITELLM.MCP.ALPHA each return tools:[]; ["litellm.mcp.alpha"] (list form) and a duplicated scope return the alpha tools only
  3. Team ceiling (team_id_jwt_field set, team litfix-team-beta grants math_beta with no tool restriction): a member with no scope sees both beta tools; a member with litellm.mcp.beta_add sees only math_beta-add and math_beta-multiply is refused; a member with litellm.mcp.alpha gets tools:[] and math_alpha-add is refused; a member of a team that grants math_alpha keeps both alpha tools; chat completion returns pong
  4. Postgres refusing connections (ALTER DATABASE ... CONNECTION LIMIT 0 plus backend termination): direct server scope, tool scope, access group scope, and a brand new subject all keep exactly their scope's tools, nothing wider, and the same results return after the limit is lifted
  5. Additive scopes: a token carrying both litellm.mcp.beta (mcp_servers: ["math_beta"]) and litellm.mcp.beta_add lists math_beta-add and math_beta-multiply and tools/call math_beta-multiply returns 6, while a token with litellm.mcp.beta_add alone still sees only add and gets the Tool 'multiply' is not allowed refusal

Both key plus scope directions are driven over HTTP in case 7 via JWT to key mappings, and with and without a team ceiling by the regression tests in test_user_api_key_auth_mcp.py::TestJwtScopeMcpGrants. Before the fix the first direction returned ["add"] and the second ["multiply"] instead of the unrestricted server. A key that reaches the server through a unified access group (access_group_ids) or a tag-style mcp_access_groups entry is treated as the same whole-server grant, with its own regression tests, and the access group lookup only runs when the key's direct grants do not already name the server

Live regression check, base eda1fab vs tip 9ed18c3

Same config without the new fields (team_id_jwt_field set, scope_mappings with models only), same scenario order, both trees on real Postgres and real upstreams, outputs normalized to sorted tool names plus status and diffed. The diff has two lines: the build marker, and the malformed {"scope":5} chat error, which now reads Unmapped scope type at 'scope' - <class 'int'> instead of Unmapped scope type - <class 'int'> (same HTTP 401, the message names the configured claim path). Cases covered: team member via JWT team_id claim (server list, tools/list, tools/call, chat completion pong), master key tools/list, team member refused on a server the team does not grant, unknown team_id claim, hostile scopes (5, list form, empty, space separated, 5000 chars) on both MCP and chat, and no Authorization header. The slow upstream and Postgres refusing connection legs were last run at 766fd564b3 against base bc6b540205 and were not rerun on this tip

  1. Team member via JWT team_id claim: server list, tools/list, tools/call, chat completion (pong)
  2. Master key tools/list, team member refused on a server the team does not grant, unknown team_id claim returns HTTP 404 Team doesn't exist in db
  3. Hostile claims on the old config: {"scope":5} HTTP 401 on both MCP and chat, list form scope accepted, empty scope refused on chat with HTTP 403 and unchanged on MCP, 5000 char scope, no Authorization header HTTP 401
  4. Slow upstream (SIGSTOP on the math_beta upstream): master key tools/list returns alpha tools with math_beta marked "status":"timeout" after 30s on both trees, team member listing and calls stay at 0.1s, tools/call math_beta-add hits the 45s client timeout identically, and both recover after SIGCONT
  5. Postgres refusing connections: team member tools/list x3, master key tools/list, and both after restore, identical on both trees

Dependent paths of the changed symbols: ScopeMapping is consumed only by LiteLLM_JWTAuth.scope_mappings and JWTAuthManager.check_scope_based_access (verified live via the chat control on both trees). get_mcp_server_access, get_allowed_mcp_servers, and get_allowed_tools_for_server are reached from /v1/mcp/server (verified live), /mcp/ tools/list and tools/call (verified live), and from paths that share the same resolver but were not driven live here: the Responses API MCP handler (tools: [{"type": "mcp"}]), MCP tool search, the MCP OAuth discovery endpoints, the MCP management endpoints, and the Admin UI MCP pages. Those are covered through the unit tests on the shared resolver only. Dashboard, SDK, DB columns, and docs do not reference the new fields, so there is nothing to update there

Local gates at 9ed18c3 on the SDK 2 lock (uv sync --inexact --frozen --extra proxy --group proxy-dev --group e2e-dev, mcp==2.2.0): make lint exit 0 (ruff prod and tests, format, strict-rule, type-discipline, test-quality and basedpyright budgets all OK against origin/main, e2e basedpyright 0 errors, circular import and import safety clean). Tests in separate processes: the mapped MCP auth test file plus the JWT handler tests, 652 passed; MCP server, REST endpoint, toolset scope and JWT enforcement tests, 478 passed, 4 skipped; JWT admission and key mapping tests from test_user_api_key_auth.py, 43 passed. Line and branch coverage from those three runs combined: 76 of 76 changed executable lines hit (44 in user_api_key_auth_mcp.py, 24 in handle_jwt.py, 6 in _types.py, 2 in user_api_key_auth.py); every new helper has full branch coverage (get_scopes 6/6, mcp_grants_from_claims 2/2, _key_tool_grant_for_server 4/4, _merge_additive_tool_grants 2/2); the partial branches left in get_mcp_server_access, get_allowed_tools_for_server and authorize_jwt are pre-existing paths this PR does not touch. Targeted mutation run at 766fd56 over get_scopes, mcp_grants_from_claims, the key/scope grant helpers, the merge, and the admission wiring: 30 of 30 mutants killed; the merge changed none of those lines

Review status at 9ed18c3: CI 96 passed, 0 failed, 1 skipped, codecov/patch passed. Greptile 5/5 with last reviewed commit 9ed18c3. Bugbot: no new issues for 9ed18c3. CodeQL passed, 0 open threads. Veria AI check run on 9ed18c3 completed with No security issues found. All 6 review threads resolved, none open. An independent verifier run on this SHA is still in progress and is not claimed here

Type

🆕 New Feature

Caveats (if any)

Severe

  • A scope tool grant narrows a team member who had the whole server
    • Team grants math_beta with no tool restriction, scope grants math_beta: ["add"], caller sees only add
    • Same rule keys already follow: key level tool allowlists intersect with the team's
    • Give the scope mcp_servers: ["math_beta"] instead to keep the full server, documented in the precedence section of the docs PR
  • scope_jwt_field moves the claim for models and admin_jwt_scope too, not only MCP
    • Once set, the literal scope claim is ignored entirely (case 6, fred and gina)
    • Intentional: one claim feeds every scope_mappings consumer. Default stays scope, so existing configs do not change

Medium

  • Access group resolution needs the DB or config tags; on DB failure only config tagged servers resolve
  • Scope grants are resolved once per request at JWT admission; changing scope_mappings needs a proxy restart, same as today for models and routes

Low

  • Docs live in docs(proxy): document scope_jwt_field and JWT scope MCP grant precedence litellm-docs#1565 and should merge alongside this PR
  • main has moved 7 commits past the merge base eda1faba14 (CI, price sync, a guardrail revert); none touch the files this PR changes, so the branch was not re-merged
  • The tools: [{"type": "mcp"}] prefetch path and Admin UI MCP pages were not driven live, unit tests only
  • Argument-level and resource-level MCP authorization are out of scope; scopes decide servers and tools only

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

ran /live-pr-risk and found no regressions/backward incompatible risks


Note

Medium Risk
Changes authorization for MCP and JWT scope handling; misconfigured scope mappings or scope_jwt_field could widen or narrow access, though team ceilings and fail-closed malformed claims limit blast radius.

Overview
Extends JWT scope_mappings so OIDC scopes can grant MCP servers, access groups, and per-server tool allowlists without a virtual key or team row. At JWT admission, matching scopes are resolved into jwt_scope_mcp_grants on UserAPIKeyAuth and folded into MCP server/tool checks alongside existing key and team permissions.

Adds scope_jwt_field (default scope, dot notation e.g. realm_access.roles) so the same claim drives model scope checks, admin scope, and the new MCP grants. Scope and key grants are additive (whole-server grants keep all tools; tool lists union), while team/org ceilings still cap the result; malformed or missing scope claims grant nothing.

Includes broad unit coverage for JWT resolution, custom claim paths, and MCP admission precedence (including no_mcp_servers opt-out).

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

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

Extend litellm_jwtauth.scope_mappings with mcp_servers, mcp_access_groups and mcp_tool_permissions so a JWT scope claim can authorize MCP servers and tools without a pre-provisioned key or team row. The MCP resolver unions the scope-derived grants into the key level, so existing team, end-user, agent, user and org ceilings still apply. Unmatched, missing, malformed or absent claims grant nothing.

Resolves LIT-4505

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration
devin-ai-integration Bot requested a review from a team September 18, 2026 23:20
@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@codspeed

codspeed Bot commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_jwt_scope_mcp_authz (9ed18c3) with main (4301ac4)

Open in CodSpeed

@greptile-apps

greptile-apps Bot commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge because no outstanding authorization regression or repository-rule violation remains

Summary

This PR maps verified JWT scopes into MCP server, access-group, and tool grants, including scopes stored in configurable nested claims. The grants are attached to the authenticated identity and combined additively with key grants while preserving team and organization ceilings. The SDK 2 merge retained the JWT grant propagation and MCP permission behavior reviewed previously

  • Scope mappings can grant complete MCP servers, access groups, or per-server tool allowlists
  • scope_jwt_field supplies one configurable claim path for MCP, model, and administrator scope checks
  • JWT grants are resolved from verified claims during admission and stored on UserAPIKeyAuth
  • Key and scope grants combine additively before higher-level permission ceilings are applied
  • Added tests cover malformed claims, nested claims, key and scope combinations, and team ceilings

Reviews (9) · Last reviewed commit: "Merge remote-tracking branch 'origin/mai..."

@joshua-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.

Comment thread litellm/proxy/auth/handle_jwt.py Resolved
@codecov

codecov Bot commented Sep 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…owlists

Evaluate each matched scope mapping as its own grant so a tool allowlist
from one scope no longer narrows a server another scope granted in full.
Drops the explanatory docstrings flagged in review

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.

joshua-berri and others added 2 commits September 19, 2026 00:02
…r tools for MCP JWT scopes

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

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

…KeyAuth

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.

@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.

…cope MCP grants additive

Adds scope_jwt_field to LiteLLM_JWTAuth (default "scope", dot notation supported) so scope_mappings, model scope checks, MCP grants, and admin_jwt_scope all read the same configured claim. A missing path yields no scopes and a non string/list value is rejected, so MCP access fails closed either way.

Key-owned and JWT-scope MCP grants on one server are now merged as independent grants: a whole-server grant from either side keeps every tool instead of being narrowed by the other side's tool allowlist, while team and higher-level ceilings still intersect the result.

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

@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

@veria-ai please review 393f5a4: JWT scope claims granting MCP servers and tools, configurable claim path, additive key and scope grants under team ceilings

@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.

…ts next to scope tools

A key granted a server through access_group_ids has no tool allowlist there, so a JWT scope tool allowlist on the same server must not narrow it. Drops the helper docstrings Greptile flagged.

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

@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

@veria-ai please review cdd73a1: key unified access group servers now count as whole-server grants next to JWT scope tool allowlists

@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.

…grants the server

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

@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

@veria-ai please review 766fd56: unified key access groups are whole-server grants, resolved only when direct grants miss

@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

@joshua-berri

Copy link
Copy Markdown
Contributor

@greptileai please review commit 9ed18c3 after the SDK 2 merge, checking JWT grants and preserved permission ceilings

@joshua-berri

Copy link
Copy Markdown
Contributor

bugbot run

Please review commit 9ed18c3 after the SDK 2 merge, checking JWT grants and permission enforcement

@joshua-berri

Copy link
Copy Markdown
Contributor

@veria-ai please review commit 9ed18c3 after the SDK 2 merge, checking JWT claims, additive grants, and authorization boundaries

@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 9ed18c3. Configure here.

@tin-berri tin-berri 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.

Approving. Resolves MCP server and tool access from JWT scope mappings, unioning scope-derived grants into the key's allowed set and adding a per-server tool grant path alongside the existing key/team resolution. No migration and no spend-table access. Flagging for the author's awareness rather than blocking: this widens which servers and tools a JWT caller can reach, so the scope-to-grant mapping is the thing to keep an eye on in staging.

@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

Thanks Tin. Agreed on the staging watch item: a mapping only widens access when the IdP issues the configured claim value, and every scope-derived grant is still capped by the team/org/end-user ceilings (precedence and the fail-closed rules are written up in BerriAI/litellm-docs#1565). The scope_mappings block in the proxy config is the single place to audit which claims map to which servers/tools.

@DSHCorrectover

Copy link
Copy Markdown

This mapping solves the decision half of gateway-level MCP authorization. Worth pairing it with the evidence half while the schema is being designed: when a JWT scope grants or denies a tool call, that resolution should leave a call_id-bound, signed receipt (scope source, decision, args digest) — otherwise enterprise deployments get authorization at the gateway but still can't answer an auditor's "prove what each agent was allowed to do and what actually ran".

The join executed == authorized is exactly the property we measured on a public benchmark: AgentDojo banking suite (pinned commit, paired runs) — 27 runs, zero attack success, 17/17 state-changing attack-path calls denied with call_id-bound Ed25519 receipts, corpus frozen at DOI 10.5281/zenodo.24356966. A receipts hook at the proxy layer (where this PR already lives) would give LiteLLM enterprise customers a replayable audit trail over every MCP tool call without touching client SDKs — the gateway is the natural chokepoint for it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants