Skip to main content

fastmcp.server.tasks.converters

SEP-1686 task result converters. Converts raw task return values to MCP result types.

Functions

convert_tool_result

convert_tool_result(server: FastMCP, raw_value: Any, tool_name: str, client_task_id: str) -> mcp.types.CallToolResult
Convert raw tool return value to MCP CallToolResult. Replicates the serialization logic from tool.run() to properly handle output_schema, structured content, etc. Args:
  • server: FastMCP server instance
  • raw_value: The raw return value from user’s tool function
  • tool_name: Name of the tool (to get output_schema and serializer)
  • client_task_id: Client task ID for related-task metadata
Returns:
  • CallToolResult with properly formatted content and structured content

convert_prompt_result

convert_prompt_result(server: FastMCP, raw_value: Any, prompt_name: str, client_task_id: str) -> mcp.types.GetPromptResult
Convert raw prompt return value to MCP GetPromptResult. The user function returns raw values (strings, dicts, lists) that need to be converted to PromptMessage objects. Args:
  • server: FastMCP server instance
  • raw_value: The raw return value from user’s prompt function
  • prompt_name: Name of the prompt
  • client_task_id: Client task ID for related-task metadata
Returns:
  • GetPromptResult with properly formatted messages

convert_resource_result

convert_resource_result(server: FastMCP, raw_value: Any, uri: str, client_task_id: str) -> dict[str, Any]
Convert raw resource return value to MCP resource contents dict. Args:
  • server: FastMCP server instance
  • raw_value: The raw return value from user’s resource function (str or bytes)
  • uri: Resource URI (for the contents response)
  • client_task_id: Client task ID for related-task metadata
Returns:
  • Dict with ‘contents’ key containing list of resource contents