Skip to main content

Module code_executor

Module code_executor 

Source
Expand description

Code execution environment for agents using MCP tools programmatically.

This module allows agents to write and execute code snippets that interact with MCP tools as library functions, rather than making individual tool calls. This improves efficiency through:

  • Control flow: loops, conditionals, error handling without repeated model calls
  • Data filtering: process results before returning to model
  • Latency: code runs locally with direct process execution
  • Context: intermediate results stay local unless explicitly logged

§Example

let executor = CodeExecutor::new(
    Language::Python3,
    Arc::new(mcp_client),
    PathBuf::from("/workspace"),
);

// Agent writes Python code
let code = r#"
files = list_files(path="/workspace", recursive=True)
filtered = [f for f in files if "test" in f]
result = {"count": len(filtered), "files": filtered[:10]}
"#;

let result = executor.execute(code).await?;

Structs§

CodeExecutor
Code executor for running agent code.
ExecutionConfig
Configuration for code execution.
ExecutionResult
Result of code execution.

Enums§

Language
Supported languages for code execution.