mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-09 07:09:11 +02:00
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:
parent
8b285bf33e
commit
24e6a42f68
2 changed files with 6 additions and 5 deletions
|
|
@ -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},"
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue