Skip to content

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

Escape server text in the fastmcp call elicitation prompt (#5909)

Asem ahmed358e50dadf10
diff --git a/fastmcp_slim/fastmcp/cli/client.py b/fastmcp_slim/fastmcp/cli/client.py
index 2d1c3c929..9a08f041a 100644
--- a/fastmcp_slim/fastmcp/cli/client.py
+++ b/fastmcp_slim/fastmcp/cli/client.py
@@ -208,7 +208,9 @@ async def _terminal_elicitation_handler(
"""
from mcp_types import ElicitRequestFormParams
- console.print(f"\n[bold yellow]Server asks:[/bold yellow] {message}")
+ console.print(
+ f"\n[bold yellow]Server asks:[/bold yellow] {_sanitize_untrusted_text(message)}"
+ )
if not isinstance(params, ElicitRequestFormParams):
answer = console.input(
@@ -236,9 +238,12 @@ async def _terminal_elicitation_handler(
result: dict[str, Any] = {}
for field_name, field_schema in properties.items():
- type_hint = field_schema.get("type", "string")
+ type_hint = _sanitize_untrusted_text(str(field_schema.get("type", "string")))
req_marker = " [red]*[/red]" if field_name in required else ""
- prompt_text = f" [cyan]{field_name}[/cyan] ({type_hint}){req_marker}: "
+ prompt_text = (
+ f" [cyan]{_sanitize_untrusted_text(field_name)}[/cyan] "
+ f"({type_hint}){req_marker}: "
+ )
raw = console.input(prompt_text)
if raw.strip().lower() == "decline":
diff --git a/tests/cli/test_client_commands.py b/tests/cli/test_client_commands.py
index d76bc8296..88ff48abe 100644
--- a/tests/cli/test_client_commands.py
+++ b/tests/cli/test_client_commands.py
@@ -20,6 +20,7 @@ from fastmcp.cli.client import (
_format_call_result_text,
_is_http_target,
_sanitize_untrusted_text,
+ _terminal_elicitation_handler,
call_command,
coerce_value,
format_tool_signature,
@@ -851,3 +852,47 @@ class TestSanitizeUntrustedText:
value = "[bold]hello[/bold]\x07"
sanitized = _sanitize_untrusted_text(value)
assert sanitized == "\\[bold]hello\\[/bold]\\x07"
+
+
+class TestTerminalElicitationHandler:
+ @pytest.fixture(autouse=True)
+ def _press_enter(self, monkeypatch: pytest.MonkeyPatch):
+ monkeypatch.setattr("builtins.input", lambda *args: "")
+
+ @staticmethod
+ def _params(message: str, field: dict[str, Any], name: str = "answer"):
+ return mcp_types.ElicitRequestFormParams(
+ message=message,
+ requested_schema={"type": "object", "properties": {name: field}},
+ )
+
+ @pytest.mark.parametrize(
+ "message",
+ [
+ "Deploy to production? [y/N]",
+ "Branch to deploy [main]",
+ "Tag format is v[/]MAJOR",
+ ],
+ )
+ async def test_message_is_printed_verbatim(
+ self, message: str, capsys: pytest.CaptureFixture[str]
+ ):
+ result = await _terminal_elicitation_handler(
+ message, None, self._params(message, {"type": "string"}), None
+ )
+
+ assert result.action == "accept"
+ assert f"Server asks: {message}" in capsys.readouterr().out
+
+ @pytest.mark.parametrize(
+ ("field_name", "field_type"),
+ [("name[bold]", "string"), ("tag", "string[/]")],
+ )
+ async def test_field_prompt_is_printed_verbatim(
+ self, field_name: str, field_type: str, capsys: pytest.CaptureFixture[str]
+ ):
+ params = self._params("Pick one", {"type": field_type}, name=field_name)
+
+ await _terminal_elicitation_handler("Pick one", None, params, None)
+
+ assert f" {field_name} ({field_type}): " in capsys.readouterr().out