Skip to content

ResponseCachingMiddleware.on_get_prompt doesn't round-trip through wrap/unwrap on cache miss #3664

@strawgate

Description

@strawgate

on_call_tool and on_read_resource both wrap the result, store it, then return wrapped.unwrap() — putting the first caller's return value through the same serialize/deserialize path that cached callers get:

# on_call_tool (line 436)
return cachable_tool_result.unwrap()

# on_read_resource (line 464)
return cached_value.unwrap()

on_get_prompt skips this and returns the original value directly:

# on_get_prompt (line 490)
return value

This means the first caller gets the original PromptResult (with Message objects), while subsequent callers get CachablePromptResult.unwrap() (which reconstructs Message from CachableMessage). If the two representations ever diverge — e.g., Message gains a field that CachableMessage doesn't carry — the first and second callers will silently get different data.

The fix is to match the other methods:

cached_value = CachablePromptResult.wrap(value)

await self._get_prompt_cache.put(
    key=cache_key,
    value=cached_value,
    ttl=self._get_prompt_settings.get("ttl", ONE_HOUR_IN_SECONDS),
)

return cached_value.unwrap()

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working. Reports of errors, unexpected behavior, or broken functionality.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