Kits¶
fastsandbox.Kit
¶
Bases: BaseModel
A kit to create sandboxes from: a base kit, mixins and kit arguments.
Attributes:
| Name | Type | Description |
|---|---|---|
base |
str
|
The base kit, as |
mixins |
tuple[str | Mixin, ...]
|
Mixin kits composed on top of the base, in order: directories, mixins written
in Python ( |
args |
dict[str, str]
|
Kit arguments, as |
fastsandbox.KitRef
dataclass
¶
A kit, as the sbx CLI reads it from its name.
KitRef.base(text) reads a text as sbx create <text> reads its base kit, and
KitRef.mixin(text) as sbx create --kit <text> reads a mixin, by the rules
docker-sandbox-kit measured on the CLI.
Example
KitRef.base("claude") KitRef(kind='agent', value='claude') KitRef.base("ghcr.io/acme/kit:1").kind 'image'
Attributes:
| Name | Type | Description |
|---|---|---|
kind |
Literal['agent', 'image', 'local', 'git', 'other']
|
|
value |
str
|
What names it: the agent, the image (without an |
base
classmethod
¶
The kit sbx create <text> takes as base kit.
A Git repository (git+https://…, git+ssh://…), an image (oci://…, or a name with
a slash, even when a directory of that name exists), a path written as one (./…,
../…, /…, ~/…), and otherwise an agent.
Raises:
| Type | Description |
|---|---|
InvalidKitError
|
For a name without a slash that is not an agent |
mixin
classmethod
¶
The kit sbx create --kit <text> takes as mixin.
A Git repository, an image written oci://…, a path written as one, a path relative to
the working directory that exists (tools, kits/tools), and otherwise an image when
the name holds a slash.
Raises:
| Type | Description |
|---|---|
InvalidKitError
|
For a name without a slash that names no directory, which the CLI takes neither for a path nor for an image, and for an empty text. |
fastsandbox.KitPackage
¶
A kit package: forged without the sbx CLI, compiled by it, or opened from a directory.
A package forged in memory has no directory, and its images are held in memory.
compiled_with
property
¶
compiled_with: str | None
The sbx version the package was compiled with, None for a forged package.
compiled_api
property
¶
compiled_api: str | None
The daemon API version the package was forged or compiled for.
images
property
¶
images: list[PackageImage]
The image archives of the package, without their bytes (image_archive).
in_memory
classmethod
¶
in_memory(template: CreateRequest | Mapping[str, Any], images: Iterable[PackageImage], metadata: Mapping[str, Any] | None = None) -> KitPackage
A package assembled by hand, held in memory: a creation request and its images.
template is the creation request of every sandbox of the package, whose name
{{name}} stands for the name of each one, as a forger of one's own assembles it
(CreateRequest.for_agent, its kits and their artifacts). images hold their archives
(PackageImage.in_memory), loaded under their reference when the daemon does not hold
them yet. metadata is what kit.json holds besides, none by default.
Raises:
| Type | Description |
|---|---|
ValueError
|
When an image holds no archive (one listed by a package rather than
made with |
InvalidRequestError
|
When |
open
classmethod
¶
open(dir: str | PathLike[str]) -> KitPackage
Opens the package forged or compiled into dir.
Raises:
| Type | Description |
|---|---|
KitPackageError
|
When |
suits
¶
Whether the package suits the daemon of client, which can create sandboxes from it.
A package compiled by the sbx CLI suits the daemon of the sbx version it was
compiled with; a forged one, the daemon of the API it was forged for, forged by this
version of the crates, which write their packages as they do. A package that records
neither suits no daemon.
save
¶
save(dir: str | PathLike[str]) -> KitPackage
Writes the package into dir, replacing a package already there.
A package forged in memory is kept that way for later processes, or given to whatever
takes a package by its directory. Returns the package of dir.
Raises:
| Type | Description |
|---|---|
KitPackageError
|
When the package cannot be written. |
image_archive
¶
The archive of the image at index of images: held in memory, or of its file.
Raises:
| Type | Description |
|---|---|
IndexError
|
When the package has no image at |
HostFileError
|
When the file of the archive cannot be read. |
request
¶
request(name: str) -> CreateRequest
The creation request of a sandbox named name from this package.
load_images
async
¶
Loads the images of the package into the daemon; returns how many.
create
async
¶
Loads the images and creates the sandbox name from the package.
create_with
async
¶
create_with(client: Client, request: CreateRequest) -> Created
Loads the images and creates a sandbox from an adjusted request.
request is the one request(name) gives, adjusted by the caller: a workspace,
variables or ports added, for instance.
install_into
async
¶
Installs the kits in place in a running sandbox.
Returns how many artifacts were installed.
add_to
async
¶
add_to(sandbox: Sandbox) -> SwapOutcome
Adds the kits to a sandbox by swapping its container.
fastsandbox.KitAgent
¶
The agent a kit describes: its program, run options and network needs.
from_artifact
classmethod
¶
The agent described by a kit artifact, if it describes one.
fastsandbox.KitForge
¶
KitForge(client: Client)
Forges kits into packages without the sbx CLI, through the socket alone.
The base kit is rebuilt from its published image, which the daemon pulls and exports;
the mixins are read from their directories, whose spec.yaml is written in the JSON
subset of YAML, or written in Python. Kit arguments are checked as the CLI checks them.
A forged package suits the daemon it was forged for while the crates keep their version.
A forge for the daemon of client.
forge
async
¶
Forges kit: in memory, or into the package directory output when given.
Raises:
| Type | Description |
|---|---|
InvalidKitError
|
When a kit is not named as the CLI names one (a base kit without a slash that is not an agent, a mixin that names no directory), a mixin is one only the CLI resolves (an image, a ZIP file, a Git repository), or the forge refuses a descriptor or a kit argument. |
KitForgeError
|
When the base kit is one only the |
HostFileError
|
When a mixin directory or its |
check
async
¶
Checks kit before forging it, without pulling or writing anything.
The base kit, the arguments that name it and the kit policy of the daemon are checked; the arguments are checked against the descriptor of the base kit only when it is forged, as the descriptor comes with its image.
Raises:
| Type | Description |
|---|---|
InvalidKitError
|
As |
KitForgeError
|
When the base kit is one only the CLI builds, or the kit policy of the daemon refuses the composition. |
forge_over
async
¶
forge_over(package: KitPackage, mixins: Iterable[str | Mixin] = (), output: str | PathLike[str] | None = None) -> KitPackage
Forges mixins over every kit of package: in memory, or into output when given.
The package keeps its kits, its base kit and its own mixins with their artifacts and
their images, and mixins come after them, given as text (a directory) or written in
Python. A kit only the CLI resolves (a mixin given by image or Git, a local base kit),
compiled once (KitCompiler, a KitCache that compiles), takes mixins forged in code
all the same. A spec composes its own mixins over a package this way; a spec without
mixins launches the package as it is, while this method needs at least one mixin.
Raises:
| Type | Description |
|---|---|
InvalidKitError
|
When a mixin names no directory, is one only the CLI resolves, or the forge refuses its descriptor. |
KitForgeError
|
When |
HostFileError
|
When a mixin directory or its |
forge_over_base
async
¶
forge_over_base(package: KitPackage, mixins: Iterable[str | Mixin] = (), output: str | PathLike[str] | None = None) -> KitPackage
Forges mixins over the base kit of package alone: in memory, or into output.
The base kit is the first kit of the package, as it was forged or compiled, taken
without pulling its image again; the other kits of the package and their images are
left out, as forge_over would keep them. A package saved or compiled once is composed
again this way with other mixins.
Raises:
| Type | Description |
|---|---|
InvalidKitError
|
When a mixin names no directory, is one only the CLI resolves, or the forge refuses its descriptor. |
KitForgeError
|
When |
HostFileError
|
When a mixin directory or its |
fastsandbox.KitCompiler
¶
KitCompiler(client: Client, *, sbx: str | PathLike[str] = 'sbx', work_dir: str | PathLike[str] | None = None, timeout: float | None = None)
Compiles kits into packages with the sbx CLI.
It runs the CLI once behind a capturing proxy, on a throw-away sandbox,
sbx-kit-compile-<pid>-<n>, which it removes when the compilation ends or fails; a
compilation cancelled, or a process killed, can leave it.
A compiler for the daemon of client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client
|
Client
|
The client of the daemon. |
required |
sbx
|
str | PathLike[str]
|
The |
'sbx'
|
work_dir
|
str | PathLike[str] | None
|
Scratch directory of the compiler; a temporary one, removed after each compilation, by default. |
None
|
timeout
|
float | None
|
Deadline of a compilation, in seconds. |
None
|
compile
async
¶
compile(base: str, output: str | PathLike[str], *, mixins: tuple[str | Mixin, ...] | list[str | Mixin] = (), args: dict[str, str] | None = None) -> KitPackage
Compiles a kit into the package directory output.
base is the base kit as sbx create takes it, composed with mixins, in the order
given, and kit args. A mixin given as text goes to the CLI as it is (--kit); a mixin
written in Python is
written into a directory of the scratch directory, given to the CLI, and removed with
it: the package lists it under the path of that directory, as the CLI lists a local
kit.
Raises:
| Type | Description |
|---|---|
KitCompileError
|
When the |
InvalidKitError
|
When the crate refuses a mixin written in Python. |
SandboxTimeoutError
|
When the compilation runs over the deadline. |
fastsandbox.KitCache
¶
KitCache(directory: str | PathLike[str] | None = None, *, sbx: str | PathLike[str] | None = None, timeout: float | None = None)
Kit packages, made on first use and kept for the next sandboxes.
By default kits are forged without the sbx CLI (KitForge) and kept in memory for the
process: nothing is written on the host, and a later process forges them again, in a few
seconds once the daemon holds the image of the base kit. Given a directory, the cache keeps
its packages there too, one directory per composition, for later processes. Given the sbx
CLI, it compiles kits with it (KitCompiler), which can take a few minutes for an agent
kit, into its directory.
A composition is its base kit, its arguments and its mixins, the text of the spec.yaml of a
mixin directory and the descriptor of a mixin written in Python included: a mixin changed is
made again. A package is used again while it suits the daemon (KitPackage.suits): one
compiled by the CLI for the sbx version it runs, one forged by this version of the crates
for the API it speaks. The packages the CLI compiles are kept apart from those forged. Each
composition is made by one caller at a time, whatever their event loops.
Attributes:
| Name | Type | Description |
|---|---|---|
directory |
The directory the packages are kept in, |
A cache in memory, or in directory, compiling with the sbx program sbx if given.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
str | PathLike[str] | None
|
Where packages are kept; |
None
|
sbx
|
str | PathLike[str] | None
|
The |
None
|
timeout
|
float | None
|
Deadline of a compilation by the CLI, in seconds. |
None
|
compiles
property
¶
compiles: bool
Whether the cache compiles its kits with the sbx CLI, rather than forging them.
default
classmethod
¶
default() -> KitCache
The kit cache of the process, shared by the clients given none.
One per value of $FASTSANDBOX_KIT_CACHE, so that a change of it takes effect.
package
async
¶
package(client: Client, kit: Kit | str) -> KitPackage
The package of kit for the daemon of client, made now unless one kept suits it.
Raises:
| Type | Description |
|---|---|
InvalidKitError
|
When a kit is not named as the CLI names one, or the forge refuses a mixin, a descriptor or a kit argument. |
KitForgeError
|
When the kit is one only the |
HostFileError
|
When a mixin directory cannot be read, for a cache that forges. |
KitCompileError
|
When the |
refresh
async
¶
refresh(client: Client, kit: Kit | str) -> KitPackage
The package of kit, made again in place of the one kept.
For a base kit of a registry or a Git repository that moved since it was made, which the cache does not see by itself.
Raises:
| Type | Description |
|---|---|
InvalidKitError
|
As |
KitForgeError
|
As |
HostFileError
|
As |
KitCompileError
|
As |
fastsandbox.kits.default_cache_dir
¶
default_cache_dir() -> Path
The default directory of the kit cache.
$FASTSANDBOX_KIT_CACHE, else $XDG_CACHE_HOME/fastsandbox/kits, else
~/.cache/fastsandbox/kits.
Mixins written in Python¶
fastsandbox.Mixin
¶
Bases: BaseModel
A mixin written in Python: a descriptor, listed under a reference.
The crate checks it when it is made, as the forge checks the mixins it reads from their
directories: the hosts of its rules, its commands, the fields it names. Kit.mixins takes
it as it takes a directory; the forge sends it to the daemon as it is, and the compiler
writes it into a directory of its own for the CLI. save() writes it into a directory of
one's choice, as the CLI reads a local kit.
Attributes:
| Name | Type | Description |
|---|---|---|
reference |
str
|
A path, as the CLI lists a local kit, whose last part names the mixin,
e.g. |
descriptor |
KitDescriptor
|
What the mixin declares. |
Raises:
| Type | Description |
|---|---|
InvalidKitError
|
When the forge would refuse the mixin. |
read
classmethod
¶
The mixin of directory, described by its spec.yaml written in JSON.
It is listed under the canonical path of the directory.
Raises:
| Type | Description |
|---|---|
InvalidKitError
|
When the forge would refuse its descriptor. |
HostFileError
|
When the directory or its |
from_ref
classmethod
¶
The mixin text names, read as sbx create --kit reads it: a directory.
A path written as one (./tools, /…, ~/…), or a name of a directory of the working
directory (tools), is read from its spec.yaml; a KitRef.mixin of a directory too.
Raises:
| Type | Description |
|---|---|
InvalidKitError
|
For a text that names no directory, or an image, a ZIP file or a Git repository, which only the CLI resolves. |
HostFileError
|
When the directory or its |
save
¶
Writes the mixin into directory, created if needed, as the CLI reads a local kit.
Its spec.yaml holds the descriptor in JSON behind its # syntax= line. Returns the
mixin of that directory, named after it and listed under its canonical path, as the CLI
lists the kit in the packages it compiles.
Raises:
| Type | Description |
|---|---|
HostFileError
|
When the directory or its |
to_json
¶
The mixin as JSON, its reference and its descriptor, the form the crate reads.
mcp
classmethod
¶
The mixin that registers servers in the configuration of the agent agent.
Built by the crate, for claude, copilot and opencode. Its install commands
register the servers inside the sandbox, as the user agent, as the kit of the agent
registers its MCP gateway (claude mcp add for claude), and it allows the hosts of
those reached at a URL. The tokens are not part of it: they are given at creation, in
the variables token_env of the servers (McpServer.env()), which the agent reads when
it connects, so that one package serves sandboxes with tokens of their own. Its
reference is /kits/docker-sandbox-mcp-<agent>.
Raises:
| Type | Description |
|---|---|
InvalidKitError
|
For an agent without an MCP adapter, no server, two servers of the same name, a name or a variable that is not one, a URL that is not an http or https one or holds a character a shell command cannot carry, a stdio server without a program or given a token, or a server reached at a URL given arguments. |
fastsandbox.KitDescriptor
¶
Bases: _Spec
The v3 descriptor of a mixin, the spec.yaml of the kit.
Build one with KitDescriptor.mixin() and the with_* methods, or read one with
parse(), read() or from_json(), which the crate checks as the forge does.
Attributes:
| Name | Type | Description |
|---|---|---|
schema_version |
Literal['3']
|
Version of the descriptor format, |
kind |
Literal['mixin']
|
Kind of kit, |
display_name |
str | None
|
Name shown for the kit. |
description |
str | None
|
What the kit is for. |
version |
str | None
|
Version of the kit, e.g. |
author |
Any
|
Author of the kit, kept as written. |
licenses |
Any
|
Licenses of the kit, kept as written. |
source_url |
str | None
|
Where the kit comes from. |
capabilities |
tuple[Capability, ...]
|
What the kit declares, in order. |
mixin
classmethod
¶
mixin(display_name: str) -> KitDescriptor
An empty mixin named display_name, to build on with the with_* methods.
parse
classmethod
¶
parse(text: str) -> KitDescriptor
The descriptor of the text of a spec.yaml written in JSON.
The comment and blank lines that open it, such as its # syntax= line, are skipped.
Raises:
| Type | Description |
|---|---|
InvalidKitError
|
When the rest is not JSON, or not a v3 mixin the forge takes. |
read
classmethod
¶
read(path: str | PathLike[str]) -> KitDescriptor
The descriptor in the spec.yaml at path.
Raises:
| Type | Description |
|---|---|
OSError
|
When the file cannot be read. |
InvalidKitError
|
As |
from_json
classmethod
¶
from_json(value: dict[str, Any]) -> KitDescriptor
The descriptor of its JSON, as spec.yaml holds it.
Raises:
| Type | Description |
|---|---|
InvalidKitError
|
When it is not a v3 mixin the forge takes. |
with_description
¶
with_description(description: str) -> KitDescriptor
The descriptor, with the description description.
with_version
¶
with_version(version: str) -> KitDescriptor
The descriptor, with the kit version version, e.g. 1.0.0.
with_install
¶
with_install(step: LifecycleStep) -> KitDescriptor
The descriptor, with a command run once, when the sandbox is created.
with_startup
¶
with_startup(step: LifecycleStep) -> KitDescriptor
The descriptor, with a command run at every start of the sandbox.
with_network_allow
¶
with_network_allow(*hosts: str) -> KitDescriptor
The descriptor, allowing hosts while the sandbox runs.
A host is host, *.domain, host:port, a CIDR prefix…, as the CLI takes it; the
crate checks the hosts when the mixin is made.
with_network_deny
¶
with_network_deny(*hosts: str) -> KitDescriptor
The descriptor, denying hosts while the sandbox runs.
A deny wins over any allow, of the sandbox or of its kits.
fastsandbox.LifecycleStep
¶
Bases: _Spec
A command a mixin runs: once, when the sandbox is created, or at each of its starts.
Attributes:
| Name | Type | Description |
|---|---|---|
command |
str | tuple[str, ...]
|
A shell command, run with |
user |
str | None
|
The user it runs as, e.g. |
description |
str | None
|
What it does, shown as it runs. |
env |
tuple[str, ...]
|
The variables of the sandbox it receives: an install command runs in an emptied
environment otherwise, which keeps only |
background |
bool | None
|
Whether a startup command runs in the background. |
with_description
¶
with_description(description: str) -> LifecycleStep
The step, described by description.
with_env
¶
with_env(*names: str) -> LifecycleStep
The step, given the variables names of the sandbox too.
with_gateway_env
¶
with_gateway_env() -> LifecycleStep
The step, given the MCP gateway of the sandbox: GATEWAY_ENV.
fastsandbox.kits.LifecycleCapability
¶
Bases: _Spec
The install and startup commands of a kit (com.docker.sandbox/lifecycle@1).
Attributes:
| Name | Type | Description |
|---|---|---|
type |
Literal['com.docker.sandbox/lifecycle@1']
|
|
name |
str | None
|
Name of the capability. |
description |
str | None
|
What the capability does. |
optional |
bool | None
|
Whether the kit works without it. |
config |
LifecycleConfig
|
The commands. |
fastsandbox.kits.LifecycleConfig
¶
Bases: _Spec
The commands of a lifecycle capability.
Attributes:
| Name | Type | Description |
|---|---|---|
install |
tuple[LifecycleStep, ...]
|
Commands run once, when the sandbox is created, before its agent starts. |
startup |
tuple[LifecycleStep, ...]
|
Commands run at every start of the sandbox, alongside its agent. |
fastsandbox.kits.NetworkCapability
¶
Bases: _Spec
The network rules of a kit (com.docker.sandbox/network-policy@1).
Attributes:
| Name | Type | Description |
|---|---|---|
type |
Literal['com.docker.sandbox/network-policy@1']
|
|
name |
str | None
|
Name of the capability. |
description |
str | None
|
What the capability does. |
optional |
bool | None
|
Whether the kit works without it. |
config |
NetworkConfig
|
The rules. |
fastsandbox.kits.NetworkConfig
¶
Bases: _Spec
The rules of a network capability.
Attributes:
| Name | Type | Description |
|---|---|---|
runtime |
NetworkRules
|
Rules while the sandbox runs. |
install |
NetworkRules
|
Hosts allowed while the install commands run. |
fastsandbox.kits.NetworkRules
¶
fastsandbox.kits.Capability
module-attribute
¶
Capability = Annotated[LifecycleCapability | NetworkCapability, Field(discriminator='type')]
A capability a mixin declares, told apart by its type.
fastsandbox.kits.LIFECYCLE
module-attribute
¶
LIFECYCLE: Final = 'com.docker.sandbox/lifecycle@1'
The type of the capability of install and startup commands.
fastsandbox.kits.NETWORK
module-attribute
¶
NETWORK: Final = 'com.docker.sandbox/network-policy@1'
The type of the capability of network rules.
fastsandbox.kits.GATEWAY_ENV
module-attribute
¶
The variables of a sandbox that name its MCP gateway: its address, and the bearer token the kits of the agents send it.
The MCP servers of an agent¶
fastsandbox.McpServer
¶
Bases: BaseModel
An MCP server for the agent of a sandbox, registered by Mixin.mcp().
Make one with McpServer.host(), McpServer.remote() or McpServer.stdio(), as the crate
makes them; Mixin.mcp() checks it. to_json() is its JSON form, as a sandbox spec holds
it, and from_json() reads one.
Attributes:
| Name | Type | Description |
|---|---|---|
kind |
Literal['host', 'remote', 'stdio']
|
|
name |
str
|
The name the agent knows the server under: letters, digits, |
url |
str | None
|
The URL of its endpoint, over streamable HTTP; |
token_env |
str | None
|
The variable of the sandbox holding its bearer token, if it takes one. |
port |
int | None
|
The port of the loopback of the host it listens on, for a server of the host. |
command |
tuple[str, ...]
|
The program of a stdio server and its arguments. |
host
classmethod
¶
A server a process of the host serves on port of its loopback, in any language.
The sandbox reaches it at http://host.docker.internal:<port>/mcp; its proxy forwards
the requests to the loopback of the host as localhost:<port>, so a server guarded
against DNS rebinding takes them as it takes those of the host. Its bearer token is
read from the variable token_env, DOCKER_SANDBOX_MCP_<NAME>_TOKEN, which the
creation request sets (env(token)) and the server checks: the token, not the origin
of a request, tells the sandbox apart from the other processes of the host. The proxy
writes no secret into the requests to the host: the sandbox holds that token, which is
to be random and its own.
remote
classmethod
¶
A remote server at url, called by the agent itself.
It takes no token until with_bearer_env().
stdio
classmethod
¶
A server the agent starts inside the sandbox, as the user agent.
The agent speaks to it over its standard input and output: program, a path or a name
the PATH of the sandbox finds, with the arguments of with_args(). It inherits the
environment of the sandbox, whose variables give it its secrets. Installing the program
is the work of another mixin, or of the image of the kit. It takes no token.
from_json
classmethod
¶
The server of its JSON form, e.g. {"kind": "host", "name": "tools", "port": 47813}.
Raises:
| Type | Description |
|---|---|
ValueError
|
When the form is not one of an MCP server. |
with_args
¶
The stdio server, its program given args after those it has.
A server reached at a URL takes none: Mixin.mcp() refuses it.
with_bearer_env
¶
The server, sent the content of the variable env of the sandbox as its bearer token.
For a key the sandbox must not hold, give the sandbox a ProxiedKey read from env
for the host of the server, or a key of its spec (KeySpec.for_hosts): the variable
holds its placeholder, which the proxy replaces with the key. A stdio server takes
none: Mixin.mcp() refuses it.
env
¶
The variables of a creation request that give the server its bearer token token.
Raises:
| Type | Description |
|---|---|
ValueError
|
When the server takes no token. |