|
| 1 | +"""Prove the refund amount is schema-hidden, resolvers memoize per call, and decline semantics differ per consumer.""" |
| 2 | + |
| 3 | +import mcp_types as types |
| 4 | + |
| 5 | +from mcp.client import Client, ClientRequestContext |
| 6 | +from stories._harness import Target, run_client |
| 7 | + |
| 8 | + |
| 9 | +async def main(target: Target, *, mode: str = "auto") -> None: |
| 10 | + # Scripted answers + per-topic counters; topics in `declines` are refused. |
| 11 | + counts = {"scope": 0, "restock": 0} |
| 12 | + answers: dict[str, dict[str, str | int | float | bool | list[str] | None]] = { |
| 13 | + "scope": {"full": True}, |
| 14 | + "restock": {"restock": True}, |
| 15 | + } |
| 16 | + declines: set[str] = set() |
| 17 | + |
| 18 | + async def on_elicit(context: ClientRequestContext, params: types.ElicitRequestParams) -> types.ElicitResult: |
| 19 | + assert isinstance(params, types.ElicitRequestFormParams) |
| 20 | + topic = "scope" if "full" in params.requested_schema["properties"] else "restock" |
| 21 | + counts[topic] += 1 |
| 22 | + if topic in declines: |
| 23 | + return types.ElicitResult(action="decline") |
| 24 | + return types.ElicitResult(action="accept", content=answers[topic]) |
| 25 | + |
| 26 | + async with Client(target, mode=mode, elicitation_callback=on_elicit) as client: |
| 27 | + # The model-facing contract is order_id + reason only; cents and restock are resolver-filled. |
| 28 | + listed = await client.list_tools() |
| 29 | + (tool,) = listed.tools |
| 30 | + assert set(tool.input_schema["properties"]) == {"order_id", "reason"}, tool.input_schema |
| 31 | + assert set(tool.input_schema.get("required", ())) == {"order_id", "reason"}, tool.input_schema |
| 32 | + |
| 33 | + # One digital line: scope auto-fills (full), restock auto-fills (False) — zero round-trips. |
| 34 | + receipt = await client.call_tool("refund_order", {"order_id": "ORD-7001", "reason": "download corrupted"}) |
| 35 | + assert receipt.structured_content == { |
| 36 | + "order_id": "ORD-7001", |
| 37 | + "refunded_cents": 1500, |
| 38 | + "restocked": False, |
| 39 | + "reason": "download corrupted", |
| 40 | + }, receipt.structured_content |
| 41 | + assert counts == {"scope": 0, "restock": 0}, counts |
| 42 | + |
| 43 | + # Full refund of a three-line order. The scope question fires exactly ONCE even though |
| 44 | + # both refund_amount and ask_restock consume it — memoized within the call. |
| 45 | + receipt = await client.call_tool("refund_order", {"order_id": "ORD-7002", "reason": "arrived broken"}) |
| 46 | + assert receipt.structured_content == { |
| 47 | + "order_id": "ORD-7002", |
| 48 | + "refunded_cents": 4800, |
| 49 | + "restocked": True, |
| 50 | + "reason": "arrived broken", |
| 51 | + }, receipt.structured_content |
| 52 | + assert counts == {"scope": 1, "restock": 1}, counts |
| 53 | + |
| 54 | + # Declining restock still refunds: the tool keeps the ElicitationResult union for |
| 55 | + # `restock`, sees the decline, and just skips the restock. The scope counter moves |
| 56 | + # again — the memo cache is per tools/call, not per connection. |
| 57 | + declines.add("restock") |
| 58 | + answers["scope"] = {"full": False, "sku": "canvas-tote"} |
| 59 | + receipt = await client.call_tool("refund_order", {"order_id": "ORD-7002", "reason": "wrong colour"}) |
| 60 | + assert receipt.structured_content == { |
| 61 | + "order_id": "ORD-7002", |
| 62 | + "refunded_cents": 2400, |
| 63 | + "restocked": False, |
| 64 | + "reason": "wrong colour", |
| 65 | + }, receipt.structured_content |
| 66 | + assert counts == {"scope": 2, "restock": 2}, counts |
| 67 | + declines.clear() |
| 68 | + |
| 69 | + # An elicited SKU is human-typed: the server validates it against the order before |
| 70 | + # any money is computed. |
| 71 | + answers["scope"] = {"full": False, "sku": "mystery-hat"} |
| 72 | + result = await client.call_tool("refund_order", {"order_id": "ORD-7002", "reason": "lost parcel"}) |
| 73 | + assert result.is_error, result |
| 74 | + assert isinstance(result.content[0], types.TextContent) |
| 75 | + assert "order has no item 'mystery-hat'" in result.content[0].text, result.content[0].text |
| 76 | + |
| 77 | + # Declining scope aborts the whole call: refund_amount and ask_restock both consume scope |
| 78 | + # unwrapped, so whichever resolves first (`cents`, in signature order) aborts, and |
| 79 | + # ask_restock never runs under any order. |
| 80 | + declines.add("scope") |
| 81 | + restock_before = counts["restock"] |
| 82 | + result = await client.call_tool("refund_order", {"order_id": "ORD-7002", "reason": "changed mind"}) |
| 83 | + assert result.is_error, result |
| 84 | + assert isinstance(result.content[0], types.TextContent) |
| 85 | + assert "Resolver for parameter 'scope' could not resolve: elicitation was decline" in result.content[0].text, ( |
| 86 | + result.content[0].text |
| 87 | + ) |
| 88 | + assert counts["restock"] == restock_before, counts |
| 89 | + declines.clear() |
| 90 | + |
| 91 | + # A ToolError raised inside a resolver surfaces exactly like one from the tool body. |
| 92 | + result = await client.call_tool("refund_order", {"order_id": "ORD-9999", "reason": "typo"}) |
| 93 | + assert result.is_error, result |
| 94 | + assert isinstance(result.content[0], types.TextContent) |
| 95 | + assert "unknown order 'ORD-9999'" in result.content[0].text, result.content[0].text |
| 96 | + |
| 97 | + # Full elicitation trajectory: scope fired in legs 2-5 (memoized within each call), |
| 98 | + # restock only in the two calls that reached it. |
| 99 | + assert counts == {"scope": 4, "restock": 2}, counts |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + run_client(main) |
0 commit comments