-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add ToolAnnotations support in FastMCP and lowlevel servers #482
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3ea2738
Add support and tests for ToolAnnotations in FastMCP and lowlevel ser…
bhosmer-ant 452278e
Fix type errors in lowlevel tool annotations test
bhosmer-ant dd10fa1
Fix import sorting and remove unused imports
bhosmer-ant fcf1051
use ToolAnnotations instead of dict throughout
bhosmer-ant 72762a7
Merge branch 'main' into basil/tool_annotations
bhosmer-ant 60c1566
revert changes to uv.lock
bhosmer-ant 343998c
Merge branch 'main' into basil/tool_annotations
bhosmer-ant a1b2ed5
remove deprecation warning suppression
bhosmer-ant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
"""Tests for tool annotations in low-level server.""" | ||
|
||
import anyio | ||
import pytest | ||
|
||
from mcp.client.session import ClientSession | ||
from mcp.server import Server | ||
from mcp.server.lowlevel import NotificationOptions | ||
from mcp.server.models import InitializationOptions | ||
from mcp.server.session import ServerSession | ||
from mcp.shared.session import RequestResponder | ||
from mcp.types import ( | ||
ClientResult, | ||
JSONRPCMessage, | ||
ServerNotification, | ||
ServerRequest, | ||
Tool, | ||
ToolAnnotations, | ||
) | ||
|
||
|
||
@pytest.mark.anyio | ||
async def test_lowlevel_server_tool_annotations(): | ||
"""Test that tool annotations work in low-level server.""" | ||
server = Server("test") | ||
|
||
# Create a tool with annotations | ||
@server.list_tools() | ||
async def list_tools(): | ||
return [ | ||
Tool( | ||
name="echo", | ||
description="Echo a message back", | ||
inputSchema={ | ||
"type": "object", | ||
"properties": { | ||
"message": {"type": "string"}, | ||
}, | ||
"required": ["message"], | ||
}, | ||
annotations=ToolAnnotations( | ||
title="Echo Tool", | ||
readOnlyHint=True, | ||
), | ||
) | ||
] | ||
|
||
server_to_client_send, server_to_client_receive = anyio.create_memory_object_stream[ | ||
JSONRPCMessage | ||
](10) | ||
client_to_server_send, client_to_server_receive = anyio.create_memory_object_stream[ | ||
JSONRPCMessage | ||
](10) | ||
|
||
# Message handler for client | ||
async def message_handler( | ||
message: RequestResponder[ServerRequest, ClientResult] | ||
| ServerNotification | ||
| Exception, | ||
) -> None: | ||
if isinstance(message, Exception): | ||
raise message | ||
|
||
# Server task | ||
async def run_server(): | ||
async with ServerSession( | ||
client_to_server_receive, | ||
server_to_client_send, | ||
InitializationOptions( | ||
server_name="test-server", | ||
server_version="1.0.0", | ||
capabilities=server.get_capabilities( | ||
notification_options=NotificationOptions(), | ||
experimental_capabilities={}, | ||
), | ||
), | ||
) as server_session: | ||
async with anyio.create_task_group() as tg: | ||
|
||
async def handle_messages(): | ||
async for message in server_session.incoming_messages: | ||
await server._handle_message(message, server_session, {}, False) | ||
|
||
tg.start_soon(handle_messages) | ||
await anyio.sleep_forever() | ||
|
||
# Run the test | ||
async with anyio.create_task_group() as tg: | ||
tg.start_soon(run_server) | ||
|
||
async with ClientSession( | ||
server_to_client_receive, | ||
client_to_server_send, | ||
message_handler=message_handler, | ||
) as client_session: | ||
# Initialize the session | ||
await client_session.initialize() | ||
|
||
# List tools | ||
tools_result = await client_session.list_tools() | ||
|
||
# Cancel the server task | ||
tg.cancel_scope.cancel() | ||
|
||
# Verify results | ||
assert tools_result is not None | ||
assert len(tools_result.tools) == 1 | ||
assert tools_result.tools[0].name == "echo" | ||
assert tools_result.tools[0].annotations is not None | ||
assert tools_result.tools[0].annotations.title == "Echo Tool" | ||
assert tools_result.tools[0].annotations.readOnlyHint is True |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do we need to change this? I see the problem, but why do we even need it here?
on top of the file there is
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's this a little further down in the function:
(and having it up at the top introduces a circular dependency)