Repository navigation
Conversation
Browser-based MCP clients such as the MCP Inspector cannot reach the ToolHive proxy: the streamable-HTTP /mcp endpoint answers an OPTIONS preflight with 405 and sets no Access-Control-Allow-Origin, and even a handled preflight would be rejected by the pre-auth Origin guard before any CORS layer ran. Add a CORS middleware derived from the existing --allowed-origins allowlist (no new flag). It is wired on the `thv run` and `thv proxy` paths just outside Origin validation, so a genuine preflight (OPTIONS carrying Access-Control-Request-Method) from an allowed origin is answered with 204 and the modern MCP headers (Mcp-Session-Id, Mcp-Protocol-Version, Last-Event-ID) before the Origin guard sees it. Because CORS reads the same list as the Origin validator, it can never grant an origin the validator would reject. Every other request passes through unchanged; a preflight from a disallowed origin still reaches the Origin guard and gets 403. Actual responses are decorated with Access-Control-Allow-Origin and expose WWW-Authenticate, set only when an upstream handler or reverse-proxied backend has not already set them, so an upstream CORS policy is preserved rather than double-set. Fixes stacklok#4297 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This branch has not been deployed
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 (e.g. the MCP Inspector) cannot connect to the ToolHive proxy. The streamable-HTTP
/mcpendpoint answers anOPTIONSpreflight with405 Method Not Allowedand sets noAccess-Control-Allow-Origin, and even a handled preflight would be rejected by the pre-auth Origin guard (403) before any CORS layer ran — so the connection fails in the browser.This adds opt-in CORS derived from the existing
--allowed-originsallowlist (no new flag), wired on thethv runandthv proxypaths only.pkg/transport/middleware/corspackage. CORS is placed just outside Origin validation so a genuine preflight (OPTIONScarryingAccess-Control-Request-Method) from an allowed origin is answered with204and the modern MCP headers before the Origin guard sees it.Mcp-Session-Id,Mcp-Protocol-VersionandLast-Event-IDin the preflight allow-headers, the standard allow-methods/credentials/max-age, and exposesWWW-AuthenticateviaAccess-Control-Expose-Headersso a browser client can read the OAuth challenge.WriteHeadertime and only whenAccess-Control-Allow-Originis still absent, so an upstream policy is preserved rather than double-set.OPTIONS, preflight from a disallowed origin, all actual requests) passes through. A preflight from a disallowed origin still reaches the Origin guard and gets403.origin.CanonicalizeOrigin), it can never grant an origin the validator would reject — CORS is a strict subset.Fixes #4297
Type of change
Test plan
task test) — newcorspackage tests cover preflight short-circuit, pass-through of non-preflight/disallowed/bare-OPTIONS, actual-response decoration, upstream-CORS reconciliation,http.Flusherpassthrough for SSE,Varyde-duplication, case-insensitive origin matching, and the factory. Ran with-race.origin,runner, andcmd/thv/appsuites still pass.task lint-fix) —golangci-lint v2.15.0(the CI-pinned version) with the repo config: 0 issues.task test-e2e) — not run locally.API Compatibility
v1beta1API.Does this introduce a user-facing change?
Yes. On the
thv runandthv proxypaths, when the effective Origin allowlist is non-empty (an explicit--allowed-origins, or a loopback-bind default):OPTIONS /mcpwithAccess-Control-Request-Method) returned405with no CORS headers; browser clients were blocked.204withAccess-Control-Allow-*headers (including the modern MCP headers) andAccess-Control-Expose-Headers: … WWW-Authenticate; actual responses from an allowed origin carryAccess-Control-Allow-Origin. Disallowed origins are unaffected (still403at the Origin guard). No behavior change when the allowlist is empty.The MCP Inspector on
:6274reaching a loopback proxy still requires naming the browser origin, since the loopback default only covers the proxy's own origin:Documented in
docs/middleware.md(new component section 14 + the execution-order note).Special notes for reviewers
thv runwires CORS viaprependCorsMiddlewareinpkg/runner(called right afterprependOriginMiddlewareinrunner.Run);thv proxyvia the newappendOriginMiddlewareshelper incmd/thv/app/proxy.go. Final outer-to-inner order is body-limit → CORS → Origin → rest. The CORS middleware reaches stdio (streamable proxy), SSE/streamable-HTTP (transparent proxy) andthv proxyfrom these two seams, matching where Origin validation is already applied.http.Flusherdirectly (not viaUnwrap) because the streamable SSE path type-asserts the writer tohttp.Flusher; a test pins this.vmcpserver's/mcpis intentionally left out of scope, matching the issue discussion.Checklist for @DevamShah before marking ready
thv run --allowed-origins http://localhost:6274 <server>, point the MCP Inspector (:6274) at the proxy/mcpURL, confirm the preflight returns204and the connection succeeds.405), plus an SSE/streamable-HTTP container and a standalonethv proxy.Access-Control-Allow-Credentials: trueis desired, or whether it should be gated/omitted for this endpoint.task test-e2eandtask lint-fixin the real toolhive dev environment.🤖 Generated with Claude Code