Skip to content

fix(fastmcp): add session_idle_timeout parameter to prevent unbounded session memory growth#3443

Open
abhijeethp wants to merge 1 commit intoPrefectHQ:mainfrom
abhijeethp:main
Open

fix(fastmcp): add session_idle_timeout parameter to prevent unbounded session memory growth#3443
abhijeethp wants to merge 1 commit intoPrefectHQ:mainfrom
abhijeethp:main

Conversation

@abhijeethp
Copy link
Contributor

Description

Add session_idle_timeout parameter to prevent unbounded session memory growth

In stateful HTTP mode (the default), StreamableHTTPSessionManager accumulates sessions in an in-memory dict without any TTL, cleanup, or max session limit. Disconnected clients leave orphaned sessions with live tasks and memory streams. Terminated sessions persist as zombie entries. Over time this causes unbounded memory growth, eventually leading to OOM on long-running servers.

The MCP Python SDK has merged support for session_idle_timeout ([modelcontextprotocol/python-sdk#2022](modelcontextprotocol/python-sdk#2022), [modelcontextprotocol/python-sdk#1994](modelcontextprotocol/python-sdk#1994)) but has not yet released it. This PR threads the parameter through FastMCP so it is ready to use once the SDK releases.

Changes

  • settings.py: Add session_idle_timeout: float | None = None (configurable via FASTMCP_SESSION_IDLE_TIMEOUT env var)
  • server/http.py: Accept session_idle_timeout in create_streamable_http_app(), pass it to StreamableHTTPSessionManager with runtime detection — logs a warning if the installed MCP SDK doesn't support it yet
  • server/mixins/transport.py: Thread session_idle_timeout through http_app() and run_http_async() with settings fallback
  • tests/client/test_streamable_http.py: 6 new tests covering parameter threading, settings defaults, settings override, SDK compatibility warning, and end-to-end idle session cleanup (auto-skipped until SDK releases)

Usage (once MCP SDK >= 1.27.0 is released)

mcp = FastMCP("MyServer")
app = mcp.http_app(session_idle_timeout=1800)  # 30 min

Or via environment variable:

export FASTMCP_SESSION_IDLE_TIMEOUT=1800

This PR was authored with the help of Claude Code.

Contributors Checklist

  • My change closes #(issue number)
  • I have followed the repository's development workflow
  • I have tested my changes manually and by adding relevant tests
  • I have performed all required documentation updates

Review Checklist

  • I have self-reviewed my changes
  • My Pull Request is ready for review

@marvin-context-protocol marvin-context-protocol bot added bug Something isn't working. Reports of errors, unexpected behavior, or broken functionality. server Related to FastMCP server implementation or server-side functionality. http Related to HTTP transport, networking, or web server functionality. labels Mar 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working. Reports of errors, unexpected behavior, or broken functionality. http Related to HTTP transport, networking, or web server functionality. server Related to FastMCP server implementation or server-side functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant