Skip to main content
Glama
bazinga012

MCP Code Executor

initialize_code_file

Create a new Python file with initial content to start coding projects that may exceed token limits, enabling structured code execution in Conda environments.

Instructions

Create a new Python file with initial content. Use this as the first step for longer code that may exceed token limits. Follow with append_to_code_file for additional code.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
contentYesInitial content to write to the file
filenameNoOptional: Name of the file (default: generated UUID)

Implementation Reference

  • The core handler function that creates a new Python code file in the storage directory with the provided content, generating a unique .py filename if not specified, and returns JSON response with file path.
    async function initializeCodeFile(content: string, filename?: string) {
        try {
            // Generate a filename if not provided
            let actualFilename;
            if (filename && typeof filename === 'string') {
                // Extract base name without extension
                const baseName = filename.replace(/\.py$/, '');
                // Add a random suffix to ensure uniqueness
                actualFilename = `${baseName}_${randomBytes(4).toString('hex')}.py`;
            } else {
                // Default filename if none provided
                actualFilename = `code_${randomBytes(4).toString('hex')}.py`;
            }
            
            const filePath = join(CODE_STORAGE_DIR, actualFilename);
            
            // Write initial content to file
            await writeFile(filePath, content, 'utf-8');
            
            return {
                type: 'text',
                text: JSON.stringify({
                    status: 'success',
                    message: 'File initialized successfully',
                    file_path: filePath,
                    filename: actualFilename
                }),
                isError: false
            };
        } catch (error) {
            return {
                type: 'text',
                text: JSON.stringify({
                    status: 'error',
                    error: error instanceof Error ? error.message : String(error)
                }),
                isError: true
            };
        }
    }
  • src/index.ts:552-569 (registration)
    Tool registration in the ListToolsRequestSchema handler, specifying name, description, and input schema for validation.
    {
        name: "initialize_code_file",
        description: "Create a new Python file with initial content. Use this as the first step for longer code that may exceed token limits. Follow with append_to_code_file for additional code.",
        inputSchema: {
            type: "object",
            properties: {
                content: {
                    type: "string",
                    description: "Initial content to write to the file"
                },
                filename: {
                    type: "string",
                    description: "Optional: Name of the file (default: generated UUID)"
                }
            },
            required: ["content"]
        }
    },
  • TypeScript interface defining the expected arguments for the initialize_code_file tool.
    interface InitializeCodeFileArgs {
        content?: string;
        filename?: string;
    }
  • Dispatch handler case in the CallToolRequestSchema that validates arguments and invokes the initializeCodeFile function, wrapping the result in MCP content format.
    case "initialize_code_file": {
        const args = request.params.arguments as InitializeCodeFileArgs;
        if (!args?.content) {
            throw new Error("Content is required");
        }
    
        const result = await initializeCodeFile(args.content, args.filename);
    
        return {
            content: [{
                type: "text",
                text: result.text,
                isError: result.isError
            }]
        };
    }

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/bazinga012/mcp_code_executor'

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