fastmcp/tests/cli
Jeremiah Lowin 4262cfc16a
Add fastmcp generate-cli command (#3065)
* Add `fastmcp generate-cli` command

Connects to any MCP server, reads its tool/resource/prompt schemas,
and writes a standalone Python CLI script with typed subcommands.

* docs: add generate-cli documentation

* docs: add generate-cli documentation; skip Windows executable test

* fix: address PR review feedback

- Sanitize tool and parameter names to valid Python identifiers
- Replace bare except Exception with specific exception types
- Escape server name in generated string literals
- Handle trailing colon edge case in _derive_server_name
- Clarify in docs that generated CLI is a client, not a bundled server

* Fix string escaping issues in generate-cli

- Use single-quoted docstrings to avoid triple-quote escaping issues
- Escape quotes in app_name derived from server_name
- Add tests for descriptions with quotes and server names with quotes

Addresses CodeRabbit review comments about insufficient escaping.

* Implement smart parameter handling for generate-cli

- Simple types (str, int, float, bool): Direct typed flags
- Arrays of simple types (list[str], list[int]): Repeatable flags via cyclopts
- Complex types (objects, nested arrays): Accept JSON strings with parsing
- JSON schema shown in help text for complex parameters
- Proper escaping of newlines and quotes in help text
- Filter out None and empty list defaults when calling tools

This gives typed, discoverable CLIs for common cases while handling
complex schemas via JSON input.

* Update generate-cli docs to explain smart parameter handling

- Document simple types as direct typed flags
- Document arrays of simple types as repeatable flags
- Document complex types as JSON strings with schema in help
- Add examples showing all three patterns

* Fix Codex review issues in generate-cli

High priority fixes:
- Complex type defaults: Serialize dict/list defaults to JSON strings
- List params: Preserve help metadata with Annotated wrapper
- Name collisions: Detect and error on sanitized name conflicts
- JSON parsing: Use isinstance check for safety with defaults

Added tests for:
- Complex types with default values
- Parameter name collision detection
- Updated existing tests to match new format

* Use pydantic_core.to_json for consistency

- Generator now uses pydantic_core.to_json() instead of json.dumps()
- Consistent with rest of fastmcp codebase
- Generated CLI still uses plain json module (standalone script)

* Move local imports to module level in generate-cli

* Handle union item types and Python keyword collisions in generate-cli
2026-02-03 21:08:51 -05:00
..
__init__.py Add tests 2025-07-06 20:50:31 -04:00
test_cli.py Mock network call in test_version_command_execution 2026-01-31 22:35:43 +00:00
test_client_commands.py Add fastmcp list and fastmcp call CLI commands (#3054) 2026-02-01 18:30:14 -05:00
test_config.py Update classes to inherit from FastMCPBaseModel instead of BaseModel (#2739) 2025-12-26 08:33:23 -05:00
test_cursor.py feat: add fastmcp install goose command (#3040) 2026-01-31 10:53:24 -05:00
test_discovery.py Add fastmcp discover and name-based server resolution (#3055) 2026-02-01 21:27:22 -05:00
test_generate_cli.py Add fastmcp generate-cli command (#3065) 2026-02-03 21:08:51 -05:00
test_goose.py feat: add fastmcp install goose command (#3040) 2026-01-31 10:53:24 -05:00
test_install.py feat: add fastmcp install goose command (#3040) 2026-01-31 10:53:24 -05:00
test_mcp_server_config_integration.py Add loq file size limits and clean up type ignores (#2859) 2026-01-13 07:29:12 -05:00
test_mcp_server_config_schema.py Hotfix: restore --transport streamable http at CLI (#1743) 2025-09-03 17:24:22 -04:00
test_project_prepare.py Enable more type checking rules (#1775) 2025-09-07 11:11:44 -04:00
test_run.py feat: expand --reload to watch frontend file types (#3028) 2026-01-29 09:23:55 -05:00
test_run_config.py Add log level support for stdio and HTTP transports (#1840) 2025-09-19 13:46:30 -04:00
test_server_args.py Unify discovery API: deduplicate at protocol layer only (#2919) 2026-01-18 21:01:53 -05:00
test_shared.py Ensure test is platform agnostic 2025-07-07 09:51:12 -04:00
test_tasks.py Replace type: ignore[attr-defined] with isinstance assertions in tests (#2665) 2025-12-21 16:37:20 -05:00
test_with_argv.py Consolidate server loading logic into FileSystemSource (#1614) 2025-08-24 20:50:25 -04:00