computer

package module
v0.0.0-...-3c10a0e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 40 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

computer

A sandboxed computer for an AI agent, as a Go library and one binary. It needs no Node and no container at run time, and it is built without cgo.

You give it folders and MCP servers. The agent gets a shell, SQLite and Python over those folders, and every MCP tool appears as a shell command and as a Python function. Nothing inside reaches the network or a file outside the folders, except through those tools.

Status: experimental. Two independent reviews each found ways out of the sandbox, which are fixed and tested here. Expect more. Do not rely on this as your only boundary around untrusted code.

Why

An agent that can only call tools must pass every result through the model. That is slow, costs tokens, and fails once a result is too large to read. With a computer the agent writes a short script; the script handles the data and only the answer returns to the model. The agent also carries four tool definitions, however many tools the servers offer.

Use it

go build -o computer ./cmd/computer

# an interactive shell over a folder
./computer -mount ./work:/home/user

# one command
./computer -mount ./work:/home/user -c 'ls | wc -l'

# a public MCP server by URL: DeepWiki, which answers questions about GitHub repositories
./computer -mount ./work:/home/user -mcp wiki=https://mcp.deepwiki.com/mcp \
    -c 'wiki --help; wiki read-wiki-structure --repo-name wazero/wazero | head'

# an MCP server started as a program: the reference "everything" server
./computer -mount ./work:/home/user -mcp 'everything=npx -y @modelcontextprotocol/server-everything' \
    -c 'everything echo --message hello'

# several servers at once, each its own command
./computer -mount ./work:/home/user -mcp wiki=https://mcp.deepwiki.com/mcp -mcp docs=https://mcp.context7.com/mcp

# as an MCP server for an agent: its tools are bash, read, edit and write
./computer -mount ./work:/home/user -mcp wiki=https://mcp.deepwiki.com/mcp mcp

With more than a few of either, put them in a file. mcpServers is the format Claude Code and Claude Desktop read, so a .mcp.json you already have works as it is; mounts are as -mount takes them.

{
  "mounts": ["./work:/home/user", "./docs:/reference:ro"],
  "mcpServers": {
    "wiki": {"type": "http", "url": "https://mcp.deepwiki.com/mcp", "headers": {"Authorization": "Bearer ${WIKI_TOKEN}"}},
    "everything": {"command": "npx", "args": ["-y", "@modelcontextprotocol/server-everything"]}
  }
}
./computer -config computer.json

${NAME} in a header or an env value is read from the environment. Flags add to the file.

A server that needs a bearer token reads it from the environment: COMPUTER_MCP_TOKEN_WIKI for a server named wiki, COMPUTER_MCP_TOKEN_MY_WIKI for my-wiki. A server started as a program runs on the host, outside the sandbox, like any other program you start; it inherits the environment except the COMPUTER_MCP_TOKEN variables, which its env can pass on by name.

For Claude Code:

claude mcp add computer -- /path/to/computer -mount /path/to/work -mcp wiki=https://mcp.deepwiki.com/mcp mcp

A folder given alone is seen in the computer at the path it has on your machine. Claude Code tells the model where it is working, and the model will use that path inside the computer. -mount host:guest names another path.

From Go:

c, err := computer.New(computer.Config{
    Mounts: []computer.Mount{{Guest: "/home/user", Host: dir}},
    Tools:  []tools.Source{{Name: "wiki", Session: session}}, // an MCP client session
    Python: python,                                           // from computer.EmbeddedPython, shared by every computer
})
defer c.Close()
res, err := c.Exec(ctx, &gbash.ExecutionRequest{Script: "wiki read-wiki-structure --repo-name wazero/wazero | wc -l"})

Mounts

A mount is a directory of the computer. The shell and every program are given the same one, so a file one writes another reads. It is a host directory, or anything that implements afero's filesystem interface: memory, a database, an object store.

computer.Mount{Guest: "/home/user", Host: dir}                     // a host directory
computer.Mount{Guest: "/scratch", FS: afero.NewMemMapFs()}         // kept in memory
computer.Mount{Guest: "/reference", Host: docs, ReadOnly: true}

A mount may be inside another: /home/user/files in /home/user. Its folder must already be there in the outer mount, as a mount point is. A path belongs to the nearest mount around it, so a read-only mount inside a writable one stays read-only.

A host directory is opened with files.Dir, which goes through Go's os.Root and cannot be left by .. or by a symbolic link. A filesystem of your own decides for itself what a name may reach.

Tools as commands and as Python

A server named wiki with a tool read_wiki_structure that takes repoName is:

wiki --help                                          # the server's tools
wiki read-wiki-structure --help                      # one tool's arguments
wiki read-wiki-structure --repo-name wazero/wazero
wiki read-wiki-structure --json '{"repoName": "wazero/wazero"}'
from tools import wiki
help(wiki.read_wiki_structure)
wiki.read_wiki_structure(repo_name="wazero/wazero")

A command prints a text result as it is and anything else as JSON, ready for jq. The Python module is the name as Python spells it: a server named my-wiki is the command my-wiki and the module my_wiki.

A server given without a name, as a bare URL, has its tools grouped by the part of their name before the first underscore: a tool notes_addNote is notes add-note. This suits one server that fronts several applications.

More commands

The extra package holds commands for everyday data and document work. cmd/computer includes them; the library does not, so an embedder passes extra.Commands() in Config.Commands. Each command reads and writes files only inside the mounts, and NAME --help lists its options.

pdftotext -f 1 -l 3 report.pdf -           # a subset of poppler's: text of pages 1 to 3 on standard output
pdftoppm -png -r 100 report.pdf page       # writes page-1.png, page-2.png, ... (also -jpeg, -f, -l)

What it is made of

Part What it does
gbash, our fork of ewhauser/gbash The shell and its commands, in Go, with jq, yq, awk, sqlite3, html-to-markdown
wazero Runs CPython as WebAssembly
MCP Go SDK Talks to the MCP servers
wasm/ Runs a WASI program like a command: memory cap, deadline, and directories that are afero filesystems
files/ A host directory as an afero filesystem that cannot be left
extra/ pdftotext and pdftoppm, a subset of poppler's, PDFium as WebAssembly; images and charts are Python's (Pillow, matplotlib)
tools/ Turns MCP tools into commands and a Python package
computer.go Assembles the above
cmd/computer The command-line program and the MCP server mode

Python

python3 is wasi-python 0.1.6: CPython 3.15 for WASI with numpy, pandas, scipy, scikit-learn, statsmodels, matplotlib, Pillow, lxml, python-docx, python-pptx and about forty more packages. The module embeds it and unpacks it once into the user's cache directory:

python, err := computer.EmbeddedPython(ctx, wasm.Program{MaxRuns: 8, CacheDir: cacheDir})

The first compile takes about 30 seconds, then under a second from CacheDir; it runs in the background, and only the first python3 waits. See python/README.md.

The boundary

  • Files. The shell and programs see only the mounted folders. Programs reach them through Go's os.Root. Neither can create a symbolic link. The shell may write only inside writable mounts.
  • Network. None, unless the embedder passes gbash's network options, which allow curl to listed URL prefixes.
  • Python and SQLite run as WebAssembly with their own memory. Python has a memory cap for each run, and a limit on how many run at once (-python-runs, 8 by default), and stops at the request's deadline. The shell's sqlite3 also runs as WebAssembly, and we set no memory cap on it.
  • The shell and its Go commands run in the host process. gbash limits them, but it does not claim to be a hardened sandbox. Put the process in a container with CPU and memory limits if runaway scripts matter.
  • MCP tool names and descriptions are treated as untrusted text when commands and Python are generated.

Known limits

  • Stopping a WebAssembly run at its deadline makes Python three to four times slower. wasm.Program.Unstoppable trades that away for trusted scripts.
  • Python has no threads, processes, sockets, ctypes or mmap, and numpy's default integer is 32-bit.
  • wasi-python needs wazero's experimental exception handling: wazero is pinned to a main commit with the fix for wazero#2503, and wazero#2522, a use-after-free in that support, is open.
  • A shell command reads a file of at most 256 MiB, and a script runs at most 1,000,000 commands and as many loop iterations; a command's captured output is cut at gbash's default of 1 MiB. A larger file has to be handled in Python. An embedder changes these with gbash.WithLimitOverrides in Config.Options.
  • Developed on macOS; the tests run on Linux in CI.

Tests

go test .

The tests use the embedded Python, unless PYTHON_DIR names an unpacked release.

License

Apache License 2.0. See LICENSE.

Documentation

Overview

Package computer is a sandboxed computer for an agent: a shell (gbash) over mounted directories, with the tools of an MCP server as commands and as a Python package, jq and sqlite3, and python3 running as WebAssembly. Nothing in it reaches the network or the host, except through those tools and mounts.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Computer

type Computer struct {
	// contains filtered or unexported fields
}

A Computer is one agent's computer. Its methods are safe for concurrent use.

func New

func New(cfg Config) (_ *Computer, err error)

New makes a computer. Close it to free what it holds on the host.

func (*Computer) About

func (c *Computer) About(toolCommands, toolModules []string) string

About is what an agent is told about this computer, given the commands that call tools and their modules in Python, as ToolCommands and ToolModules name them.

func (*Computer) Close

func (c *Computer) Close() error

Close removes what the computer keeps on the host. The caller's mounts are the caller's and stay.

func (*Computer) Describe

func (c *Computer) Describe(ctx context.Context) (string, error)

Describe is what a model is told about this computer, with the commands that call tools as they are now.

func (*Computer) Environment

func (c *Computer) Environment() *Environment

Environment is the computer as an Environment. Every method may be called from any goroutine.

func (*Computer) Exec

Exec runs a script or a command in the computer's shell, with the tools as they are now. A request with no Env starts where the last one ended: gbash keeps files between requests, and Exec carries the working directory and the exported variables.

func (*Computer) MCPServer

func (c *Computer) MCPServer(ctx context.Context, limit time.Duration) (*mcp.Server, error)

MCPServer is the computer as an MCP server for an agent, with the tools bash, read, edit and write. The caller connects it: server.Run(ctx, &mcp.StdioTransport{}) for an agent that starts the computer as a program, or server.Connect with one end of mcp.NewInMemoryTransports for an agent in this process. limit is how long a bash call may run when the agent sets no shorter time. The file tools and their wording are gbash's codingtools' read, edit and write.

A computer is one agent's: every server made from it shares the one shell, with its working directory and variables. The file tools take a relative path from the computer's WorkDir, wherever the shell has since moved. The bash tool describes the commands that call tools as they are when MCPServer is called.

func (*Computer) Mounts

func (c *Computer) Mounts() []Mount

Mounts is every directory the computer mounts, the caller's and its own.

func (*Computer) Options

func (c *Computer) Options(ctx context.Context) ([]gbash.Option, error)

Options is the computer as gbash options, with the tools as they are now, for gbash's own front ends: its command-line program, its server and its tool for a model.

func (*Computer) Session

func (c *Computer) Session(ctx context.Context) (*gbash.Session, error)

Session is the computer's shell session, for its filesystem and for interactive use. The tools are listed on every call: when they have changed, the commands and the Python package are made again and the session is a new one over the same files.

func (*Computer) ToolCommands

func (c *Computer) ToolCommands() []string

ToolCommands are the commands that call tools, as the shell last built names them.

func (*Computer) ToolModules

func (c *Computer) ToolModules() []string

ToolModules are the modules of the Python package tools, one per command of ToolCommands, in its order.

type Config

type Config struct {
	Mounts   []Mount
	WorkDir  string             // where a command starts; a mount, or a directory in one
	Commands []commands.Command // commands beyond the shell's own
	Tools    []tools.Source     // the MCP servers whose tools become commands and the Python package `tools`
	Python   *Python            // python3; nil for none
	// Options are further gbash options, applied last: gbash.WithHTTPAccess or WithNetwork for curl, which the
	// computer otherwise lacks, WithLimitOverrides, WithTracing, WithPolicy.
	Options []gbash.Option
}

Config is what one computer is made of. Only Mounts is needed for a working shell.

type Environment

type Environment struct {
	// contains filtered or unexported fields
}

An Environment is the computer as the place an agent harness's own tools act: shell commands, and files, under the rules the shell's commands are held to. Its methods use only the standard library's types, so a harness can declare the interface it needs and be given this without either importing the other.

A name is an absolute path in the computer. An error is a *fs.PathError naming that path and never the host's, and errors.Is tells fs.ErrNotExist, fs.ErrPermission (also a write the rules refuse), fs.ErrExist, fs.ErrInvalid (also a file past the size limit), syscall.ENOTDIR, syscall.EISDIR and syscall.ENOTEMPTY apart. Which of them a misuse gets is the mount's to say: a host directory answers as a file system does, and a mount that is another afero.Fs as that one does: afero's memory filesystem allows a file made under a file, which turns that file into a directory and loses what it held.

func (*Environment) List

func (e *Environment) List(ctx context.Context, name string) ([]fs.FileInfo, error)

List is a directory's entries in the order of their names.

func (*Environment) Mkdir

func (e *Environment) Mkdir(ctx context.Context, name string, parents bool) error

Mkdir makes a directory, and with parents every directory above it that is missing; with parents, one that is there already is no error.

func (*Environment) Read

func (e *Environment) Read(ctx context.Context, name string, offset, length int64) ([]byte, error)

Read reads length bytes of a file from offset, or as many as it has; the rest of it when length is negative.

func (*Environment) Remove

func (e *Environment) Remove(ctx context.Context, name string, recursive bool) error

Remove removes a file or an empty directory, or with recursive a directory and everything in it. Nothing there to remove is fs.ErrNotExist.

func (*Environment) Rename

func (e *Environment) Rename(ctx context.Context, from, to string) error

Rename moves a file or a directory.

func (*Environment) Run

func (e *Environment) Run(ctx context.Context, script, dir string, env map[string]string, replaceEnv bool, stdout, stderr io.Writer) (int, error)

Run runs script in the computer's shell with dir as its working directory, and returns its exit code. env is added to the shell's environment, or is the whole of it when replaceEnv is set. Each Run has a shell of its own over the computer's files: nothing is carried from one to the next, and one neither waits for another nor for the computer's own Exec. Output is written to stdout and stderr while the command runs, and a pipeline's commands may write at the same time. When ctx ends the command is stopped and ctx's error returned. A command that ran and failed, or that the shell does not have (127), is an exit code and no error.

func (*Environment) Stat

func (e *Environment) Stat(ctx context.Context, name string, follow bool) (fs.FileInfo, error)

Stat describes a file; a symbolic link itself when follow is false.

func (*Environment) Truncate

func (e *Environment) Truncate(ctx context.Context, name string, size int64) error

Truncate makes the file size bytes long, cutting its end off or adding zero bytes to it.

func (*Environment) Write

func (e *Environment) Write(ctx context.Context, name string, content []byte, appending bool) error

Write writes the file anew, creating it, or adds to its end when appending.

type Mount

type Mount = wasm.Dir

A Mount is a directory the shell and every program see at the path Guest: the filesystem FS, which can be anything that implements afero.Fs, or, when FS is nil, the host directory Host (see files.Dir).

type Python

type Python struct {
	// contains filtered or unexported fields
}

Python is a wasi-python, compiled once and shared by every computer of the process.

func EmbeddedPython

func EmbeddedPython(ctx context.Context, p wasm.Program) (*Python, error)

EmbeddedPython compiles the wasi-python release this module carries: CPython with numpy, pandas, scipy, scikit-learn, statsmodels, matplotlib, Pillow, lxml, python-docx, python-pptx and more. On first use it unpacks the release, about 270 MB, into computer/python-<checksum> in the user's cache directory (os.UserCacheDir), and later processes reuse it; a host must not mount that directory. Each call compiles the program again, so a host calls it once and shares the Python, as with LoadPython.

func LoadPython

func LoadPython(ctx context.Context, dir string, p wasm.Program) (*Python, error)

LoadPython reads the wasi-python release unpacked in dir, laid out as EmbeddedPython unpacks it: python.wasm, lib/python315.zip (alone in lib) and mpl-data/, matplotlib's data, and starts compiling it; the first python3 waits for the compile, and fails with its error. p's Binary is read from the file, its host call is answered by the computer's tools, and a zero MemoryPages is 8192 (512 MiB); its other fields are the caller's.

Directories

Path Synopsis
cmd
computer command
Command computer runs a sandboxed computer from the command line.
Command computer runs a sandboxed computer from the command line.
Package extra holds the PDF commands the shell lacks, pdftotext and pdftoppm.
Package extra holds the PDF commands the shell lacks, pdftotext and pdftoppm.
Package files has the filesystems a computer can be given.
Package files has the filesystems a computer can be given.
Package tools turns the tools of an MCP server into what a shell and a script can use: a command per group of tools, with a subcommand per tool, and a Python package with a module per group and a function per tool.
Package tools turns the tools of an MCP server into what a shell and a script can use: a command per group of tools, with a subcommand per tool, and a Python package with a module per group and a function per tool.
Package wasm runs a WebAssembly program built for WASI the way a command runs: with arguments, an environment, standard streams and directories, to an exit code.
Package wasm runs a WebAssembly program built for WASI the way a command runs: with arguments, an environment, standard streams and directories, to an exit code.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL