Using FastMCP with OpenAI and Avoiding Session Termination Issues

Using FastMCP with OpenAI: Avoiding Session Termination Issues

When integrating FastMCP as a Model Context Protocol (MCP) server for OpenAI’s Responses API, you may encounter a frustrating issue:
OpenAI sends DELETE requests to end the MCP session after each call, without automatically creating new sessions for subsequent requests. This will result in FastMCP responding:

1{ 2 "jsonrpc": "2.0", 3 "id": "server-error", 4 "error": { 5 "code": -32600, 6 "message": "Not Found: Session has been terminated" 7 } 8}

The Fix: stateless_http=True

When instantiating your FastMCP server, set:

1from fastmcp import FastMCP 2 3mcp = FastMCP( 4 name="MyToolServer", 5 stateless_http=True # crucial for streaming stability 6) 7 8@mcp.tool() 9def greet(name: str) -> str: 10 return f"Hello, {name}!" 11 12if __name__ == "__main__": 13 mcp.run( 14 transport="http", 15 host="0.0.0.0", 16 port=8000 17 )

##Why This Works stateless_http=True disables stateful session tracking. Every request is handled independently, so OpenAI’s DELETE calls don’t break the connection. Without it, streaming sessions will be terminated prematurely. More details: OpenAI Forum Thread

Caveats

  • Session IDs won’t persist between requests, so advanced workflows relying on session state won’t work.
  • Sampling and certain bi-directional streaming patterns may not be supported in stateless mode.

TL;DR

If your OpenAI + FastMCP streaming requests fail after one call, set stateless_http=True when starting the server. It’s the simplest fix to keep your MCP server ready for every new streaming request.