docs: stop presenting the in-memory client as the way to connect (#3443)

This commit is contained in:
Max 2026-09-05 04:15:58 +01:00 • committed by GitHub
commit 7bb486a10f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,39 +1,34 @@
from dataclasses import dataclass
from typing import Any
import anyio
from mcp import Client
from mcp.client import CacheConfig
from mcp.server import CacheHint, Server, ServerRequestContext
from mcp.types import ListToolsResult, PaginatedRequestParams, Tool
from mcp.types import ListToolsResult
@dataclass
class DemoState:
fetches: int = 0
now: float = 1_000_000.0
class Clock:
now: float = 0.0
state = DemoState()
clock = Clock() # advanced by hand below, so the TTL runs out without sleeping
async def list_tools(ctx: ServerRequestContext[Any], params: PaginatedRequestParams | None) -> ListToolsResult:
state.fetches += 1
return ListToolsResult(tools=[Tool(name="forecast", input_schema={"type": "object"})])
server = Server(
"Weather",
on_list_tools=list_tools,
cache_hints={"tools/list": CacheHint(ttl_ms=60_000, scope="public")},
)
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:
start = state.fetches
async with Client(server, cache=CacheConfig(clock=lambda: state.now)) as client:
await client.list_tools() # fetch 1
await client.list_tools() # fresh for 60s: served from the cache
state.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, {state.fetches - start} fetches")
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)