Repository navigation
fix(mcp): bound the gateway tool listing - #2931
Merged
Merged
Conversation
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]>
ChrisJBurns
approved these changes
Nov 20, 2026
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. |
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
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
Gatewayinterface.tools/list.ListGatewayToolsfollowednextCursorwith 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 aCandidateListBudgetcharged per page), counts response bytes as they are read, and bounds the session's setup and every page with one timeout. The limits areGatewayListLimitsonGatewayCalls.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.MaxRetriesat its default of 5, so atools/listanswer streamed as SSE that dropped after an event ID was resumed with aGET, 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 setsMaxRetries: -1, as the gateway call path does.ListGatewayToolsitself: every page, each limit (each case asserts how manytools/listrequests the gateway received, so it fails for its own reason), the timeout reachingtools/list, the caller cancelling, negative limits taking the defaults, the bearer on every request (initialize,notifications/initialized, each page, the closingDELETE), no redirect reaching its target, missing arguments, and thatGatewayCalls.ListLimitsreaches the embedded gateway.gatewayProtocolVersiongives the real reason for the pin; see Protocol below.Reviewer note: shared code
walkToolPagesis the looplistToolsBoundedalready 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 fullinternal/adapter/mcpsuite 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 (IdentityBrokerGatewayServersis 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_requiredresult instead of error-32042, whichclassifywould 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.ListGatewayToolstakes aGatewayListLimits, andmcpbroker.GatewayCallsgainsListLimits, which production leaves zero. No settings key, so no user-docs or configuration-reference change.Testing
Run locally:
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 runoninternal/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
ListLimitswiring.Not run locally: the full
task lint,task test:race, andtask check:prgates, left to CI.task test:workspaceis not needed: theenginemodule is unchanged and no dependency pin moved.🤖 Generated with Claude Code