mirror of
https://github.com/aljazceru/mcp-python-sdk.git
synced 2025-12-19 06:54:18 +01:00
replace inefficient use of to_jsonable_python (#545)
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
"""Base classes for FastMCP prompts."""
|
||||
|
||||
import inspect
|
||||
import json
|
||||
from collections.abc import Awaitable, Callable, Sequence
|
||||
from typing import Any, Literal
|
||||
|
||||
@@ -155,7 +154,9 @@ class Prompt(BaseModel):
|
||||
content = TextContent(type="text", text=msg)
|
||||
messages.append(UserMessage(content=content))
|
||||
else:
|
||||
content = json.dumps(pydantic_core.to_jsonable_python(msg))
|
||||
content = pydantic_core.to_json(
|
||||
msg, fallback=str, indent=2
|
||||
).decode()
|
||||
messages.append(Message(role="user", content=content))
|
||||
except Exception:
|
||||
raise ValueError(
|
||||
|
||||
@@ -9,7 +9,7 @@ from typing import Any
|
||||
import anyio
|
||||
import anyio.to_thread
|
||||
import httpx
|
||||
import pydantic.json
|
||||
import pydantic
|
||||
import pydantic_core
|
||||
from pydantic import Field, ValidationInfo
|
||||
|
||||
@@ -59,15 +59,12 @@ class FunctionResource(Resource):
|
||||
)
|
||||
if isinstance(result, Resource):
|
||||
return await result.read()
|
||||
if isinstance(result, bytes):
|
||||
elif isinstance(result, bytes):
|
||||
return result
|
||||
if isinstance(result, str):
|
||||
elif isinstance(result, str):
|
||||
return result
|
||||
try:
|
||||
return json.dumps(pydantic_core.to_jsonable_python(result))
|
||||
except (TypeError, pydantic_core.PydanticSerializationError):
|
||||
# If JSON serialization fails, try str()
|
||||
return str(result)
|
||||
else:
|
||||
return pydantic_core.to_json(result, fallback=str, indent=2).decode()
|
||||
except Exception as e:
|
||||
raise ValueError(f"Error reading resource {self.uri}: {e}")
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
from __future__ import annotations as _annotations
|
||||
|
||||
import inspect
|
||||
import json
|
||||
import re
|
||||
from collections.abc import AsyncIterator, Callable, Iterable, Sequence
|
||||
from contextlib import (
|
||||
@@ -551,10 +550,7 @@ def _convert_to_content(
|
||||
return list(chain.from_iterable(_convert_to_content(item) for item in result)) # type: ignore[reportUnknownVariableType]
|
||||
|
||||
if not isinstance(result, str):
|
||||
try:
|
||||
result = json.dumps(pydantic_core.to_jsonable_python(result))
|
||||
except Exception:
|
||||
result = str(result)
|
||||
result = pydantic_core.to_json(result, fallback=str, indent=2).decode()
|
||||
|
||||
return [TextContent(type="text", text=result)]
|
||||
|
||||
|
||||
@@ -100,7 +100,7 @@ class TestFunctionResource:
|
||||
fn=lambda: MyModel(name="test"),
|
||||
)
|
||||
content = await resource.read()
|
||||
assert content == '{"name": "test"}'
|
||||
assert content == '{\n "name": "test"\n}'
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_custom_type_conversion(self):
|
||||
|
||||
@@ -185,4 +185,4 @@ class TestResourceTemplate:
|
||||
|
||||
assert isinstance(resource, FunctionResource)
|
||||
content = await resource.read()
|
||||
assert content == "hello"
|
||||
assert content == '"hello"'
|
||||
|
||||
Reference in New Issue
Block a user