|
30 | 30 | - [Prompts](#prompts) |
31 | 31 | - [Images](#images) |
32 | 32 | - [Context](#context) |
| 33 | + - [Elicitation](#elicitation) |
| 34 | + - [Authentication](#authentication) |
33 | 35 | - [Running Your Server](#running-your-server) |
34 | 36 | - [Development Mode](#development-mode) |
35 | 37 | - [Claude Desktop Integration](#claude-desktop-integration) |
@@ -310,6 +312,44 @@ async def long_task(files: list[str], ctx: Context) -> str: |
310 | 312 | return "Processing complete" |
311 | 313 | ``` |
312 | 314 |
|
| 315 | +### Elicitation |
| 316 | + |
| 317 | +Request additional information from users during tool execution: |
| 318 | + |
| 319 | +```python |
| 320 | +from mcp.server.fastmcp import FastMCP, Context |
| 321 | +from pydantic import BaseModel, Field |
| 322 | + |
| 323 | +mcp = FastMCP("Booking System") |
| 324 | + |
| 325 | + |
| 326 | +@mcp.tool() |
| 327 | +async def book_table(date: str, party_size: int, ctx: Context) -> str: |
| 328 | + """Book a table with confirmation""" |
| 329 | + |
| 330 | + class ConfirmBooking(BaseModel): |
| 331 | + confirm: bool = Field(description="Confirm booking?") |
| 332 | + notes: str = Field(default="", description="Special requests") |
| 333 | + |
| 334 | + result = await ctx.elicit( |
| 335 | + message=f"Confirm booking for {party_size} on {date}?", |
| 336 | + schema=ConfirmBooking |
| 337 | + ) |
| 338 | + |
| 339 | + if result.action == "accept" and result.data: |
| 340 | + if result.data.confirm: |
| 341 | + return f"Booked! Notes: {result.data.notes or 'None'}" |
| 342 | + return "Booking cancelled" |
| 343 | + |
| 344 | + # User declined or cancelled |
| 345 | + return f"Booking {result.action}" |
| 346 | +``` |
| 347 | + |
| 348 | +The `elicit()` method returns an `ElicitationResult` with: |
| 349 | +- `action`: "accept", "decline", or "cancel" |
| 350 | +- `data`: The validated response (only when accepted) |
| 351 | +- `validation_error`: Any validation error message |
| 352 | + |
313 | 353 | ### Authentication |
314 | 354 |
|
315 | 355 | Authentication can be used by servers that want to expose tools accessing protected resources. |
|
0 commit comments