Skip to main content
Glama
lumile

Promptopia MCP

by lumile

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
NameRequiredDescriptionDefault
idYesID of the prompt to apply
variablesYesObject containing variable names and their values

Implementation Reference

  • 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
      }
    }
  • 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)
        }]
      }
    }
  • 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']
      }
    },
  • 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
    }

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/lumile/promptopia-mcp'

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