Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

1.5 KB
Raw Blame
import type { MCPOptions } from 'librechat-data-provider';

type ApiKeyConfig = Partial<NonNullable<MCPOptions['apiKey']>> | null | undefined;

/** Recognizes only server-generated API key fields, not similarly named explicit variables. */
export function isGeneratedUserApiKeyVariable(name: string): boolean {
  return /^MCP_API_KEY(?:_[a-f0-9]{64})?$/.test(name);
}

function getApiKeyHeaderName(apiKey: ApiKeyConfig): string {
  return apiKey?.authorization_type === 'custom'
    ? apiKey.custom_header || 'X-Api-Key'
    : 'Authorization';
}

/** The header injected by an operator-provided API key, before placeholder resolution. */
export function getAdminApiKeyHeader(
  apiKey: ApiKeyConfig,
): { name: string; value: string } | undefined {
  if (apiKey?.source !== 'admin' || !apiKey.key) {
    return;
  }
  const { key, authorization_type } = apiKey;
  const name = getApiKeyHeaderName(apiKey);
  const prefixes = { basic: 'Basic ', bearer: 'Bearer ', custom: '' };
  const prefix = prefixes[authorization_type ?? 'custom'];
  return { name, value: `${prefix}${key}` };
}

/** A shadowed catalog credential must not be injected or required by a chat connection. */
export function isApiKeyHeaderOverridden(
  apiKey: ApiKeyConfig,
  requestHeaders?: Record<string, string | undefined>,
): boolean {
  const injected = apiKey?.source === 'user' || getAdminApiKeyHeader(apiKey) != null;
  return (
    injected &&
    requestHeaders != null &&
    Object.keys(requestHeaders).some(
      (name) => name.toLowerCase() === getApiKeyHeaderName(apiKey).toLowerCase(),
    )
  );
}