Agent tools in the SDK
sandbox.tools holds the tools a coding agent needs, in all five SDKs. Their answers are short and numbered, and they are cut to a
token budget:
| Tool | What it returns |
|---|---|
view(path, start, end) |
Numbered lines of a file |
search(pattern, path) |
Matches with file and line |
repo_map(path) |
An outline of a repository: files, classes and functions |
apply_patch(patch, verify=) |
Applies a unified diff. With verify, it also runs a test or build command, so the edit and its check are one call. |
run(cmd) |
The exit code, what a test runner or compiler reported, then the output cut to the budget. Error lines and the end of the log are kept. |
diagnostics(path) |
Language-server diagnostics for a file |
A patch that does not apply, or a missing file, comes back as an error whose text says what to fix.
from svm import Sandboxfrom svm.agent_tools import AgentTools
PATCH = """--- a/calc.py+++ b/calc.py@@ -1,2 +1,2 @@ def add(a, b):- return a - b+ return a + b"""
with Sandbox.create(template="base") as sandbox: sandbox.files.write("/tmp/app/calc.py", "def add(a, b):\n return a - b\n") tools = AgentTools(sandbox, cwd="/tmp/app") # or sandbox.tools with cwd= on each call
print(tools.view("calc.py").text) print(tools.search("def add").text) print(tools.repo_map().text) print(tools.apply_patch(PATCH).text) print(tools.command("python3 -c 'from calc import add; print(add(2, 3))'").text)
sandbox.lsp.install("python") # diagnostics come from a language server print(tools.diagnostics("calc.py").text)import { Sandbox } from "@svm/sandbox";
const sandbox = await Sandbox.create({ template: "base" });try { await sandbox.git.clone("https://github.com/octocat/Hello-World.git", "/home/user/repo"); const tools = sandbox.tools; const cwd = "/home/user/repo";
console.log((await tools.view("README", { cwd, start: 1, lines: 40 })).text); console.log((await tools.search("Hello", { cwd, maxMatches: 20 })).text); console.log((await tools.repoMap({ cwd })).text);
const patch = await tools.applyPatch(undefined, { cwd, edits: [ { path: "README", old: "Hello World!", new: "Hello, SVM!" }, { path: "hello.py", content: "print(greeting)\n" }, // a new file ], }); console.log(patch.ok, patch.text);
console.log((await tools.command("git status --short", { cwd })).text); console.log((await tools.diagnostics("hello.py", { cwd })).text); // reports the undefined name} finally { await sandbox.kill();}tools := sandbox.Tools("/tmp/project", nil) // relative paths start here; nil: no cachecall := svm.ToolCall{}
view, err := tools.View(ctx, "app.py", 1, 20, call)if err != nil { return err}found, err := tools.Search(ctx, "def ", svm.SearchOptions{}, call)if err != nil { return err}repoMap, err := tools.RepoMap(ctx, ".", nil, nil, call)if err != nil { return err}edit := []map[string]any{{"path": "app.py", "old": "'hi '", "new": "'hello '"}}patched, err := tools.ApplyPatch(ctx, "", edit, false, false, "", call)if err != nil { return err}ran, err := tools.Command(ctx, "python3 app.py", false, nil, 30, call)if err != nil { return err}diagnostics, err := tools.Diagnostics(ctx, "app.py", "", 30, call)if err != nil { return err}for _, r := range []svm.ToolResult{view, found, repoMap, patched, ran, diagnostics} { fmt.Println(r.OK, r.Text)}require "svm"
box = SVM::Sandbox.create(template: "base")begin box.git.clone("https://github.com/pallets/itsdangerous.git", "/home/user/repo", depth: 1) tools = box.tools(cwd: "/home/user/repo")
puts tools.view("src/itsdangerous/signer.py", start: 1, lines: 40).text puts tools.search("def sign", glob: "*.py", max_matches: 10).text puts tools.repo_map(budget: 800).text
patch = tools.apply_patch(edits: [{ path: "src/itsdangerous/__init__.py", old: "from .signer import Signer", new: "from .signer import Signer # noqa: F401" }]) puts patch.ok ? "patched" : patch.text
test = tools.command("python3 -m pytest -q tests/test_itsdangerous/test_signer.py", timeout_seconds: 300) puts test.text # exit code, failed tests and the end of the log, cut to a token budget
puts tools.diagnostics("src/itsdangerous/signer.py").textensure box.killendimport dev.svm.sandbox.AgentTools;import dev.svm.sandbox.Client;import dev.svm.sandbox.Sandbox;import java.util.List;import java.util.Map;
public class AgentToolsExample { public static void main(String[] args) { Client client = Client.fromEnvironment(); try (Sandbox sandbox = Sandbox.create(client, "base")) { sandbox.git().clone("https://github.com/pallets/itsdangerous.git", "/home/user/repo", null, 1, null); AgentTools tools = sandbox.tools("/home/user/repo", null); // no cache
System.out.println(tools.view("README.md", 1, 20, null).text); System.out.println(tools.search("def sign", "src", 20, 5, null).text); System.out.println(tools.repoMap(".", null, null, 1024).text);
List<Map<String, Object>> edits = List.of(Map.of("path", "NOTES.md", "content", "agent notes\n")); System.out.println(tools.applyPatch(null, edits, false, false, null, null).text);
AgentTools.Result run = tools.command("ls src", false, null, 60, null); System.out.println(run.ok + " " + run.text);
System.out.println(tools.diagnostics("src/itsdangerous/signer.py", null, null, 60).text); } }}MCP servers in a sandbox
Section titled “MCP servers in a sandbox”A sandbox can run MCP servers from the platform catalog behind its own MCP gateway. Turn them on at create with mcp_servers.
Hand the gateway URL and its token to any MCP client. The token is a secret, and it expires, so fetch it when you connect.
import jsonimport urllib.request
from svm import Sandbox
# Server names come from the platform catalog ("time" and "fetch" need no secrets).servers = [{"name": "time", "catalog_id": "time"}, {"name": "fetch", "catalog_id": "fetch"}]
with Sandbox.create(template="base", mcp_servers=servers) as sandbox: url = sandbox.get_mcp_url() token = sandbox.get_mcp_token() # send it as X-Traffic-Access-Token, not Authorization print("gateway:", url)
request = urllib.request.Request(url, headers={"X-Traffic-Access-Token": token}) with urllib.request.urlopen(request) as response: print(json.load(response)) # the servers behind the gateway
# An MCP client sends JSON-RPC to f"{url}/time" with the same header.import { Sandbox } from "@svm/sandbox";
// Server names come from the platform's MCP catalog (client.getMcpCatalog()).const sandbox = await Sandbox.create({ template: "base", mcpServers: [{ name: "github", catalog_id: "github" }], wait: true,});try { const url = await sandbox.getMcpUrl(); // https://<port>-<id>.<domain>/mcp const token = await sandbox.getMcpToken(); // send as X-Traffic-Access-Token, not Authorization: Bearer console.log(url);
const servers = await fetch(url, { headers: { "X-Traffic-Access-Token": token ?? "" } }); console.log(await servers.json()); // MCP JSON-RPC goes to `${url}/github`} finally { await sandbox.kill();}sandbox, err := client.CreateSandbox(ctx, svm.CreateSandboxOptions{ Template: "base", MCPServers: []map[string]any{{"name": "github", "catalog_id": "github"}}, // ids from the MCP catalog})if err != nil { return err}defer sandbox.Kill(ctx)
url, err := sandbox.GetMcpURL(ctx)if err != nil { return err}token, err := sandbox.GetMcpToken(ctx) // a secret; it changes when the sandbox resumesif err != nil { return err}fmt.Println("MCP gateway:", url, "- send each server's JSON-RPC to", url+"/github")
// A client sends the token in X-Traffic-Access-Token (Authorization: Bearer gets 401).request, err := http.NewRequestWithContext(ctx, "GET", url, nil)if err != nil { return err}request.Header.Set("X-Traffic-Access-Token", token)response, err := http.DefaultClient.Do(request)if err != nil { return err}defer response.Body.Close()fmt.Println("server list:", response.Status)require "svm"
# Server names come from the platform catalog (catalog_id).box = SVM::Sandbox.create(template: "base", mcp_servers: [{ name: "github", catalog_id: "github" }])begin url = box.get_mcp_url # https://<port>-<sandbox id>.<domain>/mcp; a GET lists the servers token = box.get_mcp_token # send it as X-Traffic-Access-Token (not Authorization: Bearer)
puts "MCP endpoint for the github server: #{url}/github" puts "header: X-Traffic-Access-Token: #{token ? '<token>' : '(none, sandbox is not secure)'}"ensure box.killendimport dev.svm.sandbox.Client;import dev.svm.sandbox.Sandbox;import java.util.List;import java.util.Map;
public class McpGatewayExample { public static void main(String[] args) { Client client = Client.fromEnvironment(); Sandbox.CreateOptions options = new Sandbox.CreateOptions(); options.template = "base"; // Names come from the platform's MCP catalog. options.mcpServers = List.of(Map.of("name", "github", "catalog_id", "github")); options.wait = true; try (Sandbox sandbox = Sandbox.create(client, options)) { System.out.println("gateway: " + sandbox.getMcpUrl()); // An MCP client sends this token; it is null for a sandbox created with secure = false. String token = sandbox.getMcpToken(); System.out.println("token present: " + (token != null)); } }}No MCP server starts unless the sandbox asks for it. A new sandbox has none. Tool rules on the gateway can require an approval for
risky tools; sandbox.mcp_approvals lists and decides pending approvals.