mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-23 05:54:19 +02:00
Add SEP-1034 default values support for elicitation (#2545)
- Add comprehensive tests for default values in elicitation schemas - Document default values support in elicitation docs - Confirms FastMCP automatically supports defaults via Pydantic Closes #2544
This commit is contained in:
parent
7f8a010798
commit
acf4db0bf5
2 changed files with 167 additions and 1 deletions
|
|
@ -280,6 +280,41 @@ async def create_task(ctx: Context) -> str:
|
|||
return "Task creation cancelled"
|
||||
```
|
||||
|
||||
### Default Values
|
||||
|
||||
You can provide default values for elicitation fields using Pydantic's `Field(default=...)`. Clients will pre-populate form fields with these defaults, making it easier for users to provide input.
|
||||
|
||||
Default values are supported for all primitive types:
|
||||
- Strings: `Field(default="[email protected]")`
|
||||
- Integers: `Field(default=50)`
|
||||
- Numbers: `Field(default=3.14)`
|
||||
- Booleans: `Field(default=False)`
|
||||
- Enums: `Field(default=EnumValue.A)`
|
||||
|
||||
Fields with default values are automatically marked as optional (not included in the `required` list), so users can accept the default or provide their own value.
|
||||
|
||||
```python
|
||||
from pydantic import BaseModel, Field
|
||||
from enum import Enum
|
||||
|
||||
class Priority(Enum):
|
||||
LOW = "low"
|
||||
MEDIUM = "medium"
|
||||
HIGH = "high"
|
||||
|
||||
class TaskDetails(BaseModel):
|
||||
title: str = Field(description="Task title")
|
||||
description: str = Field(default="", description="Task description")
|
||||
priority: Priority = Field(default=Priority.MEDIUM, description="Task priority")
|
||||
|
||||
@mcp.tool
|
||||
async def create_task(ctx: Context) -> str:
|
||||
result = await ctx.elicit("Please provide task details", response_type=TaskDetails)
|
||||
if result.action == "accept":
|
||||
return f"Created: {result.data.title}"
|
||||
return "Task creation cancelled"
|
||||
```
|
||||
|
||||
## Multi-Turn Elicitation
|
||||
|
||||
Tools can make multiple elicitation calls to gather information progressively:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue