Terminals and computer use
Attach a terminal to a running sandbox or connect an agent to its computer tools.
Set TANGLE_SANDBOX_ID to a sandbox you own for these examples.
Stream a terminal
This Node.js program opens a shell, streams its output, and disconnects when you press Enter.
Run it with npx tsx terminal.ts.
import { createInterface } from "node:readline/promises";
import { Sandbox } from "@tangle-network/sandbox";
const apiKey = process.env.TANGLE_API_KEY;
if (!apiKey) throw new Error("Set TANGLE_API_KEY");
const client = new Sandbox({
apiKey,
baseUrl: "https://sandbox.tangle.tools",
});
const sandboxId = process.env.TANGLE_SANDBOX_ID;
if (!sandboxId) throw new Error("Set TANGLE_SANDBOX_ID");
const box = await client.get(sandboxId);
if (!box) throw new Error("Sandbox not found");
const terminal = await box.terminals.attach("docs-shell", {
cols: 100,
rows: 30,
handlers: { onData: (data) => { process.stdout.write(data); } },
});
terminal.write("pwd\r");
const input = createInterface({ input: process.stdin, output: process.stdout });
await input.question("Press Enter to disconnect. ");
input.close();
await terminal.close();Reuse the connection ID to reattach to the same terminal while it remains available.
Register onData before attaching so the handler receives replayed output.
write() sends input; resize() changes the terminal dimensions; close() disconnects the viewer.
| Operation | SDK |
|---|---|
| Create a terminal | box.terminals.create() |
| List or inspect terminals | box.terminals.list(), .get() |
| Send input over HTTP | box.terminals.input() |
| Stream live input and output | box.terminals.attach() |
Interactive agent sessions
Use box.session(sessionId).interactive() to run an agent’s native TUI.
The interactive agents guide covers starting, streaming, keyboard input, reconnecting, and stopping it.
Use agents and sessions for headless execution and structured messages.
Connect computer tools through MCP
Create the sandbox with capabilities: ["computer_use"] before running this example.
It writes a short-lived MCP configuration for an MCP-compatible agent or editor.
import { writeFile } from "node:fs/promises";
import { Sandbox } from "@tangle-network/sandbox";
const apiKey = process.env.TANGLE_API_KEY;
if (!apiKey) throw new Error("Set TANGLE_API_KEY");
const client = new Sandbox({
apiKey,
baseUrl: "https://sandbox.tangle.tools",
});
const sandboxId = process.env.TANGLE_SANDBOX_ID;
if (!sandboxId) throw new Error("Set TANGLE_SANDBOX_ID");
const box = await client.get(sandboxId);
if (!box) throw new Error("Sandbox not found");
const endpoint = await box.getMcpEndpoint({
capabilities: ["computer_use"],
ttlMinutes: 5,
});
await writeFile("tangle-mcp.json", JSON.stringify(endpoint.config, null, 2), {
mode: 0o600,
flag: "wx",
});Load tangle-mcp.json into your MCP client’s configuration.
When the token expires, delete this file and run the example again.
The example refuses to overwrite an existing configuration.
Treat the file as a credential and keep it out of version control.
Computer-use support depends on the sandbox image and deployment.