Skip to content

fix: include API key in CORS proxy requests for MCP connections#21193

Open
satishkc7 wants to merge 2 commits intoggml-org:masterfrom
satishkc7:fix/webui-cors-proxy-api-key
Open

fix: include API key in CORS proxy requests for MCP connections#21193
satishkc7 wants to merge 2 commits intoggml-org:masterfrom
satishkc7:fix/webui-cors-proxy-api-key

Conversation

@satishkc7
Copy link
Copy Markdown

Problem

When llama-server is started with both --api-key-file and --webui-mcp-proxy, the /cors-proxy endpoint is subject to the global API key validation middleware. MCP connections configured with "Use Proxy" fail with a 401 because the WebUI does not include the Authorization header in its requests to /cors-proxy.

Fixes #21167

Root Cause

In MCPService.createTransport(), requestInit.headers was only populated with buildProxiedHeaders(config.headers) — which wraps the target MCP server's headers using the X-Proxy-Header-* convention. The Authorization: Bearer <api-key> header needed to authenticate the request against llama-server itself was never included.

Fix

Import the existing getAuthHeaders() utility and spread it into requestInit.headers when useProxy is true:

if (useProxy) {
    requestInit.headers = {
        ...getAuthHeaders(),
        ...(requestInit.headers as Record<string, string>)
    };
}

getAuthHeaders() returns { Authorization: 'Bearer <key>' } when an API key is configured, or an empty object otherwise — so there is no regression when no API key is set.

Testing

  1. Start llama-server with --api-key-file <file> --webui-mcp-proxy
  2. Authenticate with the WebUI using the API key
  3. Add an MCP server with "Use Proxy" enabled
  4. Connection succeeds (previously failed with 401)

When llama-server is started with --api-key-file and --webui-mcp-proxy,
the /cors-proxy endpoint requires authentication. The WebUI was not
including the Authorization header in proxy requests, causing MCP
connections to fail with 401.

Inject getAuthHeaders() into requestInit when useProxy is true so the
proxy request carries the Bearer token alongside the forwarded target
headers.

Fixes ggml-org#21167
@satishkc7 satishkc7 requested a review from a team as a code owner March 30, 2026 22:00
@ggml-gh-bot
Copy link
Copy Markdown

ggml-gh-bot bot commented Mar 30, 2026

Hi @satishkc7, thanks for your contribution!

Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:

  • AI-generated content: This project does not accept PRs, descriptions or commit messages that are fully or predominantly AI-generated. If you have used AI to assist you in writing code, please make sure to disclose that explicitly.

Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below.

Comment on lines 127 to 130
if (config.headers) {
requestInit.headers = buildProxiedHeaders(config.headers);
}

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.

it seems to be a mistake from my #21072 , it seems like the fix is to simply:

Suggested change
if (config.headers) {
requestInit.headers = config.useProxy ? buildProxiedHeaders(config.headers) : config.headers;
}

could you confirm? cc @allozaur too

@satishkc7
Copy link
Copy Markdown
Author

Thanks for the note. The fix was written by me - I traced the issue through createTransport() in mcp.service.ts, identified that requestInit.headers was never populated with the auth header for the proxy request itself, and wrote the change. Happy to discuss the implementation directly with reviewers.

@satishkc7
Copy link
Copy Markdown
Author

Good catch, that's cleaner. I'll update the PR with your suggestion.

Apply buildProxiedHeaders only when useProxy is true, pass headers
directly to the transport otherwise.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Misc. bug: WebUI CORS proxy requests don't include API key for MCP connections

2 participants