Skip to content

Commit 80de60e

Browse files
authored
fix: Allow tool names that start with numbers (#407)
Some MCP servers return tool names that have numeric identifiers that start with a number; open up our regex to allow those names. This will not allow direct method invocations of those tools, but we can revisit the need for that in the future if it become a concern.
1 parent 952eac3 commit 80de60e

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/strands/tools/tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def validate_tool_use_name(tool: ToolUse) -> None:
4848
raise InvalidToolUseNameException(message)
4949

5050
tool_name = tool["name"]
51-
tool_name_pattern = r"^[a-zA-Z][a-zA-Z0-9_\-]*$"
51+
tool_name_pattern = r"^[a-zA-Z0-9_\-]{1,}$"
5252
tool_name_max_length = 64
5353
valid_name_pattern = bool(re.match(tool_name_pattern, tool_name))
5454
tool_name_len = len(tool_name)

tests/strands/tools/test_tools.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ def test_validate_tool_use_name_valid():
5959
# Should not raise an exception
6060
validate_tool_use_name(tool2)
6161

62+
tool3 = {"name": "34234_numbers", "toolUseId": "123"}
63+
# Should not raise an exception
64+
validate_tool_use_name(tool3)
65+
6266

6367
def test_validate_tool_use_name_missing():
6468
tool = {"toolUseId": "123"}
@@ -67,7 +71,7 @@ def test_validate_tool_use_name_missing():
6771

6872

6973
def test_validate_tool_use_name_invalid_pattern():
70-
tool = {"name": "123_invalid", "toolUseId": "123"}
74+
tool = {"name": "+123_invalid", "toolUseId": "123"}
7175
with pytest.raises(InvalidToolUseNameException, match="invalid tool name pattern"):
7276
validate_tool_use_name(tool)
7377

@@ -414,7 +418,7 @@ def test_validate_tool_use_with_valid_input():
414418
# Name - Invalid characters
415419
(
416420
{
417-
"name": "1-invalid",
421+
"name": "+1-invalid",
418422
"toolUseId": "123",
419423
"input": {},
420424
},
@@ -429,6 +433,15 @@ def test_validate_tool_use_with_valid_input():
429433
},
430434
strands.tools.InvalidToolUseNameException,
431435
),
436+
# Name - Empty
437+
(
438+
{
439+
"name": "",
440+
"toolUseId": "123",
441+
"input": {},
442+
},
443+
strands.tools.InvalidToolUseNameException,
444+
),
432445
],
433446
)
434447
def test_validate_tool_use_invalid(tool_use, expected_error):

0 commit comments

Comments
 (0)