linshenkx/prompt-optimizer · error · Error

Template must be a non-empty string

Error message

Template must be a non-empty string

What it means

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.

Source

Thrown at packages/mcp-server/src/adapters/parameter-adapter.ts:25

  /**
   * 验证提示词输入
   */
  static validatePrompt(prompt: string): void {
    if (!prompt || typeof prompt !== 'string' || prompt.trim().length === 0) {
      throw new Error('Prompt must be a non-empty string');
    }
    if (prompt.length > 50000) {
      throw new Error('Prompt must not exceed 50,000 characters');
    }
  }

  /**
   * 验证模板输入
   */
  static validateTemplate(template?: string): void {
    if (template !== undefined && (typeof template !== 'string' || template.trim().length === 0)) {
      throw new Error('Template must be a non-empty string');
    }
  }

  /**
   * 验证需求描述输入
   */
  static validateRequirements(requirements: string): void {
    if (!requirements || typeof requirements !== 'string' || requirements.trim().length === 0) {
      throw new Error('Requirements must be a non-empty string');
    }
    if (requirements.length > 10000) {
      throw new Error('Requirements must not exceed 10,000 characters');
    }
  }
}

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Omit the template key entirely instead of sending template: ''
  2. Send a meaningful template identifier string, or leave it undefined
  3. Sanitize optional args: delete args.template if it's an empty/whitespace string

Example fix

// before
await tool.call({ prompt, template: config.template ?? '' });

// after
const args: { prompt: string; template?: string } = { prompt };
if (config.template?.trim()) args.template = config.template;
await tool.call(args);
Defensive patterns

Strategy: validation

Validate before calling

if (args.template !== undefined && (typeof args.template !== 'string' || !args.template.trim())) {
  delete args.template;
}

Type guard

const isValidOptionalTemplate = (t: unknown): t is string | undefined =>
  t === undefined || (typeof t === 'string' && t.trim().length > 0);

Try / catch

try { await call(args); } catch (e) { if ((e as Error).message.includes('Template must be')) { delete args.template; await call(args); } else throw e; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27). Data as JSON: /api/errors/f56be94cb882d34a. Report an issue: GitHub.