Skip to content

Commit 294fc6b

Browse files
hesreallyhimReally Him
and
Really Him
authored
[DOC] Fix example usage of get_context (plus a little more) (#801)
Co-authored-by: Really Him <[email protected]>
1 parent f2f4dbd commit 294fc6b

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ from dataclasses import dataclass
160160

161161
from fake_database import Database # Replace with your actual DB type
162162

163-
from mcp.server.fastmcp import Context, FastMCP
163+
from mcp.server.fastmcp import FastMCP
164164

165165
# Create a named server
166166
mcp = FastMCP("My App")
@@ -192,9 +192,10 @@ mcp = FastMCP("My App", lifespan=app_lifespan)
192192

193193
# Access type-safe lifespan context in tools
194194
@mcp.tool()
195-
def query_db(ctx: Context) -> str:
195+
def query_db() -> str:
196196
"""Tool that uses initialized resources"""
197-
db = ctx.request_context.lifespan_context.db
197+
ctx = mcp.get_context()
198+
db = ctx.request_context.lifespan_context["db"]
198199
return db.query()
199200
```
200201

@@ -631,7 +632,7 @@ server = Server("example-server", lifespan=server_lifespan)
631632
# Access lifespan context in handlers
632633
@server.call_tool()
633634
async def query_db(name: str, arguments: dict) -> list:
634-
ctx = server.get_context()
635+
ctx = server.request_context
635636
db = ctx.lifespan_context["db"]
636637
return await db.query(arguments["query"])
637638
```

0 commit comments

Comments
 (0)