1b74b06753
Cut comment and docstring volume roughly in half across src, tests, examples, and docs_src: removed comments that restate the adjacent code, leftover development narration, section banners, and self-evident Args/Returns blocks, and compressed the remaining docstrings to a Google-style summary line plus only the detail that earns its place. Kept (and tightened) the load-bearing content: Raises sections, deprecation and version-availability notes, spec/RFC/issue references, why-comments for non-obvious decisions, and all coverage pragmas. The generated mcp_types.v* wire modules are untouched.
39 lines
1.7 KiB
Python
39 lines
1.7 KiB
Python
"""Multi-round tool result (2026 era): a tool returns input_required and resumes from echoed state."""
|
|
|
|
from mcp_types import ElicitRequest, ElicitRequestedSchema, ElicitRequestFormParams, ElicitResult, InputRequiredResult
|
|
|
|
from mcp.server.mcpserver import Context, MCPServer
|
|
from stories._hosting import run_server_from_args
|
|
|
|
CONFIRM_SCHEMA: ElicitRequestedSchema = {
|
|
"type": "object",
|
|
"properties": {"confirm": {"type": "boolean", "description": "Proceed with the deployment?"}},
|
|
"required": ["confirm"],
|
|
}
|
|
|
|
|
|
def build_server() -> MCPServer:
|
|
mcp = MCPServer("mrtr-example")
|
|
|
|
@mcp.tool(description="Deploy to an environment, asking the user to confirm first.")
|
|
async def deploy(env: str, ctx: Context) -> str | InputRequiredResult:
|
|
responses = ctx.input_responses
|
|
if responses is None or "confirm" not in responses:
|
|
# First round: request_state is opaque to the client and carries the step name for the retry to verify.
|
|
ask = ElicitRequest(
|
|
params=ElicitRequestFormParams(message=f"Deploy to {env}?", requested_schema=CONFIRM_SCHEMA)
|
|
)
|
|
return InputRequiredResult(input_requests={"confirm": ask}, request_state="awaiting-confirm")
|
|
# Retry round: the client echoed request_state byte-exact and supplied the answer.
|
|
assert ctx.request_state == "awaiting-confirm", ctx.request_state
|
|
answer = responses["confirm"]
|
|
if isinstance(answer, ElicitResult) and answer.action == "accept" and (answer.content or {}).get("confirm"):
|
|
return f"deployed to {env}"
|
|
return f"deployment to {env} cancelled"
|
|
|
|
return mcp
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run_server_from_args(build_server)
|