mirror of
https://github.com/PrefectHQ/fastmcp.git
synced 2026-08-09 15:19:10 +02:00
docs: standardize ToolAnnotations examples (#3952)
This commit is contained in:
parent
e1ea695d68
commit
ff8aa484ff
4 changed files with 28 additions and 22 deletions
|
|
@ -92,10 +92,12 @@ The connector must be explicitly enabled in each chat session through Developer
|
|||
|
||||
### Skip Confirmations
|
||||
|
||||
Use `annotations={"readOnlyHint": True}` to skip confirmation prompts for read-only tools:
|
||||
Use `annotations=ToolAnnotations(readOnlyHint=True)` to skip confirmation prompts for read-only tools:
|
||||
|
||||
```python
|
||||
@mcp.tool(annotations={"readOnlyHint": True})
|
||||
from mcp.types import ToolAnnotations
|
||||
|
||||
@mcp.tool(annotations=ToolAnnotations(readOnlyHint=True))
|
||||
def get_status() -> str:
|
||||
"""Check system status."""
|
||||
return "All systems operational"
|
||||
|
|
@ -153,4 +155,3 @@ def fetch(id: str) -> dict:
|
|||
5. Ask research questions
|
||||
|
||||
ChatGPT will use your `search` and `fetch` tools to find and cite relevant information.
|
||||
|
||||
|
|
|
|||
|
|
@ -915,15 +915,17 @@ Annotations serve several purposes in client applications:
|
|||
- Describing the safety profile of tools (destructive vs. non-destructive)
|
||||
- Signaling if tools interact with external systems
|
||||
|
||||
You can add annotations to a tool using the `annotations` parameter in the `@mcp.tool` decorator:
|
||||
You can add annotations to a tool using the `annotations` parameter in the `@mcp.tool` decorator. FastMCP accepts either a plain dict or `ToolAnnotations`; the examples below use `ToolAnnotations` for consistency and stronger editor/type support.
|
||||
|
||||
```python
|
||||
from mcp.types import ToolAnnotations
|
||||
|
||||
@mcp.tool(
|
||||
annotations={
|
||||
"title": "Calculate Sum",
|
||||
"readOnlyHint": True,
|
||||
"openWorldHint": False
|
||||
}
|
||||
annotations=ToolAnnotations(
|
||||
title="Calculate Sum",
|
||||
readOnlyHint=True,
|
||||
openWorldHint=False,
|
||||
)
|
||||
)
|
||||
def calculate_sum(a: float, b: float) -> float:
|
||||
"""Add two numbers together."""
|
||||
|
|
@ -959,7 +961,7 @@ from mcp.types import ToolAnnotations
|
|||
|
||||
mcp = FastMCP("Data Server")
|
||||
|
||||
@mcp.tool(annotations={"readOnlyHint": True})
|
||||
@mcp.tool(annotations=ToolAnnotations(readOnlyHint=True))
|
||||
def get_user(user_id: str) -> dict:
|
||||
"""Retrieve user information by ID."""
|
||||
return {"id": user_id, "name": "Alice"}
|
||||
|
|
@ -981,7 +983,7 @@ def update_user(user_id: str, name: str) -> dict:
|
|||
"""Update user information."""
|
||||
return {"id": user_id, "name": name, "updated": True}
|
||||
|
||||
@mcp.tool(annotations={"destructiveHint": True})
|
||||
@mcp.tool(annotations=ToolAnnotations(destructiveHint=True))
|
||||
def delete_user(user_id: str) -> dict:
|
||||
"""Permanently delete a user account."""
|
||||
return {"deleted": user_id}
|
||||
|
|
|
|||
|
|
@ -93,10 +93,12 @@ The connector must be explicitly enabled in each chat session through Developer
|
|||
|
||||
### Skip Confirmations
|
||||
|
||||
Use `annotations={"readOnlyHint": True}` to skip confirmation prompts for read-only tools:
|
||||
Use `annotations=ToolAnnotations(readOnlyHint=True)` to skip confirmation prompts for read-only tools:
|
||||
|
||||
```python
|
||||
@mcp.tool(annotations={"readOnlyHint": True})
|
||||
from mcp.types import ToolAnnotations
|
||||
|
||||
@mcp.tool(annotations=ToolAnnotations(readOnlyHint=True))
|
||||
def get_status() -> str:
|
||||
"""Check system status."""
|
||||
return "All systems operational"
|
||||
|
|
@ -154,4 +156,3 @@ def fetch(id: str) -> dict:
|
|||
5. Ask research questions
|
||||
|
||||
ChatGPT will use your `search` and `fetch` tools to find and cite relevant information.
|
||||
|
||||
|
|
|
|||
|
|
@ -792,15 +792,17 @@ Annotations serve several purposes in client applications:
|
|||
- Describing the safety profile of tools (destructive vs. non-destructive)
|
||||
- Signaling if tools interact with external systems
|
||||
|
||||
You can add annotations to a tool using the `annotations` parameter in the `@mcp.tool` decorator:
|
||||
You can add annotations to a tool using the `annotations` parameter in the `@mcp.tool` decorator. FastMCP accepts either a plain dict or `ToolAnnotations`; the examples below use `ToolAnnotations` for consistency and stronger editor/type support.
|
||||
|
||||
```python
|
||||
from mcp.types import ToolAnnotations
|
||||
|
||||
@mcp.tool(
|
||||
annotations={
|
||||
"title": "Calculate Sum",
|
||||
"readOnlyHint": True,
|
||||
"openWorldHint": False
|
||||
}
|
||||
annotations=ToolAnnotations(
|
||||
title="Calculate Sum",
|
||||
readOnlyHint=True,
|
||||
openWorldHint=False,
|
||||
)
|
||||
)
|
||||
def calculate_sum(a: float, b: float) -> float:
|
||||
"""Add two numbers together."""
|
||||
|
|
@ -836,7 +838,7 @@ from mcp.types import ToolAnnotations
|
|||
|
||||
mcp = FastMCP("Data Server")
|
||||
|
||||
@mcp.tool(annotations={"readOnlyHint": True})
|
||||
@mcp.tool(annotations=ToolAnnotations(readOnlyHint=True))
|
||||
def get_user(user_id: str) -> dict:
|
||||
"""Retrieve user information by ID."""
|
||||
return {"id": user_id, "name": "Alice"}
|
||||
|
|
@ -858,7 +860,7 @@ def update_user(user_id: str, name: str) -> dict:
|
|||
"""Update user information."""
|
||||
return {"id": user_id, "name": name, "updated": True}
|
||||
|
||||
@mcp.tool(annotations={"destructiveHint": True})
|
||||
@mcp.tool(annotations=ToolAnnotations(destructiveHint=True))
|
||||
def delete_user(user_id: str) -> dict:
|
||||
"""Permanently delete a user account."""
|
||||
return {"deleted": user_id}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue