-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Implementation of SEP-986: Specify Format for Tool Names #900
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
felixweinberger
merged 6 commits into
modelcontextprotocol:main
from
kentcdodds:cursor/validate-tool-name-format-and-add-tests-beb1
Nov 11, 2025
+303
−0
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5b0d7da
feat: implement tool name validation according to SEP specification
kentcdodds 7fc694e
fix: update tool name validation to match shipped SEP-986 spec
felixweinberger 674827d
style: apply prettier formatting
felixweinberger 5cabbe7
fix: add const assertions for TypeScript literal type inference
felixweinberger 4c8f5e1
Refactor tool name validation tests using test.each
felixweinberger 5c3ceb3
Merge branch 'main' into cursor/validate-tool-name-format-and-add-tes…
felixweinberger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,7 @@ import { Completable, CompletableDef } from './completable.js'; | |
| import { UriTemplate, Variables } from '../shared/uriTemplate.js'; | ||
| import { RequestHandlerExtra } from '../shared/protocol.js'; | ||
| import { Transport } from '../shared/transport.js'; | ||
| import { validateAndWarnToolName } from '../shared/toolNameValidation.js'; | ||
|
|
||
| /** | ||
| * High-level MCP server that provides a simpler API for working with resources, tools, and prompts. | ||
|
|
@@ -664,6 +665,9 @@ export class McpServer { | |
| _meta: Record<string, unknown> | undefined, | ||
| callback: ToolCallback<ZodRawShape | undefined> | ||
| ): RegisteredTool { | ||
| // Validate tool name according to SEP specification | ||
| validateAndWarnToolName(name); | ||
|
|
||
| const registeredTool: RegisteredTool = { | ||
| title, | ||
| description, | ||
|
|
@@ -678,6 +682,9 @@ export class McpServer { | |
| remove: () => registeredTool.update({ name: null }), | ||
| update: updates => { | ||
| if (typeof updates.name !== 'undefined' && updates.name !== name) { | ||
| if (typeof updates.name === 'string') { | ||
| validateAndWarnToolName(updates.name); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the core change |
||
| } | ||
| delete this._registeredTools[name]; | ||
| if (updates.name) this._registeredTools[updates.name] = registeredTool; | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,236 @@ | ||
| import { validateToolName, validateAndWarnToolName, issueToolNameWarning } from './toolNameValidation.js'; | ||
|
|
||
| // Spy on console.warn to capture output | ||
| let warnSpy: jest.SpyInstance; | ||
|
|
||
| beforeEach(() => { | ||
| warnSpy = jest.spyOn(console, 'warn').mockImplementation(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| describe('validateToolName', () => { | ||
| describe('valid tool names', () => { | ||
| test('should accept simple alphanumeric names', () => { | ||
| const result = validateToolName('getUser'); | ||
| expect(result.isValid).toBe(true); | ||
| expect(result.warnings).toHaveLength(0); | ||
| }); | ||
|
|
||
| test('should accept names with underscores', () => { | ||
| const result = validateToolName('get_user_profile'); | ||
| expect(result.isValid).toBe(true); | ||
| expect(result.warnings).toHaveLength(0); | ||
| }); | ||
|
|
||
| test('should accept names with dashes', () => { | ||
| const result = validateToolName('user-profile-update'); | ||
| expect(result.isValid).toBe(true); | ||
| expect(result.warnings).toHaveLength(0); | ||
| }); | ||
|
|
||
| test('should accept names with dots', () => { | ||
| const result = validateToolName('admin.tools.list'); | ||
| expect(result.isValid).toBe(true); | ||
| expect(result.warnings).toHaveLength(0); | ||
| }); | ||
|
|
||
| test('should reject names with forward slashes', () => { | ||
| const result = validateToolName('user/profile/update'); | ||
| expect(result.isValid).toBe(false); | ||
| expect(result.warnings).toContain('Tool name contains invalid characters: "/"'); | ||
| }); | ||
|
|
||
| test('should accept mixed character names', () => { | ||
| const result = validateToolName('DATA_EXPORT_v2.1'); | ||
| expect(result.isValid).toBe(true); | ||
| expect(result.warnings).toHaveLength(0); | ||
| }); | ||
|
|
||
| test('should accept single character names', () => { | ||
| const result = validateToolName('a'); | ||
| expect(result.isValid).toBe(true); | ||
| expect(result.warnings).toHaveLength(0); | ||
| }); | ||
|
|
||
| test('should accept 128 character names', () => { | ||
| const name = 'a'.repeat(128); | ||
| const result = validateToolName(name); | ||
| expect(result.isValid).toBe(true); | ||
| expect(result.warnings).toHaveLength(0); | ||
| }); | ||
| }); | ||
|
|
||
| describe('invalid tool names', () => { | ||
| test('should reject empty names', () => { | ||
| const result = validateToolName(''); | ||
| expect(result.isValid).toBe(false); | ||
| expect(result.warnings).toContain('Tool name cannot be empty'); | ||
| }); | ||
|
|
||
| test('should reject names longer than 128 characters', () => { | ||
| const name = 'a'.repeat(129); | ||
| const result = validateToolName(name); | ||
| expect(result.isValid).toBe(false); | ||
| expect(result.warnings).toContain('Tool name exceeds maximum length of 128 characters (current: 129)'); | ||
| }); | ||
|
|
||
| test('should reject names with spaces', () => { | ||
| const result = validateToolName('get user profile'); | ||
| expect(result.isValid).toBe(false); | ||
| expect(result.warnings).toContain('Tool name contains invalid characters: " "'); | ||
| }); | ||
|
|
||
| test('should reject names with commas', () => { | ||
| const result = validateToolName('get,user,profile'); | ||
| expect(result.isValid).toBe(false); | ||
| expect(result.warnings).toContain('Tool name contains invalid characters: ","'); | ||
| }); | ||
|
|
||
| test('should reject names with other special characters', () => { | ||
| const result = validateToolName('user@domain.com'); | ||
| expect(result.isValid).toBe(false); | ||
| expect(result.warnings).toContain('Tool name contains invalid characters: "@"'); | ||
| }); | ||
|
|
||
| test('should reject names with multiple invalid characters', () => { | ||
| const result = validateToolName('user name@domain,com'); | ||
| expect(result.isValid).toBe(false); | ||
| expect(result.warnings).toContain('Tool name contains invalid characters: " ", "@", ","'); | ||
| }); | ||
|
|
||
| test('should reject names with unicode characters', () => { | ||
| const result = validateToolName('user-ñame'); | ||
| expect(result.isValid).toBe(false); | ||
| expect(result.warnings).toContain('Tool name contains invalid characters: "ñ"'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('warnings for potentially problematic patterns', () => { | ||
| test('should warn about names with spaces', () => { | ||
| const result = validateToolName('get user profile'); | ||
| expect(result.isValid).toBe(false); | ||
| expect(result.warnings).toContain('Tool name contains spaces, which may cause parsing issues'); | ||
| }); | ||
|
|
||
| test('should warn about names with commas', () => { | ||
| const result = validateToolName('get,user,profile'); | ||
| expect(result.isValid).toBe(false); | ||
| expect(result.warnings).toContain('Tool name contains commas, which may cause parsing issues'); | ||
| }); | ||
|
|
||
| test('should warn about names starting with dash', () => { | ||
| const result = validateToolName('-get-user'); | ||
| expect(result.isValid).toBe(true); | ||
| expect(result.warnings).toContain('Tool name starts or ends with a dash, which may cause parsing issues in some contexts'); | ||
| }); | ||
|
|
||
| test('should warn about names ending with dash', () => { | ||
| const result = validateToolName('get-user-'); | ||
| expect(result.isValid).toBe(true); | ||
| expect(result.warnings).toContain('Tool name starts or ends with a dash, which may cause parsing issues in some contexts'); | ||
| }); | ||
|
|
||
| test('should warn about names starting with dot', () => { | ||
| const result = validateToolName('.get.user'); | ||
| expect(result.isValid).toBe(true); | ||
| expect(result.warnings).toContain('Tool name starts or ends with a dot, which may cause parsing issues in some contexts'); | ||
| }); | ||
|
|
||
| test('should warn about names ending with dot', () => { | ||
| const result = validateToolName('get.user.'); | ||
| expect(result.isValid).toBe(true); | ||
| expect(result.warnings).toContain('Tool name starts or ends with a dot, which may cause parsing issues in some contexts'); | ||
| }); | ||
|
|
||
| test('should warn about names with both leading and trailing dots', () => { | ||
| const result = validateToolName('.get.user.'); | ||
| expect(result.isValid).toBe(true); | ||
| expect(result.warnings).toContain('Tool name starts or ends with a dot, which may cause parsing issues in some contexts'); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('issueToolNameWarning', () => { | ||
| test('should output warnings to console.warn', () => { | ||
| const warnings = ['Warning 1', 'Warning 2']; | ||
| issueToolNameWarning('test-tool', warnings); | ||
|
|
||
| expect(warnSpy).toHaveBeenCalledTimes(6); // Header + 2 warnings + 3 guidance lines | ||
| const calls = warnSpy.mock.calls.map(call => call.join(' ')); | ||
| expect(calls[0]).toContain('Tool name validation warning for "test-tool"'); | ||
| expect(calls[1]).toContain('- Warning 1'); | ||
| expect(calls[2]).toContain('- Warning 2'); | ||
| expect(calls[3]).toContain('Tool registration will proceed, but this may cause compatibility issues.'); | ||
| expect(calls[4]).toContain('Consider updating the tool name'); | ||
| expect(calls[5]).toContain('See SEP: Specify Format for Tool Names'); | ||
| }); | ||
|
|
||
| test('should handle empty warnings array', () => { | ||
| issueToolNameWarning('test-tool', []); | ||
| expect(warnSpy).toHaveBeenCalledTimes(0); | ||
| }); | ||
| }); | ||
|
|
||
| describe('validateAndWarnToolName', () => { | ||
| test('should return true and issue warnings for valid names with warnings', () => { | ||
| const result = validateAndWarnToolName('-get-user-'); | ||
| expect(result).toBe(true); | ||
| expect(warnSpy).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('should return true and not issue warnings for completely valid names', () => { | ||
| const result = validateAndWarnToolName('get-user-profile'); | ||
| expect(result).toBe(true); | ||
| expect(warnSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test('should return false and issue warnings for invalid names', () => { | ||
| const result = validateAndWarnToolName('get user profile'); | ||
| expect(result).toBe(false); | ||
| expect(warnSpy).toHaveBeenCalled(); // Now issues warnings instead of errors | ||
| const warningCalls = warnSpy.mock.calls.map(call => call.join(' ')); | ||
| expect(warningCalls.some(call => call.includes('Tool name contains spaces'))).toBe(true); | ||
| }); | ||
|
|
||
| test('should return false for empty names', () => { | ||
| const result = validateAndWarnToolName(''); | ||
| expect(result).toBe(false); | ||
| expect(warnSpy).toHaveBeenCalled(); // Now issues warnings instead of errors | ||
| }); | ||
|
|
||
| test('should return false for names exceeding length limit', () => { | ||
| const longName = 'a'.repeat(129); | ||
| const result = validateAndWarnToolName(longName); | ||
| expect(result).toBe(false); | ||
| expect(warnSpy).toHaveBeenCalled(); // Now issues warnings instead of errors | ||
| }); | ||
| }); | ||
|
|
||
| describe('edge cases and robustness', () => { | ||
| test('should warn about names with only dots', () => { | ||
| const result = validateToolName('...'); | ||
| expect(result.isValid).toBe(true); | ||
| expect(result.warnings).toContain('Tool name starts or ends with a dot, which may cause parsing issues in some contexts'); | ||
| }); | ||
|
|
||
| test('should handle names with only dashes', () => { | ||
| const result = validateToolName('---'); | ||
| expect(result.isValid).toBe(true); | ||
| expect(result.warnings).toContain('Tool name starts or ends with a dash, which may cause parsing issues in some contexts'); | ||
| }); | ||
|
|
||
| test('should reject names with only forward slashes', () => { | ||
| const result = validateToolName('///'); | ||
| expect(result.isValid).toBe(false); | ||
| expect(result.warnings).toContain('Tool name contains invalid characters: "/"'); | ||
| }); | ||
|
|
||
| test('should handle names with mixed valid and invalid characters', () => { | ||
| const result = validateToolName('user@name123'); | ||
| expect(result.isValid).toBe(false); | ||
| expect(result.warnings).toContain('Tool name contains invalid characters: "@"'); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the core change