You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by "shreemaan-abhishek (via GitHub)" <gi...@apache.org> on 2026/06/12 07:46:50 UTC
[PR] fix(mcp-bridge): only queue messages for sessions with a live connection [apisix]
shreemaan-abhishek opened a new pull request, #13551:
URL: https://github.com/apache/apisix/pull/13551
### Description
The `mcp-bridge` plugin exposes a message endpoint (`POST /message?sessionId=...`) that enqueues the request body into the `mcp-session` shared dictionary, keyed by the `sessionId` query argument. Today that argument is taken as-is: a server is created for whatever `sessionId` is supplied and the body is pushed onto `<sessionId>:queue`, with a `-- TODO: check ctx.var.arg_sessionId` marking 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:
- When an SSE connection is established, the session registers a marker in the shared dictionary (`<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.
- The marker carries a TTL (60s), so it expires on its own if a session goes away without running teardown (e.g. worker reload).
- The message endpoint returns `404` when `sessionId` is missing or has no live session, instead of creating a server and queueing for it.
- `push` now 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
- [x] I have explained the need for this PR and the problem it solves
- [x] I have explained the changes or the new features added to this PR
- [x] I have added tests corresponding to this change
- [ ] I have updated the documentation to reflect this change
- [x] I have verified that this change is backward compatible (If not, please discuss on the [APISIX mailing list](https://github.com/apache/apisix/tree/master#community) first)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(mcp-bridge): only queue messages for sessions with a live connection [apisix]
Posted by "shreemaan-abhishek (via GitHub)" <gi...@apache.org>.
shreemaan-abhishek closed pull request #13551: fix(mcp-bridge): only queue messages for sessions with a live connection
URL: https://github.com/apache/apisix/pull/13551
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(mcp-bridge): only queue messages for sessions with a live connection [apisix]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #13551:
URL: https://github.com/apache/apisix/pull/13551#discussion_r3410979846
##########
apisix/plugins/mcp/server_wrapper.lua:
##########
@@ -33,6 +33,10 @@ local function sse_handler(conf, ctx, opts)
local server = opts.server
+ -- 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
Review Comment:
`server:register()` is called unconditionally and its result is ignored. If registration fails (e.g., shared dict out of memory), the code still advertises the endpoint and proceeds. Also, if `on_connect` returns an error, the early return path does not call `server:close()`, so the session marker/queue may be left behind until TTL expiry, and `/message` can accept requests for a non-live session in the meantime.
Check the result of `register()` and ensure `server:close()` runs before returning when `on_connect` fails.
##########
apisix/plugins/mcp/server.lua:
##########
@@ -89,6 +104,9 @@ function _M.start(self)
self.need_exit = true
break
end
+ -- refresh the session marker so it stays alive as long as the
+ -- connection does, and expires shortly after it goes away
+ self.message_broker:register()
ngx_sleep(30)
Review Comment:
The ping loop refreshes the live-session marker via `self.message_broker:register()`, but ignores its return value. If the shared dict is full or `set()` fails for any reason, the marker can expire while the SSE connection is still active, causing `/message` to start returning 404 for a live session.
Handle register failures by logging and terminating the session (or otherwise ensuring the marker remains consistent with the connection state).
##########
apisix/plugins/mcp/broker/shared_dict.lua:
##########
@@ -45,14 +54,52 @@ function _M.on(self, event, cb)
end
+-- record that this session has a live SSE connection on some worker. the
+-- message endpoint consults this before queueing, so that a request can only
+-- enqueue work for a session that actually exists. refreshed periodically by
+-- the owning session and removed on teardown; the TTL is a backstop for an
+-- unclean teardown.
+function _M.register(self)
+ local ok, err = shared_dict:set(self.session_id .. STORAGE_SUFFIX_SESSION,
+ true, SESSION_TTL)
+ if not ok then
+ return nil, "failed to register session: " .. err
+ end
+ return true
+end
+
+
+function _M.unregister(self)
+ shared_dict:delete(self.session_id .. STORAGE_SUFFIX_SESSION)
+ shared_dict:delete(self.session_id .. STORAGE_SUFFIX_QUEUE)
+end
+
+
+-- whether a session currently has a live SSE connection. module-level by
+-- design: the message endpoint checks this before creating a server.
+function _M.session_exists(session_id)
+ if not session_id then
+ return false
+ end
+ return shared_dict:get(session_id .. STORAGE_SUFFIX_SESSION) ~= nil
+end
+
+
function _M.push(self, message)
if not message then
return nil, "message is nil"
end
- local ok, err = shared_dict:rpush(self.session_id .. STORAGE_SUFFIX_QUEUE, message)
+ local key = self.session_id .. STORAGE_SUFFIX_QUEUE
+ local len = shared_dict:llen(key)
+ if len and len >= QUEUE_MAX_LENGTH then
+ return nil, "queue is full"
+ end
+ local ok, err = shared_dict:rpush(key, message)
if not ok then
return nil, "failed to push message to queue: " .. err
end
+ -- keep the queue from outliving its session if teardown is missed
+ shared_dict:expire(key, SESSION_TTL)
return true
Review Comment:
`push()` uses a separate `llen()` check before `rpush()` to enforce `QUEUE_MAX_LENGTH`. This is not atomic: concurrent requests can pass the length check and collectively grow the queue beyond the cap.
Consider using the length returned by `rpush()` to detect overflow and roll back the last push (or otherwise enforce the cap atomically) so the bound holds under concurrency.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org