fastmcp/tests/cli/test_generate_cli_tool_names.py
Andrei Vince 2b71bdac5c
Fix generated CLIs for tools named app, console or Client (#6056)
Fix generated CLI tool functions shadowing script globals and builtins while retaining public command names.

Fixes #6055

Co-authored-by: andreivince <andreivince21@gmail.com>
2026-11-04 11:17:50 -06:00

38 lines
1.1 KiB
Python

"""Generated commands must work whatever their tools are called."""
from typing import Any
import pytest
from fastmcp import Client, FastMCP
from fastmcp.cli.generate import generate_cli_script
@pytest.mark.parametrize("name", ["app", "console", "Client"])
async def test_tool_named_like_a_script_global(name: str) -> None:
server = FastMCP("Desktop")
received: list[str] = []
@server.tool(name=name)
def record(text: str) -> str:
received.append(text)
return text
async with Client(server) as client:
tools = await client.list_tools()
script = generate_cli_script(
server_name="desktop",
server_spec="desktop",
transport_code=repr("http://unused/mcp"),
extra_imports=set(),
tools=tools,
)
namespace: dict[str, Any] = {"__name__": "generated_cli"}
exec(compile(script, "<generated>", "exec"), namespace)
namespace["CLIENT_SPEC"] = server
await namespace["app"].run_async(
["call-tool", name, "--text", "hi"], result_action="return_value"
)
assert received == ["hi"]