{"record":{"id":"f56be94cb882d34a","repo":"linshenkx/prompt-optimizer","slug":"template-must-be-a-non-empty-string","errorCode":null,"errorMessage":"Template must be a non-empty string","messagePattern":"Template must be a non-empty string","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/mcp-server/src/adapters/parameter-adapter.ts","lineNumber":25,"sourceCode":"\n  /**\n   * 验证提示词输入\n   */\n  static validatePrompt(prompt: string): void {\n    if (!prompt || typeof prompt !== 'string' || prompt.trim().length === 0) {\n      throw new Error('Prompt must be a non-empty string');\n    }\n    if (prompt.length > 50000) {\n      throw new Error('Prompt must not exceed 50,000 characters');\n    }\n  }\n\n  /**\n   * 验证模板输入\n   */\n  static validateTemplate(template?: string): void {\n    if (template !== undefined && (typeof template !== 'string' || template.trim().length === 0)) {\n      throw new Error('Template must be a non-empty string');\n    }\n  }\n\n  /**\n   * 验证需求描述输入\n   */\n  static validateRequirements(requirements: string): void {\n    if (!requirements || typeof requirements !== 'string' || requirements.trim().length === 0) {\n      throw new Error('Requirements must be a non-empty string');\n    }\n    if (requirements.length > 10000) {\n      throw new Error('Requirements must not exceed 10,000 characters');\n    }\n  }\n}\n","sourceCodeStart":7,"sourceCodeEnd":41,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/mcp-server/src/adapters/parameter-adapter.ts#L7-L41","documentation":"ParameterAdapter.validateTemplate rejects an optional template argument that is defined but not a non-empty string. undefined is allowed (the parameter is optional), but any empty or whitespace-only string, or a non-string value, throws.","triggerScenarios":"Calling the MCP tool with template: '' (empty string instead of omitting it), template: '   ', or template: 42/null. Passing the key with an empty value is the most common case — clients that always populate optional fields produce this.","commonSituations":"Client code setting optional fields to empty strings by default; template name read from config that is unset, yielding ''; form builders that send all keys regardless of value.","solutions":["Omit the template key entirely instead of sending template: ''","Send a meaningful template identifier string, or leave it undefined","Sanitize optional args: delete args.template if it's an empty/whitespace string"],"exampleFix":"// before\nawait tool.call({ prompt, template: config.template ?? '' });\n\n// after\nconst args: { prompt: string; template?: string } = { prompt };\nif (config.template?.trim()) args.template = config.template;\nawait tool.call(args);","handlingStrategy":"validation","validationCode":"if (args.template !== undefined && (typeof args.template !== 'string' || !args.template.trim())) {\n  delete args.template;\n}","typeGuard":"const isValidOptionalTemplate = (t: unknown): t is string | undefined =>\n  t === undefined || (typeof t === 'string' && t.trim().length > 0);","tryCatchPattern":"try { await call(args); } catch (e) { if ((e as Error).message.includes('Template must be')) { delete args.template; await call(args); } else throw e; }","preventionTips":["Omit optional keys rather than sending empty strings","Sanitize tool args by stripping empty-string values before dispatch"],"tags":["validation","input-validation","template","mcp-server"],"backgroundTag":"invalid-optional-parameter","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}