- 
                Notifications
    
You must be signed in to change notification settings  - Fork 1.5k
 
[Draft] preserve key field in ResponseCachingMiddleware for prefixed tools/resources/prompts #2303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…ls/resources/prompts The ResponseCachingMiddleware was losing prefix information when caching tools, resources, and prompts from mounted/imported servers. The root cause was that the private _key attribute wasn't being serialized by Pydantic. Changes: - Add model_serializer to FastMCPComponent to include _key in serialization - Update model_validate to restore _key from deserialized data - Add key parameter when creating cached Tool/Resource/Prompt objects - Add comprehensive test for mounted server prefix preservation Fixes #2300 Co-authored-by: William Easton <[email protected]>
          
WalkthroughThe pull request adds a  Changes
 Sequence DiagramsequenceDiagram
    participant Client
    participant CachingMiddleware
    participant FastMCPComponent
    participant Cache
    
    Client->>CachingMiddleware: list_tools() - first call
    CachingMiddleware->>FastMCPComponent: retrieve tools
    FastMCPComponent->>FastMCPComponent: construct Tool with key
    activate FastMCPComponent
    FastMCPComponent->>Cache: store normalized representation
    deactivate FastMCPComponent
    CachingMiddleware-->>Client: return tools with prefixes (child_add, etc.)
    
    Client->>CachingMiddleware: list_tools() - second call
    activate Cache
    CachingMiddleware->>Cache: check cache
    Cache-->>CachingMiddleware: return cached items
    deactivate Cache
    rect rgb(200, 220, 255)
    note over CachingMiddleware: Key field preserved<br/>in cached representation
    end
    CachingMiddleware-->>Client: return same tools with prefixes intact
    Poem
 Pre-merge checks and finishing touches❌ Failed checks (1 warning)
 ✅ Passed checks (2 passed)
 ✨ Finishing touches
 🧪 Generate unit tests (beta)
 📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: ASSERTIVE Plan: Pro 📒 Files selected for processing (3)
 🧰 Additional context used📓 Path-based instructions (3)src/**/*.py📄 CodeRabbit inference engine (AGENTS.md) 
 Files: 
 **/*.py📄 CodeRabbit inference engine (AGENTS.md) 
 Files: 
 tests/**/*.py📄 CodeRabbit inference engine (AGENTS.md) 
 Files: 
 🧬 Code graph analysis (3)src/fastmcp/server/middleware/caching.py (2)
 tests/server/middleware/test_caching.py (2)
 src/fastmcp/utilities/components.py (2)
 ⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
 🔇 Additional comments (6)
 Comment   | 
    
| 
           /marvin im not sure i want a custom validator and serializer on the fastmcp component, we might just need to use custom cachable models in the response caching middleware, can you explore our options here and outline a recommendation?  | 
    
| 
           Claude finished @strawgate's task —— View job Analysis: Custom Serializers vs. Custom Cacheable ModelsI've analyzed both approaches for preserving key fields in cached responses. Here's my recommendation: Current Implementation (Custom Serializers on FastMCPComponent)Pros: 
 Cons: 
 Alternative Approach (Custom Cacheable Models in Middleware)Pros: 
 Cons: 
 Recommendation: Use Custom Cacheable Models ✅The alternative approach is architecturally superior because: 
 Implementation NotesIf you go with custom cacheable models: 
 The extra boilerplate (~100 lines) is worth the architectural cleanliness and avoiding potential bugs from changing base class behavior.  | 
    
| 
           /marvin do you think we could just remove key and just mutate the tool name when it gets returned from mounted servers?  | 
    
| 
           Claude finished @strawgate's task —— View job Analysis: Should We Remove 
 | 
    
| 
           /marvin im not sure that's convincing, for a client to call a tool we must be able to distinguish prefixes without keys already? Can you review this again? What if we just stopped using key for bookkeeping?  | 
    
| 
           Claude finished @strawgate's task —— View job Analysis: Do We Actually Need the 
 | 
    
| 
           I dont think marvin's final conclusion is valid: if you mutate the name of a tool to add a prefix, then the tool will also have that prefix in the server it was originally added in. So the ultimate (parent) server has all properly named tools, but every subserver has weirdly named tools  | 
    
Summary
The ResponseCachingMiddleware was losing prefix information when caching tools, resources, and prompts from mounted/imported servers. The root cause was that the private
_keyattribute wasn't being serialized by Pydantic.Example
Before this fix:
After this fix, both calls correctly return
["child_example_tool"].Changes
_keyfieldkeywhen creating cacheable objectsFixes #2300
Generated with Claude Code
Summary by CodeRabbit
Release Notes