|
9 | 9 | import base64 |
10 | 10 | import json |
11 | 11 | import logging |
| 12 | +from typing import Literal |
12 | 13 |
|
13 | 14 | import click |
14 | 15 | from mcp.server.fastmcp import Context, FastMCP |
@@ -166,6 +167,38 @@ async def test_elicitation(message: str, ctx: Context[ServerSession, None]) -> s |
166 | 167 | return f"Elicitation not supported or error: {str(e)}" |
167 | 168 |
|
168 | 169 |
|
| 170 | +class SEP1034DefaultsSchema(BaseModel): |
| 171 | + """Schema for testing SEP-1034 elicitation with default values for all primitive types""" |
| 172 | + |
| 173 | + name: str = Field(default="John Doe", description="User name") |
| 174 | + age: int = Field(default=30, description="User age") |
| 175 | + score: float = Field(default=95.5, description="User score") |
| 176 | + status: str = Field( |
| 177 | + default="active", |
| 178 | + description="User status", |
| 179 | + json_schema_extra={"enum": ["active", "inactive", "pending"]}, |
| 180 | + ) |
| 181 | + verified: bool = Field(default=True, description="Verification status") |
| 182 | + |
| 183 | + |
| 184 | +@mcp.tool() |
| 185 | +async def test_elicitation_sep1034_defaults(ctx: Context[ServerSession, None]) -> str: |
| 186 | + """Tests elicitation with default values for all primitive types (SEP-1034)""" |
| 187 | + try: |
| 188 | + # Request user input with defaults for all primitive types |
| 189 | + result = await ctx.elicit(message="Please provide user information", schema=SEP1034DefaultsSchema) |
| 190 | + |
| 191 | + # Type-safe discriminated union narrowing using action field |
| 192 | + if result.action == "accept": |
| 193 | + content = result.data.model_dump_json() |
| 194 | + else: # decline or cancel |
| 195 | + content = "{}" |
| 196 | + |
| 197 | + return f"Elicitation result: action={result.action}, content={content}" |
| 198 | + except Exception as e: |
| 199 | + return f"Elicitation not supported or error: {str(e)}" |
| 200 | + |
| 201 | + |
169 | 202 | @mcp.tool() |
170 | 203 | def test_error_handling() -> str: |
171 | 204 | """Tests error response handling""" |
|
0 commit comments