Environment
Workspace
A workspace for private and shared agent skills i.e. Python modules that implement special agent skills. These are skills that are not pre-installed in the code execution container.
A workspace defines paths for private and shared skills, both in the container and on the host machine. Workspace paths on the host machine can be bind-mounted into the container, if desired. This is especially useful when skills are being (inter)actively developed, so that they can be inspected and edited on the host machine while being executed in the container.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
Path | str | None
|
Root path of the workspace directory on the host. |
None
|
key
|
str | None
|
A key to designate:
|
None
|
Source code in freeact/environment.py
private_skills_host_path
property
private_skills_host_path: Path
Path to private skills directory on host.
shared_skills_host_path
property
shared_skills_host_path: Path
Path to shared skills directory on host.
private_images_host_path
property
private_images_host_path: Path
Path to private images directory on host.
private_skills_container_path
property
private_skills_container_path: str
Path to private skills directory in container.
shared_skills_container_path
property
shared_skills_container_path: str
Path to shared skills directory in container.
private_mcp_container_path
property
private_mcp_container_path: str
Path to private MCP directory in container.
CodeExecutionContainer
CodeExecutionContainer(tag: str, env: dict[str, str] | None = None, executor_port: int | None = None, resource_port: int | None = None, show_pull_progress: bool = True, workspace_path: Path | str | None = None, workspace_key: str | None = None)
Bases: ExecutionContainer
Context manager for the lifecycle of a code execution Docker container.
Extends ipybox's ExecutionContainer
with workspace-specific bind mounts of skill directories.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tag
|
str
|
Name and optionally tag of the |
required |
env
|
dict[str, str] | None
|
Environment variables to set in the container |
None
|
executor_port
|
int | None
|
Host port for the container's executor port. A random port is allocated if not specified. |
None
|
resource_port
|
int | None
|
Host port for the container's resource port. A random port is allocated if not specified. |
None
|
show_pull_progress
|
bool
|
Whether to show progress when pulling the Docker image. |
True
|
workspace_path
|
Path | str | None
|
Path to workspace directory on host. Defaults to "workspace". |
None
|
workspace_key
|
str | None
|
Key to designate private sub-directories on host. Defaults to "default". |
None
|
Source code in freeact/environment.py
CodeExecutionResult
dataclass
Result of a code execution in a code executor.
images
instance-attribute
Images generated during code execution. Keys are image file paths in the
container.workspace
,
values are pre-loaded images from these files.
is_error
instance-attribute
is_error: bool
Whether the execution resulted in an error. If True
, text
contains
the corresponding error trace.
CodeExecution
CodeExecution(execution: Execution, images_dir: Path)
A code execution running in a code executor.
Source code in freeact/environment.py
result
async
result(timeout: float = 120) -> CodeExecutionResult
Retrieves the complete result of this code execution. Waits until the result is available.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timeout
|
float
|
Maximum time in seconds to wait for the execution result |
120
|
Raises:
Type | Description |
---|---|
TimeoutError
|
If code execution duration exceeds the specified timeout |
Source code in freeact/environment.py
stream
async
stream(timeout: float = 120) -> AsyncIterator[str]
Streams the code execution result as it is generated. Once the stream
is consumed, a result
is
immediately available without waiting.
Generated images are not streamed. They can be obtained from the
return value of result
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timeout
|
float
|
Maximum time in seconds to wait for the complete execution result |
120
|
Raises:
Type | Description |
---|---|
TimeoutError
|
If code execution duration exceeds the specified timeout |
Source code in freeact/environment.py
CodeExecutor
Context manager for executing code in an IPython kernel running in a
CodeExecutionContainer
.
The kernel is created on entering the context and destroyed on exit.
Code execution is stateful for a given CodeExecutor
instance. Definitions and
variables of previous executions are available to subsequent executions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
workspace
|
Workspace
|
The workspace of the code execution container |
required |
port
|
int
|
Host port for the container's executor port |
required |
host
|
str
|
Hostname or IP address of the container's host |
'localhost'
|
Source code in freeact/environment.py
execute
async
execute(code: str, timeout: float = 120) -> CodeExecutionResult
Executes code in this executor's IPython kernel and returns the result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
code
|
str
|
Code to execute |
required |
timeout
|
float
|
Maximum time in seconds to wait for the execution result |
120
|
Raises:
Type | Description |
---|---|
TimeoutError
|
If code execution duration exceeds the specified timeout |
Source code in freeact/environment.py
submit
async
submit(code: str) -> CodeExecution
Submits code for execution in this executor's IPython kernel and returns
a CodeExecution
object for consuming the
execution result.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
code
|
str
|
Python code to execute |
required |
Returns:
Type | Description |
---|---|
CodeExecution
|
A |
Source code in freeact/environment.py
CodeProvider
Context manager for
- loading the source code of Python modules and generated MCP client functions
from a
CodeExecutionContainer
. - registering MCP servers and generating client functions for their tools in a
CodeExecutionContainer
.
Source code loaded with this context manager is provided as skill sources to code action models so that they can include them into code actions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
workspace
|
Workspace
|
The workspace of the code execution container |
required |
port
|
int
|
Host port for the container's resource port |
required |
host
|
str
|
Hostname or IP address of the container's host |
'localhost'
|
Source code in freeact/environment.py
register_mcp_servers
async
Registers MCP servers and generates Python client functions for their tools. These
functions can be included into code actions, and calling them runs the corresponding
MCP server tools. This works for both stdio
and sse
based MCP servers.
The source code of generated client functions can be loaded with the
get_sources
method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
server_params_dict
|
dict[str, dict[str, Any]]
|
Dictionary of application-defined server names and their MCP
server parameters. |
required |
Returns:
Type | Description |
---|---|
dict[str, list[str]]
|
Dictionary of server names and provided tool names. Tool names are sanitized |
dict[str, list[str]]
|
to be valid Python module names. |
Source code in freeact/environment.py
get_sources
async
get_sources(module_names: list[str] | None = None, mcp_tool_names: Mapping[str, list[str] | None] | None = None) -> str
Loads the source code of given Python modules and generated MCP client functions and returns them in the following format:
```python
# Module: {module_name_1}
{module_source_1}
```
```python
# Module: {module_name_2}
{module_source_2}
```
...
Module names of generated MCP client functions follow the pattern
mcpgen.{server_name}.{tool_name}
. Hence, calling
is equivalent to
For loading the source code of all generated client functions for an MCP server,
use None
as value in the mcp_tool_names
dictionary:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
module_names
|
list[str] | None
|
Names of modules available on the container's Python path |
None
|
mcp_tool_names
|
Mapping[str, list[str] | None] | None
|
Dictionary of MCP server names and their tool names (as returned by
|
None
|
Returns:
Type | Description |
---|---|
str
|
The formatted source code of all requested Python modules and generated MCP client functions. |
Source code in freeact/environment.py
CodeExecutionEnvironment
CodeExecutionEnvironment(container: CodeExecutionContainer, host: str = 'localhost')
An environment for
- executing code actions in,
- loading source code from,
- and registering MCP servers at
a running CodeExecutionContainer
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
container
|
CodeExecutionContainer
|
A running code execution container. |
required |
Source code in freeact/environment.py
code_executor
async
code_executor() -> AsyncIterator[CodeExecutor]
Context manager for CodeExecutor
s in this environment.
Source code in freeact/environment.py
code_provider
async
code_provider() -> AsyncIterator[CodeProvider]
Context manager for CodeProvider
s in this environment.
Source code in freeact/environment.py
dotenv_variables
dotenv_variables(dotenv_path: Path | None = Path('.env'), export: bool = True, **kwargs) -> Dict[str, str]
Load environment variables from a .env
file.
Reads environment variables from a .env
file and optionally exports them to os.environ
.
If no path is provided, searches for a .env
file in parent directories.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dotenv_path
|
Path | None
|
Path to the |
Path('.env')
|
export
|
bool
|
Whether to export variables to current environment. Defaults to |
True
|
**kwargs
|
Additional keyword arguments passed to |
{}
|
Returns:
Type | Description |
---|---|
Dict[str, str]
|
Dictionary mapping environment variable names to their values. |
Source code in freeact/environment.py
execution_environment
async
execution_environment(host: str = 'localhost', ipybox_tag: str = 'ghcr.io/gradion-ai/ipybox:minimal', ipybox_env: dict[str, str] = dotenv_variables(), executor_port: int | None = None, resource_port: int | None = None, workspace_path: Path | str | None = None, workspace_key: str | None = None) -> AsyncIterator[CodeExecutionEnvironment]
Context manager providing a CodeExecutionEnvironment
. It
manages the lifecycle of the environment's CodeExecutionContainer
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ipybox_tag
|
str
|
Name and optionally tag of the |
'ghcr.io/gradion-ai/ipybox:minimal'
|
ipybox_env
|
dict[str, str]
|
Environment variables to set in the |
dotenv_variables()
|
executor_port
|
int | None
|
Host port for the container's executor port. A random port is allocated if not specified |
None
|
resource_port
|
int | None
|
Host port for the container's resource port. A random port is allocated if not specified |
None
|
workspace_path
|
Path | str | None
|
Path to workspace directory on host. Defaults to "workspace". |
None
|
workspace_key
|
str | None
|
Key to designate private sub-directories on host. Defaults to "default". |
None
|