Skip to main content
Glama

code_executor

Execute code snippets directly within Grok MCP to test algorithms, debug programs, or run computational tasks using specified models and parameters.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
promptYes
modelNogrok-4-1-fast
max_turnsNo

Implementation Reference

  • The code_executor tool handler function. It accepts a prompt, optional model (default 'grok-4-1-fast'), and optional max_turns parameter. Creates an XAI client, configures it with code_execution tool, sends the prompt, and returns the response content along with any code execution outputs.
    async def code_executor(
        prompt: str,
        model: str = "grok-4-1-fast",
        max_turns: Optional[int] = None
    ):
        client = Client(api_key=XAI_API_KEY)
        
        chat_params = {"model": model, "tools": [code_execution()], "include": ["code_execution_call_output"]}
        if max_turns:
            chat_params["max_turns"] = max_turns
        
        chat = client.chat.create(**chat_params)
        chat.append(user(prompt))
        response = chat.sample()
        
        client.close()
    
        result = [response.content]
        if response.tool_outputs:
            result.append("\n\n**Code Output(s):**")
            for output in response.tool_outputs:
                result.append(f"```\n{output.message.content}\n```")
        return "\n".join(result)
  • src/server.py:340-340 (registration)
    The @mcp.tool() decorator registers the code_executor function as an MCP tool. The mcp instance is a FastMCP server created at line 11.
    @mcp.tool()
  • Import of code_execution tool from xai_sdk.tools, which is used in the code_executor handler to enable code execution capabilities.
    from xai_sdk.tools import web_search as xai_web_search, x_search as xai_x_search, code_execution
  • Import of Client from xai_sdk, used to create the API client instance in the code_executor handler.
    from xai_sdk import Client

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/merterbak/Grok-MCP'

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