Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/add-ai-secure-mcp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/ai-secure-mcp': minor
---

Add `@tanstack/ai-secure-mcp`, a security envelope around `createMCPServer` for self-hosting an MCP server on the internet. `createSecureMcpServer` wraps your tools with hashed-token bearer auth, scopes and a per-client target allowlist, a deny-by-default egress helper a tool opts into (it is not an enforced network boundary — a tool that calls global `fetch` still reaches the network), a per-client sliding-window rate limit, a redacted append-only JSONL audit log, a pluggable tripwire notifier (`ntfyNotifier`, `webhookNotifier`, `silentNotifier`), and two-phase propose/confirm approval. A go-live preflight refuses a public Tailscale funnel while a secure default is off (unhashed token, audit off, or notifier off or silent).
5 changes: 5 additions & 0 deletions .changeset/bump-quickjs-bun-0-2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/ai-isolate-quickjs-bun': patch
---

Update `quickjs-bun` to 0.2.0. Version 0.1.2 cannot compile its native code on Ubuntu 26.04, so the driver failed to create a context there. The driver now needs Bun 1.4.0 or later.
14 changes: 14 additions & 0 deletions .changeset/extract-ai-isolate-contract.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
'@tanstack/ai-isolate': minor
'@tanstack/ai-code-mode': patch
'@tanstack/ai-isolate-quickjs': patch
'@tanstack/ai-isolate-quickjs-bun': patch
'@tanstack/ai-isolate-node': patch
'@tanstack/ai-isolate-cloudflare': patch
'@tanstack/ai-isolate-e2b': patch
'@tanstack/ai-isolate-daytona': patch
---

Add `@tanstack/ai-isolate`, the engine-agnostic isolate base. It owns the contract every sandbox driver implements (`IsolateDriver`, `IsolateConfig`, `IsolateContext`, `ExecutionResult`, `NormalizedError`, `ToolBinding`) plus the shared `wrapCode` helper. These previously lived inside `@tanstack/ai-code-mode`, which forced every driver to depend on Code Mode just for the contract.

Drivers (`quickjs`, `quickjs-bun`, `node`, `cloudflare`, `e2b`, `daytona`) now depend on `@tanstack/ai-isolate` instead of `@tanstack/ai-code-mode`. It is a regular dependency of each driver, so you install nothing extra. `@tanstack/ai-code-mode` re-exports the same types and `wrapCode`, so its public API is unchanged.
6 changes: 3 additions & 3 deletions .github/workflows/bun-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ on:
pull_request:
paths:
- 'packages/ai-isolate-quickjs-bun/**'
- 'packages/ai-code-mode/**'
- 'packages/ai-isolate/**'
- '.github/workflows/bun-test.yml'

concurrency:
Expand All @@ -42,10 +42,10 @@ jobs:
- name: Setup Bun
uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0
with:
# The driver requires Bun >= 1.3.14 (see the package `engines` field).
# The driver requires Bun >= 1.4.0 (see the package `engines` field).
bun-version: latest
- name: Build package and its workspace dependencies
# `@tanstack/ai-code-mode` is a `workspace:*` peer dependency and is
# `@tanstack/ai-isolate` is a workspace dependency and is
# imported at runtime (`wrapCode`) by the driver, so it must be built to
# `dist/` before the Bun tests can import it. nx builds the dependency
# graph first via `^build`.
Expand Down
2 changes: 1 addition & 1 deletion docs/code-mode/code-mode-isolates.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ octane: @tanstack/ai-isolate-quickjs-bun

<!-- ::end:tabs -->

The driver needs Bun 1.3.14 or later. On Node.js, it throws an error when it creates a context, so use the Node or QuickJS WASM driver there. On Windows, set the `QUICKJS_BUN_NATIVE_LIBRARY` environment variable to a prebuilt QuickJS dynamic library.
The driver needs Bun 1.4.0 or later. On Node.js, it throws an error when it creates a context, so use the Node or QuickJS WASM driver there. On Windows, set the `QUICKJS_BUN_NATIVE_LIBRARY` environment variable to a prebuilt QuickJS dynamic library.

```typescript
import { createQuickJSBunIsolateDriver } from '@tanstack/ai-isolate-quickjs-bun'
Expand Down
2 changes: 1 addition & 1 deletion examples/ts-code-mode-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"motion": "^11.15.0",
"nitro": "3.0.260610-beta",
"puppeteer": "^24.34.0",
"quickjs-bun": "0.1.2",
"quickjs-bun": "0.2.0",
"quickjs-emscripten": "^0.31.0",
"quickjs-emscripten-core": "^0.31.0",
"react": "^19.2.3",
Expand Down
1 change: 1 addition & 0 deletions packages/ai-code-mode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"tanstack-intent"
],
"dependencies": {
"@tanstack/ai-isolate": "workspace:^",
"sucrase": "^3.35.0"
},
"peerDependencies": {
Expand Down
5 changes: 3 additions & 2 deletions packages/ai-code-mode/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ export {
// TypeScript stripping utility
export { stripTypeScript } from './strip-typescript'

// Code wrapper utility (used by isolate drivers)
export { wrapCode } from './code-wrapper'
// Code wrapper utility (used by isolate drivers) — now lives in
// @tanstack/ai-isolate, re-exported here to keep the public API stable.
export { wrapCode } from '@tanstack/ai-isolate'

// All types
export type {
Expand Down
157 changes: 21 additions & 136 deletions packages/ai-code-mode/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,144 +11,29 @@ import type { SecretParameterHandler } from './validate-bindings'
// Isolate Driver Interfaces
// ============================================================================

/**
* Interface for isolate/sandbox drivers
* Each runtime environment implements this to provide sandboxed code execution
*/
export interface IsolateDriver {
/**
* Create a new isolated execution context with tool bindings
*/
createContext: (config: IsolateConfig) => Promise<IsolateContext>
}

/**
* Configuration for creating an isolate context
*/
export interface IsolateConfig {
/**
* Tools transformed into callable bindings for the sandbox
*/
bindings: Record<string, ToolBinding>

/**
* Execution timeout in milliseconds (default: 30000)
*/
timeout?: number

/**
* Memory limit in MB (default: 128)
*/
memoryLimit?: number
}

/**
* Isolated execution context with tool bindings injected
*/
export interface IsolateContext {
/**
* Execute generated code and return results. Drivers stop the run when
* `options.signal` aborts, if their runtime can.
*/
execute: <T = unknown>(
code: string,
options?: { signal?: AbortSignal },
) => Promise<ExecutionResult<T>>

/**
* Clean up sandbox resources
*/
dispose: () => Promise<void>
}

/**
* Result of code execution in the sandbox
*/
export interface ExecutionResult<T = unknown> {
/**
* Whether execution completed without errors
*/
success: boolean

/**
* Return value from the executed code (if successful)
*/
value?: T

/**
* Normalized error information (if failed)
*/
error?: NormalizedError

/**
* Console output captured during execution
*/
logs?: Array<string>
}

/**
* Normalized error format for cross-runtime compatibility
*/
export interface NormalizedError {
/**
* Error name/type
*/
name: string

/**
* Error message
*/
message: string

/**
* Stack trace (if available)
*/
stack?: string

/**
* Error code (if available)
*/
code?: string
}

// ============================================================================
// Tool Binding Interfaces
// ============================================================================

/**
* A tool transformed into a format suitable for sandbox injection
*/
export interface ToolBinding {
/**
* Unique tool identifier
*/
name: string

/**
* Human-readable description for the LLM
*/
description: string

/**
* JSON Schema for tool input parameters
*/
inputSchema: Record<string, unknown>

/**
* JSON Schema for tool output (optional)
*/
outputSchema?: Record<string, unknown> | undefined

/**
* The execute function that will be injected into the sandbox.
* Accepts optional context for emitting custom events.
*/
execute: (args: unknown, context?: ToolExecutionContext) => Promise<unknown>
// The engine-agnostic isolate contract now lives in @tanstack/ai-isolate so
// driver packages can depend on it without pulling in Code Mode. Imported for
// local use below and re-exported to keep the @tanstack/ai-code-mode public
// API stable.
import type {
ExecutionResult,
IsolateConfig,
IsolateContext,
IsolateDriver,
NormalizedError,
ToolBinding,
} from '@tanstack/ai-isolate'

export type {
ExecutionResult,
IsolateConfig,
IsolateContext,
IsolateDriver,
NormalizedError,
ToolBinding,
ToolExecutionContext,
}

// Re-export for convenience
export type { ToolExecutionContext }

/**
* Runs around each external call of a program. `run` runs the tool.
* `call.name` is the tool name as code mode knows it, without the
Expand Down
2 changes: 1 addition & 1 deletion packages/ai-isolate-cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"code-execution"
],
"dependencies": {
"@tanstack/ai-code-mode": "workspace:^"
"@tanstack/ai-isolate": "workspace:^"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20260317.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/ai-isolate-cloudflare/src/isolate-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {
IsolateContext,
IsolateDriver,
ToolBinding,
} from '@tanstack/ai-code-mode'
} from '@tanstack/ai-isolate'
import type {
ExecuteRequest,
ExecuteResponse,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
createCloudflareIsolateDriver,
type CloudflareIsolateDriverConfig,
} from '../src/isolate-driver'
import type { ToolBinding } from '@tanstack/ai-code-mode'
import type { ToolBinding } from '@tanstack/ai-isolate'
import type { ExecuteRequest, ExecuteResponse } from '../src/types'

const WORKER_URL = 'https://code-mode.example.com'
Expand Down
5 changes: 2 additions & 3 deletions packages/ai-isolate-daytona/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@
"isolate",
"code-execution"
],
"peerDependencies": {
"@tanstack/ai-code-mode": "workspace:^"
"dependencies": {
"@tanstack/ai-isolate": "workspace:^"
},
"devDependencies": {
"@daytona/sdk": "^0.192.0",
"@tanstack/ai-code-mode": "workspace:*",
"@vitest/coverage-v8": "4.1.12"
}
}
2 changes: 1 addition & 1 deletion packages/ai-isolate-daytona/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ export type {
IsolateContext,
IsolateDriver,
NormalizedError,
} from '@tanstack/ai-code-mode'
} from '@tanstack/ai-isolate'
2 changes: 1 addition & 1 deletion packages/ai-isolate-daytona/src/isolate-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
IsolateDriver,
NormalizedError,
ToolBinding,
} from '@tanstack/ai-code-mode'
} from '@tanstack/ai-isolate'
import type {
DaytonaCodeRunResponse,
DaytonaExecutionEnvelope,
Expand Down
2 changes: 1 addition & 1 deletion packages/ai-isolate-daytona/tests/isolate-driver.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it, vi } from 'vitest'
import { createDaytonaIsolateDriver } from '../src/isolate-driver'
import { DAYTONA_RESULT_MARKER } from '../src/wrap-code'
import type { ToolBinding } from '@tanstack/ai-code-mode'
import type { ToolBinding } from '@tanstack/ai-isolate'
import type {
DaytonaCodeRunParams,
DaytonaCodeRunResponse,
Expand Down
2 changes: 1 addition & 1 deletion packages/ai-isolate-daytona/tests/live.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CodeLanguage, Daytona } from '@daytona/sdk'
import { describe, expect, it } from 'vitest'
import { createDaytonaIsolateDriver } from '../src/isolate-driver'
import { DAYTONA_RESULT_MARKER } from '../src/wrap-code'
import type { ToolBinding } from '@tanstack/ai-code-mode'
import type { ToolBinding } from '@tanstack/ai-isolate'
import type {
DaytonaCodeRunParams,
DaytonaCodeRunResponse,
Expand Down
4 changes: 2 additions & 2 deletions packages/ai-isolate-e2b/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
"isolate",
"code-execution"
],
"peerDependencies": {
"@tanstack/ai-code-mode": "workspace:^"
"dependencies": {
"@tanstack/ai-isolate": "workspace:^"
},
"devDependencies": {
"@tanstack/ai": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/ai-isolate-e2b/src/isolate-driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
IsolateContext,
IsolateDriver,
ToolBinding,
} from '@tanstack/ai-code-mode'
} from '@tanstack/ai-isolate'
import type { RunnerMessage, ToolReply } from './runner'

/** The slice of an E2B `CommandHandle` the driver uses. */
Expand Down
2 changes: 1 addition & 1 deletion packages/ai-isolate-e2b/tests/isolate-driver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { basename, join } from 'node:path'
import { describe, expect, it } from 'vitest'
import { createE2BIsolateDriver } from '../src/index'
import type { ChildProcess } from 'node:child_process'
import type { ToolBinding } from '@tanstack/ai-code-mode'
import type { ToolBinding } from '@tanstack/ai-isolate'
import type { E2BCommandHandleLike, E2BSandboxLike } from '../src/index'

interface FakeOptions {
Expand Down
4 changes: 1 addition & 3 deletions packages/ai-isolate-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,9 @@
"code-execution"
],
"dependencies": {
"@tanstack/ai-isolate": "workspace:^",
"isolated-vm": "^7.0.2"
},
"peerDependencies": {
"@tanstack/ai-code-mode": "workspace:^"
},
"devDependencies": {
"@vitest/coverage-v8": "4.1.12"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ai-isolate-node/src/error-normalizer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { NormalizedError } from '@tanstack/ai-code-mode'
import type { NormalizedError } from '@tanstack/ai-isolate'

/**
* Normalize various error types into a consistent format
Expand Down
4 changes: 2 additions & 2 deletions packages/ai-isolate-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ export {
type NodeIsolateDriverConfig,
} from './isolate-driver'

// Re-export types from ai-code-mode for convenience
// Re-export types from ai-isolate for convenience
export type {
IsolateDriver,
IsolateConfig,
IsolateContext,
ExecutionResult,
NormalizedError,
} from '@tanstack/ai-code-mode'
} from '@tanstack/ai-isolate'
4 changes: 2 additions & 2 deletions packages/ai-isolate-node/src/isolate-context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { wrapCode } from '@tanstack/ai-code-mode'
import { wrapCode } from '@tanstack/ai-isolate'
import { normalizeError } from './error-normalizer'
import type ivm from 'isolated-vm'
import type { ExecutionResult, IsolateContext } from '@tanstack/ai-code-mode'
import type { ExecutionResult, IsolateContext } from '@tanstack/ai-isolate'

/**
* IsolateContext implementation using isolated-vm
Expand Down
Loading
Loading