|
7 | 7 | from autogen_core.tools import BaseTool |
8 | 8 | from autogen_core.utils import schema_to_pydantic_model |
9 | 9 | from mcp import ClientSession, Tool |
10 | | -from mcp.types import EmbeddedResource, ImageContent, TextContent |
| 10 | +from mcp.types import AnyUrl, EmbeddedResource, ImageContent, TextContent |
11 | 11 | from pydantic import BaseModel |
| 12 | +import json |
12 | 13 |
|
13 | 14 | from ._config import McpServerParams |
14 | 15 | from ._session import create_mcp_server_session |
@@ -116,16 +117,23 @@ async def from_server_params(cls, server_params: TServerParams, tool_name: str) |
116 | 117 |
|
117 | 118 | return cls(server_params=server_params, tool=matching_tool) |
118 | 119 |
|
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]) |
129 | 137 |
|
130 | 138 | def _format_errors(self, error: Exception) -> str: |
131 | 139 | """Recursively format errors into a string.""" |
|
0 commit comments