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)
This commit is contained in:
Jeremiah Lowin 2026-02-03 14:15:15 -05:00
commit 24e6a42f68
No known key found for this signature in database
2 changed files with 6 additions and 5 deletions

View file

@ -98,10 +98,10 @@ def _schema_to_python_type(schema: dict[str, Any]) -> tuple[str, bool]:
def _format_schema_for_help(schema: dict[str, Any]) -> str:
"""Format a JSON schema for display in help text."""
import json
import pydantic_core
# Pretty print the schema, indented for help text
schema_str = json.dumps(schema, indent=2)
schema_str = pydantic_core.to_json(schema, indent=2).decode()
# Indent each line for help text alignment
lines = schema_str.split("\n")
indented = "\n ".join(lines)
@ -202,9 +202,9 @@ def _tool_function_source(tool: mcp.types.Tool) -> str:
if default is not None:
# For complex types with defaults, serialize to JSON string
if needs_json:
import json
import pydantic_core
default_str = json.dumps(default)
default_str = pydantic_core.to_json(default, fallback=str).decode()
annotation = f'Annotated[{py_type}, cyclopts.Parameter(help="{help_escaped}")]'
param_lines.append(
f" {safe_name}: {annotation} = {default_str!r},"

View file

@ -356,7 +356,8 @@ class TestToolFunctionSource:
)
source = _tool_function_source(tool)
# Default should be JSON string, not Python dict
assert '= \'{"timeout": 30, "retry": true}\'' in source
# pydantic_core.to_json produces compact JSON
assert '= \'{"timeout":30,"retry":true}\'' in source
# Should parse safely even with default
assert "isinstance(options, str)" in source
compile(source, "<test>", "exec")