mirror of
https://github.com/aljazceru/mcp-python-sdk.git
synced 2025-12-19 14:54:24 +01:00
Add ToolAnnotations support in FastMCP and lowlevel servers (#482)
This commit is contained in:
@@ -41,6 +41,7 @@ from mcp.types import (
|
||||
GetPromptResult,
|
||||
ImageContent,
|
||||
TextContent,
|
||||
ToolAnnotations,
|
||||
)
|
||||
from mcp.types import Prompt as MCPPrompt
|
||||
from mcp.types import PromptArgument as MCPPromptArgument
|
||||
@@ -176,6 +177,7 @@ class FastMCP:
|
||||
name=info.name,
|
||||
description=info.description,
|
||||
inputSchema=info.parameters,
|
||||
annotations=info.annotations,
|
||||
)
|
||||
for info in tools
|
||||
]
|
||||
@@ -244,6 +246,7 @@ class FastMCP:
|
||||
fn: AnyFunction,
|
||||
name: str | None = None,
|
||||
description: str | None = None,
|
||||
annotations: ToolAnnotations | None = None,
|
||||
) -> None:
|
||||
"""Add a tool to the server.
|
||||
|
||||
@@ -254,11 +257,17 @@ class FastMCP:
|
||||
fn: The function to register as a tool
|
||||
name: Optional name for the tool (defaults to function name)
|
||||
description: Optional description of what the tool does
|
||||
annotations: Optional ToolAnnotations providing additional tool information
|
||||
"""
|
||||
self._tool_manager.add_tool(fn, name=name, description=description)
|
||||
self._tool_manager.add_tool(
|
||||
fn, name=name, description=description, annotations=annotations
|
||||
)
|
||||
|
||||
def tool(
|
||||
self, name: str | None = None, description: str | None = None
|
||||
self,
|
||||
name: str | None = None,
|
||||
description: str | None = None,
|
||||
annotations: ToolAnnotations | None = None,
|
||||
) -> Callable[[AnyFunction], AnyFunction]:
|
||||
"""Decorator to register a tool.
|
||||
|
||||
@@ -269,6 +278,7 @@ class FastMCP:
|
||||
Args:
|
||||
name: Optional name for the tool (defaults to function name)
|
||||
description: Optional description of what the tool does
|
||||
annotations: Optional ToolAnnotations providing additional tool information
|
||||
|
||||
Example:
|
||||
@server.tool()
|
||||
@@ -293,7 +303,9 @@ class FastMCP:
|
||||
)
|
||||
|
||||
def decorator(fn: AnyFunction) -> AnyFunction:
|
||||
self.add_tool(fn, name=name, description=description)
|
||||
self.add_tool(
|
||||
fn, name=name, description=description, annotations=annotations
|
||||
)
|
||||
return fn
|
||||
|
||||
return decorator
|
||||
|
||||
@@ -8,6 +8,7 @@ from pydantic import BaseModel, Field
|
||||
|
||||
from mcp.server.fastmcp.exceptions import ToolError
|
||||
from mcp.server.fastmcp.utilities.func_metadata import FuncMetadata, func_metadata
|
||||
from mcp.types import ToolAnnotations
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from mcp.server.fastmcp.server import Context
|
||||
@@ -30,6 +31,9 @@ class Tool(BaseModel):
|
||||
context_kwarg: str | None = Field(
|
||||
None, description="Name of the kwarg that should receive context"
|
||||
)
|
||||
annotations: ToolAnnotations | None = Field(
|
||||
None, description="Optional annotations for the tool"
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_function(
|
||||
@@ -38,9 +42,10 @@ class Tool(BaseModel):
|
||||
name: str | None = None,
|
||||
description: str | None = None,
|
||||
context_kwarg: str | None = None,
|
||||
annotations: ToolAnnotations | None = None,
|
||||
) -> Tool:
|
||||
"""Create a Tool from a function."""
|
||||
from mcp.server.fastmcp import Context
|
||||
from mcp.server.fastmcp.server import Context
|
||||
|
||||
func_name = name or fn.__name__
|
||||
|
||||
@@ -73,6 +78,7 @@ class Tool(BaseModel):
|
||||
fn_metadata=func_arg_metadata,
|
||||
is_async=is_async,
|
||||
context_kwarg=context_kwarg,
|
||||
annotations=annotations,
|
||||
)
|
||||
|
||||
async def run(
|
||||
|
||||
@@ -7,6 +7,7 @@ from mcp.server.fastmcp.exceptions import ToolError
|
||||
from mcp.server.fastmcp.tools.base import Tool
|
||||
from mcp.server.fastmcp.utilities.logging import get_logger
|
||||
from mcp.shared.context import LifespanContextT
|
||||
from mcp.types import ToolAnnotations
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from mcp.server.fastmcp.server import Context
|
||||
@@ -35,9 +36,12 @@ class ToolManager:
|
||||
fn: Callable[..., Any],
|
||||
name: str | None = None,
|
||||
description: str | None = None,
|
||||
annotations: ToolAnnotations | None = None,
|
||||
) -> Tool:
|
||||
"""Add a tool to the server."""
|
||||
tool = Tool.from_function(fn, name=name, description=description)
|
||||
tool = Tool.from_function(
|
||||
fn, name=name, description=description, annotations=annotations
|
||||
)
|
||||
existing = self._tools.get(tool.name)
|
||||
if existing:
|
||||
if self.warn_on_duplicate_tools:
|
||||
|
||||
@@ -705,6 +705,54 @@ class ListToolsRequest(PaginatedRequest[RequestParams | None, Literal["tools/lis
|
||||
params: RequestParams | None = None
|
||||
|
||||
|
||||
class ToolAnnotations(BaseModel):
|
||||
"""
|
||||
Additional properties describing a Tool to clients.
|
||||
|
||||
NOTE: all properties in ToolAnnotations are **hints**.
|
||||
They are not guaranteed to provide a faithful description of
|
||||
tool behavior (including descriptive properties like `title`).
|
||||
|
||||
Clients should never make tool use decisions based on ToolAnnotations
|
||||
received from untrusted servers.
|
||||
"""
|
||||
|
||||
title: str | None = None
|
||||
"""A human-readable title for the tool."""
|
||||
|
||||
readOnlyHint: bool | None = None
|
||||
"""
|
||||
If true, the tool does not modify its environment.
|
||||
Default: false
|
||||
"""
|
||||
|
||||
destructiveHint: bool | None = None
|
||||
"""
|
||||
If true, the tool may perform destructive updates to its environment.
|
||||
If false, the tool performs only additive updates.
|
||||
(This property is meaningful only when `readOnlyHint == false`)
|
||||
Default: true
|
||||
"""
|
||||
|
||||
idempotentHint: bool | None = None
|
||||
"""
|
||||
If true, calling the tool repeatedly with the same arguments
|
||||
will have no additional effect on the its environment.
|
||||
(This property is meaningful only when `readOnlyHint == false`)
|
||||
Default: false
|
||||
"""
|
||||
|
||||
openWorldHint: bool | None = None
|
||||
"""
|
||||
If true, this tool may interact with an "open world" of external
|
||||
entities. If false, the tool's domain of interaction is closed.
|
||||
For example, the world of a web search tool is open, whereas that
|
||||
of a memory tool is not.
|
||||
Default: true
|
||||
"""
|
||||
model_config = ConfigDict(extra="allow")
|
||||
|
||||
|
||||
class Tool(BaseModel):
|
||||
"""Definition for a tool the client can call."""
|
||||
|
||||
@@ -714,6 +762,8 @@ class Tool(BaseModel):
|
||||
"""A human-readable description of the tool."""
|
||||
inputSchema: dict[str, Any]
|
||||
"""A JSON Schema object defining the expected parameters for the tool."""
|
||||
annotations: ToolAnnotations | None = None
|
||||
"""Optional additional tool information."""
|
||||
model_config = ConfigDict(extra="allow")
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user