Skip to main content
Glama
bazinga012

MCP Code Executor

execute_code_file

Execute Python code files to run scripts and applications, enabling automated testing and program execution within a controlled environment.

Instructions

Execute an existing Python file. Use this as the final step after building up code with initialize_code_file and append_to_code_file.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYesFull path to the Python file to execute

Implementation Reference

  • Core handler function that executes the Python code from the given file path using the configured environment, captures output and errors, and returns a structured JSON response.
    async function executeCodeFromFile(filePath: string) {
        try {
            // Ensure file exists
            await access(filePath);
    
            // Get platform-specific command with unbuffered output
            const pythonCmd = platform() === 'win32' ? `python -u "${filePath}"` : `python3 -u "${filePath}"`;
            const { command, options } = getPlatformSpecificCommand(pythonCmd);
    
            // Execute code with unbuffered Python
            const { stdout, stderr } = await execAsync(command, {
                cwd: CODE_STORAGE_DIR,
                env: { ...process.env, PYTHONUNBUFFERED: '1' },
                ...options
            });
    
            const response = {
                status: stderr ? 'error' : 'success',
                output: stderr || stdout,
                file_path: filePath
            };
    
            return {
                type: 'text',
                text: JSON.stringify(response),
                isError: !!stderr
            };
        } catch (error) {
            const response = {
                status: 'error',
                error: error instanceof Error ? error.message : String(error),
                file_path: filePath
            };
    
            return {
                type: 'text',
                text: JSON.stringify(response),
                isError: true
            };
        }
  • src/index.ts:588-602 (registration)
    Tool registration in the ListTools response, defining the name, description, and input schema.
    {
        name: "execute_code_file",
        description: "Execute an existing Python file. Use this as the final step after building up code with initialize_code_file and append_to_code_file.",
        inputSchema: {
            type: "object",
            properties: {
                file_path: {
                    type: "string",
                    description: "Full path to the Python file to execute"
                }
            },
            required: ["file_path"]
        }
    },
    {
  • TypeScript interface defining the expected input arguments for the tool.
    interface ExecuteCodeFileArgs {
        file_path?: string;
    }
  • Dispatch handler within CallToolRequestSchema that validates input and invokes the core execution function.
    case "execute_code_file": {
        const args = request.params.arguments as ExecuteCodeFileArgs;
        if (!args?.file_path) {
            throw new Error("File path is required");
        }
    
        const result = await executeCodeFromFile(args.file_path);
    
        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