Open History to see the last commit
1/**
2 * ngrok-backed tool-bridge provisioner — make the host tool bridge reachable
3 * from REMOTE sandboxes (Daytona, Vercel, …) while developing locally.
4 *
5 * The default bridge ({@link nodeHttpBridgeProvisioner}) binds `localhost`, so
6 * only same-machine providers (local-process, Docker) can reach it. A cloud
7 * sandbox is a remote VM and can't dial your machine's loopback. This
8 * provisioner stands up the normal loopback bridge, opens an ngrok tunnel to its
9 * port, and advertises the public `https://…/mcp` URL (with the same per-run
10 * bearer token) to the sandbox — so bridged tools / code mode work there too.
11 *
12 * In PRODUCTION you don't need this: a deployed orchestrator already has a public
13 * URL, so a provisioner can advertise that directly (derived from the request).
14 * ngrok is the local-dev stand-in for "the orchestrator is reachable".
15 *
16 * `@ngrok/ngrok` is an OPTIONAL peer dependency — it's loaded lazily, so this
17 * subpath imports cleanly without it; only {@link withNgrokBridge} /
18 * {@link ngrokBridgeProvisioner} require it at run time. Set `NGROK_AUTHTOKEN`.
19 */
20import { defineChatMiddleware } from '@tanstack/ai'
21import { provideToolBridgeProvisioner } from './capabilities'
22import { startHostToolBridge } from './tool-bridge'
23import type { ToolBridgeProvisioner } from './tool-bridge'
24
25/** Whether ngrok tunnelling is configured (an authtoken is present). */
26export function ngrokConfigured(): boolean {
27 return Boolean(process.env.NGROK_AUTHTOKEN)
28}
29
30/**
31 * A {@link ToolBridgeProvisioner} that tunnels the loopback bridge through ngrok
32 * (one ephemeral tunnel per run; both are torn down together). Requires the
33 * optional `@ngrok/ngrok` peer dependency and `NGROK_AUTHTOKEN`.
34 */
35export const ngrokBridgeProvisioner: ToolBridgeProvisioner = {
36 async provision(tools, options) {
37 // Lazy + optional: only needed when this provisioner actually runs.
38 const { default: ngrok } = await import('@ngrok/ngrok')
39 const { provider: _provider, ...core } = options
40 const bridge = await startHostToolBridge(tools, {
41 hostForSandbox: '127.0.0.1',
42 bindAddress: '127.0.0.1',
43 ...core,
44 })
45 try {
46 const port = Number(new URL(bridge.url).port)
47 const listener = await ngrok.forward({
48 addr: port,
49 authtoken_from_env: true,
50 })
51 const publicUrl = listener.url()
52 if (!publicUrl) {
53 throw new Error('ngrok did not return a public URL')
54 }
55 return {
56 ...bridge,
57 url: `${publicUrl}/mcp`,
58 close: async () => {
59 try {
60 await listener.close()
61 } finally {
62 await bridge.close()
63 }
64 },
65 }
66 } catch (error) {
67 // Don't leak the loopback bridge if the tunnel couldn't be opened.
68 await bridge.close()
69 throw error
70 }
71 },
72}
73
74/**
75 * Chat middleware that routes the tool bridge through ngrok. Add it AFTER
76 * `withSandbox(...)` for cloud providers so the in-sandbox harness can reach the
77 * host tools. Not needed for local-process / Docker (they reach the bridge
78 * directly) — just don't add it there.
79 */
80export const withNgrokBridge = defineChatMiddleware({
81 name: 'ngrok-bridge',
82 setup(ctx) {
83 provideToolBridgeProvisioner(ctx, ngrokBridgeProvisioner)
84 },
85})