Add opt-in CORS support to the MCP proxy - #6245
Open
lorenzozanee wants to merge 1 commit into
Open
Conversation
Browser-based MCP clients (e.g. MCP Inspector) cannot use the transparent MCP proxy: OPTIONS preflights are forwarded to the backend, which answers 405, and proxied responses carry no Access-Control-Allow-* headers. Setting --allowed-origins on thv proxy or thv run now enables CORS for exactly those origins, using the same explicit allowlist enforced by Origin-header validation. CORS stays disabled by default. Signed-off-by: lorenzozanee <wyz0707@proton.me>
lorenzozanee
requested review from
ChrisJBurns,
JAORMX,
amirejaz,
aponcedeleonch,
blkt,
jhrozek,
rdimitrov and
reyortiz3
as code owners
August 9, 2026 22:55
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Browser-based MCP clients (for example MCP Inspector) cannot use the transparent MCP proxy endpoint: the browser's OPTIONS preflight is forwarded to the backend MCP server, which answers 405 Method Not Allowed, and proxied responses carry no Access-Control-Allow-* headers, so the browser blocks them with a CORS error.
This adds opt-in CORS support to the proxy. Setting --allowed-origins on thv proxy or thv run enables CORS for exactly those origins:
Access-Control-Allow-Headers includes MCP-Protocol-Version (ToolHive reads and validates it on the request path) and Last-Event-ID (used for SSE stream resumption).
Fixes #4297
Type of change
Test plan
task test)task test-e2e)task lint-fix)Unit tests cover the new middleware (preflight 204 + headers for an allowed origin, no CORS when no origins are configured, exact-match only) and the proxy wiring (OPTIONS answered by the proxy instead of the backend 405; disallowed origins still 403 from Origin validation). Manually verified against a running proxy: an allow-listed origin now receives 204 + Access-Control-Allow-Origin on preflight and 200 + headers on the real request, while the default configuration keeps the previous backend 405 behavior.
Changes
Does this introduce a user-facing change?
Yes. --allowed-origins on thv proxy and thv run now also enables CORS for the listed origins on the MCP proxy endpoint, so browser-based clients (e.g. MCP Inspector) can connect. When the flag is omitted, behavior is unchanged: CORS stays disabled. Operators who handle CORS at an upstream gateway can keep omitting the flag.