-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Add a client-side response cache honoring SEP-2549 caching hints #3023
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
1797d9a
Move CacheableMethod to mcp_types.methods and derive CACHEABLE_METHOD…
maxisbey d2d4a25
Add client response cache primitives: config, keys, store protocol, i…
maxisbey 5f8a392
Add client response cache coordinator: scope arms, era gating, TTL re…
maxisbey ba95005
Wire the response cache into Client: configuration, server identity, …
maxisbey 0a19550
Serve cacheable client verbs through the response cache
maxisbey 8fae9cf
Treat negative inbound ttlMs as zero at the client parse seams
maxisbey 98941ac
Document the client response cache
maxisbey c689c20
Add end-to-end hardening tests for the client response cache
maxisbey e3bb712
Cover float negative ttlMs on the discover seam in the auto-mode test
maxisbey b81b7dd
Document eviction timing, refetch policy, shared-store races, and the…
maxisbey efac31f
Tighten response cache: session guard, identity and meta handling, re…
maxisbey d1cebd1
Apply configured cache hints to mapping handler results and fix unkno…
maxisbey 21a779a
Trim comments and docstrings
maxisbey 804043b
Address review feedback: drop tutorial globals, plain-ASCII docs pros…
maxisbey fb1c510
Keep the tutorial handler a plain function with a separate state holder
maxisbey ef7fdff
Era-scope cache arms and harden store interaction paths
maxisbey af36ead
Strip userinfo textually and address review notes on the docs
maxisbey b44a891
Address review feedback on identity stripping and stale tool-map pruning
maxisbey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,15 +1,34 @@ | ||||||||||
| from typing import Any | ||||||||||
|
|
||||||||||
| from mcp_types import ListToolsResult, PaginatedRequestParams, Tool | ||||||||||
|
|
||||||||||
| from mcp import Client | ||||||||||
| from mcp.server import CacheHint, MCPServer | ||||||||||
| from mcp.client import CacheConfig | ||||||||||
| from mcp.server import CacheHint, Server, ServerRequestContext | ||||||||||
|
|
||||||||||
| fetches = 0 | ||||||||||
| now = 1_000_000.0 | ||||||||||
|
|
||||||||||
|
|
||||||||||
| mcp = MCPServer("Weather", cache_hints={"tools/list": CacheHint(ttl_ms=60_000, scope="public")}) | ||||||||||
| async def list_tools(ctx: ServerRequestContext[Any], params: PaginatedRequestParams | None) -> ListToolsResult: | ||||||||||
| global fetches | ||||||||||
| fetches += 1 | ||||||||||
| return ListToolsResult(tools=[Tool(name="forecast", input_schema={"type": "object"})]) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| @mcp.tool() | ||||||||||
| def forecast(city: str) -> str: | ||||||||||
| return f"Sunny in {city}" | ||||||||||
| server = Server( | ||||||||||
| "Weather", | ||||||||||
| on_list_tools=list_tools, | ||||||||||
| cache_hints={"tools/list": CacheHint(ttl_ms=60_000, scope="public")}, | ||||||||||
| ) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| async def main() -> None: | ||||||||||
| async with Client(mcp) as client: | ||||||||||
| tools = await client.list_tools() | ||||||||||
| print(f"{len(tools.tools)} tools, fresh for {tools.ttl_ms / 1000:.0f}s, scope={tools.cache_scope}") | ||||||||||
| global now | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Prompt for AI agents
Suggested change
|
||||||||||
| async with Client(server, cache=CacheConfig(clock=lambda: now)) as client: | ||||||||||
| await client.list_tools() # fetch 1 | ||||||||||
| await client.list_tools() # fresh for 60s: served from the cache | ||||||||||
| now += 60.0 | ||||||||||
| await client.list_tools() # the TTL ran out: fetch 2 | ||||||||||
| await client.list_tools(cache_mode="refresh") # skip the cache read: fetch 3 | ||||||||||
| print(f"4 calls, {fetches} fetches") | ||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.