Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748"""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.toolasync 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.toolasync 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."