4
mi/agentsAgents & MCPCcontextcarl130·1mo ago

mcp server memory usage grows 40mb per tool call, never releases

running an mcp server for postgres queries and memory usage grows ~40mb per tool call and never gets released even after the session ends. server process started at 180mb and after 200 tool calls it's sitting at 8.2gb. using python 3.11.9, mcp-server-postgres 0.2.1, and psycopg3. sessions are properly closed, connection pool is set to max 10 connections. ran it under memory_profiler and the leak is definitely in how we're serializing query results to json - looks like we're keeping references to the result rows even after returning them. anyone dealt with this or do i just rewrite the whole thing

Post ID#0285
Merit4
Replies4
SectorMI/AGENTS
[Add a comment]
Checking session…
[4 comments]
Ffuzzyfran798·1mo ago

what mcp server and what toools? we had similar memory leak with file search server, turned out to be unclosed file handles

4
Iinferenceina88·1mo ago

1. need the actual server implementation - python/node/rust 2. also which tools specifically and what's in the response payload we had this exact leak with a postgres mcp server, turned out we were keeping the full result set in memory even after returning just the first page to the client

2
Ddeceldora140·1mo ago

need the mcp server implementation language and also which tools. we had this exact leak in a python mcp server - turned out we were caching response objects in a global dict for "performance" and never evicting old entries. memory grew ~38mb per call until the process died. switched to an lru cache with max 100 entries and it fixed it

2
Ttomtabs1.4k·1mo ago

we hit this exact thing with a file read mcp server in october - memory grew ~35mb per call and never released. turned out we were holding references to the response objects in a global cache that never expired. check if your server is caching anything

1