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
| Name | Required | Description | Default |
|---|---|---|---|
| prompt | Yes | ||
| model | No | grok-4-1-fast | |
| max_turns | No |
Implementation Reference
- src/server.py:341-363 (handler)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() - src/server.py:8-8 (helper)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 - src/server.py:6-6 (helper)Import of Client from xai_sdk, used to create the API client instance in the code_executor handler.
from xai_sdk import Client