π The fast, Pythonic way to build MCP servers and clients.
Print install mcp-json and inspect JSON without Rich wrapping (#5615)
Asem ahmed527e2e7584cc
diff --git a/fastmcp_slim/fastmcp/cli/cli.py b/fastmcp_slim/fastmcp/cli/cli.pyindex fbc155927..233553747 100644--- a/fastmcp_slim/fastmcp/cli/cli.py+++ b/fastmcp_slim/fastmcp/cli/cli.py@@ -1010,8 +1010,9 @@ async def inspect(console.print(f" Server: [bold]{info.name}[/bold]")console.print(f" Format: {format.value}")else:- # Output JSON to stdout- console.print(formatted_json.decode("utf-8"))+ # Output JSON to stdout. The console would wrap long lines and+ # interpret [markup], corrupting the JSON.+ print(formatted_json.decode("utf-8"))except Exception as e:logger.exception(diff --git a/fastmcp_slim/fastmcp/cli/install/mcp_json.py b/fastmcp_slim/fastmcp/cli/install/mcp_json.pyindex ed2ac7382..b25fd434c 100644--- a/fastmcp_slim/fastmcp/cli/install/mcp_json.py+++ b/fastmcp_slim/fastmcp/cli/install/mcp_json.py@@ -1,5 +1,6 @@"""MCP configuration JSON generation for FastMCP install using Cyclopts."""+import builtinsimport jsonimport sysfrom pathlib import Path@@ -85,8 +86,9 @@ def install_mcp_json(pyperclip.copy(json_output)print(f"[green]MCP configuration for '{name}' copied to clipboard[/green]")else:- # Print to stdout (for piping)- print(json_output)+ # Print to stdout (for piping). Rich's print would wrap long lines+ # and interpret [markup], corrupting the JSON.+ builtins.print(json_output)return Truediff --git a/tests/cli/test_cli.py b/tests/cli/test_cli.pyindex 046b6eb9b..b205698ca 100644--- a/tests/cli/test_cli.py+++ b/tests/cli/test_cli.py@@ -1,3 +1,4 @@+import jsonimport subprocessfrom pathlib import Pathfrom unittest.mock import Mock, patch@@ -650,3 +651,29 @@ def test_prompt(name: str) -> str:assert data["server"]["name"] == "InspectTestServer"assert len(data["tools"]) == 1assert len(data["prompts"]) == 1++ async def test_inspect_command_stdout_is_valid_json(self, tmp_path, capsys):+ """Long descriptions and bracketed text must reach stdout unchanged."""+ server_file = tmp_path / "test_server.py"+ server_file.write_text("""+import fastmcp++mcp = fastmcp.FastMCP("InspectTestServer")++def test_tool(x: int) -> int:+ \"\"\"Multiply a number by two and return the product, as plain text [bold]with markup[/bold] inside.\"\"\"+ return x * 2+""")++ command, bound, _ = app.parse_args(+ ["inspect", str(server_file), "--format", "fastmcp"]+ )++ await command(**bound.arguments)++ data = json.loads(capsys.readouterr().out)+ assert data["tools"][0]["description"] == (+ "Multiply a number by two and return the product, as plain text "+ "[bold]with markup[/bold] inside."+ )diff --git a/tests/cli/test_install.py b/tests/cli/test_install.pyindex 29a497a1d..094491f4c 100644--- a/tests/cli/test_install.py+++ b/tests/cli/test_install.py@@ -1,8 +1,10 @@+import jsonfrom pathlib import Pathimport pytestfrom fastmcp.cli.install import install_app+from fastmcp.cli.install.mcp_json import install_mcp_jsonfrom fastmcp.cli.install.shared import validate_server_namefrom fastmcp.cli.install.stdio import install_stdio@@ -314,6 +316,28 @@ class TestMcpJsonInstall:assert bound.arguments["copy"] is True+ def test_install_mcp_json_stdout_is_valid_json(+ self, tmp_path: Path, capsys: pytest.CaptureFixture[str]+ ):+ """Long paths and bracketed values must reach stdout unchanged."""+ server_dir = tmp_path / "a-long-directory-name" / "another-long-directory-name"+ server_dir.mkdir(parents=True)+ server_file = server_dir / "server.py"+ server_file.write_text("# placeholder")++ result = install_mcp_json(+ file=server_file,+ server_object=None,+ name="test-server",+ env_vars={"PATTERN": "[red]value[/red]"},+ )++ assert result is True+ config = json.loads(capsys.readouterr().out)+ server_config = config["test-server"]+ assert server_config["args"][-1] == str(server_file.resolve())+ assert server_config["env"] == {"PATTERN": "[red]value[/red]"}+class TestStdioInstall:"""Test stdio install command."""