Skip to content

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

Merged
allozaur merged 2 commits intoggml-org:masterfrom
satishkc7:fix/webui-cors-proxy-api-key
Mar 31, 2026
Merged

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

Conversation

@satishkc7
Copy link
Copy Markdown
Contributor

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

This comment was marked as off-topic.

Comment thread tools/server/webui/src/lib/services/mcp.service.ts
@satishkc7
Copy link
Copy Markdown
Contributor 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
Contributor 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.
Copy link
Copy Markdown
Contributor

@allozaur allozaur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will merge this and push static build in another PR that im about to merge

@allozaur allozaur merged commit fcc2d59 into ggml-org:master Mar 31, 2026
6 checks passed
slartibardfast pushed a commit to slartibardfast/llama.cpp that referenced this pull request Apr 12, 2026
…-org#21193)

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

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

* fix: simplify headers assignment based on reviewer suggestion

Apply buildProxiedHeaders only when useProxy is true, pass headers
directly to the transport otherwise.
Seunghhon pushed a commit to Seunghhon/llama.cpp that referenced this pull request Apr 26, 2026
…-org#21193)

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

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

* fix: simplify headers assignment based on reviewer suggestion

Apply buildProxiedHeaders only when useProxy is true, pass headers
directly to the transport otherwise.
rsenthilkumar6 pushed a commit to rsenthilkumar6/llama.cpp that referenced this pull request May 1, 2026
…-org#21193)

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

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

* fix: simplify headers assignment based on reviewer 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

3 participants