Skip to content

Commit 35df181

Browse files
committed
fix: fix serialize case by case since EmbededResource has uri which is not JSON serialized
1 parent 41f4960 commit 35df181

2 files changed

Lines changed: 37 additions & 22 deletions

File tree

python/packages/autogen-ext/src/autogen_ext/tools/mcp/_base.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
from autogen_core.tools import BaseTool
88
from autogen_core.utils import schema_to_pydantic_model
99
from mcp import ClientSession, Tool
10-
from mcp.types import EmbeddedResource, ImageContent, TextContent
10+
from mcp.types import AnyUrl, EmbeddedResource, ImageContent, TextContent
1111
from pydantic import BaseModel
12+
import json
1213

1314
from ._config import McpServerParams
1415
from ._session import create_mcp_server_session
@@ -116,16 +117,23 @@ async def from_server_params(cls, server_params: TServerParams, tool_name: str)
116117

117118
return cls(server_params=server_params, tool=matching_tool)
118119

119-
def return_value_as_string(self, value: Any) -> str:
120-
if isinstance(value, list):
121-
valid_types = (TextContent, ImageContent, EmbeddedResource)
122-
return list(
123-
map(
124-
lambda t: (t.model_dump_json() if isinstance(t, valid_types) else str(t)),
125-
value,
126-
)
127-
)
128-
return str(value)
120+
def return_value_as_string(self, value: list) -> str:
121+
"""Return a string representation of the result."""
122+
123+
def serialize_item(item):
124+
if isinstance(item, (TextContent, ImageContent)):
125+
return item.model_dump()
126+
elif isinstance(item, EmbeddedResource):
127+
type = item.type
128+
resource = {}
129+
for key, val in item.resource.model_dump().items():
130+
if isinstance(val, AnyUrl):
131+
resource[key] = str(val)
132+
annotations = item.annotations.model_dump() if item.annotations else None
133+
return {"type": type, "resource": resource, "annotations": annotations}
134+
else:
135+
return str(item)
136+
return json.dumps([serialize_item(item) for item in value])
129137

130138
def _format_errors(self, error: Exception) -> str:
131139
"""Recursively format errors into a string."""

python/packages/autogen-ext/tests/tools/test_mcp_tools.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@
1616
mcp_server_tools,
1717
)
1818
from mcp import ClientSession, Tool
19-
from mcp.types import EmbeddedResource, ImageContent, TextContent, TextResourceContents
19+
from mcp.types import (
20+
AnyUrl,
21+
Annotations,
22+
EmbeddedResource,
23+
ImageContent,
24+
TextContent,
25+
TextResourceContents,
26+
)
2027

2128

2229
@pytest.fixture
@@ -202,29 +209,29 @@ async def test_adapter_from_server_params_with_return_value_as_string(
202209

203210
adapter = await StdioMcpToolAdapter.from_server_params(sample_server_params, "test_tool")
204211

205-
assert (
206-
adapter.return_value_as_string(
212+
assert adapter.return_value_as_string(
207213
[
208-
TextContent(text="this is a sample text", type="text"),
214+
TextContent(
215+
text="this is a sample text",
216+
type="text",
217+
annotations=Annotations(audience=["user", "assistant"], priority=0.7),
218+
),
209219
ImageContent(
210220
data="this is a sample base64 encoded image",
211221
mimeType="image/png",
212222
type="image",
223+
annotations=None,
213224
),
214225
EmbeddedResource(
215226
type="resource",
216227
resource=TextResourceContents(
217228
text="this is a sample text",
218-
uri="http://example.com/test",
229+
uri=AnyUrl(url="http://example.com/test"),
219230
),
231+
annotations=Annotations(audience=["user"], priority=0.3),
220232
),
221233
]
222-
)
223-
) == [
224-
'{"type":"text","text":"this is a sample text","annotations":null}',
225-
'{"type":"image","data":"this is a sample base64 encoded image","mimeType":"image/png","annotations":null}',
226-
'{"type":"resource","resource":{"uri":"http://example.com/test","mimeType":null,"text":"this is a sample text"},"annotations":null}',
227-
]
234+
) == '[{"type": "text", "text": "this is a sample text", "annotations": {"audience": ["user", "assistant"], "priority": 0.7}}, {"type": "image", "data": "this is a sample base64 encoded image", "mimeType": "image/png", "annotations": null}, {"type": "resource", "resource": {"uri": "http://example.com/test"}, "annotations": {"audience": ["user"], "priority": 0.3}}]'
228235

229236

230237
@pytest.mark.asyncio

0 commit comments

Comments
 (0)