-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathserver.py
More file actions
47 lines (36 loc) · 1.91 KB
/
Copy pathserver.py
File metadata and controls
47 lines (36 loc) · 1.91 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
"""Elicitation (handshake-era push style): a tool blocks on user input mid-call."""
from pydantic import BaseModel
from mcp.server.elicitation import AcceptedElicitation
from mcp.server.mcpserver import Context, MCPServer
from stories._hosting import run_server_from_args
class Registration(BaseModel):
username: str
plan: str | None = None
def build_server() -> MCPServer:
mcp = MCPServer("legacy-elicitation-example")
@mcp.tool(description="Register a new account by asking the user for their details.")
async def register_user(ctx: Context) -> str:
answer = await ctx.elicit("Please provide your registration details:", Registration)
if not isinstance(answer, AcceptedElicitation):
return f"registration {answer.action}"
return f"registered {answer.data.username} (plan: {answer.data.plan or 'free'})"
@mcp.tool(description="Link a third-party account by directing the user to a sign-in URL.")
async def link_account(provider: str, ctx: Context) -> str:
# elicitation_id must be unique per elicitation, not per provider — scope it to this request.
elicitation_id = f"link-{provider}-{ctx.request_context.request_id}"
answer = await ctx.elicit_url(
f"Sign in to {provider} to link your account",
url=f"https://example.com/oauth/{provider}/authorize",
elicitation_id=elicitation_id,
)
if answer.action != "accept":
return f"link {answer.action}"
# Out-of-band flow finished: tell the client which elicitation completed.
# The 2-hop `ctx.request_context.*` reach is interim; a later release shortens it.
await ctx.request_context.session.send_elicit_complete(
elicitation_id, related_request_id=ctx.request_context.request_id
)
return f"linked {provider}"
return mcp
if __name__ == "__main__":
run_server_from_args(build_server)