toolhive icon indicating copy to clipboard operation
toolhive copied to clipboard

vMCP streamable-http returns 400 on valid client responses to server-initiated requests

Open jhrozek opened this issue 7 months ago • 5 comments

Summary

The vMCP streamable-http transport returns 400 Bad Request — Invalid or malformed MCP request when an MCP client POSTs a JSON-RPC response to a server-initiated request. Per the MCP streamable HTTP spec, the server MUST return 202 Accepted for accepted responses/notifications, not 400.

This breaks clients that respond to server-initiated pings — VS Code's MCP extension, for example, tears down the session on the 400 and the log shows will retry with new session ID.

Reproduction

  1. Connect any MCP client that sends a ping on a server-initiated request (e.g. VS Code 1.116.x with a vMCP-backed MCP server).

  2. After a successful initialize + tools/list, when the server emits a ping request on the SSE backchannel, the client POSTs its response back:

    POST /mcp HTTP/1.1
    Mcp-Session-Id: <session-id>
    Content-Type: application/json
    
    {"jsonrpc":"2.0","id":1,"result":{}}
    
  3. vMCP responds:

    HTTP/1.1 400 Bad Request
    Content-Type: text/plain; charset=utf-8
    
    Invalid or malformed MCP request
    
  4. Client treats this as a fatal transport error and tears down the session.

Expected

Per MCP spec (streamable HTTP, "Sending Messages to the Server", item 4):

If the input is a JSON-RPC response or notification: If the server accepts the input, the server MUST return HTTP status code 202 Accepted with no body. If the server cannot accept the input, it MUST return an HTTP error status code (e.g., 400 Bad Request).

So the server should:

  1. Look up the response by id against the pending server-initiated request table for that session.
  2. If it matches, complete the internal bookkeeping and return 202.
  3. Only return 400 if no matching pending request exists.

Root cause

We do emit server-initiated pings — see pkg/transport/proxy/httpsse/pinger.go:47 and pkg/transport/proxy/transparent/pinger.go:122. But the POST handler in the streamable transport doesn't correlate inbound responses against pending server requests; any response-shaped body is rejected.

Impact

  • Any MCP client that responds to server-initiated requests (pings, future elicitation/create, sampling/createMessage) will have its sessions torn down.
  • VS Code specifically logs this as Connection state: Error 400 status ... will retry with new session ID.
  • End users see the connection flap every ~30s (our ping interval) and no tool calls work reliably.

Where to fix

The streamable-http POST handler needs a response-vs-request branch. Relevant directories:

  • pkg/transport/proxy/streamable/ — the transport.
  • pkg/mcp/parser.go — JSON-RPC parsing.
  • Wherever pending server-initiated requests are tracked per session (may not yet exist — likely needs to be introduced).

jhrozek avatar Apr 22 '26 14:04 jhrozek

Hitting this on Cursor as well (3.4.16 / 3.4.17, darwin arm64), against a vMCP fronted by the embedded auth server with Okta upstream, exposed via a Cloudflare tunnel.

Direct Cursor → tunnel URL: works for ~30s (until the first server-initiated ping), then Cursor surfaces:

[error] Client error: Streamable HTTP error: Error POSTing to endpoint: Invalid or malformed MCP request
[error] Client error: Failed to send response: ...
[warning] [V2 FSM] connection:transport_error: conn=connected,auth=unknown -> conn=failed

Via thv run local proxy: same root cause, but harder to notice. thv run is the "real" MCP client as far as the vMCP and Okta are concerned; it presents a localhost URL to Cursor. The user-visible flap takes a few minutes to surface because the proxy mediates the stdio↔HTTP boundary and presumably swallows or retries some of the 400s.

The vMCP audit log captures the rejected POST verbatim:

{"type":"http_request","outcome":"failure","target":{"endpoint":"/mcp","method":"POST"},
 "data":{"request":{"id":1,"jsonrpc":"2.0","result":{}},
         "response":"Invalid or malformed MCP request\n"}}

Rejected bodies repeat on the ~12s ping cadence, one per pending server-initiated request the client had in flight when its session died.

Adding context in case it helps prioritize. Fixing this would also unbreak the "demoable" path for the Okta + Cloudflare flavor of vMCP.

danbarr avatar May 14 '26 17:05 danbarr

I'd like to work on this.

jstar0 avatar Jun 08 '26 15:06 jstar0

Thanks @jstar0, I've assigned the issue to you.

eleftherias avatar Jun 29 '26 06:06 eleftherias

Rechecked on upstream main at fc57a223 using the repository Taskfile with the vMCP response-post test selected. The real Serve path sends a server-initiated elicitation, accepts the client's response-only POST with 202 Accepted and an empty body, and completes the pending tool call with the correlated response.

The behavior is present on current main. Open PR #5885 is test-only coverage for the vMCP path, the Streamable HTTP proxy used by thv run, and the transparent proxy used by thv proxy. It is rebased on main at fc57a223, with exact head 4c3e271e373a83b19563b9782b5fa37de2989e9e. The focused Taskfile test passed locally, and all 42 hosted checks now pass on that head. No runtime code changes are included.

Could a maintainer confirm whether #5009 should close based on current main behavior and #5885 as regression coverage, or whether another failing case should remain in scope?

jstar0 avatar Oct 04 '26 15:10 jstar0

As @jstar0 verified, current main accepts a client's response-only POST to a server-initiated request with 202 Accepted and correlates it to the pending request, and #5885 added regression tests for the vMCP, thv run streamable and thv proxy transparent paths. If you still see 400s on a recent release (e.g. from VS Code or Cursor pings), the client version and audit log line would help. Thanks @jhrozek, @danbarr and @jstar0!

If you think we've got this wrong and it isn't actually fixed for you, please reply here and tell us why. We're happy to take another look and reopen it.

ChrisJBurns avatar Nov 30 '26 15:11 ChrisJBurns