From 82e5b3fc98166813b5c8afc5818eaf490acfe83d Mon Sep 17 00:00:00 2001 From: Jeremiah Lowin <153965+jlowin@users.noreply.github.com> Date: Sat, 30 Nov 2024 09:45:19 -0500 Subject: [PATCH] Make content method a function --- src/fastmcp/server.py | 87 ++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 46 deletions(-) diff --git a/src/fastmcp/server.py b/src/fastmcp/server.py index 1e56962ba..cd4bfad8c 100644 --- a/src/fastmcp/server.py +++ b/src/fastmcp/server.py @@ -111,7 +111,7 @@ class FastMCP: """Call a tool by name with arguments.""" try: result = await self._tool_manager.call_tool(name, arguments) - return self._convert_to_content(result) + return _convert_to_content(result) except Exception as e: logger.error(f"Error calling tool {name}: {e}") return [ @@ -148,51 +148,6 @@ class FastMCP: logger.error(f"Error reading resource {uri}: {e}") raise ResourceError(str(e)) - def _convert_to_content( - self, value: Any - ) -> Sequence[Union[TextContent, ImageContent]]: - """Convert a tool result to MCP content types.""" - - # Already a sequence of valid content types - if isinstance(value, (list, tuple)): - if all(isinstance(x, (TextContent, ImageContent)) for x in value): - return value - # Handle mixed content including Image objects - result = [] - for item in value: - if isinstance(item, (TextContent, ImageContent)): - result.append(item) - elif isinstance(item, Image): - result.append(item.to_image_content()) - else: - result.append( - TextContent( - type="text", - text=json.dumps( - item, indent=2, default=pydantic.json.pydantic_encoder - ), - ) - ) - return result - - # Single content type - if isinstance(value, (TextContent, ImageContent)): - return [value] - - # Image helper - if isinstance(value, Image): - return [value.to_image_content()] - - # All other types - convert to JSON string with pydantic encoder - return [ - TextContent( - type="text", - text=json.dumps( - value, indent=2, default=pydantic.json.pydantic_encoder - ), - ) - ] - def add_tool( self, func: Callable, @@ -318,3 +273,43 @@ class FastMCP: port=self.settings.port, log_level=self.settings.log_level, ) + + +def _convert_to_content(value: Any) -> Sequence[Union[TextContent, ImageContent]]: + """Convert a tool result to MCP content types.""" + + # Already a sequence of valid content types + if isinstance(value, (list, tuple)): + if all(isinstance(x, (TextContent, ImageContent)) for x in value): + return value + # Handle mixed content including Image objects + result = [] + for item in value: + if isinstance(item, (TextContent, ImageContent)): + result.append(item) + elif isinstance(item, Image): + result.append(item.to_image_content()) + else: + result.append( + TextContent( + type="text", + text=json.dumps(item, default=pydantic.json.pydantic_encoder), + ) + ) + return result + + # Single content type + if isinstance(value, (TextContent, ImageContent)): + return [value] + + # Image helper + if isinstance(value, Image): + return [value.to_image_content()] + + # All other types - convert to JSON string with pydantic encoder + return [ + TextContent( + type="text", + text=json.dumps(value, indent=2, default=pydantic.json.pydantic_encoder), + ) + ]