Open History to see the last commit
1---
2title: Tools
3id: tools
4order: 6
5description: "Bridge your app's host tools (with their DB, secrets, and closures) into the in-sandbox agent over an authenticated MCP tool-proxy."
6---
7
8The agent inside the sandbox always has its own **native tools**: Bash, file
9edits, and search, running directly on the sandbox filesystem. That covers
10everything the agent does locally to the working tree.
11
12What it cannot do on its own is reach back into _your_ app: your database, your
13secrets, the closures you captured when you defined a tool. For that, the
14`chat()`-provided **server tools** are **bridged** into the sandbox.
15
16> This page is about your own host tools, bridged back to the orchestrator. If
17> instead you want to give the agent third-party MCP servers it talks to
18> directly with no host round-trip. Those are declared on the workspace, see
19> [Provisioning](./provisioning). For server tools in general, see the main
20> [server tools](../tools/server-tools) doc.
21
22## Native vs bridged tools
23
24When you pass `tools` to `chat()` with a sandbox in the middleware, each tool is
25exposed to the in-sandbox agent over a **host-side MCP tool-proxy**:
26
271. The agent calls the tool by name, as it would any MCP tool.
282. The call is proxied back across the sandbox boundary to the host.
293. The tool's `execute()` runs **on the host**, keeping its DB handle, secrets,
30 and any closures it captured.
314. The result is returned into the sandbox as the tool-call output.
32
33So `execute()` never ships into the sandbox; only the call and its result cross
34the boundary. The tool keeps running where it was defined.
35
36```ts
37import { chat } from '@tanstack/ai'
38import { grokBuildText } from '@tanstack/ai-grok-build'
39import { withSandbox } from '@tanstack/ai-sandbox'
40import { repoSandbox } from './sandbox'
41import { messages, threadId } from './chat-context'
42import { getTodos } from './tools'
43import { db } from './db'
44
45chat({
46 threadId,
47 adapter: grokBuildText('grok-build'),
48 messages,
49 // `execute()` closes over `db` and runs on the host, never in the sandbox.
50 tools: [
51 getTodos.server(async ({ userId }: { userId: string }) =>
52 db.todos.find({ userId }),
53 ),
54 ],
55 middleware: [withSandbox(repoSandbox)],
56})
57```
58
59The bridge is gated by a **random per-run bearer token**, so the proxy endpoint
60is not an open door: only the agent for this run, holding that token, can
61invoke your tools.
62
63## Reaching the bridge
64
65The bridge is an HTTP endpoint the **sandbox calls back to**. For bridged tools
66to work, the sandbox has to be able to open a connection to your orchestrator.
67That holds in two cases and breaks in a third.
68
69| Topology | Host the sandbox dials | Setup |
70| --- | --- | --- |
71| Local process | `localhost` | None. Works by default. |
72| Docker container (`dockerSandbox`) | `host.docker.internal` | None. Works by default. |
73| Docker Sandboxes (`sbxSandbox`) | `host.docker.internal` | The guest URL stays `host.docker.internal`. The `sbx` proxy rewrites that host to `localhost` before the policy match. A deny or ask allowlist must include `localhost`. `sbxSandbox()` adds `localhost` when it writes a real allowlist. |
74| Deployed orchestrator (production) | Your public host, derived from the request | None. Works by default. |
75| Remote cloud sandbox, driven from your laptop | Your laptop, which has no public URL | Tunnel the bridge with `withNgrokBridge`. |
76
77### Local process / Docker container
78
79The orchestrator is the same machine as the sandbox, reached on `localhost` (local-process) or `host.docker.internal` (Docker container). Bridged tools work with no extra configuration. If the HTTP listener cannot start, bridge startup rejects with the original Node.js error.
80
81### Docker Sandboxes (`sbxSandbox`)
82
83The guest still dials `host.docker.internal`. The host HTTP proxy rewrites
84that host to `localhost` before it checks `sbx policy`. If you write a deny
85or ask allowlist, allow `localhost` (or let `sbxSandbox()` add it).
86`denyNetwork` alone does not write that allowlist.
87
88### A deployed orchestrator (production)
89
90A deployed orchestrator already has a public URL, so the bridge is reachable out
91of the box. The provisioner advertises your public host (derived from the
92incoming request, instead of `localhost`, and every call is still gated by the
93per-run bearer token, so exposing the endpoint publicly is safe. This is the
94same path the edge/Cloudflare deployment uses; see [Cloudflare](./cloudflare).
95
96### A remote cloud sandbox, driven from your laptop
97
98With a cloud provider (Daytona, Vercel, Sprites) in **local dev**, the sandbox is a remote
99VM. It **cannot dial your machine's `localhost`**, and your laptop has no public
100URL, so bridged tools can't reach the host until you expose the bridge.
101
102The `@tanstack/ai-sandbox/ngrok` subpath tunnels the loopback bridge through
103[ngrok](https://ngrok.com) so a remote sandbox can reach it. Set
104`NGROK_AUTHTOKEN`, then add `withNgrokBridge` **after** `withSandbox(...)`:
105
106```ts
107import { chat } from '@tanstack/ai'
108import { grokBuildText } from '@tanstack/ai-grok-build'
109import { withSandbox } from '@tanstack/ai-sandbox'
110import { withNgrokBridge } from '@tanstack/ai-sandbox/ngrok'
111import { repoSandbox } from './sandbox'
112import { messages, threadId } from './chat-context'
113import { getTodos } from './tools'
114import { db } from './db'
115
116chat({
117 threadId,
118 adapter: grokBuildText('grok-build'),
119 messages,
120 tools: [
121 getTodos.server(async ({ userId }: { userId: string }) =>
122 db.todos.find({ userId }),
123 ),
124 ],
125 // Cloud provider in local dev → tunnel the host bridge so the remote sandbox
126 // can reach it. Local process / Docker don't need this.
127 middleware: [withSandbox(repoSandbox), withNgrokBridge],
128})
129```
130
131`@ngrok/ngrok` is an **optional peer dependency**, install it alongside the
132subpath (`npm i @ngrok/ngrok`). `withNgrokBridge` is purely a local-dev
133convenience: in production your deployed orchestrator is already reachable, so
134you ship without it.