Skip to content

fix(upstream): hold a GET stream for streamable-HTTP upstreams that advertise tools.listChanged (#1910) - #1915

Merged
Dumbris merged 8 commits into
mainfrom
fix/1910-http-list-changed
Nov 16, 2026
Merged

Dumbris merged 8 commits into
mainfrom
fix/1910-http-list-changed

Conversation

@Dumbris

@Dumbris Dumbris commented Nov 16, 2026

Copy link
Copy Markdown
Member

Closes #1910

Problem

A streamable-HTTP upstream delivers server-initiated messages, notifications/tools/list_changed among them, only on a standalone GET stream that the client keeps open. mcpproxy never opened one, so tool changes on HTTP upstreams waited for the discovery poll.

Fix

mcpproxy now holds one GET stream per connection, and only for an upstream whose initialize result advertises capabilities.tools.listChanged. Other upstreams get no GET and no extra held connection, which matters for scale-to-zero hosts.

Why not mcp-go's WithContinuousListening:

  • It is fixed before initialize, so it cannot depend on the advertised capability.
  • It runs on the connect context and the 180s-timeout client.
  • It retries every second forever on 400/406/401/5xx, logging at Error each time.
  • It stops for good on a 404, so a session re-init would lose the stream.

The listener is internal/transport/listen_stream.go; its lifecycle is in internal/upstream/core/list_changed_stream.go.

Responses that end the stream after one request: 405, 400, 406, 401, 403, other 4xx, a cross-origin redirect, a non-event-stream 2xx, or OAuth authorization-required. mcpproxy logs one INFO line and polling covers the server from then on.

Session lost: a 404 stops the stream; the next re-init or reconnect opens a new one.

Transient failures: network errors, 408, 429, 5xx, and streams that end within 30s back off from 1s to 2m with ±25% jitter, honouring Retry-After.

Client: the stream has its own http.Client, with no total timeout and a 30s response-header timeout. It never follows a redirect to another origin, so static headers are not leaked. Event size is bounded at 1 MiB.

Lifecycle:

  • The stream's context comes from the instance gate, so server removal ends it.
  • Disconnect and reconnect stop it.
  • Session re-init waits for the old listener, then starts one on the new session. There are never two listeners, and a re-init that finishes after Disconnect starts nothing.

Dispatch:

  • Notifications go into a non-blocking queue that coalesces per method and holds only tools/prompts list_changed. A separate goroutine drains it, so Disconnect never waits on a callback blocked on the managed lock.
  • A ping on the stream is answered on the stream's own session; any other request gets method-not-found.

Test fixtures: an in-process fixture with a held GET makes httptest.Server.Close and http.Server.Shutdown wait. Fixtures now cut client connections first, through a per-package closeHTTPFixture helper. Production code has no test switch.

Docs: docs/api/mcp-protocol.md (Tool Change Notifications) and docs/features/search-discovery.md.

Tests

  • internal/transport/listen_stream*_test.go:
    • notification delivery with session and static headers
    • every refusal stops after exactly one GET
    • transient errors back off without a hot loop
    • ping answered on the stream's own session
    • the GET client has no total timeout
    • OAuth bearer
    • an oversized event is dropped
    • a cross-origin redirect is refused
    • a stalled refusal body does not hold the listener
  • internal/upstream/core/list_changed_stream_test.go:
    • a real mcp-go server push reaches the handler
    • no capability, no GET
    • refused GET is not retried
    • stop on Disconnect, then reconnect
    • stop on retire
    • re-init moves the stream to the new session
    • no restart after Disconnect
    • Disconnect does not wait on a blocked dispatch
    • a late re-init failure keeps the newer stream
    • queue coalescing and filtering
  • Passed:
    • go test -race on ./internal/transport/, ./internal/upstream/... and ./internal/runtime/...
    • -tags server race over internal/server, serveredition and httpapi with the CI skip list
    • ./scripts/test-api-e2e.sh
    • Go E2E/MCP set
    • golangci-lint v2 bare and with --build-tags server
  • Cross-review with opencode gpt-6.1-sol, in three chunks:
    • Listener: 4 findings (event bound, cross-origin redirect, drain block, session pinning), all fixed; clean on re-review.
    • Lifecycle: 1 finding (ignored methods could fill the queue), fixed; clean on re-review.
    • Fixtures and docs: 1 finding (the scope-latency workflow copies only its own test file), fixed.

…dvertise tools.listChanged (#1910)

A streamable-HTTP upstream can send notifications/tools/list_changed outside
a request only on a standalone GET stream, and mcpproxy never opened one, so
tool changes on HTTP upstreams waited for the 5-minute discovery poll.

mcpproxy now runs its own listener (internal/transport/listen_stream.go)
instead of mcp-go's WithContinuousListening, which is fixed before
initialize (so it cannot depend on the advertised capability), runs on the
connect attempt's context and the basic client's 180s timeout, retries every
second forever on anything but 405/404 with Error logs, and stops for good
after a 404.

- Opened only when initialize advertises tools.listChanged, on the plain and
  OAuth paths (session id, protocol version, static headers and the
  transport's own OAuth bearer).
- 405/400/406/401/403/other 4xx or a non-event-stream answer: one request,
  one INFO line, then the discovery poll covers it. 404: stops until a
  session re-initialize or reconnect. Network errors, 408, 429, 5xx and
  streams that end at once back off exponentially (1s to 2m, jittered).
- Own http.Client with no total timeout; POST clients are unchanged.
- Lifecycle in core (list_changed_stream.go): context from the instance
  gate (retirement ends it), stopped by Disconnect and the reconnect path,
  moved to the new session by ReinitializeSession after the old listener
  exits, and never restarted for a connection Disconnect released.
- A ping request on the stream is answered; other requests get
  method-not-found.

Test fixtures that closed an in-process upstream before disconnecting the
client (httptest.Server.Close and http.Server.Shutdown wait for the held
GET) now cut client connections first or use Close.

Docs: docs/api/mcp-protocol.md, docs/features/search-discovery.md.
…dispatch; a late re-init failure ends only its own stream (#1910)

The listener queued nothing and ran the tools/prompts-changed handlers on
its own goroutine. managed.Client holds mc.mu across core Disconnect and
those callbacks take mc.mu.RLock, so Disconnect (stopListChangedStream
with wait) could stall for the full 5s stop timeout while holding mc.mu.
Notifications now go to a non-blocking, per-method coalescing queue drained
by a separate dispatch goroutine; a stop waits only for the reader.

endListChangedStream now takes the re-initialized client and does nothing
unless the stream still belongs to it, matching restartListChangedStream,
so a late re-init failure on an old connection cannot end the stream of
the connection a reconnect put in its place.
…redirects, never blocks draining a refusal, answers on its own session (#1910)

- An SSE event that grows past 1 MiB across many data: lines is dropped
  whole instead of accumulating without bound.
- A cross-origin redirect ends the stream: static headers such as an API
  key are not sent to the other origin (Go strips only Authorization and
  Cookie). Polling covers the server.
- The response body is closed without draining, so a refusal whose body
  never finishes cannot hold the listener.
- A request received on the stream is answered with the session id that
  opened the stream, not the transport's current one after a re-init.

Raised by cross-review.
…notifications (#1910)

Notifications the dispatcher ignores (logging, progress, resources) are no
longer queued, so they cannot fill the bounded queue while a callback is
blocked and drop a later tools/list_changed. Raised by cross-review.
…T stream redirect and jitter (#1910)

The scope-latency workflow copies only scope_latency_test.go onto the
merge-base checkout, so it cannot call the new closeHTTPFixture helper.
Raised by cross-review.
@Dumbris
Dumbris enabled auto-merge (squash) November 16, 2026 08:52
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Deploying mcpproxy-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 8fac375
Status: ✅  Deploy successful!
Preview URL: https://3a02112c.mcpproxy-docs.pages.dev
Branch Preview URL: https://fix-1910-http-list-changed.mcpproxy-docs.pages.dev

View logs

…ort; refused-GET test counts per session (#1910)

The listener's transport was cloned from http.DefaultTransport before
baseTransport put it behind the instance's dial gate. It is now an explicit
*http.Transport, so the Spec 116 FR-024a write-site check holds by
construction. The refused-GET test asserts one GET per session, so a second
connect on a slow CI host (a new session) is not mistaken for a retry.
The GET stream opens asynchronously after connect, so a test that closes
its fixture right after connecting could see the GET arrive after a single
CloseClientConnections; httptest.Server.Close then waited on it forever
(CI: TestClient_GetPrompt_UpstreamError_ReturnsWrappedError hit the 20m
timeout). Close now runs in the background, which closes the listener
first, while open connections are cut until it returns.
…1910)

http.Server.Shutdown waits for every connection to go idle, and the
runtime's standalone GET stream to the upstream never does, so the cleanup
hung (Windows CI: TestAgentTokenRateLimit_EndToEnd hit the 20m timeout).
@github-actions

Copy link
Copy Markdown
Contributor

📦 Build Artifacts

Workflow Run: View Run
Branch: fix/1910-http-list-changed

Available Artifacts

  • archive-darwin-amd64 (32 MB)
  • archive-darwin-arm64 (29 MB)
  • archive-linux-amd64 (20 MB)
  • archive-linux-arm64 (18 MB)
  • archive-windows-amd64 (32 MB)
  • archive-windows-arm64 (29 MB)
  • frontend-dist-pr (0 MB)
  • installer-dmg-darwin-amd64 (28 MB)
  • installer-dmg-darwin-arm64 (26 MB)
  • smart-mcp-proxymcpproxy-goOBK1OD.dockerbuild (0 MB)

How to Download

Option 1: GitHub Web UI (easiest)

  1. Go to the workflow run page linked above
  2. Scroll to the bottom "Artifacts" section
  3. Click on the artifact you want to download

Option 2: GitHub CLI

gh run download 43851996951 --repo smart-mcp-proxy/mcpproxy-go

Note: Artifacts expire in 14 days.

@codecov-commenter

codecov-commenter commented Nov 16, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 77.11172% with 84 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
internal/transport/listen_stream.go 69.72% 44 Missing and 22 partials ⚠️
internal/upstream/core/list_changed_stream.go 90.37% 10 Missing and 3 partials ⚠️
internal/upstream/core/connection_lifecycle.go 66.66% 3 Missing ⚠️
internal/upstream/core/connection.go 0.00% 1 Missing ⚠️
internal/upstream/core/session_reinit.go 66.66% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@Dumbris
Dumbris merged commit b3cdd22 into main Nov 16, 2026
54 of 55 checks passed
Dumbris added a commit that referenced this pull request Nov 18, 2026
…ver the real push

TestToolsListChanged_TrustedServer_DoesNotJoinPreChangeList failed on
Ubuntu (job 135193801169): expected "changed", got "approved", in 0.27s.

Its stage server advertised tools.listChanged. Since #1915 (GH #1910)
that gives the client a standing GET stream, so the test's own
DeleteTools/AddTool also pushed a real list_changed: two
notification-driven runs, not one. When the real push took the
per-server discovery marker first, the test's manual
onUpstreamToolsListChanged was coalesced and returned at once, and the
record was read before the owning run had stored "changed". A 300ms
head start for the real push reproduces it 3/3. The owning run was
fresh and did store "changed", so production behaviour is correct.

The test's server no longer advertises the capability, so the manual
call is the only notification. The test now:
- marks its own run through a context value and requires that run to
  have happened, instead of counting every run;
- arms only after connect-time discovery has run and released the
  marker, since that discovery could otherwise absorb the
  notification.
With the head start the test passes. Reverting the fix fails it on the
own-run check, and reintroducing the joining bug (a fresh list
adopting the in-flight one) fails it on the status.

TestToolsListChanged_AdvertisedPush_HoldsChangedContract covers the
transport path the first test now avoids. A trusted manual-mode
upstream advertises the capability, changes its approved tool and
pushes the notification itself. The test waits for discovery to
settle, then requires "changed", the old ApprovedHash kept and the
previous description recorded. It fails without the capability and
with trust_mode auto.

Test-only. Both tests pass 50/50 under -race, 20/20 with GOMAXPROCS=1,
and 10/10 with -tags server -shuffle=on.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Streamable HTTP upstreams never deliver notifications/tools/list_changed (no GET stream), so tool changes wait for the discovery poll

2 participants