Spaces:
Running
Running
Jeremiah Lowin Claude commited on
Commit ·
3caabbd
1
Parent(s): 226fdf7
Add tests for title priority logic in tools
Browse files- 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>
- tests/tools/test_tool.py +49 -2
tests/tools/test_tool.py
CHANGED
|
@@ -1247,7 +1247,6 @@ class TestToolTitle:
|
|
| 1247 |
assert tool.name == "calc"
|
| 1248 |
assert tool.title == "Advanced Calculator Tool"
|
| 1249 |
assert tool.description == "Custom description"
|
| 1250 |
-
assert tool.get_display_name() == "Advanced Calculator Tool"
|
| 1251 |
|
| 1252 |
# Test MCP conversion includes title
|
| 1253 |
mcp_tool = tool.to_mcp_tool()
|
|
@@ -1266,9 +1265,57 @@ class TestToolTitle:
|
|
| 1266 |
|
| 1267 |
assert tool.name == "multiply"
|
| 1268 |
assert tool.title is None
|
| 1269 |
-
assert tool.get_display_name() == "multiply"
|
| 1270 |
|
| 1271 |
# Test MCP conversion doesn't include title when None
|
| 1272 |
mcp_tool = tool.to_mcp_tool()
|
| 1273 |
assert mcp_tool.name == "multiply"
|
| 1274 |
assert not hasattr(mcp_tool, "title") or mcp_tool.title is None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1247 |
assert tool.name == "calc"
|
| 1248 |
assert tool.title == "Advanced Calculator Tool"
|
| 1249 |
assert tool.description == "Custom description"
|
|
|
|
| 1250 |
|
| 1251 |
# Test MCP conversion includes title
|
| 1252 |
mcp_tool = tool.to_mcp_tool()
|
|
|
|
| 1265 |
|
| 1266 |
assert tool.name == "multiply"
|
| 1267 |
assert tool.title is None
|
|
|
|
| 1268 |
|
| 1269 |
# Test MCP conversion doesn't include title when None
|
| 1270 |
mcp_tool = tool.to_mcp_tool()
|
| 1271 |
assert mcp_tool.name == "multiply"
|
| 1272 |
assert not hasattr(mcp_tool, "title") or mcp_tool.title is None
|
| 1273 |
+
|
| 1274 |
+
def test_tool_title_priority(self):
|
| 1275 |
+
"""Test that explicit title takes priority over annotations.title."""
|
| 1276 |
+
from mcp.types import ToolAnnotations
|
| 1277 |
+
|
| 1278 |
+
def divide(x: int, y: int) -> float:
|
| 1279 |
+
"""Divide two numbers."""
|
| 1280 |
+
return x / y
|
| 1281 |
+
|
| 1282 |
+
# Test with both explicit title and annotations.title
|
| 1283 |
+
annotations = ToolAnnotations(title="Annotation Title")
|
| 1284 |
+
tool = Tool.from_function(
|
| 1285 |
+
divide,
|
| 1286 |
+
name="div",
|
| 1287 |
+
title="Explicit Title",
|
| 1288 |
+
annotations=annotations,
|
| 1289 |
+
)
|
| 1290 |
+
|
| 1291 |
+
assert tool.title == "Explicit Title"
|
| 1292 |
+
assert tool.annotations is not None
|
| 1293 |
+
assert tool.annotations.title == "Annotation Title"
|
| 1294 |
+
|
| 1295 |
+
# Explicit title should take priority
|
| 1296 |
+
mcp_tool = tool.to_mcp_tool()
|
| 1297 |
+
assert mcp_tool.title == "Explicit Title"
|
| 1298 |
+
|
| 1299 |
+
def test_tool_annotations_title_fallback(self):
|
| 1300 |
+
"""Test that annotations.title is used when no explicit title is provided."""
|
| 1301 |
+
from mcp.types import ToolAnnotations
|
| 1302 |
+
|
| 1303 |
+
def modulo(x: int, y: int) -> int:
|
| 1304 |
+
"""Get modulo of two numbers."""
|
| 1305 |
+
return x % y
|
| 1306 |
+
|
| 1307 |
+
# Test with only annotations.title (no explicit title)
|
| 1308 |
+
annotations = ToolAnnotations(title="Annotation Title")
|
| 1309 |
+
tool = Tool.from_function(
|
| 1310 |
+
modulo,
|
| 1311 |
+
name="mod",
|
| 1312 |
+
annotations=annotations,
|
| 1313 |
+
)
|
| 1314 |
+
|
| 1315 |
+
assert tool.title is None
|
| 1316 |
+
assert tool.annotations is not None
|
| 1317 |
+
assert tool.annotations.title == "Annotation Title"
|
| 1318 |
+
|
| 1319 |
+
# Should fall back to annotations.title
|
| 1320 |
+
mcp_tool = tool.to_mcp_tool()
|
| 1321 |
+
assert mcp_tool.title == "Annotation Title"
|