apply_prompt
Apply variables to template prompts to generate customized content for various use cases.
Instructions
Applies variables to a template prompt and returns the result
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| id | Yes | ID of the prompt to apply | |
| variables | Yes | Object containing variable names and their values |
Implementation Reference
- src/services/prompts.service.ts:270-318 (handler)Core handler function that implements the apply_prompt tool logic: validates input, loads prompt, checks and replaces variables in single-content or multi-message prompts, returns formatted result.async applyPrompt(params: ApplyPromptParams): Promise<ApplyPromptResult> { if (!params.id || !params.id.trim()) { throw new ValidationError('Prompt ID is required') } if (!params.variables || typeof params.variables !== 'object') { throw new ValidationError('Variables must be provided as an object') } try { const prompt = await this.getPrompt(params.id) // Check if all required variables are provided const missingVariables = prompt.variables.filter( variable => !(variable in params.variables) ) if (missingVariables.length > 0) { throw new ValidationError( `Missing required variables: ${missingVariables.join(', ')}` ) } if (isMultiMessagePrompt(prompt)) { // Handle multi-message prompts const appliedMessages = prompt.messages.map(message => ({ ...message, content: { ...message.content, ...(message.content.type === 'text' && message.content.text && { text: this.replaceVariables(message.content.text, params.variables) }) } })) return { result: JSON.stringify(appliedMessages, null, 2), messages: appliedMessages } } else { // Handle single content prompts const result = this.replaceVariables(prompt.content, params.variables) return { result } } } catch (error) { console.error('Failed to apply prompt:', error) throw error } }
- src/handlers/tools.handler.ts:260-271 (handler)MCP tool dispatch handler for 'apply_prompt': extracts args, calls PromptsService.applyPrompt, formats response as MCP content.case 'apply_prompt': { const { id, variables } = args const result = await this.promptsService.applyPrompt({ id, variables }) return { content: [{ type: 'text', text: typeof result.result === 'string' ? result.result : JSON.stringify(result, null, 2) }] } }
- src/handlers/tools.handler.ts:130-146 (registration)Tool registration in listTools(): defines name, description, and inputSchema for 'apply_prompt'.name: 'apply_prompt', description: 'Applies variables to a template prompt and returns the result', inputSchema: { type: 'object', properties: { id: { type: 'string', description: 'ID of the prompt to apply' }, variables: { type: 'object', description: 'Object containing variable names and their values' } }, required: ['id', 'variables'] } },
- src/types/index.ts:69-77 (schema)Type definitions for ApplyPromptParams (input) and ApplyPromptResult (output) used by the tool.export interface ApplyPromptParams { id: string variables: Record<string, string> } export interface ApplyPromptResult { result: string messages?: PromptMessage[] // For multi-message prompts }