toolNameValidation.js 3.9 KB

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