60e0ffc959
Upgrade checks / Notify on failure (push) Has been cancelled
Upgrade checks / Close issue on success (push) Has been cancelled
Schema Crash Test / Real-world schema crash test (232K schemas) (push) Has been cancelled
Run static analysis / static_analysis (push) Has been cancelled
Tests / Tests: Python 3.10 on ubuntu-latest (push) Has been cancelled
Tests / Tests: Python 3.13 on ubuntu-latest (push) Has been cancelled
Tests / Tests: Python 3.10 on windows-latest (push) Has been cancelled
Tests / Tests with lowest-direct dependencies (push) Has been cancelled
Tests / MCP conformance tests (push) Has been cancelled
Tests / Integration tests (push) Has been cancelled
Tests / Package install smoke (push) Has been cancelled
Upgrade checks / Static analysis (push) Has been cancelled
Upgrade checks / Tests: Python 3.10 on ubuntu-latest (push) Has been cancelled
Upgrade checks / Tests: Python 3.13 on ubuntu-latest (push) Has been cancelled
Upgrade checks / Tests: Python 3.10 on windows-latest (push) Has been cancelled
Upgrade checks / Integration tests (push) Has been cancelled
Update MCPServerConfig Schema / update-config-schema (push) Has been cancelled
Update SDK Documentation / update-sdk-docs (push) Has been cancelled
48 lines
1.1 KiB
Python
48 lines
1.1 KiB
Python
"""
|
|
FastMCP Elicitation Example
|
|
|
|
Demonstrates tools that ask users for input during execution.
|
|
|
|
Try it with the CLI:
|
|
|
|
fastmcp list examples/elicitation.py
|
|
fastmcp call examples/elicitation.py greet
|
|
fastmcp call examples/elicitation.py survey
|
|
"""
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from fastmcp import Context, FastMCP
|
|
|
|
mcp = FastMCP("Elicitation Demo")
|
|
|
|
|
|
@mcp.tool
|
|
async def greet(ctx: Context) -> str:
|
|
"""Greet the user by name (asks for their name)."""
|
|
result = await ctx.elicit("What is your name?", response_type=str)
|
|
|
|
if result.action == "accept":
|
|
return f"Hello, {result.data}!"
|
|
return "Maybe next time!"
|
|
|
|
|
|
@mcp.tool
|
|
async def survey(ctx: Context) -> str:
|
|
"""Run a short survey collecting structured info."""
|
|
|
|
@dataclass
|
|
class SurveyResponse:
|
|
favorite_color: str
|
|
lucky_number: int
|
|
|
|
result = await ctx.elicit(
|
|
"Quick survey — tell us about yourself:",
|
|
response_type=SurveyResponse,
|
|
)
|
|
|
|
if result.action == "accept":
|
|
resp = result.data
|
|
return f"Got it — you like {resp.favorite_color} and your lucky number is {resp.lucky_number}."
|
|
return "Survey skipped."
|