Skip to content

ToolHive MCP proxy endpoint does not support CORS #4297

Open

Description

@milichev

Description

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

  1. Add an MCP to thv.
  2. Configure a client, e.g. OpenCode.
  3. Get the proxy MCP URL from the client config, e.g. http://localhost:43969/mcp and paste it in MCP inspector.
  4. Attempt to connect.
  5. 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.

Actual Behavior

  • OPTIONS request: 405 Method Not Allowed.
  • Console error: net::ERR_FAILED (missing Access-Control-Allow-Origin).

Environment

  • OS: macOS M4.
  • ToolHive: Version 0.24.0 (0.24.0)

Activity

  1. added
    bug
    on Mar 20, 2026
  2. ChrisJBurns commented on Jul 30, 2026

    @ChrisJBurns
    Collaborator

    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.

  3. added
    good-tenth-issue
    on Jul 30, 2026
  4. DevamShah commented on Aug 28, 2026

    @DevamShah

    @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-http containers

    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:

    1. Stdio thv run — the middleware is unreachable by construction. Different proxy package entirely.
    2. 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:

    ResolveAllowedOrigins(127.0.0.1, 43969, nil) = [http://localhost:43969 http://127.0.0.1:43969 http://[::1]:43969]
    origin middleware verdict for Origin: http://localhost:6274 -> 403
    

    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.

  5. aponcedeleonch commented on Nov 12, 2026

    @aponcedeleonch
    Member

    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.

  6. aponcedeleonch commented on Nov 12, 2026

    @aponcedeleonch
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

buggood-tenth-issueproxy

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions