Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(mcp): attempting to fix unbounded mcp memory use (#11877)

Valery Meleshkin0bb97d3ca49b
diff --git a/web/src/features/mcp/server/transport.ts b/web/src/features/mcp/server/transport.ts
index c98fa0684f..a2c0d31e37 100644
--- a/web/src/features/mcp/server/transport.ts
+++ b/web/src/features/mcp/server/transport.ts
@@ -68,21 +68,31 @@ export async function handleMcpRequest(
enableDnsRebindingProtection: true, // CVE-2025-66414: Protect against DNS rebinding attacks
});
- // Connect server to transport
- await server.connect(transport);
+ try {
+ // Connect server to transport
+ await server.connect(transport);
- logger.info("MCP server connected via Streamable HTTP transport", {
- method: req.method,
- });
+ logger.info("MCP server connected via Streamable HTTP transport", {
+ method: req.method,
+ });
- // Handle the request through the transport
- // IMPORTANT: The transport manages the response lifecycle internally.
- // It will send the response and end it when appropriate.
- // Do NOT call res.end() after this - the transport handles it.
- await transport.handleRequest(req, res, req.body);
+ // Handle the request through the transport
+ // IMPORTANT: The transport manages the response lifecycle internally.
+ // It will send the response and end it when appropriate.
+ // Do NOT call res.end() after this - the transport handles it.
+ await transport.handleRequest(req, res, req.body);
- // Note: Do NOT end the response here. The transport has already
- // sent the response (JSON or SSE) and ended it appropriately.
+ // Note: Do NOT end the response here. The transport has already
+ // sent the response (JSON or SSE) and ended it appropriately.
+ } finally {
+ // Clean up server and transport to prevent memory leaks
+ // server.close() internally calls transport.close()
+ await server.close().catch((err) => {
+ logger.warn("Error closing MCP server", {
+ error: err instanceof Error ? err.message : "Unknown",
+ });
+ });
+ }
} catch (error) {
logger.error("MCP transport error", {
message: error instanceof Error ? error.message : "Unknown error",
diff --git a/web/src/pages/api/public/mcp/index.ts b/web/src/pages/api/public/mcp/index.ts
index 86ccf89ccf..4a5e214d8c 100644
--- a/web/src/pages/api/public/mcp/index.ts
+++ b/web/src/pages/api/public/mcp/index.ts
@@ -174,6 +174,8 @@ export default async function handler(
*/
export const config = {
api: {
- bodyParser: true,
+ bodyParser: {
+ sizeLimit: "1mb",
+ },
},
};