Merge pull request #610 from jlowin/fix-tools-without-descriptions

Fix handling tools without descriptions
This commit is contained in:
Jeremiah Lowin 2025-05-27 14:30:24 -04:00 committed by GitHub
commit 7718ad348d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 14 additions and 3 deletions

View file

@ -148,7 +148,7 @@ class ResourceTemplate(BaseModel):
f"URI parameters {uri_params} must be a subset of the function arguments: {func_params}"
)
description = description or fn.__doc__ or ""
description = description or fn.__doc__
if not inspect.isroutine(fn):
fn = fn.__call__

View file

@ -36,7 +36,9 @@ class Tool(BaseModel):
fn: Callable[..., Any]
name: str = Field(description="Name of the tool")
description: str = Field(description="Description of what the tool does")
description: str | None = Field(
default=None, description="Description of what the tool does"
)
parameters: dict[str, Any] = Field(description="JSON schema for tool parameters")
tags: Annotated[set[str], BeforeValidator(_convert_set_defaults)] = Field(
default_factory=set, description="Tags for the tool"
@ -74,7 +76,7 @@ class Tool(BaseModel):
if func_name == "<lambda>":
raise ValueError("You must provide a name for lambda functions")
func_doc = description or fn.__doc__ or ""
func_doc = description or fn.__doc__
# if the fn is a callable class, we need to get the __call__ method from here out
if not inspect.isroutine(fn):

View file

@ -30,6 +30,10 @@ def fastmcp_server():
"""Greet someone by name."""
return f"Hello, {name}!"
@server.tool()
def tool_without_description() -> str:
return "Hello?"
@server.tool()
def add(a: int, b: int) -> int:
"""Add two numbers together."""
@ -110,6 +114,11 @@ class TestTools:
assert "greet" in tools
assert "add" in tools
assert "error_tool" in tools
assert "tool_without_description" in tools
async def test_tool_without_description(self, proxy_server):
tools = await proxy_server.get_tools()
assert tools["tool_without_description"].description is None
async def test_list_tools_same_as_original(self, fastmcp_server, proxy_server):
assert (