Skip to main content
Glama

validate_prompt

Check if provided parameters match a prompt template's requirements before rendering to ensure compatibility and prevent errors.

Instructions

Validate parameters against a prompt template without rendering

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
templateIdYesThe ID of the template
parametersYesParameters to validate

Implementation Reference

  • Core implementation of prompt parameter validation: checks template existence, required parameters using helper, and type validation for each parameter.
    validate(
      templateId: string,
      params: Record<string, unknown>
    ): { valid: boolean; errors: string[] } {
      const template = this.get(templateId);
      if (!template) {
        return { valid: false, errors: [`Template not found: ${templateId}`] };
      }
    
      const paramValidation = validateParameters(template, params);
      if (!paramValidation.valid) {
        return {
          valid: false,
          errors: [`Missing required parameters: ${paramValidation.missing.join(", ")}`],
        };
      }
    
      // Type validation
      const errors: string[] = [];
      for (const param of template.parameters) {
        const value = params[param.name];
        if (value !== undefined && !this.validateType(value, param.type)) {
          errors.push(
            `Parameter '${param.name}' should be type '${param.type}', got '${typeof value}'`
          );
        }
      }
    
      return {
        valid: errors.length === 0,
        errors,
      };
    }
  • MCP tool registration for 'validate_prompt', defining input schema with Zod and thin handler delegating to PromptManager.validate.
    server.tool(
      "validate_prompt",
      "Validate parameters against a prompt template without rendering",
      {
        templateId: z.string().describe("The ID of the template"),
        parameters: z.record(z.any()).describe("Parameters to validate"),
      },
      async (args) => {
        const validation = registry.prompts.validate(args.templateId, args.parameters);
    
        return {
          content: [
            {
              type: "text",
              text: JSON.stringify(validation, null, 2),
            },
          ],
        };
      }
    );
  • Helper function to check for missing required parameters in prompt templates.
    function validateParameters(
      template: PromptTemplate,
      params: Record<string, unknown>
    ): { valid: boolean; missing: string[] } {
      const required = template.parameters.filter((p) => p.required);
      const missing = required.filter((p) => !(p.name in params));
      return {
        valid: missing.length === 0,
        missing: missing.map((p) => p.name),
      };
    }
  • Private helper to validate parameter types (string, number, boolean, array, object).
    private validateType(value: unknown, expectedType: string): boolean {
      switch (expectedType) {
        case "string":
          return typeof value === "string";
        case "number":
          return typeof value === "number";
        case "boolean":
          return typeof value === "boolean";
        case "array":
          return Array.isArray(value);
        case "object":
          return typeof value === "object" && value !== null && !Array.isArray(value);
        default:
          return true;
      }
    }
  • Zod input schema for the validate_prompt tool: templateId (string), parameters (record).
    {
      templateId: z.string().describe("The ID of the template"),
      parameters: z.record(z.any()).describe("Parameters to validate"),
    },

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/ishuru/open-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server