Repository navigation
mcpcompat/client: add real-client integration tests for protocol-status handling (stacklok/toolhive#7034) - #522
Merged
rdimitrov merged 4 commits intoNov 20, 2026
Conversation
…us handling The error-mapping logic (errorbody.go, errors.go, rfc9728.go) was only covered by unit tests that feed seeded contexts into the mappers, plus a happy-path end-to-end test. Nothing drove the real client against a real go-sdk server that answers with a failing HTTP status. Add tests that run the mcpcompat client over real HTTP against a go-sdk Streamable HTTP server. A thin front handler only injects faults for selected requests; initialize and all other calls are served by the SDK. Covered: - JSON-RPC errors on non-2xx responses keep code, message and data, and are not reclassified as auth or session errors - method-not-found is distinguishable from a genuinely terminated session, and the session stays usable after method-not-found - 401 status, WWW-Authenticate and the RFC 9728 resource_metadata URL are retained, including missing, malformed and multiple challenge headers and empty-body responses - a cleanup DELETE answered with a different non-2xx does not overwrite the original 401 - error-body capture respects the 8 KiB limit - the same client recovers after recoverable errors - concurrent clients, deadlines, cancellation and server disconnect Each test client uses its own HTTP transport so parallel tests do not close each other's pooled connections. Tests only; no production code changes. Refs stacklok/toolhive#7034
Review follow-ups on the new protocol-status integration tests: - onMethod disarms with CompareAndSwap, so exactly one request is faulted even with concurrent calls; drop the per-handler arm.Store(false). - Deadline subtest disarms the fault before the recovery call, so a deadline that expires before the request reaches the server can no longer stall the recovery call (latent flake). - Add TestProtocolStatus_ConcurrentCallsOnOneClient: parallel 401/400/ok calls on one client, which is where per-call error capture could leak through the shared RoundTripper. - RecoveryAfterRecoverableErrors now checks each failure's classification, not only "not terminated". Step 8 uses -32010 because go-sdk reserves -32003 (client closing) and reports a server error with that code as "connection closed", dropping the JSON-RPC error. - Share the echo server (newEchoSDKServer) with newTestServer, share an isolatedHTTPClient helper with deleteSession, and parse the echo argument once in the harness (rpcCall.echoMessage). - Expose maxCapturedErrBody through export_test.go instead of copying 8 KiB. - Flag the 404 + JSON-RPC body classification as a known gap in Bare404WithJSONRPCBody rather than as intended behavior. Co-Authored-By: Claude Opus 5.5 <[email protected]>
mapStatusError reported ErrSessionTerminated for every HTTP 404 except
JSON-RPC method-not-found. go-sdk servers on newer protocol versions
also return 404 with a JSON-RPC error for application errors on a live
session, and go-sdk keeps the connection open for those, so ToolHive
would tear down and re-initialize a healthy session.
captureErrorBody now records, for a 404 only, whether the body is a
JSON-RPC error whose non-null ID equals the request's ID (re-read via
req.GetBody). mapStatusError surfaces that case unchanged. Every other
404 still maps to ErrSessionTerminated, including the JSON-RPC-shaped
session-not-found responses of the TypeScript SDK (null ID) and the
Python SDK ("server-error" ID), plain-text bodies, notifications and
bodies truncated at the capture limit.
Tests: table-driven TestCaptureErrorBody_404AnswersRequest, and the
integration test (renamed TestProtocolStatus_404WithJSONRPCBody) now
asserts the live-session case plus the TypeScript and Python variants.
A mutation check confirms both fail without the new branch.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…request" This reverts commit b7530e6. A JSON-RPC error carrying the request's own ID does not prove the session is alive. ToolHive's proxy (pkg/transport/session.WriteNotFound) answers an expired session with HTTP 404 and a -32001 "Session not found" error that echoes the request ID, and vMCP relies on that 404 mapping to ErrSessionTerminated so the client re-initializes. With the reverted change, clients behind ToolHive's proxy would retry a dead session forever. The broad rule (every 404 except JSON-RPC method-not-found is a terminated session) is restored and now pinned by TestProtocolStatus_Bare404WithJSONRPCBody, which covers ToolHive's echoed-ID format, the TypeScript (null ID) and Python (placeholder ID) SDK formats, and an application error with an echoed ID. Co-Authored-By: Claude Opus 5.5 <[email protected]>
rdimitrov
approved these changes
Nov 20, 2026
rdimitrov
left a comment
Member
There was a problem hiding this comment.
LGTM. Thanks for the thorough harness. I pushed review follow-ups (one-shot CAS fault arming, a deadline-case flake fix, same-client concurrency coverage, per-step classification checks in the recovery test, shared helpers). I also pinned the broad 404 -> ErrSessionTerminated rule against ToolHive's proxy format (echoed id), the TypeScript SDK (null id) and the Python SDK (placeholder id), since vMCP's session recovery depends on it.
jhrozek
approved these changes
Nov 20, 2026
Closed
4 tasks
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.
What this does
Adds integration tests that drive the real
mcpcompatclient over real HTTP against a real go-sdk MCP server. They prove the protocol-status handling works end to end: JSON-RPC errors, method-not-found versus session termination, auth challenges, and recovery on the same client.Tests only. No production code, dependency, or API changes.
Refs stacklok/toolhive#7034
Why it was needed
The error handling in
errorbody.go,errors.goandrfc9728.gowas only covered by unit tests. Those feed a pre-seeded context straight into the error mappers. The only end-to-end test used a healthy server. Nothing checked the whole chain when a real server answers with a failing HTTP status:go-sdk decodes (or doesn't decode) the response, then the shim captures the status and
WWW-Authenticate, then the mapper classifies the error.How the tests work
Each test starts a real go-sdk Streamable HTTP server behind a thin handler. That handler only injects a fault (a status code, a JSON-RPC error body, an auth challenge) for chosen requests. Everything else, including the initialize handshake, is served by the real SDK. Nothing is mocked on the client/server path, and no network or API key is needed.
sequenceDiagram participant T as Test participant C as mcpcompat client participant R as headerRoundTripper<br/>(captureErrorBody) participant F as Fault-injecting handler participant S as go-sdk StreamableHTTPHandler + Server T->>C: Initialize / CallTool / ListResources C->>R: HTTP request via go-sdk transport R->>F: forward request alt no fault armed F->>S: pass through S-->>F: real MCP response else fault armed for this request F-->>R: injected status / headers / body end F-->>R: response R-->>R: record status, WWW-Authenticate, body (max 8 KiB) R-->>C: response (body restored) C-->>C: mapCallError / mapConnectError C-->>T: classified error (or result) T->>C: next request on the same clientWhat is covered (maps to the issue's four checkboxes)
DELETEof the session givesErrSessionTerminated, as do the JSON-RPC-shaped "session not found" 404s of ToolHive's proxy, the TypeScript SDK and the Python SDK.WWW-Authenticateand the RFC 9728resource_metadataURL are kept, including empty-body, missing, malformed and multiple-header cases. A cleanupDELETEanswered with a different non-2xx does not overwrite the original 401.Also covered:
export_test.go).Tests are split by topic:
protocol_status_(shared harness),protocol_errors_,protocol_auth_andprotocol_session_integration_test.go.404 classification (deliberately unchanged)
The shim reports
ErrSessionTerminatedfor every HTTP 404 except one carrying JSON-RPC method-not-found (-32601). That rule is broad on purpose, andTestProtocolStatus_Bare404WithJSONRPCBodynow pins it:-32001 "Session not found"with the request'sidechoed (pkg/transport/session.WriteNotFound)ErrSessionTerminated"id": nullErrSessionTerminated"id": "server-error"ErrSessionTerminatedidechoedErrSessionTerminated(go-sdk keeps the connection, so the session still works)A narrower rule was tried during review (keep the session when the JSON-RPC error echoes the request
id) and reverted. ToolHive's proxy echoes theidin its session-not-found 404, and vMCP relies on 404 →ErrSessionTerminated→ re-initialize. With the narrower rule, clients behind ToolHive's proxy would retry a dead session forever. The cost of the broad rule is an occasional unnecessary re-initialize when a server reports an application error with a 404.Things worth knowing
*jsonrpc.Erroris dropped. The tests avoid these codes where they assert on the decoded error. This is go-sdk behavior and may be worth raising upstream.Reliability
httptest.Server.Close()closes the sharedhttp.DefaultTransport's idle connections and breaks other parallel tests. This showed up as a rare flake and is fixed.onMethoddisarms withCompareAndSwap, so concurrent matching requests cannot both be faulted.t.Cleanup.DELETEtest fails if the "first non-2xx response wins" guard is removed.Verification (local)
task(golangci-lint: 0 issues,go vet,go test -race ./...): passtask license-check: passmcpcompat/clientcoverage: 72.1%-race:-count=30 -cpu 1,2,4, plus-count=50 -shuffle=on: all passgovulncheck; no dependencies changed since.