You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ToolHive MCP proxy endpoint does not support CORS #4297
The ToolHive MCP HTTP proxy (/mcp) lacks CORS support, blocking web-based clients (e.g., MCP Inspector). The server returns 405 Method Not Allowed on OPTIONS preflight and misses Access-Control-Allow-Origin.
Steps to Reproduce
Add an MCP to thv.
Configure a client, e.g. OpenCode.
Get the proxy MCP URL from the client config, e.g. http://localhost:43969/mcp and paste it in MCP inspector.
Attempt to connect.
Connection fails due to 405 error and CORS.
Request URL http://localhost:43969/mcp
Request Method OPTIONS
Status Code 405 Method Not Allowed
Remote Address 127.0.0.1:43969
Referrer Policy strict-origin-when-cross-origin
GET http://localhost:43969/mcp net::ERR_FAILED
Access to fetch at 'http://localhost:43969/mcp' from origin 'http://localhost:6274' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Expected Behavior
OPTIONS handles preflight with 200/204.
Proper Access-Control-Allow-* headers present on the /mcp route.
Thanks for the report, @milichev, and apologies that this sat without a reply for so long. The behaviour you describe is real and still present.
Some status you should have. @DevamShah opened #5588 against this issue, adding opt-in CORS behind an --allow-origins flag. It went through two rounds of review and was closed unmerged on 14 July as review-queue housekeeping rather than on the merits, so the work is available to anyone who wants to resume it.
One thing worth knowing about why this is not a simple fix. CORS headers are omitted on the proxy deliberately, because pkg/transport/middleware/origin enforces Origin validation per the MCP specification's security warning, and reflecting the request Origin or emitting * would bypass that protection. So any fix has to be an explicit opt-in allowlist, never a default relaxation.
Three design questions need settling before more code goes in: whether the preflight is handled by default or only behind a flag, which proxy surface carries the behaviour, and the fact that your repro is the thv run path while #5588 wired the flag elsewhere. There were also smaller review items outstanding on that PR: a missing Last-Event-ID header for SSE stream resumption and a data race from capturing the caller's origins slice.
We have labelled this good-tenth-issue to signal that we would welcome a community PR. Anyone picking it up, including @DevamShah if you want to return to it, should align on those three questions in this issue first so the next attempt does not stall the same way. Please also ask to have the issue assigned before opening a PR, per the contributor guidelines.
@ChrisJBurns thanks for the status — that's a useful summary of where this stands.
I'd like to work on this — please assign me. I should have asked for the assignment before opening #5588 rather than after the fact; that was my miss on CONTRIBUTING.md#claiming-an-issue, and I'll follow the order this time.
On your third point — that the repro is the thv run path while #5588 wired the flag elsewhere — you're right, and it's worse than "wired elsewhere". I traced it against main at 0a74004. There are exactly two places the transparent proxy is constructed:
cmd/thv/app/proxy.go:290 — thv proxy
pkg/transport/http.go:340 — HTTPTransport, used by thv run only for sse / streamable-httpcontainers
thv run against a stdio server never touches the transparent proxy at all. pkg/transport/factory.go:49 routes stdio to NewStdioTransport, which at pkg/transport/stdio.go:227 builds streamable.NewHTTPProxy. That proxy serves /mcp itself (StreamableHTTPEndpoint, pkg/transport/proxy/streamable/streamable_proxy.go:36) and its handleStreamableRequest switch has no OPTIONS case, so it falls to the default branch and returns 405 (streamable_proxy.go:414).
So there were two separate gaps in #5588, not one:
Stdio thv run — the middleware is unreachable by construction. Different proxy package entirely.
SSE / streamable-http thv run — the middleware is reachable, but buildProxyOptions (pkg/transport/http.go:431) never appends the option, so it stays off regardless.
I drove the handler directly in a scratch Go test against main to confirm rather than infer:
streamable /mcp OPTIONS -> 405 ACAO=""
That is @milichev's reported symptom, on the surface he actually hit, produced by code the PR did not touch.
A second problem that I don't think has been named yet. Handling the preflight is necessary but not sufficient on the thv run path. prependOriginMiddleware (pkg/runner/middleware.go:552) is prepended as the outermost handler for every thv run workload, and for a loopback bind origin.ResolveAllowedOrigins (origin.go:224) derives the allowlist as the proxy's own origin, not the browser client's. Same scratch test, same run:
Inspector on :6274 talking to a proxy on :43969 is not in that set. So even with a perfect CORS layer, the preflight gets a 403 from Origin validation before CORS is reached. Any fix that doesn't also put the browser's origin into the Origin allowlist will still fail this repro.
That shapes the two design questions, so here's my proposal rather than an open-ended question:
(a) Opt-in flag or preflight by default? I'd argue neither of the shapes #5588 used. --allowed-origins already exists on both thv run (run_flags.go:183) and thv proxy (proxy.go:139) and is already the operator's statement of "these browser origins are trusted". CORS should be derived from that existing allowlist rather than carrying a second flag. Concretely: if an origin is in the effective Origin allowlist, answer its preflight and emit Access-Control-Allow-Origin for it; otherwise emit nothing, exactly as today. That keeps CORS a strict subset of what Origin validation already permits — it can never widen the DNS-rebind surface, which was your stated constraint — makes the repro a single documented flag (--allowed-origins http://localhost:6274), and fixes the 403 and the 405 in one step, since both read the same list. It also avoids shipping --allow-origins next to an existing --allowed-origins, which #5588 would have done and which I think is a genuine UX trap.
The residual question that leaves: loopback binds get a derived allowlist today, so under this scheme a plain thv run still won't talk to Inspector until the user passes the flag. I think that's correct and I'd rather be explicit about it than auto-trust arbitrary loopback ports, but it's your call whether the loopback default should also cover the browser side.
(b) Which surface carries it? To actually close this issue it has to be pkg/transport/proxy/streamable at minimum, because that's the reported path. If we want one implementation rather than three, the natural seam is a shared middleware applied where the Origin middleware is already applied — pkg/runner/middleware.go for the thv run paths, plus cmd/thv/app/proxy.go:247 for standalone thv proxy, which already calls ResolveAllowedOrigins. That reaches stdio, sse, streamable-http and thv proxy from one place, and keeps CORS and Origin validation reading the same allowlist by construction. pkg/vmcp/server also serves /mcp and I'd leave it out of scope unless you say otherwise.
There's an existing in-repo precedent for the response shape at pkg/auth/token.go:1351 (the RFC-9728 discovery handler answers OPTIONS with 204 and sets Access-Control-Max-Age), though it reflects any origin — appropriate for a public discovery endpoint, not for /mcp.
If (a) and (b) look right to you I'll build it that way. If you'd rather keep an explicit separate opt-in flag, say so and I'll do that instead — I just don't want to write it twice. The smaller review items from #5588 (Last-Event-ID in the allow-headers list for SSE resumption, and the slices.Clone on the captured origins slice that CI's race detector flagged) I've got noted and they'll land in whatever shape we agree.
Sorry for the slow response on this one, @DevamShah, and thanks for the detailed trace of the stdio path and the derived loopback allowlist.
Here's where things stand. #6669 by @lorenzozanee is open against this issue and was reviewed on September 29. The direction from that review lines up with what you proposed: CORS is derived from the existing --allowed-origins allowlist rather than a second flag, it is enabled explicitly on the thv run and thv proxy paths, and the management API stays validation-only. Inspector will still need --allowed-origins http://localhost:6274, since the loopback default only covers the proxy's own origin. That PR is now waiting on its author for the review changes.
To avoid two parallel implementations we're not assigning this right now. If #6669 doesn't move forward, we'll update here and you'd be welcome to pick it up from that direction. @milichev, you can follow #6669 for progress; this issue will close when it merges.
Quick update, @DevamShah: #6669 has been closed, so this is open again. If you're still up for it, it's yours. I've assigned you. The direction in the September 29 review on #6669 is the one to follow (opt-in CORS on the MCP proxy paths only, modern MCP headers in preflight, reconciling upstream CORS headers, exposing WWW-Authenticate, and only short-circuiting genuine preflights). Please link your PR here with Fixes #4297.
Activity
ChrisJBurns commentedon Jul 30, 2026
Thanks for the report, @milichev, and apologies that this sat without a reply for so long. The behaviour you describe is real and still present.
Some status you should have. @DevamShah opened #5588 against this issue, adding opt-in CORS behind an
--allow-originsflag. It went through two rounds of review and was closed unmerged on 14 July as review-queue housekeeping rather than on the merits, so the work is available to anyone who wants to resume it.One thing worth knowing about why this is not a simple fix. CORS headers are omitted on the proxy deliberately, because
pkg/transport/middleware/originenforces Origin validation per the MCP specification's security warning, and reflecting the request Origin or emitting*would bypass that protection. So any fix has to be an explicit opt-in allowlist, never a default relaxation.Three design questions need settling before more code goes in: whether the preflight is handled by default or only behind a flag, which proxy surface carries the behaviour, and the fact that your repro is the
thv runpath while #5588 wired the flag elsewhere. There were also smaller review items outstanding on that PR: a missingLast-Event-IDheader for SSE stream resumption and a data race from capturing the caller's origins slice.We have labelled this
good-tenth-issueto signal that we would welcome a community PR. Anyone picking it up, including @DevamShah if you want to return to it, should align on those three questions in this issue first so the next attempt does not stall the same way. Please also ask to have the issue assigned before opening a PR, per the contributor guidelines.DevamShah commentedon Aug 28, 2026
@ChrisJBurns thanks for the status — that's a useful summary of where this stands.
I'd like to work on this — please assign me. I should have asked for the assignment before opening #5588 rather than after the fact; that was my miss on CONTRIBUTING.md#claiming-an-issue, and I'll follow the order this time.
On your third point — that the repro is the
thv runpath while #5588 wired the flag elsewhere — you're right, and it's worse than "wired elsewhere". I traced it against main at0a74004. There are exactly two places the transparent proxy is constructed:cmd/thv/app/proxy.go:290—thv proxypkg/transport/http.go:340—HTTPTransport, used bythv runonly forsse/streamable-httpcontainersthv runagainst a stdio server never touches the transparent proxy at all.pkg/transport/factory.go:49routes stdio toNewStdioTransport, which atpkg/transport/stdio.go:227buildsstreamable.NewHTTPProxy. That proxy serves/mcpitself (StreamableHTTPEndpoint,pkg/transport/proxy/streamable/streamable_proxy.go:36) and itshandleStreamableRequestswitch has noOPTIONScase, so it falls to the default branch and returns 405 (streamable_proxy.go:414).So there were two separate gaps in #5588, not one:
thv run— the middleware is unreachable by construction. Different proxy package entirely.thv run— the middleware is reachable, butbuildProxyOptions(pkg/transport/http.go:431) never appends the option, so it stays off regardless.I drove the handler directly in a scratch Go test against main to confirm rather than infer:
That is @milichev's reported symptom, on the surface he actually hit, produced by code the PR did not touch.
A second problem that I don't think has been named yet. Handling the preflight is necessary but not sufficient on the
thv runpath.prependOriginMiddleware(pkg/runner/middleware.go:552) is prepended as the outermost handler for everythv runworkload, and for a loopback bindorigin.ResolveAllowedOrigins(origin.go:224) derives the allowlist as the proxy's own origin, not the browser client's. Same scratch test, same run:Inspector on
:6274talking to a proxy on:43969is not in that set. So even with a perfect CORS layer, the preflight gets a 403 from Origin validation before CORS is reached. Any fix that doesn't also put the browser's origin into the Origin allowlist will still fail this repro.That shapes the two design questions, so here's my proposal rather than an open-ended question:
(a) Opt-in flag or preflight by default? I'd argue neither of the shapes #5588 used.
--allowed-originsalready exists on boththv run(run_flags.go:183) andthv proxy(proxy.go:139) and is already the operator's statement of "these browser origins are trusted". CORS should be derived from that existing allowlist rather than carrying a second flag. Concretely: if an origin is in the effective Origin allowlist, answer its preflight and emitAccess-Control-Allow-Originfor it; otherwise emit nothing, exactly as today. That keeps CORS a strict subset of what Origin validation already permits — it can never widen the DNS-rebind surface, which was your stated constraint — makes the repro a single documented flag (--allowed-origins http://localhost:6274), and fixes the 403 and the 405 in one step, since both read the same list. It also avoids shipping--allow-originsnext to an existing--allowed-origins, which #5588 would have done and which I think is a genuine UX trap.The residual question that leaves: loopback binds get a derived allowlist today, so under this scheme a plain
thv runstill won't talk to Inspector until the user passes the flag. I think that's correct and I'd rather be explicit about it than auto-trust arbitrary loopback ports, but it's your call whether the loopback default should also cover the browser side.(b) Which surface carries it? To actually close this issue it has to be
pkg/transport/proxy/streamableat minimum, because that's the reported path. If we want one implementation rather than three, the natural seam is a shared middleware applied where the Origin middleware is already applied —pkg/runner/middleware.gofor thethv runpaths, pluscmd/thv/app/proxy.go:247for standalonethv proxy, which already callsResolveAllowedOrigins. That reaches stdio, sse, streamable-http andthv proxyfrom one place, and keeps CORS and Origin validation reading the same allowlist by construction.pkg/vmcp/serveralso serves/mcpand I'd leave it out of scope unless you say otherwise.There's an existing in-repo precedent for the response shape at
pkg/auth/token.go:1351(the RFC-9728 discovery handler answersOPTIONSwith 204 and setsAccess-Control-Max-Age), though it reflects any origin — appropriate for a public discovery endpoint, not for/mcp.If (a) and (b) look right to you I'll build it that way. If you'd rather keep an explicit separate opt-in flag, say so and I'll do that instead — I just don't want to write it twice. The smaller review items from #5588 (
Last-Event-IDin the allow-headers list for SSE resumption, and theslices.Cloneon the captured origins slice that CI's race detector flagged) I've got noted and they'll land in whatever shape we agree.aponcedeleonch commentedon Nov 12, 2026
Sorry for the slow response on this one, @DevamShah, and thanks for the detailed trace of the stdio path and the derived loopback allowlist.
Here's where things stand. #6669 by @lorenzozanee is open against this issue and was reviewed on September 29. The direction from that review lines up with what you proposed: CORS is derived from the existing
--allowed-originsallowlist rather than a second flag, it is enabled explicitly on thethv runandthv proxypaths, and the management API stays validation-only. Inspector will still need--allowed-origins http://localhost:6274, since the loopback default only covers the proxy's own origin. That PR is now waiting on its author for the review changes.To avoid two parallel implementations we're not assigning this right now. If #6669 doesn't move forward, we'll update here and you'd be welcome to pick it up from that direction. @milichev, you can follow #6669 for progress; this issue will close when it merges.
aponcedeleonch commentedon Nov 12, 2026
Quick update, @DevamShah: #6669 has been closed, so this is open again. If you're still up for it, it's yours. I've assigned you. The direction in the September 29 review on #6669 is the one to follow (opt-in CORS on the MCP proxy paths only, modern MCP headers in preflight, reconciling upstream CORS headers, exposing
WWW-Authenticate, and only short-circuiting genuine preflights). Please link your PR here withFixes #4297.