Skip to content

MCPToolConfig toolsFilter yields empty tools/list responses (200 with 0-byte body; superfluous WriteHeader from bodylimit middleware) #5797

Description

@alex-feel

Bug description

With an MCPToolConfig toolsFilter attached to an MCPServer (via spec.toolConfigRef), every tools/list through the proxy returns HTTP 200 with an EMPTY body (0 bytes), while tools/call passes through fine. The proxy logs exactly one line per tools/list request:

http: superfluous response.WriteHeader call from github.com/stacklok/toolhive/pkg/bodylimit.(*bodySizeResponseWriter).WriteHeader (middleware.go:170)

The filter is not filtering everything out: in the reproduction it lists exactly the backend's full tool set, and the same request without the filter returns all of those tools.

Steps to reproduce

  1. Operator v0.34.0, an MCPServer with transport: streamable-http whose backend replies to tools/list with Content-Type: application/json (reproduced with googleapis/mcp-toolbox 1.6.0, --prebuilt clickhouse --address 0.0.0.0 --port 5000, its three tools execute_sql, list_databases, list_tables).
  2. Attach a filter listing the full tool set:
apiVersion: toolhive.stacklok.dev/v1beta1
kind: MCPToolConfig
metadata:
  name: toolbox-tools
spec:
  toolsFilter:
    - execute_sql
    - list_databases
    - list_tables
  1. Through the proxy: initialize (protocolVersion: 2025-03-26, so mcp-toolbox issues an Mcp-Session-Id) → 200 with session id; notifications/initialized → 202; tools/list with the session id → HTTP 200, Content-Type: application/json, Transfer-Encoding: chunked, empty body, plus the superfluous WriteHeader log line above. tools/call with the same session works and returns real results.
  2. Remove spec.toolConfigRef from the MCPServer (nothing else changes): the same tools/list returns 794 bytes with all three tools.

Cedar authorization (authzConfig) stayed active in both runs, so the authz middleware's own tools/list response filtering is not the trigger — the empty body appears exactly and only when the MCPToolConfig filter is attached.

Expected behavior

tools/list returns the (filtered) tool list.

Actual behavior

tools/list returns HTTP 200 with a 0-byte body; clients see no tools, so tool discovery is broken while tool calls still work.

Environment (if relevant)

  • OS/version: Kubernetes v1.26.6 (operator mode)
  • ToolHive version: v0.34.0; pkg/mcp/tool_filter.go is byte-identical between v0.34.0 and v0.36.0, so this does not look fixed as of v0.36.0

Additional context

toolFilterWriter.WriteHeader already deletes Content-Length (pkg/mcp/tool_filter.go:325-328), so this is not the known Content-Length-mismatch failure mode. The superfluous WriteHeader source points at an ordering interaction between the buffering filter writer and pkg/bodylimit's bodySizeResponseWriter (its Write promotes an implicit WriteHeader(200)), suggesting headers are committed on the underlying writer before the filter's buffered body is flushed, after which the processed body never reaches the client. Happy to provide more logs or run instrumented builds.

Activity

  1. added
    needs-triage
    on Jul 14, 2026
  2. added
    bug
    and removed
    needs-triage
    on Jul 14, 2026
  3. aponcedeleonch commented on Jul 14, 2026

    @aponcedeleonch
    Member

    Thanks for the unusually thorough report. The reproduction steps, the code pointers, and the analysis all made this fast to confirm.

    Reproduced on current main. I wired the real middleware stack (bodylimit → tool filter → mcp parser → authz) behind a real reverse proxy with the production FlushInterval: -1, pointed at a backend that returns tools/list as application/json with chunked encoding. Result was a byte-for-byte match: HTTP 200, Transfer-Encoding: chunked, 0-byte body, plus the exact superfluous response.WriteHeader ... bodylimit (middleware.go:170) line. Toggling the middlewares confirmed your observation that it only breaks when both the authz filter and the MCPToolConfig tool filter are in the chain; either one alone works.

    Your read was almost exactly right. One refinement: the empty body isn't caused by the superfluous WriteHeader itself (that line shows up in the authz-only case too, where the body is still delivered fine). The actual root cause is two stacked buffering response writers. The tool filter middleware appends before authz, so toolFilterWriter sits underneath the authz writer. toolFilterWriter only emits its buffered body in Flush(), and the middleware never flushes its own writer after next.ServeHTTP returns, it relies on a downstream flush arriving after the body write. When authz is also present, authz buffers the whole response and only calls WriteHeader/Write in its post-proxy FlushAndFilter. By then the earlier (empty) flush has already committed the response headers on the socket, and authz's late write lands back in toolFilterWriter's buffer with nothing left to flush it. So the filtered body is stranded and the client gets a committed 200 with 0 bytes. Your point about Content-Length stripping being unrelated is correct.

    The narrow fix is to have the list-tools middleware flush its own toolFilterWriter after next.ServeHTTP returns, mirroring how authz explicitly drives its own flush, so a late write from an inner buffering writer isn't left stranded. We'll also want to look at the general pattern of stacked buffering writers so this class of "commit headers early, write body late" ordering can't drop bodies again.

    If you'd like to pick up the fix yourself, we'd happily review a PR, but if that's not something you have time for, we'll get to it as soon as we can. Marking this a confirmed bug. Thanks again, offer of instrumented builds appreciated but you've given us more than enough to work with.

  4. added a commit that references this issue on Jul 15, 2026
    0324eab
  5. aponcedeleonch commented on Jul 15, 2026

    @aponcedeleonch
    Member

    hey @alex-feel , the fix landed on #5809 . Hope it solves the issue. Feel free to re-open in this one or create a new one if you still experience some weirdness

  6. alex-feel commented on Jul 15, 2026

    @alex-feel
    ContributorAuthor

    Thank you @aponcedeleonch! I'll check it.

  7. added a commit that references this issue on Jul 15, 2026
    1ec7352
  8. added a commit that references this issue on Jul 15, 2026
    cf29720
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugproxy

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions