Return JSON-RPC error for filtered tool calls - #5944
Merged
Conversation
When the tool-call filtering middleware (thv run --tools-filter) blocked a tools/call for a filtered tool, it returned a bare HTTP 400 with no body. Clients expect a protocol-level JSON-RPC error, and a 400 on a validly received JSON-RPC request is off-spec: under MCP streamable HTTP an application-level failure rides back in an HTTP 200 carrying a JSON-RPC error object. Return a JSON-RPC error (code -32602, message "tool not found") over HTTP 200 when the client's Accept header allows JSON. The generic message deliberately matches what a client sees for a tool that does not exist, so a filtered tool is not distinguishable from a nonexistent one (the filter's long-standing documented intent). The MCP spec's own error-handling example uses -32602 for unknown tools, and go-sdk returns the same, so a filtered tool now looks identical to the real unknown-tool response. A client that accepts only text/event-stream (the legacy HTTP+SSE transport, whose real response is delivered on a separate stream this middleware does not control) keeps the HTTP 400 fallback; the malformed- request case likewise stays 400. Also drop the dead, now-inaccurate expectedStatus column from the middleware scenario table (the new httptest-based tests assert status directly). Closes item 2 of #5764. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
JAORMX
requested review from
ChrisJBurns,
amirejaz,
blkt,
jhrozek and
rdimitrov
as code owners
July 23, 2026 14:47
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #5944 +/- ##
=======================================
Coverage 71.76% 71.77%
=======================================
Files 705 705
Lines 72159 72190 +31
=======================================
+ Hits 51786 51811 +25
- Misses 16675 16676 +1
- Partials 3698 3703 +5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
ChrisJBurns
approved these changes
Jul 23, 2026
5 tasks
This was referenced Jul 24, 2026
JAORMX
added a commit
that referenced
this pull request
Jul 24, 2026
TestStandaloneSSE_ListChangedRefiltersThroughExistingMiddleware asserted that a tools/call for a filtered tool returns HTTP 400. That is no longer true: #5944 changed the tool-call filter to return a JSON-RPC error over HTTP 200 (a filtered tool is now indistinguishable from a nonexistent one). This test was added by #5934 and the two PRs landed close together, so each was green in isolation but the combination left the Tests check red on main (a semantic merge collision) — deterministically failing every subsequent PR's required unit-test job. Update step (3) to assert the current behavior: HTTP 200 with a JSON-RPC error body ("tool not found"). The tool is still blocked (never forwarded); only the response envelope changed. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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
Why: When
thv run --tools-filterblocked atools/callfor a filtered tool, the middleware returned a bare HTTP 400 with no body. Clients expect a protocol-level JSON-RPC error, and a 400 on a validly received JSON-RPC request is off-spec — under MCP streamable HTTP an application-level failure rides back in an HTTP 200 carrying a JSON-RPC error object (the spec's "Error Handling" section lists "Unknown tools" as a protocol error with a canonical-32602example).What:
tools/callnow returns a JSON-RPC error{"code":-32602,"message":"tool not found"}over HTTP 200 when the client'sAcceptheader allows JSON (application/json,application/*,*/*, or absent), echoing the requestid.NewToolCallMappingMiddleware).-32602matches what the MCP spec and the go-sdk return for an unknown tool, so the two responses look identical.text/event-stream(the legacy HTTP+SSE transport, whose real response is delivered on a separate stream this middleware can't inject into) keeps the HTTP 400 fallback — preserving the documented session-management limitation.toolCallBogus) intentionally stays HTTP 400, per this item's "keep 400 only for malformed" guidance.clientAcceptsJSONandwriteFilteredToolCallError, modeled on the existingwriteRateLimited/classificationErrorBodypatterns.expectedStatuscolumn from the middleware scenario table (it was never asserted, and its filtered-case values were falsified by this change); the new httptest-based tests assert status directly.Closes item 2 of #5764. Item 3 (version-agnostic proxying docs + strict mode) remains, so this does not close the issue.
Type of change
Test plan
go test -race ./pkg/mcp/...passes. New/updated coverage:TestNewToolCallMappingMiddleware_FilteredTool(httptest): filtered call withAccept: application/json(numeric, string, andnullids), a multi-value Accept, and no Accept header → HTTP 200 +application/json+ JSON-RPC-32602"tool not found",idechoed, message does not leak "filter", andnextnever called;Accept: text/event-streamonly → HTTP 400.TestClientAcceptsJSON:application/json, multi-value,*/*,application/*, empty,;q=param, uppercase (case-insensitive), and theapplication/json-patch+jsonfalse-positive guard._AllowedToolPassesThrough(passthrough) and_BogusRequest(still 400 even withAccept: application/json).task lintreports 0 issues;task buildsucceeds.Does this introduce a user-facing change?
Yes. A blocked
tools/callthroughthv run --tools-filternow returns a JSON-RPC-32602"tool not found" error at HTTP 200 (for JSON-capable clients) instead of a bare HTTP 400, matching how a real MCP server reports an unknown tool.Special notes for reviewers
-32602is the prescribed shape and that-32602(not-32601orisError:true) is correct for an unknown tool. Security: fail-closed confirmed (the call is never forwarded in either branch), the echoedidis safelyjson.Marshal-ed (no reflection/injection), and the generic message meets the no-oracle goal.toolCallBogus(malformed request) path stays 400 per this item's wording; the spec technically buckets malformed requests as protocol errors too, so upgrading it to-32602is a reasonable future follow-up.Generated with Claude Code