toolNameValidation.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * Tool name validation utilities according to SEP: Specify Format for Tool Names
  3. *
  4. * Tool names SHOULD be between 1 and 128 characters in length (inclusive).
  5. * Tool names are case-sensitive.
  6. * Allowed characters: uppercase and lowercase ASCII letters (A-Z, a-z), digits
  7. * (0-9), underscore (_), dash (-), and dot (.).
  8. * Tool names SHOULD NOT contain spaces, commas, or other special characters.
  9. */
  10. /**
  11. * Regular expression for valid tool names according to SEP-986 specification
  12. */
  13. const TOOL_NAME_REGEX = /^[A-Za-z0-9._-]{1,128}$/;
  14. /**
  15. * Validates a tool name according to the SEP specification
  16. * @param name - The tool name to validate
  17. * @returns An object containing validation result and any warnings
  18. */
  19. export function validateToolName(name) {
  20. const warnings = [];
  21. // Check length
  22. if (name.length === 0) {
  23. return {
  24. isValid: false,
  25. warnings: ['Tool name cannot be empty']
  26. };
  27. }
  28. if (name.length > 128) {
  29. return {
  30. isValid: false,
  31. warnings: [`Tool name exceeds maximum length of 128 characters (current: ${name.length})`]
  32. };
  33. }
  34. // Check for specific problematic patterns (these are warnings, not validation failures)
  35. if (name.includes(' ')) {
  36. warnings.push('Tool name contains spaces, which may cause parsing issues');
  37. }
  38. if (name.includes(',')) {
  39. warnings.push('Tool name contains commas, which may cause parsing issues');
  40. }
  41. // Check for potentially confusing patterns (leading/trailing dashes, dots, slashes)
  42. if (name.startsWith('-') || name.endsWith('-')) {
  43. warnings.push('Tool name starts or ends with a dash, which may cause parsing issues in some contexts');
  44. }
  45. if (name.startsWith('.') || name.endsWith('.')) {
  46. warnings.push('Tool name starts or ends with a dot, which may cause parsing issues in some contexts');
  47. }
  48. // Check for invalid characters
  49. if (!TOOL_NAME_REGEX.test(name)) {
  50. const invalidChars = name
  51. .split('')
  52. .filter(char => !/[A-Za-z0-9._-]/.test(char))
  53. .filter((char, index, arr) => arr.indexOf(char) === index); // Remove duplicates
  54. warnings.push(`Tool name contains invalid characters: ${invalidChars.map(c => `"${c}"`).join(', ')}`, 'Allowed characters are: A-Z, a-z, 0-9, underscore (_), dash (-), and dot (.)');
  55. return {
  56. isValid: false,
  57. warnings
  58. };
  59. }
  60. return {
  61. isValid: true,
  62. warnings
  63. };
  64. }
  65. /**
  66. * Issues warnings for non-conforming tool names
  67. * @param name - The tool name that triggered the warnings
  68. * @param warnings - Array of warning messages
  69. */
  70. export function issueToolNameWarning(name, warnings) {
  71. if (warnings.length > 0) {
  72. console.warn(`Tool name validation warning for "${name}":`);
  73. for (const warning of warnings) {
  74. console.warn(` - ${warning}`);
  75. }
  76. console.warn('Tool registration will proceed, but this may cause compatibility issues.');
  77. console.warn('Consider updating the tool name to conform to the MCP tool naming standard.');
  78. console.warn('See SEP: Specify Format for Tool Names (https://github.com/modelcontextprotocol/modelcontextprotocol/issues/986) for more details.');
  79. }
  80. }
  81. /**
  82. * Validates a tool name and issues warnings for non-conforming names
  83. * @param name - The tool name to validate
  84. * @returns true if the name is valid, false otherwise
  85. */
  86. export function validateAndWarnToolName(name) {
  87. const result = validateToolName(name);
  88. // Always issue warnings for any validation issues (both invalid names and warnings)
  89. issueToolNameWarning(name, result.warnings);
  90. return result.isValid;
  91. }
  92. //# sourceMappingURL=toolNameValidation.js.map