Open History to see the last commit
1---
2title: Tools
3id: tools
4order: 6
5description: "Give the in-sandbox agent your app's own server tools. They run on your host, with their database, secrets, and closures, over an authenticated MCP tool proxy."
6---
7
8A host tool runs in your app, with your database and secrets. The agent in the sandbox calls it. The tool code does not run in the sandbox.
9
10Pass your server tools to `chat()`. TanStack AI bridges them into the sandbox. The agent calls them like any MCP tool, and each tool runs on your host.
11
12## Bridge a tool into the sandbox
13
141. Define a server tool, the same as for any `chat()` call.
152. Pass it in `tools`, next to `withSandbox`.
16
17```ts
18import { chat, toolDefinition } from '@tanstack/ai'
19import { GROK_CLI_INSTALL_COMMAND, grokBuildText } from '@tanstack/ai-grok-build'
20import {
21 createSecrets,
22 defineSandbox,
23 defineWorkspace,
24 githubRepo,
25 withSandbox,
26} from '@tanstack/ai-sandbox'
27import { dockerSandbox } from '@tanstack/ai-sandbox-docker'
28import { z } from 'zod'
29
30// Lives on your host. The sandbox never sees it.
31const todos = [{ id: '1', userId: 'u1', title: 'Fix the login bug' }]
32
33const getTodos = toolDefinition({
34 name: 'getTodos',
35 description: 'List the todos of a user.',
36 inputSchema: z.object({ userId: z.string() }),
37}).server(async ({ userId }) => todos.filter((t) => t.userId === userId))
38
39const repoSandbox = defineSandbox({
40 id: 'repo-agent',
41 provider: dockerSandbox({ image: 'node:22' }),
42 workspace: defineWorkspace({
43 source: githubRepo({ repo: 'owner/app' }),
44 setup: [GROK_CLI_INSTALL_COMMAND],
45 secrets: createSecrets({ XAI_API_KEY: process.env.XAI_API_KEY ?? '' }),
46 }),
47})
48
49const stream = chat({
50 threadId: 'thread-1',
51 adapter: grokBuildText('grok-build'),
52 messages: [{ role: 'user', content: 'Fix the bugs in the todos of user u1.' }],
53 tools: [getTodos],
54 middleware: [withSandbox(repoSandbox)],
55})
56```
57
58The agent can now call `getTodos`. The tool reads `todos` on your host and sends the result into the sandbox.
59
60## How it works
61
62The agent always has its own native tools (Bash, file edits, search). They run on the sandbox filesystem. Your `chat()` tools go over a host-side MCP tool proxy:
63
641. The agent calls the tool by name, like any MCP tool.
652. The call crosses the sandbox boundary to your host.
663. The tool's `execute()` runs on your host, with its database handle, secrets, and closures.
674. The result goes back into the sandbox as the tool output.
68
69`execute()` never goes into the sandbox. Only the call and the result cross the boundary.
70
71A random bearer token for each run protects the bridge. Only the agent of this run has that token, so only it can call your tools.
72
73> [!NOTE]
74> This page is about your own tools, bridged back to your server. To give the agent third-party MCP servers that it calls directly, declare them on the workspace. See [Provisioning](./provisioning). For server tools in general, see [Server Tools](../tools/server-tools).
75
76## Make sure the sandbox can reach the bridge
77
78The bridge is an HTTP endpoint that the sandbox calls back to. So the sandbox must be able to connect to your server.
79
80| Where it runs | Host the sandbox calls | Setup |
81| --- | --- | --- |
82| Local process | `localhost` | None. |
83| Docker container (`dockerSandbox`) | `host.docker.internal` | None. |
84| Docker Sandboxes (`sbxSandbox`) | `host.docker.internal` | A deny or ask allowlist must include `localhost`. `sbxSandbox()` adds it when it writes a real allowlist. |
85| Deployed server (production) | A URL your provisioner advertises | Provide a bridge provisioner. |
86| Cloud sandbox, driven from your laptop | Your laptop, which has no public URL | Tunnel the bridge with `withNgrokBridge`. |
87
88### Local process and Docker container
89
90The sandbox runs on the same machine as your server. It reaches it on `localhost` (local process) or `host.docker.internal` (Docker container). Bridged tools need no extra setup. If the HTTP listener cannot start, bridge startup rejects with the original Node.js error.
91
92### Docker Sandboxes (`sbxSandbox`)
93
94The guest still calls `host.docker.internal`. The host HTTP proxy changes that host to `localhost` before it checks `sbx policy`. If you write a deny or ask allowlist, allow `localhost` (or let `sbxSandbox()` add it). `denyNetwork` alone does not write that allowlist.
95
96### A deployed server (production)
97
98The default provisioner (`nodeHttpBridgeProvisioner`) binds loopback and tells the sandbox to call `127.0.0.1` (`host.docker.internal` for Docker). It does not read your public host from the request. So a remote sandbox cannot reach it, even from a deployed server.
99
100Provide your own `ToolBridgeProvisioner` from a chat middleware with `provideToolBridgeProvisioner(ctx, provisioner)`. Its `provision()` returns the bridge with a `url` the sandbox can reach, for example your server's public URL. `withNgrokBridge` from `@tanstack/ai-sandbox/ngrok` is a working example. Every call still needs the bearer token for the run, so a public endpoint is safe.
101
102The Cloudflare package ships its own provisioner, so the edge setup needs no extra step. See [Cloudflare](./cloudflare).
103
104### A cloud sandbox, driven from your laptop
105
106A cloud provider (Daytona, Vercel, Sprites, and the others) runs a remote VM. In local dev, that VM cannot call your `localhost`, and your laptop has no public URL.
107
108The `@tanstack/ai-sandbox/ngrok` subpath tunnels the bridge through [ngrok](https://ngrok.com):
109
1101. Install the optional peer dependency `@ngrok/ngrok`:
111
112<!-- ::start:tabs variant="package-manager" mode="install" -->
113
114react: @ngrok/ngrok
115vue: @ngrok/ngrok
116solid: @ngrok/ngrok
117svelte: @ngrok/ngrok
118preact: @ngrok/ngrok
119angular: @ngrok/ngrok
120vanilla: @ngrok/ngrok
121octane: @ngrok/ngrok
122
123<!-- ::end:tabs -->
124
1252. Set `NGROK_AUTHTOKEN`.
1263. Add `withNgrokBridge` **after** `withSandbox(...)`:
127
128```ts
129import { chat } from '@tanstack/ai'
130import { grokBuildText } from '@tanstack/ai-grok-build'
131import { withSandbox } from '@tanstack/ai-sandbox'
132import { withNgrokBridge } from '@tanstack/ai-sandbox/ngrok'
133import { repoSandbox } from './sandbox'
134import { getTodos } from './tools'
135
136chat({
137 threadId: 'thread-1',
138 adapter: grokBuildText('grok-build'),
139 messages: [{ role: 'user', content: 'Fix the bugs in the todos of user u1.' }],
140 tools: [getTodos],
141 // Cloud provider in local dev: tunnel the bridge so the remote sandbox can
142 // reach it. Local process and Docker do not need this.
143 middleware: [withSandbox(repoSandbox), withNgrokBridge],
144})
145```
146
147`withNgrokBridge` is for local dev only. In production your deployed server is already reachable, so you ship without it.