docs: stop presenting the in-memory client as the way to connect (#3443)
This commit is contained in:
parent
0c9136841f
commit
7bb486a10f
2 changed files with 803 additions and 598 deletions
|
|
@ -7,6 +7,7 @@ TOOLS = [Tool(name="forecast", input_schema={"type": "object"})]
|
|||
|
||||
|
||||
async def list_tools(ctx: ServerRequestContext[Any], params: PaginatedRequestParams | None) -> ListToolsResult:
|
||||
print("tools/list served")
|
||||
return ListToolsResult(tools=TOOLS, ttl_ms=1_000)
|
||||
|
||||
|
||||
|
|
@ -15,3 +16,4 @@ server = Server(
|
|||
on_list_tools=list_tools,
|
||||
cache_hints={"tools/list": CacheHint(ttl_ms=60_000, scope="public")},
|
||||
)
|
||||
app = server.streamable_http_app()
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue