-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathclient.py
More file actions
32 lines (23 loc) · 1.5 KB
/
Copy pathclient.py
File metadata and controls
32 lines (23 loc) · 1.5 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
"""Auto-answer form and URL elicitations and assert the tool result reflects them."""
import mcp_types as types
from mcp.client import Client, ClientRequestContext
from stories._harness import Target, run_client
async def on_elicit(context: ClientRequestContext, params: types.ElicitRequestParams) -> types.ElicitResult:
if isinstance(params, types.ElicitRequestURLParams):
# A real client would ask consent and open params.url in a browser, returning
# `accept` right away; the server's notifications/elicitation/complete arrives
# afterward (once the out-of-band flow finishes) for the client to correlate.
assert params.url.startswith("https://example.com/")
return types.ElicitResult(action="accept")
assert "username" in params.requested_schema["properties"]
return types.ElicitResult(action="accept", content={"username": "alice", "plan": "pro"})
async def main(target: Target, *, mode: str = "auto") -> None:
async with Client(target, mode=mode, elicitation_callback=on_elicit) as client:
registered = await client.call_tool("register_user", {})
assert isinstance(registered.content[0], types.TextContent)
assert registered.content[0].text == "registered alice (plan: pro)", registered
linked = await client.call_tool("link_account", {"provider": "github"})
assert isinstance(linked.content[0], types.TextContent)
assert linked.content[0].text == "linked github", linked
if __name__ == "__main__":
run_client(main)