refactor(mcp): extract needs_discovery flag and reduceStaticHeaders helper

This commit is contained in:
Ishaan Jaffer 2026-03-10 16:44:18 -07:00
parent 7c804fc8a7
commit 748fb2ad1e
2 changed files with 20 additions and 28 deletions

View file

@ -618,16 +618,17 @@ class MCPServerManager:
mcp_info["description"] = mcp_server.description
auth_type = cast(MCPAuthType, mcp_server.auth_type)
if (
mcp_server.url
server_url = mcp_server.url
needs_discovery = (
bool(server_url)
and auth_type == MCPAuth.oauth2
and not mcp_server.authorization_url
):
mcp_oauth_metadata = await self._descovery_metadata(
server_url=mcp_server.url,
)
else:
mcp_oauth_metadata = None
)
mcp_oauth_metadata = (
await self._descovery_metadata(server_url=server_url) # type: ignore[arg-type]
if needs_discovery
else None
)
resolved_scopes = scopes or (
mcp_oauth_metadata.scopes if mcp_oauth_metadata else None

View file

@ -35,6 +35,15 @@ const AUTH_TYPES_REQUIRING_AUTH_VALUE = [AUTH_TYPE.API_KEY, AUTH_TYPE.BEARER_TOK
const AUTH_TYPES_REQUIRING_CREDENTIALS = [...AUTH_TYPES_REQUIRING_AUTH_VALUE, AUTH_TYPE.OAUTH2];
const CREATE_OAUTH_UI_STATE_KEY = "litellm-mcp-oauth-create-state";
const reduceStaticHeaders = (list: unknown): Record<string, string> => {
if (!Array.isArray(list)) return {};
return list.reduce((acc: Record<string, string>, entry: Record<string, string>) => {
const header = entry?.header?.trim();
if (header) acc[header] = entry?.value ?? "";
return acc;
}, {});
};
const CreateMCPServer: React.FC<CreateMCPServerProps> = ({
userRole,
accessToken,
@ -118,16 +127,7 @@ const CreateMCPServer: React.FC<CreateMCPServerProps> = ({
if (!url || !transport) {
return null;
}
const staticHeaders = Array.isArray(values.static_headers)
? values.static_headers.reduce((acc: Record<string, string>, entry: Record<string, string>) => {
const header = entry?.header?.trim();
if (!header) {
return acc;
}
acc[header] = entry?.value ?? "";
return acc;
}, {})
: ({} as Record<string, string>);
const staticHeaders = reduceStaticHeaders(values.static_headers);
return {
server_id: undefined,
@ -282,16 +282,7 @@ const CreateMCPServer: React.FC<CreateMCPServerProps> = ({
// Transform access groups into objects with name property
const accessGroups = restValues.mcp_access_groups;
const staticHeaders = Array.isArray(staticHeadersList)
? staticHeadersList.reduce((acc: Record<string, string>, entry: Record<string, string>) => {
const header = entry?.header?.trim();
if (!header) {
return acc;
}
acc[header] = entry?.value ?? "";
return acc;
}, {})
: ({} as Record<string, string>);
const staticHeaders = reduceStaticHeaders(staticHeadersList);
const credentialsPayload =
credentialValues && typeof credentialValues === "object"