Skip to main content
Glama
Garblesnarff

Gemini MCP Server for Claude Desktop

gemini-code-execute

Execute Python code in a secure sandbox environment with intelligent enhancement based on user preferences and context.

Instructions

Execute Python code using Gemini's built-in code execution sandbox (with learned user preferences)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeYesPython code to execute in the sandbox
contextNoOptional context for intelligent enhancement (e.g., "data-science", "automation", "testing")

Implementation Reference

  • The main handler function that executes Python code using Gemini's code execution capabilities, with optional context enhancement via the intelligence system.
    async execute(args) {
      const code = validateNonEmptyString(args.code, 'code');
      const context = args.context ? validateString(args.context, 'context') : null;
    
      log(`Executing Python code: ${code.substring(0, 100)}... with context: ${context || 'general'}`, this.name);
    
      try {
        const basePrompt = `Please execute this Python code and show the results:\n\n\`\`\`python\n${code}\n\`\`\`\n\nExecute the code and provide both the code output and any results.`;
    
        let enhancedPrompt = basePrompt;
        if (this.intelligenceSystem.initialized) {
          try {
            enhancedPrompt = await this.intelligenceSystem.enhancePrompt(basePrompt, context, this.name);
            log('Applied Tool Intelligence enhancement', this.name);
          } catch (err) {
            log(`Tool Intelligence enhancement failed: ${err.message}`, this.name);
          }
        }
    
        let prompt = enhancedPrompt;
        if (context) {
          prompt += `\n\nContext: ${context}`;
        }
    
        const candidate = await this.geminiService.generateText('CODE_EXECUTION', prompt);
        const responseText = candidate; // Assuming generateText returns the direct text content
        const executionResults = []; // Code execution results are not directly extracted from generateText
    
        if (responseText || executionResults.length > 0) {
          let finalResponse = `✓ Python code executed successfully:\n\n**Code:**\n\`\`\`python\n${code}\n\`\`\`\n\n`; // eslint-disable-line max-len
    
          if (responseText) {
            finalResponse += `**Response:**\n${responseText}\n\n`;
          }
    
          if (executionResults.length > 0) {
            finalResponse += `**Execution Results:**\n${JSON.stringify(executionResults, null, 2)}\n`;
          }
    
          if (this.intelligenceSystem.initialized) {
            try {
              const resultSummary = `Code executed successfully with ${executionResults.length} results and ${responseText ? 'response text' : 'no response text'}`; // eslint-disable-line max-len
              await this.intelligenceSystem.learnFromInteraction(basePrompt, enhancedPrompt, resultSummary, context, this.name);
              log('Tool Intelligence learned from interaction', this.name);
            } catch (err) {
              log(`Tool Intelligence learning failed: ${err.message}`, this.name);
            }
          }
    
          if (context && this.intelligenceSystem.initialized) {
            finalResponse += `\n\n---\n_Enhancement applied based on context: ${context}_`;
          }
    
          log('Code execution completed successfully', this.name);
          return {
            content: [
              {
                type: 'text',
                text: finalResponse,
              },
            ],
          };
        }
        log('No response text or execution results generated', this.name);
        return {
          content: [
            {
              type: 'text',
              text: `Code execution completed but no output was generated. The code may have run successfully without producing visible results.\n\n**Code:**\n\`\`\`python\n${code}\n\`\`\``, // eslint-disable-line max-len
            },
          ],
        };
      } catch (error) {
        log(`Error executing code: ${error.message}`, this.name);
        throw new Error(`Error executing code: ${error.message}`);
      }
    }
  • JSON schema defining the input parameters for the tool: required 'code' string and optional 'context' string.
      type: 'object',
      properties: {
        code: {
          type: 'string',
          description: 'Python code to execute in the sandbox',
        },
        context: {
          type: 'string',
          description: 'Optional context for intelligent enhancement (e.g., "data-science", "automation", "testing")',
        },
      },
      required: ['code'],
    },
  • Registers an instance of the CodeExecutionTool (named 'gemini-code-execute') with the tool registry.
    registerTool(new CodeExecutionTool(intelligenceSystem, geminiService));
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It mentions 'code execution sandbox' and 'learned user preferences', hinting at a safe, isolated environment and personalized behavior, but fails to detail critical aspects like execution timeouts, memory limits, supported Python versions, error handling, or security restrictions. For a code execution tool with zero annotation coverage, this leaves significant gaps in understanding its operational behavior.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that front-loads the core functionality ('Execute Python code') and adds relevant context ('using Gemini's built-in code execution sandbox' and 'with learned user preferences'). There's no wasted verbiage, and it effectively communicates the tool's essence without unnecessary elaboration.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity of a code execution tool, the absence of annotations, and no output schema, the description is insufficiently complete. It doesn't explain what happens during execution (e.g., sandbox isolation, result formats, error outputs) or how 'learned user preferences' manifest. For a tool that could have significant behavioral nuances and safety implications, more detail is needed to guide the agent effectively.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The schema description coverage is 100%, with clear descriptions for both parameters ('code' and 'context') in the input schema. The description adds minimal value beyond this, only implying that 'context' might influence enhancements based on user preferences. Since the schema already documents parameters thoroughly, the baseline score of 3 is appropriate, as the description doesn't significantly enhance parameter understanding.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('Execute Python code') and the resource ('Gemini's built-in code execution sandbox'), which is specific and unambiguous. However, it doesn't explicitly differentiate from sibling tools like 'gemini-chat' or 'gemini-nano-banana-pro', which might also involve code execution or processing. The mention of 'learned user preferences' adds nuance but doesn't fully establish uniqueness among siblings.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention prerequisites, limitations, or scenarios where other tools (e.g., 'gemini-chat' for conversational code help or 'gemini-analyze-image' for image-related tasks) might be more appropriate. The lack of explicit usage context leaves the agent without direction on tool selection.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other 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/Garblesnarff/gemini-mcp-server'

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