Skip to main content
Glama

render_template

Generate dynamic content by applying variables to predefined templates within the DollhouseMCP server for AI persona management.

Instructions

Render a template element with provided variables

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
nameYesThe template name to render
variablesYesVariables to use in the template

Implementation Reference

  • Registration of the 'render_template' tool including input schema, description, and handler function that executes the tool by calling server.renderTemplate
    // Element-specific tools
    {
      tool: {
        name: "render_template",
        description: "Render a template element with provided variables",
        inputSchema: {
          type: "object",
          properties: {
            name: {
              type: "string",
              description: "The template name to render",
            },
            variables: {
              type: "object",
              description: "Variables to use in the template",
              additionalProperties: true,
            },
          },
          required: ["name", "variables"],
        },
      },
      handler: (args: RenderTemplateArgs) => server.renderTemplate(args.name, args.variables)
    },
  • Type definition for tool input arguments
    interface RenderTemplateArgs {
      name: string;
      variables: Record<string, any>;
    }
  • Interface definition for the underlying server.renderTemplate service method
    renderTemplate(name: string, variables: Record<string, any>): Promise<any>;
  • Core implementation of template rendering logic in Template class (called indirectly via server.renderTemplate)
    async render<T extends Record<string, unknown>>(
      variables: T = {} as T, 
      includeDepth: number = 0
    ): Promise<string> {
      // SECURITY FIX #4: Prevent infinite include loops
      if (includeDepth > this.MAX_INCLUDE_DEPTH) {
        SecurityMonitor.logSecurityEvent({
          type: 'INCLUDE_DEPTH_EXCEEDED',
          severity: 'HIGH',
          source: 'Template.render',
          details: `Include depth ${includeDepth} exceeds maximum ${this.MAX_INCLUDE_DEPTH}`
        });
        throw ErrorHandler.createError('Maximum template include depth exceeded', ErrorCategory.VALIDATION_ERROR, ValidationErrorCodes.MAX_INCLUDE_DEPTH);
      }
    
      // Compile the template
      const compiled = this.compile();
      
      // Validate and sanitize all provided variables
      const sanitizedVariables = await this.validateAndSanitizeVariables(variables);
      
      // Start with the template content
      let rendered = compiled.content;
      
      // Replace tokens in reverse order to maintain positions
      const sortedTokens = [...compiled.tokens].sort((a, b) => b.position - a.position);
      
      for (const token of sortedTokens) {
        const value = this.resolveVariable(token.variable, sanitizedVariables);
        const stringValue = this.formatValue(value);
        
        // Replace the token with the sanitized value
        rendered = rendered.substring(0, token.position) + 
                   stringValue + 
                   rendered.substring(token.position + token.token.length);
      }
      
      // Process includes if any
      if (this.metadata.includes && this.metadata.includes.length > 0) {
        rendered = await this.processIncludes(rendered, sanitizedVariables, includeDepth);
      }
      
      // Update usage statistics
      // NOTE: These updates are not atomic and may have race conditions under concurrent access
      // This is acceptable for usage statistics which don't require perfect accuracy
      // For production systems requiring atomic counters, consider using a database or atomic operations
      this.metadata.usage_count = (this.metadata.usage_count || 0) + 1;
      this.metadata.last_used = new Date().toISOString();
      
      // SECURITY FIX #5: Log template usage for audit trail
      SecurityMonitor.logSecurityEvent({
        type: 'TEMPLATE_RENDERED',
        severity: 'LOW',
        source: 'Template.render',
        details: `Template ${this.metadata.name} rendered with ${Object.keys(sanitizedVariables).length} variables`
      });
      
      return rendered;
    }
  • Global registration of all element tools (including render_template) into the MCP tool registry during server setup
    // Register element tools (new generic tools for all element types)
    this.toolRegistry.registerMany(getElementTools(instance));

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/DollhouseMCP/DollhouseMCP'

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