-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathtutorial003.py
More file actions
34 lines (22 loc) · 891 Bytes
/
Copy pathtutorial003.py
File metadata and controls
34 lines (22 loc) · 891 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from dataclasses import dataclass
import anyio
from mcp import Client
from mcp.client import CacheConfig
from mcp.types import ListToolsResult
@dataclass
class Clock:
now: float = 0.0
clock = Clock() # advanced by hand below, so the TTL runs out without sleeping
async def run(client: Client) -> ListToolsResult:
tools = await client.list_tools() # fetch 1
await client.list_tools() # still fresh: served from the cache
clock.now += 2
await client.list_tools() # past the one-second TTL: fetch 2
await client.list_tools(cache_mode="refresh") # skip the cache read: fetch 3
return tools
async def main() -> None:
async with Client("http://localhost:8000/mcp", cache=CacheConfig(clock=lambda: clock.now)) as client:
tools = await run(client)
print(tools.ttl_ms, tools.cache_scope)
if __name__ == "__main__":
anyio.run(main)