Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

Honor the SDK ElicitResult returned by an elicitation handler (#5339)

Asem ahmed00a2fba4ccb3
diff --git a/fastmcp_slim/fastmcp/client/elicitation.py b/fastmcp_slim/fastmcp/client/elicitation.py
index 2b9ce5db4..6440eeb72 100644
--- a/fastmcp_slim/fastmcp/client/elicitation.py
+++ b/fastmcp_slim/fastmcp/client/elicitation.py
@@ -62,8 +62,9 @@ def create_elicitation_callback(
params,
context, # ty: ignore[invalid-argument-type]
)
- # if the user returns data, we assume they've accepted the elicitation
- if not isinstance(result, ElicitResult):
+ # if the user returns data, we assume they've accepted the elicitation.
+ # An ElicitResult, including the SDK's own, carries an explicit action.
+ if not isinstance(result, MCPElicitResult):
result = ElicitResult(action="accept", content=result)
content = to_jsonable_python(result.content)
if not isinstance(content, dict | None):
diff --git a/tests/client/client/test_input_required_driver.py b/tests/client/client/test_input_required_driver.py
index e96197d58..cafea72d7 100644
--- a/tests/client/client/test_input_required_driver.py
+++ b/tests/client/client/test_input_required_driver.py
@@ -17,8 +17,9 @@ exercise the client-side *answering* path, which the spec's decisive finding say
is era-neutral by construction once the driver is wired.
"""
-from typing import Annotated, cast
+from typing import Annotated, Literal, cast
+import mcp_types
import pytest
from mcp.client._input_required import (
DEFAULT_INPUT_REQUIRED_MAX_ROUNDS,
@@ -133,6 +134,39 @@ class TestCallToolMRTR:
assert result.data.result == "Hello, Bob!"
assert result.structured_content == {"result": "Hello, Bob!"}
+ @pytest.mark.parametrize("action", ["decline", "cancel"])
+ @pytest.mark.parametrize("result_type", [ElicitResult, mcp_types.ElicitResult])
+ async def test_declined_elicitation_is_not_an_accepted_answer(
+ self, action: Literal["decline", "cancel"], result_type: type
+ ):
+ """A handler's decline or cancel reaches the resolver as a refusal, whether
+ it returns FastMCP's `ElicitResult` or the SDK's own."""
+
+ async def handler(message, response_type, params, ctx):
+ return result_type(action=action)
+
+ async with Client(
+ _mrtr_server(), mode="auto", elicitation_handler=handler
+ ) as client:
+ result = await client.call_tool("greet", {}, raise_on_error=False)
+
+ assert result.is_error is True
+ assert isinstance(result.content[0], mcp_types.TextContent)
+ assert "received an accepted elicitation" not in result.content[0].text
+
+ async def test_sdk_elicit_result_accept_carries_its_content(self):
+ """An accepted SDK `ElicitResult` supplies its content to the resolver."""
+
+ async def handler(message, response_type, params, ctx):
+ return mcp_types.ElicitResult(action="accept", content={"name": "Ada"})
+
+ async with Client(
+ _mrtr_server(), mode="auto", elicitation_handler=handler
+ ) as client:
+ result = await client.call_tool("greet", {})
+
+ assert result.data.result == "Hello, Ada!"
+
async def test_multi_round_dependent_resolvers(self):
"""A resolver depending on another's answer is asked in a later round; the
driver loops until the dependent chain terminates."""
diff --git a/tests/client/test_elicitation.py b/tests/client/test_elicitation.py
index 87c89e3a1..dd0c57c87 100644
--- a/tests/client/test_elicitation.py
+++ b/tests/client/test_elicitation.py
@@ -2,6 +2,7 @@ from dataclasses import asdict, dataclass
from enum import Enum
from typing import Any, Literal, cast
+import mcp_types
import pytest
from pydantic import BaseModel
from typing_extensions import TypedDict
@@ -82,6 +83,32 @@ async def test_elicitation_decline(fastmcp_server):
assert result.data == "No name provided."
+ "response, expected",
+ [
+ (mcp_types.ElicitResult(action="decline"), "No name provided."),
+ (mcp_types.ElicitResult(action="cancel"), "No name provided."),
+ (
+ mcp_types.ElicitResult(action="accept", content={"name": "Alice"}),
+ "Hello, Alice!",
+ ),
+ ],
+)
+async def test_elicitation_handler_returns_sdk_elicit_result(
+ fastmcp_server, response: mcp_types.ElicitResult, expected: str
+):
+ """The SDK's ElicitResult keeps its action instead of becoming accepted content."""
+
+ async def elicitation_handler(message, response_type, params, ctx):
+ return response
+
+ async with Client(
+ fastmcp_server, mode="legacy", elicitation_handler=elicitation_handler
+ ) as client:
+ result = await client.call_tool("ask_for_name")
+ assert result.data == expected
+
+
async def test_elicitation_handler_parameters():
"""Test that elicitation handler receives correct parameters."""
mcp = FastMCP("TestServer")