-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add example and test for parameter descriptions in FastMCP tools
- Loading branch information
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,21 @@ | ||
""" | ||
FastMCP Example showing parameter descriptions | ||
""" | ||
|
||
from pydantic import Field | ||
|
||
from mcp.server.fastmcp import FastMCP | ||
|
||
# Create server | ||
mcp = FastMCP("Parameter Descriptions Server") | ||
|
||
|
||
@mcp.tool() | ||
def greet_user( | ||
name: str = Field(description="The name of the person to greet"), | ||
title: str = Field(description="Optional title like Mr/Ms/Dr", default=""), | ||
times: int = Field(description="Number of times to repeat the greeting", default=1), | ||
) -> str: | ||
"""Greet a user with optional title and repetition""" | ||
greeting = f"Hello {title + ' ' if title else ''}{name}!" | ||
return "\n".join([greeting] * times) |
This file contains 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,30 @@ | ||
"""Test that parameter descriptions are properly exposed through list_tools""" | ||
|
||
import pytest | ||
from pydantic import Field | ||
|
||
from mcp.server.fastmcp import FastMCP | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_parameter_descriptions(): | ||
mcp = FastMCP("Test Server") | ||
|
||
@mcp.tool() | ||
def greet( | ||
name: str = Field(description="The name to greet"), | ||
title: str = Field(description="Optional title", default=""), | ||
) -> str: | ||
"""A greeting tool""" | ||
return f"Hello {title} {name}" | ||
|
||
tools = await mcp.list_tools() | ||
assert len(tools) == 1 | ||
tool = tools[0] | ||
|
||
# Check that parameter descriptions are present in the schema | ||
properties = tool.inputSchema["properties"] | ||
assert "name" in properties | ||
assert properties["name"]["description"] == "The name to greet" | ||
assert "title" in properties | ||
assert properties["title"]["description"] == "Optional title" |