Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "LiteLLM_MCPServerTable" ADD COLUMN IF NOT EXISTS "token_exchange_profile" TEXT;
1 change: 1 addition & 0 deletions litellm-proxy-extras/litellm_proxy_extras/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ model LiteLLM_MCPServerTable {
// RFC 8707 resource indicators are a separate concept, named "resource" in the v2 egress types.
audience String?
subject_token_type String?
token_exchange_profile String?
allow_all_keys Boolean @default(false)
available_on_public_internet Boolean @default(true)
delegate_auth_to_upstream Boolean @default(false)
Expand Down
1 change: 1 addition & 0 deletions litellm/models/mcp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class LiteLLM_MCPServerTable(LiteLLMPydanticObjectBase):
token_exchange_endpoint: Optional[str] = None
audience: Optional[str] = None
subject_token_type: Optional[str] = None
token_exchange_profile: Optional[str] = None
allow_all_keys: bool = False
available_on_public_internet: bool = True
delegate_auth_to_upstream: bool = False
Expand Down
2 changes: 2 additions & 0 deletions litellm/proxy/_experimental/mcp_server/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"token_exchange_endpoint",
"audience",
"subject_token_type",
"token_exchange_profile",
}
)

Expand All @@ -70,6 +71,7 @@
"token_exchange_endpoint",
"audience",
"subject_token_type",
"token_exchange_profile",
}
)

Expand Down
5 changes: 4 additions & 1 deletion litellm/proxy/_experimental/mcp_server/mcp_server_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,8 @@ async def build_mcp_server_from_table(
subject_token_type=mcp_server.subject_token_type
or (credentials_dict.get("subject_token_type") if credentials_dict else None)
or DEFAULT_SUBJECT_TOKEN_TYPE,
token_exchange_profile=(credentials_dict.get("token_exchange_profile") if credentials_dict else None)
token_exchange_profile=mcp_server.token_exchange_profile
or (credentials_dict.get("token_exchange_profile") if credentials_dict else None)
or "rfc8693",
timeout=getattr(mcp_server, "timeout", None),
max_concurrent_requests=getattr(mcp_server, "max_concurrent_requests", None),
Expand Down Expand Up @@ -4637,6 +4638,7 @@ async def _noop(session):
token_exchange_endpoint=server.token_exchange_endpoint,
audience=server.audience,
subject_token_type=server.subject_token_type,
token_exchange_profile=server.token_exchange_profile,
allow_all_keys=server.allow_all_keys,
instructions=server.instructions,
timeout=server.timeout,
Expand Down Expand Up @@ -4744,6 +4746,7 @@ def _build_mcp_server_table(self, server: MCPServer) -> LiteLLM_MCPServerTable:
token_exchange_endpoint=server.token_exchange_endpoint,
audience=server.audience,
subject_token_type=server.subject_token_type,
token_exchange_profile=server.token_exchange_profile,
allow_all_keys=server.allow_all_keys,
available_on_public_internet=server.available_on_public_internet,
delegate_auth_to_upstream=server.delegate_auth_to_upstream,
Expand Down
2 changes: 2 additions & 0 deletions litellm/proxy/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,7 @@ class NewMCPServerRequest(LiteLLMPydanticObjectBase):
token_exchange_endpoint: Optional[str] = None
audience: Optional[str] = None
subject_token_type: Optional[str] = None
token_exchange_profile: Optional[str] = None
allow_all_keys: bool = False
available_on_public_internet: bool = True
delegate_auth_to_upstream: bool = False
Expand Down Expand Up @@ -1355,6 +1356,7 @@ class UpdateMCPServerRequest(LiteLLMPydanticObjectBase):
token_exchange_endpoint: Optional[str] = None
audience: Optional[str] = None
subject_token_type: Optional[str] = None
token_exchange_profile: Optional[str] = None
allow_all_keys: bool = False
available_on_public_internet: bool = True
delegate_auth_to_upstream: bool = False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ def _sanitize_mcp_server_for_non_admin(
sanitized.token_exchange_endpoint = None
sanitized.audience = None
sanitized.subject_token_type = None
sanitized.token_exchange_profile = None
# Drop env vars entirely rather than only blanking global values: the
# names alone (DB_PASSWORD, GITHUB_API_KEY, ...) leak what secrets the
# admin configured. Non-admins get the per-user vars they must fill in
Expand Down Expand Up @@ -584,6 +585,7 @@ def _sanitize_mcp_server_for_virtual_key(
sanitized.token_exchange_endpoint = None
sanitized.audience = None
sanitized.subject_token_type = None
sanitized.token_exchange_profile = None

sanitized.health_check_error = None
sanitized.last_health_check = None
Expand Down
1 change: 1 addition & 0 deletions litellm/proxy/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ model LiteLLM_MCPServerTable {
// RFC 8707 resource indicators are a separate concept, named "resource" in the v2 egress types.
audience String?
subject_token_type String?
token_exchange_profile String?
allow_all_keys Boolean @default(false)
available_on_public_internet Boolean @default(true)
delegate_auth_to_upstream Boolean @default(false)
Expand Down
4 changes: 4 additions & 0 deletions litellm/types/mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ class MCPCredentials(TypedDict, total=False):
Token exchange wire dialect: "rfc8693" (default, the standard token-exchange grant) or
"entra_obo" (Microsoft Entra On-Behalf-Of, the RFC 7523 jwt-bearer grant + requested_token_use
extension). Not a secret; stored unencrypted.

Legacy input shape: lifted into the dedicated ``token_exchange_profile`` column on
write and stripped from the stored blob; the column is authoritative. Prefer the
top-level request field.
"""


Expand Down
1 change: 1 addition & 0 deletions schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ model LiteLLM_MCPServerTable {
// RFC 8707 resource indicators are a separate concept, named "resource" in the v2 egress types.
audience String?
subject_token_type String?
token_exchange_profile String?
allow_all_keys Boolean @default(false)
available_on_public_internet Boolean @default(true)
delegate_auth_to_upstream Boolean @default(false)
Expand Down
3 changes: 3 additions & 0 deletions tests/mcp_tests/test_mcp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,7 @@ async def test_add_update_server_with_alias():
mock_mcp_server.token_exchange_endpoint = None
mock_mcp_server.audience = None
mock_mcp_server.subject_token_type = None
mock_mcp_server.token_exchange_profile = None
# Additional fields used by build_mcp_server_from_table
mock_mcp_server.extra_headers = None
mock_mcp_server.allow_all_keys = False
Expand Down Expand Up @@ -1621,6 +1622,7 @@ async def test_add_update_server_without_alias():
mock_mcp_server.token_exchange_endpoint = None
mock_mcp_server.audience = None
mock_mcp_server.subject_token_type = None
mock_mcp_server.token_exchange_profile = None
# Additional fields used by build_mcp_server_from_table
mock_mcp_server.extra_headers = None
mock_mcp_server.allow_all_keys = False
Expand Down Expand Up @@ -1683,6 +1685,7 @@ async def test_add_update_server_fallback_to_server_id():
mock_mcp_server.token_exchange_endpoint = None
mock_mcp_server.audience = None
mock_mcp_server.subject_token_type = None
mock_mcp_server.token_exchange_profile = None
# Additional fields used by build_mcp_server_from_table - set explicitly
# to avoid MagicMock objects being passed to Pydantic MCPServer constructor
mock_mcp_server.extra_headers = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,7 @@ def test_prepare_mcp_server_data_create_carries_token_exchange_columns():
token_exchange_endpoint="https://idp.example.com/oauth2/token",
audience="https://upstream.example.com",
subject_token_type="urn:ietf:params:oauth:token-type:jwt",
token_exchange_profile="entra_obo",
credentials={"client_id": "te-client", "client_secret": "te-secret"},
)

Expand All @@ -748,6 +749,7 @@ def test_prepare_mcp_server_data_create_carries_token_exchange_columns():
assert data["token_exchange_endpoint"] == "https://idp.example.com/oauth2/token"
assert data["audience"] == "https://upstream.example.com"
assert data["subject_token_type"] == "urn:ietf:params:oauth:token-type:jwt"
assert data["token_exchange_profile"] == "entra_obo"


def test_prepare_mcp_server_data_update_carries_token_exchange_columns():
Expand All @@ -761,10 +763,12 @@ def test_prepare_mcp_server_data_update_carries_token_exchange_columns():
token_exchange_endpoint="https://idp.example.com/oauth2/token",
audience="https://upstream.example.com",
subject_token_type="urn:ietf:params:oauth:token-type:jwt",
token_exchange_profile="entra_obo",
)

data = _prepare_mcp_server_data(request, exclude_unset=True)

assert data["token_exchange_endpoint"] == "https://idp.example.com/oauth2/token"
assert data["audience"] == "https://upstream.example.com"
assert data["subject_token_type"] == "urn:ietf:params:oauth:token-type:jwt"
assert data["token_exchange_profile"] == "entra_obo"
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ async def test_auth_type_switch_clears_stale_flow_scoped_fields():
"token_exchange_endpoint",
"audience",
"subject_token_type",
"token_exchange_profile",
):
assert data_dict[stale_field] is None, f"{stale_field} must be cleared on auth_type switch"
assert data_dict["credentials"] is None
Expand Down Expand Up @@ -235,6 +236,7 @@ async def test_auth_type_switch_back_to_oauth2_clears_token_exchange_fields():
assert data_dict["token_exchange_endpoint"] is None
assert data_dict["audience"] is None
assert data_dict["subject_token_type"] is None
assert data_dict["token_exchange_profile"] is None


@pytest.mark.asyncio
Expand All @@ -257,6 +259,7 @@ async def test_unchanged_auth_type_does_not_clear_flow_fields():
"token_exchange_endpoint",
"audience",
"subject_token_type",
"token_exchange_profile",
):
assert flow_field not in data_dict

Expand Down Expand Up @@ -309,6 +312,7 @@ def _existing_row(auth_type: str, credentials: dict | None = None):
existing.token_exchange_endpoint = None
existing.audience = None
existing.subject_token_type = None
existing.token_exchange_profile = None
return existing


Expand All @@ -328,6 +332,7 @@ async def test_create_lifts_blob_token_exchange_settings_into_columns():
"token_exchange_endpoint": "https://idp.example.com/oauth2/token",
"audience": "api://upstream",
"subject_token_type": "urn:ietf:params:oauth:token-type:jwt",
"token_exchange_profile": "entra_obo",
},
)

Expand All @@ -337,8 +342,9 @@ async def test_create_lifts_blob_token_exchange_settings_into_columns():
assert data_dict["token_exchange_endpoint"] == "https://idp.example.com/oauth2/token"
assert data_dict["audience"] == "api://upstream"
assert data_dict["subject_token_type"] == "urn:ietf:params:oauth:token-type:jwt"
assert data_dict["token_exchange_profile"] == "entra_obo"
stored_blob = json.loads(data_dict["credentials"])
for te_field in ("token_exchange_endpoint", "audience", "subject_token_type"):
for te_field in ("token_exchange_endpoint", "audience", "subject_token_type", "token_exchange_profile"):
assert te_field not in stored_blob
assert "client_id" in stored_blob

Expand Down Expand Up @@ -374,6 +380,7 @@ async def test_credentials_merge_migrates_legacy_blob_te_settings():
"client_id": "enc-old-cid",
"token_exchange_endpoint": "https://legacy-idp.example.com/token",
"audience": "api://legacy",
"token_exchange_profile": "entra_obo",
},
)
mock_prisma.db.litellm_mcpservertable.find_unique = AsyncMock(return_value=existing)
Expand All @@ -388,8 +395,9 @@ async def test_credentials_merge_migrates_legacy_blob_te_settings():

assert data_dict["token_exchange_endpoint"] == "https://legacy-idp.example.com/token"
assert data_dict["audience"] == "api://legacy"
assert data_dict["token_exchange_profile"] == "entra_obo"
merged_blob = json.loads(data_dict["credentials"])
for te_field in ("token_exchange_endpoint", "audience", "subject_token_type"):
for te_field in ("token_exchange_endpoint", "audience", "subject_token_type", "token_exchange_profile"):
assert te_field not in merged_blob


Expand Down Expand Up @@ -465,6 +473,7 @@ async def test_auth_type_switch_clears_flow_fields_with_external_fields_set():
"token_exchange_endpoint",
"audience",
"subject_token_type",
"token_exchange_profile",
):
assert data_dict[stale_field] is None, f"{stale_field} must be cleared via the fields_set path"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5187,6 +5187,7 @@ async def test_list_tools_with_legacy_db_m2m_server_resolves_oauth2_flow():
legacy_server.token_exchange_endpoint = None
legacy_server.audience = None
legacy_server.subject_token_type = None
legacy_server.token_exchange_profile = None
legacy_server.token_url = "https://oauth.example.com/token"
legacy_server.authorization_url = None
legacy_server.client_id = "client-id"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4482,6 +4482,81 @@ async def test_round_trip_token_exchange_columns_preserved(self):
assert rebuilt_table.audience == "https://upstream.example.com"
assert rebuilt_table.subject_token_type == "urn:ietf:params:oauth:token-type:jwt"

@pytest.mark.asyncio
async def test_build_mcp_server_from_table_reads_token_exchange_profile_column(self):
"""The profile dialect selector (rfc8693 vs entra_obo) is read from its dedicated column
so a server created via the REST API/UI as entra_obo resolves to the Entra dialect."""
manager = MCPServerManager()

table_record = LiteLLM_MCPServerTable(
server_id="te-profile",
server_name="te_profile",
url="https://upstream.example.com/mcp",
transport=MCPTransport.http,
auth_type=MCPAuth.oauth2_token_exchange,
token_exchange_endpoint="https://login.microsoftonline.com/tenant/oauth2/v2.0/token",
token_exchange_profile="entra_obo",
)

mcp_server = await manager.build_mcp_server_from_table(table_record)

assert mcp_server.token_exchange_profile == "entra_obo"

@pytest.mark.asyncio
async def test_build_mcp_server_from_table_token_exchange_profile_defaults_rfc8693(self):
"""token_exchange_profile falls back to rfc8693 when neither column nor blob sets it."""
manager = MCPServerManager()

table_record = LiteLLM_MCPServerTable(
server_id="te-profile-default",
server_name="te_profile_default",
url="https://upstream.example.com/mcp",
transport=MCPTransport.http,
auth_type=MCPAuth.oauth2_token_exchange,
token_exchange_endpoint="https://idp.example.com/oauth2/token",
)

mcp_server = await manager.build_mcp_server_from_table(table_record)

assert mcp_server.token_exchange_profile == "rfc8693"

@pytest.mark.asyncio
async def test_build_mcp_server_from_table_token_exchange_profile_blob_fallback(self):
"""Backwards compatibility: a server with the profile only in the credentials blob still loads."""
manager = MCPServerManager()

table_record = LiteLLM_MCPServerTable(
server_id="te-profile-blob",
server_name="te_profile_blob",
url="https://upstream.example.com/mcp",
transport=MCPTransport.http,
auth_type=MCPAuth.oauth2_token_exchange,
credentials={"token_exchange_profile": "entra_obo"},
)

mcp_server = await manager.build_mcp_server_from_table(table_record)

assert mcp_server.token_exchange_profile == "entra_obo"

@pytest.mark.asyncio
async def test_round_trip_token_exchange_profile_preserved(self):
"""token_exchange_profile survives LiteLLM_MCPServerTable -> MCPServer -> LiteLLM_MCPServerTable."""
manager = MCPServerManager()

table_record = LiteLLM_MCPServerTable(
server_id="te-profile-rt",
server_name="te_profile_rt",
url="https://upstream.example.com/mcp",
transport=MCPTransport.http,
auth_type=MCPAuth.oauth2_token_exchange,
token_exchange_profile="entra_obo",
)

mcp_server = await manager.build_mcp_server_from_table(table_record)
rebuilt_table = manager._build_mcp_server_table(mcp_server)

assert rebuilt_table.token_exchange_profile == "entra_obo"


class TestInternalDelegatePkceWarningLog:
@pytest.mark.asyncio
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,7 @@ async def test_build_mcp_server_from_table_with_sigv4_credentials(self):
table_record.token_exchange_endpoint = None
table_record.audience = None
table_record.subject_token_type = None
table_record.token_exchange_profile = None
table_record.instructions = None
table_record.source_url = None

Expand Down Expand Up @@ -921,6 +922,7 @@ async def test_build_mcp_server_from_table_without_sigv4_credentials(self):
table_record.token_exchange_endpoint = None
table_record.audience = None
table_record.subject_token_type = None
table_record.token_exchange_profile = None
table_record.instructions = None
table_record.source_url = None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3775,6 +3775,7 @@ def test_sanitize_mcp_server_for_non_admin_clears_credential_fields():
server.token_exchange_endpoint = "https://idp/token-exchange"
server.audience = "https://upstream/api"
server.subject_token_type = "urn:ietf:params:oauth:token-type:jwt"
server.token_exchange_profile = "entra_obo"

sanitized = _sanitize_mcp_server_for_non_admin(server)

Expand All @@ -3795,6 +3796,7 @@ def test_sanitize_mcp_server_for_non_admin_clears_credential_fields():
assert sanitized.token_exchange_endpoint is None
assert sanitized.audience is None
assert sanitized.subject_token_type is None
assert sanitized.token_exchange_profile is None

# Identity / metadata fields are preserved so the UI can list the
# server without exposing secrets.
Expand Down Expand Up @@ -3858,13 +3860,15 @@ def test_sanitize_virtual_key_clears_token_exchange_endpoint_and_audience():
server.token_exchange_endpoint = "https://idp/token-exchange"
server.audience = "https://upstream/api"
server.subject_token_type = "urn:ietf:params:oauth:token-type:jwt"
server.token_exchange_profile = "entra_obo"

sanitized = mgmt._sanitize_mcp_server_for_virtual_key(server)

assert sanitized.token_url is None
assert sanitized.token_exchange_endpoint is None
assert sanitized.audience is None
assert sanitized.subject_token_type is None
assert sanitized.token_exchange_profile is None


def _server_with_env_vars(server_id: str = "srv-env"):
Expand Down
Loading
Loading