Add tests for title priority logic in tools

- Test that explicit title takes priority over annotations.title
- Test that annotations.title is used as fallback when no explicit title
- Ensures the improved to_mcp_tool logic works correctly
- Add proper type guards for annotations None checks

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Jeremiah Lowin 2025-06-28 19:58:38 -04:00
commit 7d3dcdca03

View file

@ -1247,7 +1247,6 @@ class TestToolTitle:
assert tool.name == "calc"
assert tool.title == "Advanced Calculator Tool"
assert tool.description == "Custom description"
assert tool.get_display_name() == "Advanced Calculator Tool"
# Test MCP conversion includes title
mcp_tool = tool.to_mcp_tool()
@ -1266,9 +1265,57 @@ class TestToolTitle:
assert tool.name == "multiply"
assert tool.title is None
assert tool.get_display_name() == "multiply"
# Test MCP conversion doesn't include title when None
mcp_tool = tool.to_mcp_tool()
assert mcp_tool.name == "multiply"
assert not hasattr(mcp_tool, "title") or mcp_tool.title is None
def test_tool_title_priority(self):
"""Test that explicit title takes priority over annotations.title."""
from mcp.types import ToolAnnotations
def divide(x: int, y: int) -> float:
"""Divide two numbers."""
return x / y
# Test with both explicit title and annotations.title
annotations = ToolAnnotations(title="Annotation Title")
tool = Tool.from_function(
divide,
name="div",
title="Explicit Title",
annotations=annotations,
)
assert tool.title == "Explicit Title"
assert tool.annotations is not None
assert tool.annotations.title == "Annotation Title"
# Explicit title should take priority
mcp_tool = tool.to_mcp_tool()
assert mcp_tool.title == "Explicit Title"
def test_tool_annotations_title_fallback(self):
"""Test that annotations.title is used when no explicit title is provided."""
from mcp.types import ToolAnnotations
def modulo(x: int, y: int) -> int:
"""Get modulo of two numbers."""
return x % y
# Test with only annotations.title (no explicit title)
annotations = ToolAnnotations(title="Annotation Title")
tool = Tool.from_function(
modulo,
name="mod",
annotations=annotations,
)
assert tool.title is None
assert tool.annotations is not None
assert tool.annotations.title == "Annotation Title"
# Should fall back to annotations.title
mcp_tool = tool.to_mcp_tool()
assert mcp_tool.title == "Annotation Title"