Handling Idle User Connections #8790
Replies: 3 comments 3 replies
-
Thanks @nhtruong will keep this in mind to address. Happy to collaborate |
Beta Was this translation helpful? Give feedback.
-
The MCP community is actively wrestling with the same statefulness challenges, and there's a clear trend toward more ephemeral, stateless approaches, especially for serverless deployments. The reason for caching connections in the first place was related to MCP statefulness in the beginning of the framework. See: modelcontextprotocol/modelcontextprotocol#102 I'm definitely leaning Option 1. The timer-based approach would work, but it's adding complexity to manage complexity as we need to make sure timers are properly cleaned up. You could still do request-scoped caching for the burst scenarios (like when a user's chat uses 3 MCP tools). Create connections on-demand but reuse them within the same request context, then let them close naturally when the request completes. This would position LibreChat well for both current scale needs and future MCP protocol evolution, where stateless/serverless compatibility is clearly becoming a priority. |
Beta Was this translation helpful? Give feedback.
-
@danny-avila I see this code has just been added into LibreChat/packages/api/src/mcp/MCPManager.ts Lines 242 to 251 in d7d0276 I assume the headers are set per toolCall instead of during the construction of the connection is because this value might change after the connection has been established? With ephemeral connections, we won't have to worry about reusing connections with outdated info 😁 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Problem
Right now LibreChat caches the user-level connections in MCPManager with a TTL of 15 minutes PER USER (not per connection). After 15 minutes, a user is considered idle and all of there MCP connections are to be removed.
The idle check is not on a timer, but through calling
MCPManager.checkIdleConnections()
function as a side effect of many user interactions (Opening tool selection dialogs, Loading agent/assistant panels, Chat involving MCP ...). This check loops through all users with MCP connections and disconnect the connections of idle users, which is expensive at scale when we have thousands of users.The expensive
MCPManager.checkIdleConnections()
can be called multiple times, especially during a Chat event. For example when a user starts a chat with an agent and selects 3 user MCP tools, it will result in the idle check being called 5 times through:Solutions
1. Do not cache user-level connections
Correct me if I'm wrong about the following assumptions:
So maybe we don't need to store the connections in memory for reuse and gains the following benefits:
2. Clear idle connections on a timer
Instead performing this task as a side effect of user interactions, the idle check should run on a timer to check for idle connections (not idle users), which has 2 flavors:
Each of these timer approaches has their own pros and cons but they both have the following benefits:
@danny-avila this came up as a part of the horizontal-scaling discussion, specifically about MCP connection management which involves lazy-loading. I just thought this is big enough to warrant its own discussion.
Beta Was this translation helpful? Give feedback.
All reactions