Skip to main content
Code execution engine for MCP tools. This module provides secure code execution capabilities for MCP clients, allowing agents to interact with tools through Python code instead of direct tool calls.

CodeExecutor

from mcp_use.client.code_executor import CodeExecutor

method init

Initialize the code executor.Parameters
client
MCPClient
required
The MCPClient instance to use for tool calls.
Signature
def __init__(client: MCPClient):

method execute

Execute Python code with access to MCP tools.Parameters
code
str
required
Python code to execute.
timeout
float
default:"30.0"
Execution timeout in seconds.
Returns
returns
dict[str, Any]
Signature
def execute(code: str, timeout: float = 30.0):