-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathtest_elicitation_callback.py
More file actions
51 lines (39 loc) · 1.79 KB
/
test_elicitation_callback.py
File metadata and controls
51 lines (39 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from __future__ import annotations
import pytest
from pydantic import BaseModel, Field
from mcp import Client
from mcp.client.session import ClientSession
from mcp.server.mcpserver import Context, MCPServer
from mcp.shared._context import RequestContext
from mcp.types import ElicitRequestParams, ElicitResult, TextContent
class AnswerSchema(BaseModel):
answer: str = Field(description="The user's answer")
@pytest.mark.anyio
async def test_set_elicitation_callback():
server = MCPServer("test")
updated_answer = "Updated answer"
async def updated_callback(
context: RequestContext[ClientSession],
params: ElicitRequestParams,
) -> ElicitResult:
return ElicitResult(action="accept", content={"answer": updated_answer})
@server.tool("ask")
async def ask(prompt: str, ctx: Context) -> str:
result = await ctx.elicit(message=prompt, schema=AnswerSchema)
if result.action == "accept" and result.data:
return result.data.answer
return "no answer" # pragma: no cover
async with Client(server) as client:
# Before setting callback — default rejects with error
result = await client.call_tool("ask", {"prompt": "question?"})
assert result.is_error is True
# Set new callback — should succeed
client.session.set_elicitation_callback(updated_callback)
result = await client.call_tool("ask", {"prompt": "question?"})
assert result.is_error is False
assert isinstance(result.content[0], TextContent)
assert result.content[0].text == updated_answer
# Reset to None — back to default error
client.session.set_elicitation_callback(None)
result = await client.call_tool("ask", {"prompt": "question?"})
assert result.is_error is True