fix: correct Context type parameters across examples and tests (#2256)
This commit is contained in:
parent
51c53f2c18
commit
31a38b5078
1 changed files with 59 additions and 75 deletions
19
README.v2.md
19
README.v2.md
|
|
@ -346,13 +346,12 @@ Tools can optionally receive a Context object by including a parameter with the
|
|||
<!-- snippet-source examples/snippets/servers/tool_progress.py -->
|
||||
```python
|
||||
from mcp.server.mcpserver import Context, MCPServer
|
||||
from mcp.server.session import ServerSession
|
||||
|
||||
mcp = MCPServer(name="Progress Example")
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def long_running_task(task_name: str, ctx: Context[ServerSession, None], steps: int = 5) -> str:
|
||||
async def long_running_task(task_name: str, ctx: Context, steps: int = 5) -> str:
|
||||
"""Execute a task with progress updates."""
|
||||
await ctx.info(f"Starting: {task_name}")
|
||||
|
||||
|
|
@ -694,13 +693,12 @@ The Context object provides the following capabilities:
|
|||
<!-- snippet-source examples/snippets/servers/tool_progress.py -->
|
||||
```python
|
||||
from mcp.server.mcpserver import Context, MCPServer
|
||||
from mcp.server.session import ServerSession
|
||||
|
||||
mcp = MCPServer(name="Progress Example")
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def long_running_task(task_name: str, ctx: Context[ServerSession, None], steps: int = 5) -> str:
|
||||
async def long_running_task(task_name: str, ctx: Context, steps: int = 5) -> str:
|
||||
"""Execute a task with progress updates."""
|
||||
await ctx.info(f"Starting: {task_name}")
|
||||
|
||||
|
|
@ -826,7 +824,6 @@ import uuid
|
|||
from pydantic import BaseModel, Field
|
||||
|
||||
from mcp.server.mcpserver import Context, MCPServer
|
||||
from mcp.server.session import ServerSession
|
||||
from mcp.shared.exceptions import UrlElicitationRequiredError
|
||||
from mcp.types import ElicitRequestURLParams
|
||||
|
||||
|
|
@ -844,7 +841,7 @@ class BookingPreferences(BaseModel):
|
|||
|
||||
|
||||
@mcp.tool()
|
||||
async def book_table(date: str, time: str, party_size: int, ctx: Context[ServerSession, None]) -> str:
|
||||
async def book_table(date: str, time: str, party_size: int, ctx: Context) -> str:
|
||||
"""Book a table with date availability check.
|
||||
|
||||
This demonstrates form mode elicitation for collecting non-sensitive user input.
|
||||
|
|
@ -868,7 +865,7 @@ async def book_table(date: str, time: str, party_size: int, ctx: Context[ServerS
|
|||
|
||||
|
||||
@mcp.tool()
|
||||
async def secure_payment(amount: float, ctx: Context[ServerSession, None]) -> str:
|
||||
async def secure_payment(amount: float, ctx: Context) -> str:
|
||||
"""Process a secure payment requiring URL confirmation.
|
||||
|
||||
This demonstrates URL mode elicitation using ctx.elicit_url() for
|
||||
|
|
@ -892,7 +889,7 @@ async def secure_payment(amount: float, ctx: Context[ServerSession, None]) -> st
|
|||
|
||||
|
||||
@mcp.tool()
|
||||
async def connect_service(service_name: str, ctx: Context[ServerSession, None]) -> str:
|
||||
async def connect_service(service_name: str, ctx: Context) -> str:
|
||||
"""Connect to a third-party service requiring OAuth authorization.
|
||||
|
||||
This demonstrates the "throw error" pattern using UrlElicitationRequiredError.
|
||||
|
|
@ -933,14 +930,13 @@ Tools can interact with LLMs through sampling (generating text):
|
|||
<!-- snippet-source examples/snippets/servers/sampling.py -->
|
||||
```python
|
||||
from mcp.server.mcpserver import Context, MCPServer
|
||||
from mcp.server.session import ServerSession
|
||||
from mcp.types import SamplingMessage, TextContent
|
||||
|
||||
mcp = MCPServer(name="Sampling Example")
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def generate_poem(topic: str, ctx: Context[ServerSession, None]) -> str:
|
||||
async def generate_poem(topic: str, ctx: Context) -> str:
|
||||
"""Generate a poem using LLM sampling."""
|
||||
prompt = f"Write a short poem about {topic}"
|
||||
|
||||
|
|
@ -970,13 +966,12 @@ Tools can send logs and notifications through the context:
|
|||
<!-- snippet-source examples/snippets/servers/notifications.py -->
|
||||
```python
|
||||
from mcp.server.mcpserver import Context, MCPServer
|
||||
from mcp.server.session import ServerSession
|
||||
|
||||
mcp = MCPServer(name="Notifications Example")
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def process_data(data: str, ctx: Context[ServerSession, None]) -> str:
|
||||
async def process_data(data: str, ctx: Context) -> str:
|
||||
"""Process data with logging."""
|
||||
# Different log levels
|
||||
await ctx.debug(f"Debug: Processing '{data}'")
|
||||
|
|
|
|||
Loading…
Reference in a new issue