fastmcp/docs/python-sdk/fastmcp-server-tasks-converters.mdx
marvin-context-protocol[bot] d35b867b10
chore: Update SDK documentation (#2517)
Co-authored-by: marvin-context-protocol[bot] <225465937+marvin-context-protocol[bot]@users.noreply.github.com>
2025-12-09 21:07:20 -05:00

77 lines
2.5 KiB
Text

---
title: converters
sidebarTitle: converters
---
# `fastmcp.server.tasks.converters`
SEP-1686 task result converters.
Converts raw task return values to MCP result types.
## Functions
### `convert_tool_result` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/tasks/converters.py#L19" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```python
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` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/tasks/converters.py#L96" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```python
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` <sup><a href="https://github.com/jlowin/fastmcp/blob/main/src/fastmcp/server/tasks/converters.py#L150" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```python
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