You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ResponseCachingMiddleware serves one session's cached tools/call result to another session #5574
With ResponseCachingMiddleware installed, a tools/call whose result depends on session-scoped state (Context.set_state / get_state) returns another session's cached answer: session B making the same call as session A gets session A's result, even after B has stored its own value. Without the middleware, each session sees its own state. The same happens for resources/read and prompts/get handlers that read session state. On main, cache keys include the caller's access token but not the session identity.
Example Code
importasynciofromfastmcpimportClient, FastMCPfromfastmcp.server.contextimportContextfromfastmcp.server.middleware.cachingimportResponseCachingMiddlewaremcp=FastMCP()
mcp.add_middleware(ResponseCachingMiddleware())
@mcp.toolasyncdefremember(key: str, value: str, ctx: Context) ->str:
awaitctx.set_state(key, value)
return"stored"@mcp.toolasyncdefrecall(key: str, ctx: Context) ->str:
returnawaitctx.get_state(key) or"<unset>"asyncdefmain():
# Session A stores and recalls its own value (the recall result is cached).asyncwithClient(mcp, mode="legacy") asa:
awaita.call_tool("remember", {"key": "k", "value": "A-secret"})
print("A:", (awaita.call_tool("recall", {"key": "k"})).content[0].text)
# Session B stores its OWN value, then makes the same recall call.asyncwithClient(mcp, mode="legacy") asb:
awaitb.call_tool("remember", {"key": "k", "value": "B-secret"})
print("B:", (awaitb.call_tool("recall", {"key": "k"})).content[0].text)
asyncio.run(main())
Working as designed: sessions without a token share one cache partition. Exclude tools whose output depends on session state from ResponseCachingMiddleware.
Activity
zzstoatzz commentedon Nov 11, 2026
Working as designed: sessions without a token share one cache partition. Exclude tools whose output depends on session state from ResponseCachingMiddleware.