Skip to main content
Glama

code_executor

Execute code snippets directly within the Grok MCP environment to test algorithms, debug scripts, or prototype solutions using specified AI models.

Input Schema

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

Implementation Reference

  • The code_executor tool handler that executes code using XAI SDK. It creates a chat session with the code_execution tool enabled, processes the user prompt, and returns both the response content and any code execution outputs.
    @mcp.tool() 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)
  • Input schema definition for code_executor tool with parameters: prompt (required), model (defaults to 'grok-4-1-fast'), and max_turns (optional).
    @mcp.tool() async def code_executor( prompt: str, model: str = "grok-4-1-fast", max_turns: Optional[int] = None ):
  • src/server.py:278-278 (registration)
    Tool registration using the @mcp.tool() decorator which registers code_executor as an MCP tool.
    @mcp.tool()
  • Import of code_execution tool from xai_sdk.tools which is used by the code_executor handler.
    from xai_sdk.tools import web_search as xai_web_search, x_search as xai_x_search, code_execution

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