Spaces:
Running
Running
Jeremiah Lowin commited on
Commit ·
3abe601
1
Parent(s): b1ed0e1
Make content method a function
Browse files- src/fastmcp/server.py +41 -46
src/fastmcp/server.py
CHANGED
|
@@ -111,7 +111,7 @@ class FastMCP:
|
|
| 111 |
"""Call a tool by name with arguments."""
|
| 112 |
try:
|
| 113 |
result = await self._tool_manager.call_tool(name, arguments)
|
| 114 |
-
return
|
| 115 |
except Exception as e:
|
| 116 |
logger.error(f"Error calling tool {name}: {e}")
|
| 117 |
return [
|
|
@@ -148,51 +148,6 @@ class FastMCP:
|
|
| 148 |
logger.error(f"Error reading resource {uri}: {e}")
|
| 149 |
raise ResourceError(str(e))
|
| 150 |
|
| 151 |
-
def _convert_to_content(
|
| 152 |
-
self, value: Any
|
| 153 |
-
) -> Sequence[Union[TextContent, ImageContent]]:
|
| 154 |
-
"""Convert a tool result to MCP content types."""
|
| 155 |
-
|
| 156 |
-
# Already a sequence of valid content types
|
| 157 |
-
if isinstance(value, (list, tuple)):
|
| 158 |
-
if all(isinstance(x, (TextContent, ImageContent)) for x in value):
|
| 159 |
-
return value
|
| 160 |
-
# Handle mixed content including Image objects
|
| 161 |
-
result = []
|
| 162 |
-
for item in value:
|
| 163 |
-
if isinstance(item, (TextContent, ImageContent)):
|
| 164 |
-
result.append(item)
|
| 165 |
-
elif isinstance(item, Image):
|
| 166 |
-
result.append(item.to_image_content())
|
| 167 |
-
else:
|
| 168 |
-
result.append(
|
| 169 |
-
TextContent(
|
| 170 |
-
type="text",
|
| 171 |
-
text=json.dumps(
|
| 172 |
-
item, indent=2, default=pydantic.json.pydantic_encoder
|
| 173 |
-
),
|
| 174 |
-
)
|
| 175 |
-
)
|
| 176 |
-
return result
|
| 177 |
-
|
| 178 |
-
# Single content type
|
| 179 |
-
if isinstance(value, (TextContent, ImageContent)):
|
| 180 |
-
return [value]
|
| 181 |
-
|
| 182 |
-
# Image helper
|
| 183 |
-
if isinstance(value, Image):
|
| 184 |
-
return [value.to_image_content()]
|
| 185 |
-
|
| 186 |
-
# All other types - convert to JSON string with pydantic encoder
|
| 187 |
-
return [
|
| 188 |
-
TextContent(
|
| 189 |
-
type="text",
|
| 190 |
-
text=json.dumps(
|
| 191 |
-
value, indent=2, default=pydantic.json.pydantic_encoder
|
| 192 |
-
),
|
| 193 |
-
)
|
| 194 |
-
]
|
| 195 |
-
|
| 196 |
def add_tool(
|
| 197 |
self,
|
| 198 |
func: Callable,
|
|
@@ -318,3 +273,43 @@ class FastMCP:
|
|
| 318 |
port=self.settings.port,
|
| 319 |
log_level=self.settings.log_level,
|
| 320 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
"""Call a tool by name with arguments."""
|
| 112 |
try:
|
| 113 |
result = await self._tool_manager.call_tool(name, arguments)
|
| 114 |
+
return _convert_to_content(result)
|
| 115 |
except Exception as e:
|
| 116 |
logger.error(f"Error calling tool {name}: {e}")
|
| 117 |
return [
|
|
|
|
| 148 |
logger.error(f"Error reading resource {uri}: {e}")
|
| 149 |
raise ResourceError(str(e))
|
| 150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
def add_tool(
|
| 152 |
self,
|
| 153 |
func: Callable,
|
|
|
|
| 273 |
port=self.settings.port,
|
| 274 |
log_level=self.settings.log_level,
|
| 275 |
)
|
| 276 |
+
|
| 277 |
+
|
| 278 |
+
def _convert_to_content(value: Any) -> Sequence[Union[TextContent, ImageContent]]:
|
| 279 |
+
"""Convert a tool result to MCP content types."""
|
| 280 |
+
|
| 281 |
+
# Already a sequence of valid content types
|
| 282 |
+
if isinstance(value, (list, tuple)):
|
| 283 |
+
if all(isinstance(x, (TextContent, ImageContent)) for x in value):
|
| 284 |
+
return value
|
| 285 |
+
# Handle mixed content including Image objects
|
| 286 |
+
result = []
|
| 287 |
+
for item in value:
|
| 288 |
+
if isinstance(item, (TextContent, ImageContent)):
|
| 289 |
+
result.append(item)
|
| 290 |
+
elif isinstance(item, Image):
|
| 291 |
+
result.append(item.to_image_content())
|
| 292 |
+
else:
|
| 293 |
+
result.append(
|
| 294 |
+
TextContent(
|
| 295 |
+
type="text",
|
| 296 |
+
text=json.dumps(item, default=pydantic.json.pydantic_encoder),
|
| 297 |
+
)
|
| 298 |
+
)
|
| 299 |
+
return result
|
| 300 |
+
|
| 301 |
+
# Single content type
|
| 302 |
+
if isinstance(value, (TextContent, ImageContent)):
|
| 303 |
+
return [value]
|
| 304 |
+
|
| 305 |
+
# Image helper
|
| 306 |
+
if isinstance(value, Image):
|
| 307 |
+
return [value.to_image_content()]
|
| 308 |
+
|
| 309 |
+
# All other types - convert to JSON string with pydantic encoder
|
| 310 |
+
return [
|
| 311 |
+
TextContent(
|
| 312 |
+
type="text",
|
| 313 |
+
text=json.dumps(value, indent=2, default=pydantic.json.pydantic_encoder),
|
| 314 |
+
)
|
| 315 |
+
]
|