fix(mcp-bridge): only queue messages for sessions with a live connection#13551
Open
shreemaan-abhishek wants to merge 2 commits into
Open
fix(mcp-bridge): only queue messages for sessions with a live connection#13551shreemaan-abhishek wants to merge 2 commits into
shreemaan-abhishek wants to merge 2 commits into
Conversation
5 tasks
There was a problem hiding this comment.
Pull request overview
This PR updates the mcp-bridge plugin’s message ingestion path to avoid unbounded shared-dict growth by only accepting messages for sessions with an active SSE connection, and by adding per-session queue bounds/TTLs to prevent abandoned queues from lingering.
Changes:
- Add a “live session” marker in the shared-dict broker with a TTL, refreshed by the SSE ping loop and cleaned up on teardown.
- Gate
POST .../message?sessionId=...so it returns404unless a live session marker exists. - Bound per-session queue length and apply a TTL to queue keys; add tests for session existence and queue bounding.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
t/plugin/mcp-bridge.t |
Adds tests for session existence checks and queue capacity behavior. |
apisix/plugins/mcp/server.lua |
Exposes session_exists() and refreshes the live-session marker during the ping loop; unregisters on close. |
apisix/plugins/mcp/server_wrapper.lua |
Registers live sessions before advertising the endpoint; rejects message requests for non-live sessions. |
apisix/plugins/mcp/broker/shared_dict.lua |
Implements session marker/TTL, unregister cleanup, queue length cap, and queue TTL refresh on push. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+36
to
40
| -- mark the session as live before advertising its endpoint, so a message | ||
| -- sent right after the client learns the session id is accepted | ||
| server:register() | ||
|
|
||
| -- send endpoint event to advertise the message endpoint |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.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.
Description
The
mcp-bridgeplugin exposes a message endpoint (POST /message?sessionId=...) that enqueues the request body into themcp-sessionshared dictionary, keyed by thesessionIdquery argument. Today that argument is taken as-is: a server is created for whateversessionIdis supplied and the body is pushed onto<sessionId>:queue, with a-- TODO: check ctx.var.arg_sessionIdmarking the missing validation. Because there is no check that the session has a live SSE connection, and the queue keys have no TTL or length bound, messages can be queued under session ids that no consumer will ever drain, and those queues stay in shared memory until the worker is reset.This PR makes the message endpoint accept only sessions that currently have a live SSE connection, and bounds the per-session queue:
<sessionId>:session) before its endpoint is advertised, and the ping loop refreshes it while the connection stays open. Teardown removes the marker and the queue.404whensessionIdis missing or has no live session, instead of creating a server and queueing for it.pushnow bounds the queue length per session and (re)arms a TTL on the queue key, so a single session cannot grow without limit and an abandoned queue does not linger.The session lookup is exposed as
mcp_server.session_exists()(delegating to the shared-dict broker) so the message endpoint can check it without standing up a server.Which issue(s) this PR fixes:
Fixes #
Checklist