Spaces:
Running
Running
Jeremiah Lowin commited on
Commit ·
4d797e0
1
Parent(s): 11ebe03
Add docs
Browse files- docs/servers/tools.mdx +38 -27
docs/servers/tools.mdx
CHANGED
|
@@ -251,6 +251,8 @@ Use `async def` when your tool needs to perform operations that might wait for e
|
|
| 251 |
|
| 252 |
### Return Values
|
| 253 |
|
|
|
|
|
|
|
| 254 |
FastMCP automatically converts the value returned by your function into the appropriate MCP content format for the client:
|
| 255 |
|
| 256 |
- **`str`**: Sent as `TextContent`.
|
|
@@ -264,43 +266,52 @@ FastMCP automatically converts the value returned by your function into the appr
|
|
| 264 |
|
| 265 |
FastMCP will attempt to serialize other types to a string if possible.
|
| 266 |
|
| 267 |
-
|
| 268 |
-
At this time, FastMCP responds only to your tool's return *value*, not its return *annotation*.
|
| 269 |
-
</Tip>
|
| 270 |
|
| 271 |
-
|
| 272 |
-
from fastmcp import FastMCP
|
| 273 |
-
from fastmcp.utilities.types import Image
|
| 274 |
-
import io
|
| 275 |
|
| 276 |
-
|
| 277 |
-
from PIL import Image as PILImage
|
| 278 |
-
except ImportError:
|
| 279 |
-
raise ImportError("Please install the `pillow` library to run this example.")
|
| 280 |
|
| 281 |
-
|
| 282 |
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
img = PILImage.new("RGB", (width, height), color=color)
|
| 288 |
|
| 289 |
-
|
| 290 |
-
buffer = io.BytesIO()
|
| 291 |
-
img.save(buffer, format="PNG")
|
| 292 |
-
img_bytes = buffer.getvalue()
|
| 293 |
|
| 294 |
-
|
| 295 |
-
|
|
|
|
|
|
|
|
|
|
| 296 |
|
| 297 |
@mcp.tool
|
| 298 |
-
def
|
| 299 |
-
"""
|
| 300 |
-
|
| 301 |
-
return None
|
| 302 |
```
|
| 303 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
### Error Handling
|
| 305 |
|
| 306 |
<VersionBadge version="2.4.1" />
|
|
|
|
| 251 |
|
| 252 |
### Return Values
|
| 253 |
|
| 254 |
+
#### Output Conversion
|
| 255 |
+
|
| 256 |
FastMCP automatically converts the value returned by your function into the appropriate MCP content format for the client:
|
| 257 |
|
| 258 |
- **`str`**: Sent as `TextContent`.
|
|
|
|
| 266 |
|
| 267 |
FastMCP will attempt to serialize other types to a string if possible.
|
| 268 |
|
| 269 |
+
#### Output Schemas
|
|
|
|
|
|
|
| 270 |
|
| 271 |
+
<VersionBadge version="2.10.0" />
|
|
|
|
|
|
|
|
|
|
| 272 |
|
| 273 |
+
FastMCP will automatically generate MCP [output schemas](https://modelcontextprotocol.io/specification/2025-06-18/server/tools#output-schema) for your tools based on their return type annotations. This helps MCP clients understand what type of data to expect from your tool, enabling better validation and type safety.
|
|
|
|
|
|
|
|
|
|
| 274 |
|
| 275 |
+
When you add a return type annotation to your tool function, FastMCP will generate a JSON schema describing the expected output format and include it in the tool definition sent to MCP clients.
|
| 276 |
|
| 277 |
+
<CodeGroup>
|
| 278 |
+
```python Tool Definition
|
| 279 |
+
from dataclasses import dataclass
|
| 280 |
+
from fastmcp import FastMCP
|
|
|
|
| 281 |
|
| 282 |
+
mcp = FastMCP()
|
|
|
|
|
|
|
|
|
|
| 283 |
|
| 284 |
+
@dataclass
|
| 285 |
+
class Person:
|
| 286 |
+
name: str
|
| 287 |
+
age: int
|
| 288 |
+
email: str
|
| 289 |
|
| 290 |
@mcp.tool
|
| 291 |
+
def get_user_profile(user_id: str) -> Person:
|
| 292 |
+
"""Get a user's profile information."""
|
| 293 |
+
return Person(name="Alice", age=30, email="alice@example.com")
|
|
|
|
| 294 |
```
|
| 295 |
|
| 296 |
+
```json Generated Output Schema
|
| 297 |
+
{
|
| 298 |
+
"properties": {
|
| 299 |
+
"name": {"title": "Name", "type": "string"},
|
| 300 |
+
"age": {"title": "Age", "type": "integer"},
|
| 301 |
+
"email": {"title": "Email", "type": "string"}
|
| 302 |
+
},
|
| 303 |
+
"required": ["name", "age", "email"],
|
| 304 |
+
"title": "Person",
|
| 305 |
+
"type": "object"
|
| 306 |
+
}
|
| 307 |
+
```
|
| 308 |
+
</CodeGroup>
|
| 309 |
+
The output schema is automatically generated for most common types including basic types, collections, union types, Pydantic models, TypedDict structures, and dataclasses. For FastMCP's special types (`Image`, `Audio`, `File`), the output schema reflects their MCP equivalents rather than the FastMCP wrapper types.
|
| 310 |
+
|
| 311 |
+
<Note>
|
| 312 |
+
If your return type annotation cannot be converted to a JSON schema (e.g., complex custom classes without Pydantic support), the output schema will be omitted from the tool definition. The tool will still function normally, but clients won't receive type information about the expected output.
|
| 313 |
+
</Note>
|
| 314 |
+
|
| 315 |
### Error Handling
|
| 316 |
|
| 317 |
<VersionBadge version="2.4.1" />
|