mirror of
https://github.com/aljazceru/mcp-python-sdk.git
synced 2025-12-19 06:54:18 +01:00
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
This commit is contained in:
committed by
GitHub
parent
dfbe56d2b2
commit
df2d3a57c2
50
tests/issues/test_355_type_error.py
Normal file
50
tests/issues/test_355_type_error.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from collections.abc import AsyncIterator
|
||||
from contextlib import asynccontextmanager
|
||||
from dataclasses import dataclass
|
||||
|
||||
from mcp.server.fastmcp import Context, FastMCP
|
||||
|
||||
|
||||
class Database: # Replace with your actual DB type
|
||||
@classmethod
|
||||
async def connect(cls):
|
||||
return cls()
|
||||
|
||||
async def disconnect(self):
|
||||
pass
|
||||
|
||||
def query(self):
|
||||
return "Hello, World!"
|
||||
|
||||
|
||||
# Create a named server
|
||||
mcp = FastMCP("My App")
|
||||
|
||||
|
||||
@dataclass
|
||||
class AppContext:
|
||||
db: Database
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def app_lifespan(server: FastMCP) -> AsyncIterator[AppContext]:
|
||||
"""Manage application lifecycle with type-safe context"""
|
||||
# Initialize on startup
|
||||
db = await Database.connect()
|
||||
try:
|
||||
yield AppContext(db=db)
|
||||
finally:
|
||||
# Cleanup on shutdown
|
||||
await db.disconnect()
|
||||
|
||||
|
||||
# Pass lifespan to server
|
||||
mcp = FastMCP("My App", lifespan=app_lifespan)
|
||||
|
||||
|
||||
# Access type-safe lifespan context in tools
|
||||
@mcp.tool()
|
||||
def query_db(ctx: Context) -> str:
|
||||
"""Tool that uses initialized resources"""
|
||||
db = ctx.request_context.lifespan_context.db
|
||||
return db.query()
|
||||
Reference in New Issue
Block a user