fix: correct Context type parameters across examples and tests (#2256)
This commit is contained in:
parent
51c53f2c18
commit
31a38b5078
5 changed files with 59 additions and 75 deletions
|
|
@ -13,7 +13,6 @@ import click
|
|||
from mcp.server import ServerRequestContext
|
||||
from mcp.server.mcpserver import Context, MCPServer
|
||||
from mcp.server.mcpserver.prompts.base import UserMessage
|
||||
from mcp.server.session import ServerSession
|
||||
from mcp.server.streamable_http import EventCallback, EventMessage, EventStore
|
||||
from mcp.types import (
|
||||
AudioContent,
|
||||
|
|
@ -142,7 +141,7 @@ def test_multiple_content_types() -> list[TextContent | ImageContent | EmbeddedR
|
|||
|
||||
|
||||
@mcp.tool()
|
||||
async def test_tool_with_logging(ctx: Context[ServerSession, None]) -> str:
|
||||
async def test_tool_with_logging(ctx: Context) -> str:
|
||||
"""Tests tool that emits log messages during execution"""
|
||||
await ctx.info("Tool execution started")
|
||||
await asyncio.sleep(0.05)
|
||||
|
|
@ -155,7 +154,7 @@ async def test_tool_with_logging(ctx: Context[ServerSession, None]) -> str:
|
|||
|
||||
|
||||
@mcp.tool()
|
||||
async def test_tool_with_progress(ctx: Context[ServerSession, None]) -> str:
|
||||
async def test_tool_with_progress(ctx: Context) -> str:
|
||||
"""Tests tool that reports progress notifications"""
|
||||
await ctx.report_progress(progress=0, total=100, message="Completed step 0 of 100")
|
||||
await asyncio.sleep(0.05)
|
||||
|
|
@ -173,7 +172,7 @@ async def test_tool_with_progress(ctx: Context[ServerSession, None]) -> str:
|
|||
|
||||
|
||||
@mcp.tool()
|
||||
async def test_sampling(prompt: str, ctx: Context[ServerSession, None]) -> str:
|
||||
async def test_sampling(prompt: str, ctx: Context) -> str:
|
||||
"""Tests server-initiated sampling (LLM completion request)"""
|
||||
try:
|
||||
# Request sampling from client
|
||||
|
|
@ -198,7 +197,7 @@ class UserResponse(BaseModel):
|
|||
|
||||
|
||||
@mcp.tool()
|
||||
async def test_elicitation(message: str, ctx: Context[ServerSession, None]) -> str:
|
||||
async def test_elicitation(message: str, ctx: Context) -> str:
|
||||
"""Tests server-initiated elicitation (user input request)"""
|
||||
try:
|
||||
# Request user input from client
|
||||
|
|
@ -230,7 +229,7 @@ class SEP1034DefaultsSchema(BaseModel):
|
|||
|
||||
|
||||
@mcp.tool()
|
||||
async def test_elicitation_sep1034_defaults(ctx: Context[ServerSession, None]) -> str:
|
||||
async def test_elicitation_sep1034_defaults(ctx: Context) -> str:
|
||||
"""Tests elicitation with default values for all primitive types (SEP-1034)"""
|
||||
try:
|
||||
# Request user input with defaults for all primitive types
|
||||
|
|
@ -289,7 +288,7 @@ class EnumSchemasTestSchema(BaseModel):
|
|||
|
||||
|
||||
@mcp.tool()
|
||||
async def test_elicitation_sep1330_enums(ctx: Context[ServerSession, None]) -> str:
|
||||
async def test_elicitation_sep1330_enums(ctx: Context) -> str:
|
||||
"""Tests elicitation with enum schema variations per SEP-1330"""
|
||||
try:
|
||||
result = await ctx.elicit(
|
||||
|
|
@ -313,7 +312,7 @@ def test_error_handling() -> str:
|
|||
|
||||
|
||||
@mcp.tool()
|
||||
async def test_reconnection(ctx: Context[ServerSession, None]) -> str:
|
||||
async def test_reconnection(ctx: Context) -> str:
|
||||
"""Tests SSE polling by closing stream mid-call (SEP-1699)"""
|
||||
await ctx.info("Before disconnect")
|
||||
|
||||
|
|
|
|||
|
|
@ -10,7 +10,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
|
||||
|
||||
|
|
@ -28,7 +27,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.
|
||||
|
|
@ -52,7 +51,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
|
||||
|
|
@ -76,7 +75,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.
|
||||
|
|
|
|||
|
|
@ -1,11 +1,10 @@
|
|||
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}'")
|
||||
|
|
|
|||
|
|
@ -1,12 +1,11 @@
|
|||
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}"
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +1,10 @@
|
|||
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}")
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue