create_resource_template
Generate template code for new MCP resources by specifying resource name, URI pattern, description, and output directory. Simplifies resource creation within the MCP Maker server framework.
Instructions
Generates template code for a new MCP resource
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| description | Yes | ||
| output_dir | Yes | ||
| resource_name | Yes | ||
| resource_uri_pattern | Yes |
Implementation Reference
- The main handler function that validates input, generates the resource file path, checks for existence, compiles a Handlebars template with derived variables, writes the file, and returns success/error details.export async function createResourceTemplate( options: ResourceTemplateOptions ): Promise<{ success: boolean; message: string; filePath?: string }> { try { // Validate options const validatedOptions = resourceTemplateSchema.parse(options); // Use the provided output directory (now required and absolute) const baseDir = validatedOptions.output_dir; // Ensure the resources directory exists const resourcesDir = path.join(baseDir, "src", "resources"); await ensureDir(resourcesDir); // Generate the file path for the new resource const resourceFilename = `${validatedOptions.resource_name.replace( /-/g, "_" )}.ts`; const resourceFilePath = path.join(resourcesDir, resourceFilename); // Check if the file already exists const fileExists = await pathExists(resourceFilePath); if (fileExists) { return { success: false, message: `A resource with the name "${validatedOptions.resource_name}" already exists at ${resourceFilePath}`, }; } // Generate the resource content using the template const resourceContent = await compileTemplate( getTemplatePath("resource.hbs"), { ...validatedOptions, // Additional template variables resource_camel_case: validatedOptions.resource_name .replace(/-/g, "_") .replace(/_([a-z])/g, (_, char) => char.toUpperCase()), resource_pascal_case: validatedOptions.resource_name .replace(/-/g, "_") .split("_") .map((part) => part.charAt(0).toUpperCase() + part.slice(1)) .join(""), } ); // Write the resource file await writeFile(resourceFilePath, resourceContent); console.log( chalk.green( `Resource template generated successfully at: ${resourceFilePath}` ) ); return { success: true, message: `Resource template generated successfully at: ${resourceFilePath}`, filePath: resourceFilePath, }; } catch (error: any) { console.error(chalk.red("Error creating resource template:"), error); return { success: false, message: `Error creating resource template: ${ error.message || String(error) }`, }; } }
- Zod validation schema for tool inputs, enforcing snake_case for resource_name and absolute path for output_dir.export const resourceTemplateSchema = z.object({ resource_name: z .string() .min(1) .refine((val) => /^[a-z0-9_-]+$/.test(val), { message: "Resource name must be in snake_case (lowercase with underscores)", }), resource_uri_pattern: z.string().min(1), description: z.string().min(1), output_dir: z.string().refine((val) => path.isAbsolute(val), { message: "output_dir must be an absolute path", }), });
- src/server.ts:79-94 (registration)MCP server tool registration, including tool name, description, Zod input schema, and wrapper async handler that delegates to createResourceTemplate.server.tool( "create_resource_template", "Generates template code for a new MCP resource", { resource_name: z.string().min(1), resource_uri_pattern: z.string().min(1), description: z.string().min(1), output_dir: z.string(), }, async (params: ResourceTemplateOptions) => { const result = await createResourceTemplate(params); return { content: [{ type: "text", text: result.message }], }; } );
- src/types.ts:45-53 (schema)TypeScript interface defining the shape of options passed to the createResourceTemplate handler.export interface ResourceTemplateOptions { resource_name: string; resource_uri_pattern: string; description: string; /** * Absolute path to the directory where the resource should be generated */ output_dir: string; }