Open
Description
In the low level api part, the first example is:
from contextlib import asynccontextmanager
from collections.abc import AsyncIterator
from fake_database import Database # Replace with your actual DB type
from mcp.server import Server
@asynccontextmanager
async def server_lifespan(server: Server) -> AsyncIterator[dict]:
"""Manage server startup and shutdown lifecycle."""
# Initialize resources on startup
db = await Database.connect()
try:
yield {"db": db}
finally:
# Clean up on shutdown
await db.disconnect()
# Pass lifespan to server
server = Server("example-server", lifespan=server_lifespan)
# Access lifespan context in handlers
@server.call_tool()
async def query_db(name: str, arguments: dict) -> list:
ctx = server.get_context()
db = ctx.lifespan_context["db"]
return await db.query(arguments["query"])
I tried to run this example, but my editor shows that the class Server
does not have a method named get_context
, and it actually does so in runtime.
Metadata
Metadata
Assignees
Labels
No labels