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
| Name | Required | Description | Default |
|---|---|---|---|
| prompt | Yes | ||
| model | No | grok-4-1-fast | |
| max_turns | No |
Implementation Reference
- src/server.py:278-301 (handler)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)
- src/server.py:278-283 (schema)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()
- src/server.py:7-7 (helper)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