|
31 | 31 | - [Images](#images) |
32 | 32 | - [Context](#context) |
33 | 33 | - [Completions](#completions) |
| 34 | + - [Elicitation](#elicitation) |
| 35 | + - [Authentication](#authentication) |
34 | 36 | - [Running Your Server](#running-your-server) |
35 | 37 | - [Development Mode](#development-mode) |
36 | 38 | - [Claude Desktop Integration](#claude-desktop-integration) |
@@ -74,7 +76,7 @@ The Model Context Protocol allows applications to provide context for LLMs in a |
74 | 76 |
|
75 | 77 | ### Adding MCP to your python project |
76 | 78 |
|
77 | | -We recommend using [uv](https://docs.astral.sh/uv/) to manage your Python projects. |
| 79 | +We recommend using [uv](https://docs.astral.sh/uv/) to manage your Python projects. |
78 | 80 |
|
79 | 81 | If you haven't created a uv-managed project yet, create one: |
80 | 82 |
|
@@ -372,6 +374,43 @@ async def handle_completion( |
372 | 374 | return Completion(values=filtered) |
373 | 375 | return None |
374 | 376 | ``` |
| 377 | +### Elicitation |
| 378 | + |
| 379 | +Request additional information from users during tool execution: |
| 380 | + |
| 381 | +```python |
| 382 | +from mcp.server.fastmcp import FastMCP, Context |
| 383 | +from pydantic import BaseModel, Field |
| 384 | + |
| 385 | +mcp = FastMCP("Booking System") |
| 386 | + |
| 387 | + |
| 388 | +@mcp.tool() |
| 389 | +async def book_table(date: str, party_size: int, ctx: Context) -> str: |
| 390 | + """Book a table with confirmation""" |
| 391 | + |
| 392 | + class ConfirmBooking(BaseModel): |
| 393 | + confirm: bool = Field(description="Confirm booking?") |
| 394 | + notes: str = Field(default="", description="Special requests") |
| 395 | + |
| 396 | + result = await ctx.elicit( |
| 397 | + message=f"Confirm booking for {party_size} on {date}?", |
| 398 | + schema=ConfirmBooking |
| 399 | + ) |
| 400 | + |
| 401 | + if result.action == "accept" and result.data: |
| 402 | + if result.data.confirm: |
| 403 | + return f"Booked! Notes: {result.data.notes or 'None'}" |
| 404 | + return "Booking cancelled" |
| 405 | + |
| 406 | + # User declined or cancelled |
| 407 | + return f"Booking {result.action}" |
| 408 | +``` |
| 409 | + |
| 410 | +The `elicit()` method returns an `ElicitationResult` with: |
| 411 | +- `action`: "accept", "decline", or "cancel" |
| 412 | +- `data`: The validated response (only when accepted) |
| 413 | +- `validation_error`: Any validation error message |
375 | 414 |
|
376 | 415 | ### Authentication |
377 | 416 |
|
|
0 commit comments