Skip to content

ctx.elicit() accepts values that were not offered when using dict or nested-list choices #5328

Description

@asasemahmed

What happened?

With the titled {"low": {...}} shorthand or the multi-select [["a", "b"]] shorthand, ctx.elicit() returns whatever the client sends as accepted data, even a value that wasn't offered or a string where a list is expected. The plain ["low", "high"] shorthand rejects such answers.

Example Code

import asyncio

from fastmcp import Client, Context, FastMCP

mcp = FastMCP()


@mcp.tool
async def pick(ctx: Context) -> str:
    single = await ctx.elicit(
        "Priority?",
        response_type={"low": {"title": "Low"}, "high": {"title": "High"}},
    )
    multi = await ctx.elicit("Tags?", response_type=[["bug", "feature"]])
    return f"{single.data!r} {multi.data!r}"


async def handler(message, response_type, params, context):
    if message == "Priority?":
        return {"value": "urgent"}
    return {"value": "not-a-list"}


async def main():
    async with Client(mcp, mode="legacy", elicitation_handler=handler) as client:
        result = await client.call_tool("pick", raise_on_error=False)
        print(result.content[0].text)


asyncio.run(main())

Output: 'urgent' 'not-a-list'

Version Information

FastMCP version:                                          4.0.11.dev11+89b00b7f
MCP version:                                                              2.0.0
Python version:                                                         3.12.14
Platform:                                             Windows-11-10.0.26200-SP0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working. Reports of errors, unexpected behavior, or broken functionality.serverRelated to FastMCP server implementation or server-side functionality.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions