Skip to main content
Glama

generate_code

Convert natural language descriptions into Zig code for efficient development. The tool accepts prompts and context, enabling precise code generation tailored to specific requirements within the Zig MCP Server environment.

Instructions

Generate Zig code from natural language description

Input Schema

NameRequiredDescriptionDefault
contextNoAdditional context or requirements
promptYesNatural language description of desired code

Input Schema (JSON Schema)

{ "properties": { "context": { "description": "Additional context or requirements", "type": "string" }, "prompt": { "description": "Natural language description of desired code", "type": "string" } }, "required": [ "prompt" ], "type": "object" }

Implementation Reference

  • The core handler function for the 'generate_code' tool. It parses the prompt and context using ZigCodeGenerator, generates Zig code, and formats the response with notes.
    private async generateCode(prompt: string, context?: string): Promise<string> { Logger.debug(`Generating code for prompt: ${prompt}`); // Parse requirements and generate appropriate code using new utility classes const requirements = ZigCodeGenerator.parseRequirements(prompt, context); const code = ZigCodeGenerator.generateZigCode(requirements); return ` # Generated Zig Code ${code} ## Generation Notes: - Code follows modern Zig patterns and style guide - Includes comprehensive error handling where appropriate - Uses comptime optimizations when beneficial - Includes basic tests and documentation - Follows zero-cost abstraction principles ## Next Steps: 1. Review and customize the generated code for your specific needs 2. Add comprehensive tests 3. Consider performance implications for your use case 4. Add proper documentation comments `.trim(); }
  • Input schema definition for the generate_code tool, specifying prompt as required string and optional context.
    inputSchema: { type: 'object', properties: { prompt: { type: 'string', description: 'Natural language description of desired code', }, context: { type: 'string', description: 'Additional context or requirements', }, }, required: ['prompt'], },
  • src/index.ts:217-234 (registration)
    Registration of the generate_code tool in the ListToolsRequestSchema handler, including name, description, and input schema.
    { name: 'generate_code', description: 'Generate modern Zig code from natural language descriptions', inputSchema: { type: 'object', properties: { prompt: { type: 'string', description: 'Natural language description of desired code', }, context: { type: 'string', description: 'Additional context or requirements', }, }, required: ['prompt'], }, },
  • Dispatcher case in CallToolRequestSchema that handles generate_code tool calls by validating input and invoking the generateCode handler.
    case 'generate_code': this.validateStringParam(args?.prompt, 'prompt'); return { content: [ { type: 'text', text: await this.generateCode(args.prompt, args.context as string | undefined), }, ], };
  • Core helper function that generates the actual Zig code based on parsed requirements, including imports, error handling, main functionality (struct/enum/function), and tests.
    static generateZigCode(requirements: CodeGenerationRequirements): string { const hasFeature = (feature: string) => requirements.features.has(feature); let code = '//! Generated Zig code\n\n'; // Add standard imports code += 'const std = @import("std");\n'; // Add testing import if needed if (requirements.testing) { code += 'const testing = std.testing;\n'; } code += '\n'; // Add error set if needed if (requirements.errorHandling) { code += 'const Error = error{\n InvalidInput,\n OutOfMemory,\n InvalidOperation,\n};\n\n'; } // Generate main functionality if (hasFeature('struct')) { code += this.generateStruct(requirements); } else if (hasFeature('enum')) { code += this.generateEnum(requirements); } else if (hasFeature('union')) { code += this.generateUnion(requirements); } else if (hasFeature('function') || hasFeature('implement')) { code += this.generateFunction(requirements); } else { // Default to function code += this.generateFunction(requirements); } // Add tests if requested if (requirements.testing) { code += '\n\n' + this.generateTests(requirements); } return code; }

Other Tools

Related Tools

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/openSVM/zig-mcp-server'

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