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:
@@ -9,6 +9,7 @@ from mcp.server.fastmcp.exceptions import ToolError
|
||||
from mcp.server.fastmcp.tools import ToolManager
|
||||
from mcp.server.session import ServerSessionT
|
||||
from mcp.shared.context import LifespanContextT
|
||||
from mcp.types import ToolAnnotations
|
||||
|
||||
|
||||
class TestAddTools:
|
||||
@@ -321,3 +322,43 @@ class TestContextHandling:
|
||||
ctx = mcp.get_context()
|
||||
with pytest.raises(ToolError, match="Error executing tool tool_with_context"):
|
||||
await manager.call_tool("tool_with_context", {"x": 42}, context=ctx)
|
||||
|
||||
|
||||
class TestToolAnnotations:
|
||||
def test_tool_annotations(self):
|
||||
"""Test that tool annotations are correctly added to tools."""
|
||||
|
||||
def read_data(path: str) -> str:
|
||||
"""Read data from a file."""
|
||||
return f"Data from {path}"
|
||||
|
||||
annotations = ToolAnnotations(
|
||||
title="File Reader",
|
||||
readOnlyHint=True,
|
||||
openWorldHint=False,
|
||||
)
|
||||
|
||||
manager = ToolManager()
|
||||
tool = manager.add_tool(read_data, annotations=annotations)
|
||||
|
||||
assert tool.annotations is not None
|
||||
assert tool.annotations.title == "File Reader"
|
||||
assert tool.annotations.readOnlyHint is True
|
||||
assert tool.annotations.openWorldHint is False
|
||||
|
||||
@pytest.mark.anyio
|
||||
async def test_tool_annotations_in_fastmcp(self):
|
||||
"""Test that tool annotations are included in MCPTool conversion."""
|
||||
|
||||
app = FastMCP()
|
||||
|
||||
@app.tool(annotations=ToolAnnotations(title="Echo Tool", readOnlyHint=True))
|
||||
def echo(message: str) -> str:
|
||||
"""Echo a message back."""
|
||||
return message
|
||||
|
||||
tools = await app.list_tools()
|
||||
assert len(tools) == 1
|
||||
assert tools[0].annotations is not None
|
||||
assert tools[0].annotations.title == "Echo Tool"
|
||||
assert tools[0].annotations.readOnlyHint is True
|
||||
|
||||
Reference in New Issue
Block a user