Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

Add tests for title priority logic in tools

Jeremiah Lowin7d3dcdca036f
diff --git a/tests/tools/test_tool.py b/tests/tools/test_tool.py
index 9350147c1..a2278d6d1 100644
--- a/tests/tools/test_tool.py
+++ b/tests/tools/test_tool.py
@@ -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"