UNPKG

@mettamatt/code-reasoning

Version:

Enhanced MCP server for code reasoning using sequential thinking methodology, optimized for programming tasks

115 lines (114 loc) 4.29 kB
/** * @fileoverview Prompt manager for MCP prompts. * * This class handles the management of prompts, including registration, * validation, and application of prompt templates. * * It implements the standard MCP CompleteRequestSchema protocol for * providing auto-completion of prompt arguments with previously stored values. */ import { Prompt, PromptResult } from './types.js'; /** * Manages prompt templates and their operations. * Uses the CompleteRequestSchema MCP protocol for argument completion. */ export declare class PromptManager { private prompts; private templates; private valueManager; private readonly baseStringSchema; private readonly codeSchema; private readonly workingDirectorySchema; private readonly PromptArgumentSchema; private readonly PromptDataSchema; /** * Creates a new PromptManager instance with default code reasoning prompts. * * @param configDir Optional directory for configuration files. Defaults to the centralized CONFIG_DIR. */ constructor(configDir?: string); /** * Registers a new prompt and its template function. * * @param prompt The prompt definition * @param template The template function that applies arguments to generate a result */ registerPrompt(prompt: Prompt, template: (args: Record<string, string>) => PromptResult): void; /** * Gets all available prompts. * * Note: Previously stored values for prompt arguments are provided through * the CompleteRequestSchema MCP protocol, not through the prompt objects. * * @returns An array of all registered prompts */ getAllPrompts(): Prompt[]; /** * Gets a specific prompt by name. * * @param name The name of the prompt to retrieve * @returns The prompt or undefined if not found */ getPrompt(name: string): Prompt | undefined; /** * Gets stored values for a specific prompt. * This method is used by the CompleteRequestSchema handler to provide * auto-completion of prompt arguments. * * @param name The name of the prompt * @returns The stored values for the prompt */ getStoredValues(name: string): Record<string, string>; /** * Merges provided arguments with stored values, with provided args taking precedence. * This is a helper method to simplify the applyPrompt method. * * @param promptName The name of the prompt * @param args The provided arguments * @returns The merged arguments */ private mergeWithStoredValues; /** * Applies a prompt with the given arguments. * Merges provided arguments with previously stored values, * with provided arguments taking precedence. * * @param name The name of the prompt to apply * @param args The arguments to apply to the prompt template * @returns The result of applying the prompt * @throws Error if the prompt doesn't exist or arguments are invalid */ applyPrompt(name: string, args?: Record<string, string>): PromptResult; /** * Loads custom prompts from JSON files in a directory. * * @param directory The directory containing JSON prompt files */ loadCustomPrompts(directory: string): Promise<void>; /** * Gets the appropriate schema for a given argument. * * @param argName The name of the argument * @param promptName The name of the prompt * @returns A Zod schema for validating and sanitizing the argument */ private getSchemaForArg; /** * Validates prompt arguments against the prompt definition. * * @param prompt The prompt to validate against * @param args The arguments to validate * @returns Array of validation error messages, empty if valid */ private validatePromptArguments; /** * Applies a template string with argument values. * Sanitizes input values to prevent template injection and other security issues. * * @param template The template string * @param args The argument values to apply * @param promptName The name of the prompt (for context-aware sanitization) * @returns The template with arguments applied */ private applyTemplate; }