Skip to content

ResponseCachingMiddleware serves one session's cached tools/call result to another session #5574

Closed as not planned

Description

@BlueX888

What happened?

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

import asyncio

from fastmcp import Client, FastMCP
from fastmcp.server.context import Context
from fastmcp.server.middleware.caching import ResponseCachingMiddleware

mcp = FastMCP()
mcp.add_middleware(ResponseCachingMiddleware())


@mcp.tool
async def remember(key: str, value: str, ctx: Context) -> str:
    await ctx.set_state(key, value)
    return "stored"


@mcp.tool
async def recall(key: str, ctx: Context) -> str:
    return await ctx.get_state(key) or "<unset>"


async def main():
    # Session A stores and recalls its own value (the recall result is cached).
    async with Client(mcp, mode="legacy") as a:
        await a.call_tool("remember", {"key": "k", "value": "A-secret"})
        print("A:", (await a.call_tool("recall", {"key": "k"})).content[0].text)

    # Session B stores its OWN value, then makes the same recall call.
    async with Client(mcp, mode="legacy") as b:
        await b.call_tool("remember", {"key": "k", "value": "B-secret"})
        print("B:", (await b.call_tool("recall", {"key": "k"})).content[0].text)


asyncio.run(main())

Expected:

A: A-secret
B: B-secret

Actual:

A: A-secret
B: A-secret

Version Information

FastMCP version: 4.0.11.dev1+0ebabc0c
MCP version: 2.0.0
Python version: 3.12.14
Platform: macOS-26.6.2-arm64-arm-64bit

Activity

  1. added
    bugSomething isn't working. Reports of errors, unexpected behavior, or broken functionality.
    securitySecurity fixes: input validation, SSRF/LFI prevention, auth hardening, injection defenses.
    serverRelated to FastMCP server implementation or server-side functionality.
    on Oct 7, 2026
  2. zzstoatzz commented on Nov 11, 2026

    @zzstoatzz
    Collaborator

    Working as designed: sessions without a token share one cache partition. Exclude tools whose output depends on session state from ResponseCachingMiddleware.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working. Reports of errors, unexpected behavior, or broken functionality.high-prioritysecuritySecurity fixes: input validation, SSRF/LFI prevention, auth hardening, injection defenses.serverRelated to FastMCP server implementation or server-side functionality.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions