|
| 1 | +""" |
| 2 | +Test the elicitation feature using stdio transport. |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +from pydantic import BaseModel, Field |
| 7 | + |
| 8 | +from mcp.server.fastmcp import Context, FastMCP |
| 9 | +from mcp.shared.memory import create_connected_server_and_client_session |
| 10 | +from mcp.types import ElicitResult, TextContent |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.anyio |
| 14 | +async def test_stdio_elicitation(): |
| 15 | + """Test the elicitation feature using stdio transport.""" |
| 16 | + |
| 17 | + # Create a FastMCP server with a tool that uses elicitation |
| 18 | + mcp = FastMCP(name="StdioElicitationServer") |
| 19 | + |
| 20 | + @mcp.tool(description="A tool that uses elicitation") |
| 21 | + async def ask_user(prompt: str, ctx: Context) -> str: |
| 22 | + class AnswerSchema(BaseModel): |
| 23 | + answer: str = Field(description="The user's answer to the question") |
| 24 | + |
| 25 | + result = await ctx.elicit( |
| 26 | + message=f"Tool wants to ask: {prompt}", |
| 27 | + schema=AnswerSchema, |
| 28 | + ) |
| 29 | + |
| 30 | + if result.action == "accept" and result.data: |
| 31 | + return f"User answered: {result.data.answer}" |
| 32 | + elif result.action == "decline": |
| 33 | + return "User declined to answer" |
| 34 | + else: |
| 35 | + return "User cancelled" |
| 36 | + |
| 37 | + # Create a custom handler for elicitation requests |
| 38 | + async def elicitation_callback(context, params): |
| 39 | + # Verify the elicitation parameters |
| 40 | + if params.message == "Tool wants to ask: What is your name?": |
| 41 | + return ElicitResult(action="accept", content={"answer": "Test User"}) |
| 42 | + else: |
| 43 | + raise ValueError(f"Unexpected elicitation message: {params.message}") |
| 44 | + |
| 45 | + # Use memory-based session to test with stdio transport |
| 46 | + async with create_connected_server_and_client_session( |
| 47 | + mcp._mcp_server, elicitation_callback=elicitation_callback |
| 48 | + ) as client_session: |
| 49 | + # First initialize the session |
| 50 | + result = await client_session.initialize() |
| 51 | + assert result.serverInfo.name == "StdioElicitationServer" |
| 52 | + |
| 53 | + # Call the tool that uses elicitation |
| 54 | + tool_result = await client_session.call_tool("ask_user", {"prompt": "What is your name?"}) |
| 55 | + |
| 56 | + # Verify the result |
| 57 | + assert len(tool_result.content) == 1 |
| 58 | + assert isinstance(tool_result.content[0], TextContent) |
| 59 | + assert tool_result.content[0].text == "User answered: Test User" |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.anyio |
| 63 | +async def test_stdio_elicitation_decline(): |
| 64 | + """Test elicitation with user declining.""" |
| 65 | + |
| 66 | + mcp = FastMCP(name="StdioElicitationDeclineServer") |
| 67 | + |
| 68 | + @mcp.tool(description="A tool that uses elicitation") |
| 69 | + async def ask_user(prompt: str, ctx: Context) -> str: |
| 70 | + class AnswerSchema(BaseModel): |
| 71 | + answer: str = Field(description="The user's answer to the question") |
| 72 | + |
| 73 | + result = await ctx.elicit( |
| 74 | + message=f"Tool wants to ask: {prompt}", |
| 75 | + schema=AnswerSchema, |
| 76 | + ) |
| 77 | + |
| 78 | + if result.action == "accept" and result.data: |
| 79 | + return f"User answered: {result.data.answer}" |
| 80 | + elif result.action == "decline": |
| 81 | + return "User declined to answer" |
| 82 | + else: |
| 83 | + return "User cancelled" |
| 84 | + |
| 85 | + # Create a custom handler that declines |
| 86 | + async def elicitation_callback(context, params): |
| 87 | + return ElicitResult(action="decline") |
| 88 | + |
| 89 | + async with create_connected_server_and_client_session( |
| 90 | + mcp._mcp_server, elicitation_callback=elicitation_callback |
| 91 | + ) as client_session: |
| 92 | + # Initialize |
| 93 | + await client_session.initialize() |
| 94 | + |
| 95 | + # Call the tool |
| 96 | + tool_result = await client_session.call_tool("ask_user", {"prompt": "What is your name?"}) |
| 97 | + |
| 98 | + # Verify the result |
| 99 | + assert len(tool_result.content) == 1 |
| 100 | + assert isinstance(tool_result.content[0], TextContent) |
| 101 | + assert tool_result.content[0].text == "User declined to answer" |
0 commit comments