Skip to content

Commit d429e84

Browse files
fix(mcp): skip JWT injection when extra_headers already has Authorization
When a server uses per-user OAuth tokens, the resolved token is passed into _get_tools_from_server via extra_headers. The JWT injection guard only checked mcp_auth_header and the server's static headers, so the signer would silently overwrite the user's OAuth Authorization header. Add a check for an existing Authorization entry in extra_headers so caller-supplied per-user OAuth tokens take precedence over JWT signing. Co-authored-by: Yassin Kortam <yassin@berri.ai>
1 parent 8da32a5 commit d429e84

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

litellm/proxy/_experimental/mcp_server/mcp_server_manager.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,8 +1437,10 @@ async def _get_tools_from_server(
14371437
# MCPJWTSigner: inject signed JWT for tools/list (list path skips pre_call_hook).
14381438
# Skip entirely when the signer is not configured (avoid an unnecessary
14391439
# dict copy on every list call), when the server has its own static
1440-
# Authorization header, or when a per-user mcp_auth_header has already
1441-
# been resolved — admin-configured static auth and per-user OAuth must
1440+
# Authorization header, when a per-user mcp_auth_header has already
1441+
# been resolved, or when the caller already supplied an Authorization
1442+
# entry in extra_headers (e.g. a per-user OAuth token resolved
1443+
# upstream) — admin-configured static auth and per-user OAuth must
14421444
# take precedence so the signer doesn't silently overwrite e.g. an
14431445
# upstream API key or a user's OAuth token (MCPClient._get_auth_headers
14441446
# applies extra_headers after writing Authorization from auth_value, so
@@ -1454,11 +1456,16 @@ async def _get_tools_from_server(
14541456
isinstance(k, str) and k.lower() == "authorization"
14551457
for k in static_headers.keys()
14561458
)
1459+
has_extra_authorization = bool(extra_headers) and any(
1460+
isinstance(k, str) and k.lower() == "authorization"
1461+
for k in (extra_headers or {}).keys()
1462+
)
14571463

14581464
if (
14591465
get_mcp_jwt_signer() is not None
14601466
and not has_static_authorization
14611467
and not mcp_auth_header
1468+
and not has_extra_authorization
14621469
):
14631470
extra_headers = await inject_mcp_jwt_headers_for_upstream(
14641471
user_api_key_dict=user_api_key_auth,

0 commit comments

Comments
 (0)