Skip to content

fix(mcp): bound the gateway tool listing - #2931

Merged
jhrozek merged 5 commits into
mainfrom
fix/gateway-listing-bounds
Nov 21, 2026
Merged

jhrozek merged 5 commits into
mainfrom
fix/gateway-listing-bounds

Conversation

@jhrozek

@jhrozek jhrozek commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Summary

Follow-up to #2888, part of #2728. Fixes from reviewing #2888, sent as a separate PR while its author is away. A second PR, stacked on this one, changes the Gateway interface.

  • Bound the gateway tools/list. ListGatewayTools followed nextCursor with no check for a repeated cursor, no cap on pages, tools, or bytes, and no deadline for the whole listing; the embedded gateway's in-process client has no request timeout either. The listing now shares the bounded loop the ordinary MCP catalogue listing uses (walkToolPages, with a CandidateListBudget charged per page), counts response bytes as they are read, and bounds the session's setup and every page with one timeout. The limits are GatewayListLimits on GatewayCalls.ListLimits, which only tests set; a zero or negative field takes the default (10000 tools, 256 pages, 16 MiB, 30 s). The size limits take the values of the catalogue's budget for ordinary MCP servers, but bound each listing of one gateway route, while the catalogue's budget is shared by every server listed together.
  • Never resume a dropped answer. The listing left go-sdk's MaxRetries at its default of 5, so a tools/list answer streamed as SSE that dropped after an event ID was resumed with a GET, whose body the byte budget does not count. With retries left on, the new test's 8 KiB tool got through its 4 KiB budget that way. The listing now sets MaxRetries: -1, as the gateway call path does.
  • Tests for ListGatewayTools itself: every page, each limit (each case asserts how many tools/list requests the gateway received, so it fails for its own reason), the timeout reaching tools/list, the caller cancelling, negative limits taking the defaults, the bearer on every request (initialize, notifications/initialized, each page, the closing DELETE), no redirect reaching its target, missing arguments, and that GatewayCalls.ListLimits reaches the embedded gateway.
  • The comment on gatewayProtocolVersion gives the real reason for the pin; see Protocol below.

Reviewer note: shared code

walkToolPages is the loop listToolsBounded already had, moved into a helper so both listings use it. Its order of checks and budget charges is unchanged, so it now backs every catalogue MCP listing, not only gateway routes; the full internal/adapter/mcp suite runs against it.

Behaviour

A gateway listing over a limit, with a repeated cursor, past its timeout, or whose answer drops, now fails the enrollment, or the refresh of a granted catalogue (RefreshGrantedAuthorizationCatalogue), closed, as any gateway listing failure already did. No production path enables gateway routes yet (IdentityBrokerGatewayServers is test-only), so nothing changes for users.

Not bounded here: a whole catalogue freeze. Each gateway backend gets its own listing limits, so a freeze over N gateway routes can take up to N × 30 s and read up to N × 16 MiB while holding the attachment's lock. That can wait for the external gateway (#2812), the first production path to enable gateway routes.

Tokens

No change in which token is sent (the workspace enrollment access token, until #2729) or how often it is read. The refresh token is never sent. The bearer is set on every request of a listing, and redirects are refused; the new tests pin both.

Protocol

The gateway paths stay pinned to MCP 2025-11-25. On 2026-07-28 the vMCP answers a call that needs URL elicitation with an input_required result instead of error -32042, which classify would report as completed although the call never ran. #2917 tracks moving both gateway paths to 2026-07-28. The repeated-cursor refusal is a local safety rule; cursors are opaque to the protocol, and the page cap bounds the loop either way.

API compatibility

No wire change. Go-only changes inside internal/: mcp.ListGatewayTools takes a GatewayListLimits, and mcpbroker.GatewayCalls gains ListLimits, which production leaves zero. No settings key, so no user-docs or configuration-reference change.

Testing

Run locally:

  • Each commit vetted on its own (go vet ./internal/adapter/mcp/ ./internal/adapter/mcpbroker/ ./internal/app/); go vet ./cmd/mecak8s/; go build ./....
  • go test -race -count=1 ./internal/adapter/mcp/ ./internal/adapter/mcpbroker/.
  • go test -race -count=3 ./internal/adapter/mcp/ -run 'TestListGatewayTools|TestGatewayListLimits'.
  • go test ./internal/app/ -run 'MCPBroker|IdentityBroker|Gateway|AS2|IDJAG|MCPReconcil', go test ./internal/adapter/server/ -run MCPBroker, go test ./cmd/mecak8s/ -run Gateway.
  • golangci-lint run on internal/adapter/mcp/, internal/adapter/mcpbroker/, internal/app/.
  • go run ./cmd/mecademo: tool call, permission ask and approval, result.

Each new guard was checked by removing it and seeing its test fail: the page, tool, and byte limits, negative limits taking the defaults, the timeout, the resume refusal, the redirect refusal, and the ListLimits wiring.

Not run locally: the full task lint, task test:race, and task check:pr gates, left to CI. task test:workspace is not needed: the engine module is unchanged and no dependency pin moved.

🤖 Generated with Claude Code

jhrozek and others added 5 commits November 20, 2026 19:04
ListGatewayTools followed nextCursor for as long as the gateway sent one,
with no check for a repeated cursor, no cap on pages, tools, or bytes, and
no deadline for the whole listing. The embedded gateway's in-process
client has no request timeout either, so a gateway repeating a cursor
kept the catalogue freeze listing, and growing, forever.

Share the bounded loop of listToolsBounded (cursor-cycle refusal and a
CandidateListBudget charge per page) as walkToolPages, and use it for the
gateway listing. Count response bytes as they are read, and bound the
session's setup and every page with one timeout, as the vMCP bounds
listing its own backends. The session's closing DELETE is bounded by the
SDK on its own.

The limits are GatewayListLimits on GatewayCalls.ListLimits, which only
tests set; a zero or negative field takes the default: 10000 tools, 256
pages, 16 MiB, and 30 s. The size limits take the values of the
catalogue's budget, but bound each listing of one gateway route, while the
catalogue's budget is shared by every MCP server listed together.
A listing over a limit fails with ErrGatewayList rather than returning
part of the tools. Each refused case asserts how many tools/list requests
the gateway received, so it fails for its own reason; the timeout case
answers initialize at once and blocks tools/list.

Co-Authored-By: mecatl <[email protected]>
The bounded listing put a byte budget under bearerTransport. Pin what
that stack must keep doing: the bearer is in the Authorization header of
every request (initialize, notifications/initialized, every tools/list
page, and the closing DELETE), a redirect fails the listing without any
request reaching its target, and a listing without an endpoint, client,
or token is refused before any request.

Co-Authored-By: mecatl <[email protected]>
The comment said the pin keeps the initialize handshake and a stateful
session. The load-bearing reason is the call outcome: on 2026-07-28 the
vMCP answers a call needing URL elicitation with an "input_required"
result instead of error -32042, and classify would report that call,
which never ran, as completed. The session-not-found rejection also
assumes a session, and on 2026-07-28 closing the request's stream, not
notifications/cancelled, cancels a call. Say so, say why the listing,
which would work on either revision, uses the same one, and point at
#2917, which tracks moving both paths to 2026-07-28.

Co-Authored-By: mecatl <[email protected]>
The listing left the SDK's MaxRetries at its default of 5. When a
tools/list answer streamed as SSE drops after an event ID, the SDK
resumes it with a GET carrying Last-Event-ID, and the byte budget does
not count GET bodies (it skips them for the ordinary client's standalone
notification stream). So a gateway could send a listing past MaxBytes,
bounded only by the timeout. The test shows it: with retries on, an
8 KiB tool reached a 4 KiB budget through the resumed GET.

Set MaxRetries to -1, as the gateway call path does: a dropped answer
fails the listing, and no GET is sent.

Co-Authored-By: mecatl <[email protected]>
Nothing tested that the limits set on GatewayCalls reach the embedded
gateway's listing; deleting the wiring in newToolHiveProcess passed
every test. List through the real vMCP with a one-tool limit and expect
the listing to fail.

Co-Authored-By: mecatl <[email protected]>
@jhrozek
jhrozek merged commit e238528 into main Nov 21, 2026
43 checks passed
@jhrozek
jhrozek deleted the fix/gateway-listing-bounds branch November 21, 2026 08:04
@ChrisJBurns

Copy link
Copy Markdown
Contributor

Heads-up on ticket references: #2728 was closed on 2026-11-20 after #2887 and #2888 merged, so this PR is best described as a follow-up to #2888 rather than part of #2728. The bound on the listing wasn't tracked in any ticket, so no other reference is needed. The open point about bounding a whole catalogue freeze across N gateway routes fits #2812's scope, since it is the first production path that enables gateway routes.

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.

2 participants