Skip to content

Commit 296d54d

Browse files
WebMCP: Implement tool name validation
The spec PR webmachinelearning/webmcp#152 is tightening tool name validation to match what the MCP specification accepts [1], and we think it's reasonable for the web platform to throw exceptions when tool names do not match this criteria, which is what this CL does. [1]: https://modelcontextprotocol.io/specification/2025-11-25/server/tools#tool-names R=masonf Bug: 489045948 Change-Id: I7d1af5d8cd18e5be7db5ddc9f9ce6d28e73cfd3f Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7744064 Commit-Queue: Dominic Farolino <dom@chromium.org> Reviewed-by: Mason Freed <masonf@chromium.org> Cr-Commit-Position: refs/heads/main@{#1625865}
1 parent 24b49df commit 296d54d

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
test(() => {
2+
// Valid names
3+
const validNames = [
4+
'a',
5+
'A',
6+
'0',
7+
'valid-name',
8+
'valid_name',
9+
'valid.name',
10+
'valid-name.with_extras-123',
11+
'a'.repeat(128)
12+
];
13+
14+
for (const name of validNames) {
15+
navigator.modelContext.registerTool({
16+
name: name,
17+
description: 'valid name test',
18+
execute: async () => 'empty'
19+
});
20+
}
21+
}, 'Valid tool names are accepted.');
22+
23+
test(() => {
24+
// Invalid names
25+
const invalidNames = [
26+
'', // Empty
27+
'a'.repeat(129), // Too long
28+
'name with space',
29+
'name@special',
30+
'name#special',
31+
'name$special',
32+
'name%',
33+
'name^',
34+
'name&',
35+
'name*',
36+
'name(',
37+
'name)',
38+
'name+',
39+
'name=',
40+
'name[',
41+
'name]',
42+
'name{',
43+
'name}',
44+
'name|',
45+
'name\\',
46+
'name:',
47+
'name;',
48+
'name"',
49+
'name\'',
50+
'name<',
51+
'name>',
52+
'name?',
53+
'name/',
54+
'name`',
55+
'name~',
56+
];
57+
58+
for (const name of invalidNames) {
59+
assert_throws_dom(
60+
'InvalidStateError',
61+
() => {
62+
navigator.modelContext.registerTool({
63+
name: name,
64+
description: 'invalid name test',
65+
execute: async () => 'empty'
66+
});
67+
},
68+
`Tool name '${name}' is invalid.`
69+
);
70+
}
71+
}, 'Invalid tool names throw InvalidStateError.');

0 commit comments

Comments
 (0)