- MCP Server ToolsUpdated
- Managed MCP with chat()Updated
- Manual MCPUpdated
- MCP Type GenerationUpdated
- MCP Client InputUpdated
- MCP Server Tools: AdvancedNew
- MCP EventsNew
- MCP AppsUpdated
- Serve Tools over HTTPUpdated
- Serve Resources and PromptsUpdated
- Ask for InputUpdated
- Draft Text from a ToolUpdated
- MCP Server TasksUpdated
- MCP Server AuthUpdated
- MCP Server SessionsUpdated
- MCP Server on stdioUpdated
Persistence (Advanced)
- GenerationsUpdated
- Realtime Voice ChatUpdated
- Text-to-SpeechUpdated
- TranscriptionUpdated
- Transcription: AdvancedNew
- Voice CreationUpdated
- Audio RecordingUpdated
- Audio GenerationUpdated
- Image GenerationUpdated
- Image Generation: AdvancedNew
- Video GenerationUpdated
- Video Generation: AdvancedNew
- Live GenerationUpdated
- World GenerationUpdated
- Generation HooksUpdated
- Build your first harnessUpdated
- Host optionsNew
- Send and show mediaUpdated
- Connect clientsUpdated
- Write a client in any languageNew
- Access and tenantsNew
- Run in the terminalUpdated
- Deploy a harnessUpdated
- Send inputs safelyUpdated
- Run a tool without a model
- Durable sessionsUpdated
- Run background work on workersNew
- List, rename, and fork sessionsUpdated
- Lifecycle and retentionNew
- Run side effects onceUpdated
- Wait for a personNew
- Build on the session logUpdated
- Control how a turn endsUpdated
- Store settings per threadUpdated
- Compact a long sessionUpdated
- Fork and reset a threadUpdated
- One log for a session treeUpdated
- Write a pluginUpdated
- Track usage and costUpdated
- Ask before risky tool callsUpdated
- Give your agent coding toolsUpdated
- Sandbox and extend the coding toolsUpdated
- Run the coding tools for many threadsNew
- Undo a turnUpdated
- Build a coding agentUpdated
- Delegate to coding agentsUpdated
- Switch between agent profilesUpdated
- Auth and connectorsUpdated
- Share a thread between peopleUpdated
- Connect model providersUpdated
- Build your own UIUpdated
- Code mode in a harnessUpdated
- Work until a goal is metUpdated
- Run agents from a harnessUpdated
- Hand off a conversationNew
- Add skillsUpdated
- Use MCP serversUpdated
- Give your agent a computerNew
- Use from any MCP clientUpdated
- Use a harness from an editor (ACP)Updated
- OverviewUpdated
- Quick StartUpdated
- ProvidersUpdated
- HarnessesUpdated
- Harness AuthUpdated
- WorkspaceUpdated
- ToolsUpdated
- PolicyUpdated
- Lifecycle & SnapshotsUpdated
- Portable SnapshotsUpdated
- Keep Files After ReloadUpdated
- Save a Named VersionUpdated
- Branch From a VersionUpdated
- Send a Frozen FileUpdated
- Let the Agent Save and ForkUpdated
- Pick Which Files to KeepUpdated
- What a Snapshot StoresUpdated
- Instance DurabilityUpdated
- Durable RunsUpdated
- EventsUpdated
Sandboxes (Advanced)
- Runtime ContextUpdated
- Bring Your Own KeyUpdated
- Keep Keys on the ServerNew
- Debug LoggingUpdated
- Multimodal ContentUpdated
- Files APIUpdated
- Per-Model Type SafetyUpdated
- Runtime Adapter SwitchingUpdated
- Tree-ShakingUpdated
- Extend AdapterUpdated
- Extend Adapter: AdvancedNew
- Typed Pre-Configured OptionsUpdated
- Test with a Fake ModelUpdated
- Prompt CachingUpdated
- Mid-Conversation ChangesUpdated
- Approval Flow ProcessingUpdated
Harness Auth
Pass credentials into localProcessSandbox() yourself. The sandbox type does not know whether the machine has grok login or only an API key.
You tell it with authMode on the harness adapter:
- 'api-key' (default): inject the key and use it. Use this in Docker, in a cloud sandbox, on a CI runner, or on any machine with no CLI login.
- 'host': use the CLI login on the machine. Do not inject an API key into that process.
You can set authMode in two places:
- On the adapter factory: grokBuildText('composer-2.5', { authMode: 'host' }).
- For one call: chat({ modelOptions: { authMode: 'host' } }).
Use an API key#
This is the default, so you can omit authMode. Pass the key as a workspace secret:
import { chat } from '@tanstack/ai'
import { GROK_CLI_INSTALL_COMMAND, grokBuildText } from '@tanstack/ai-grok-build'
import {
createSecrets,
defineSandbox,
defineWorkspace,
githubRepo,
withSandbox,
} from '@tanstack/ai-sandbox'
import { dockerSandbox } from '@tanstack/ai-sandbox-docker'
const sandbox = defineSandbox({
id: 'repo-agent',
provider: dockerSandbox({ image: 'node:22' }),
workspace: defineWorkspace({
source: githubRepo({ repo: 'owner/app' }),
setup: [GROK_CLI_INSTALL_COMMAND],
secrets: createSecrets({
XAI_API_KEY: process.env.XAI_API_KEY ?? '',
}),
}),
})
const stream = chat({
threadId: 'thread-1',
adapter: grokBuildText('composer-2.5'),
messages: [{ role: 'user', content: 'Summarize this repo.' }],
middleware: [withSandbox(sandbox)],
})Use the host login#
If the machine already ran grok login, claude login, or codex login, use this setup:
import { chat } from '@tanstack/ai'
import { grokBuildText } from '@tanstack/ai-grok-build'
import { defineSandbox, defineWorkspace, withSandbox } from '@tanstack/ai-sandbox'
import { localProcessSandbox } from '@tanstack/ai-sandbox-local-process'
const sandbox = defineSandbox({
id: 'repo-agent',
provider: localProcessSandbox({
scrubEnv: ['XAI_API_KEY', 'GROK_API_KEY'],
}),
workspace: defineWorkspace({
source: { type: 'local', path: '/abs/path/to/repo' },
}),
})
const stream = chat({
threadId: 'thread-1',
adapter: grokBuildText('composer-2.5', { authMode: 'host' }),
messages: [{ role: 'user', content: 'Summarize this repo.' }],
middleware: [withSandbox(sandbox)],
})scrubEnv removes keys that the host process inherited. If the CLI sees XAI_API_KEY, it can use that key and not your login.
What each harness reads#
| Adapter | authMode: 'api-key' (default) | authMode: 'host' |
|---|---|---|
| Grok Build | XAI_API_KEY | grok login |
| Claude Code | ANTHROPIC_API_KEY | claude login |
| Codex | CODEX_API_KEY | codex login |
| ACP-Compatible | Set authMethodId (for Grok, xai.api_key). | Skips ACP authenticate. |
OpenCode reads OPENAI_API_KEY from the process env. It has no authMode flag.
Let the user pick (client and server)#
The React chat example has this choice on /repo-report. The client sends authMode. The server builds the adapter with that value.
Client:
import { fetchServerSentEvents, useChat } from '@tanstack/ai-react'
function Report() {
const { sendMessage } = useChat({
connection: fetchServerSentEvents('/api/sandbox-repo-report'),
threadId: 'support-chat',
forwardedProps: { authMode: 'api-key' },
})
return (
<button type="button" onClick={() => sendMessage('Report on this repo')}>
Run
</button>
)
}Server:
import { chat, toServerSentEventsResponse } from '@tanstack/ai'
import { grokBuildText } from '@tanstack/ai-grok-build'
import { withSandbox } from '@tanstack/ai-sandbox'
import { sandbox } from './sandbox'
export async function POST(request: Request) {
const body: unknown = await request.json()
const forwarded =
typeof body === 'object' &&
body !== null &&
'forwardedProps' in body &&
typeof body.forwardedProps === 'object' &&
body.forwardedProps !== null
? body.forwardedProps
: {}
const authMode =
'authMode' in forwarded && forwarded.authMode === 'host'
? 'host'
: 'api-key'
const threadId =
typeof body === 'object' &&
body !== null &&
'threadId' in body &&
typeof body.threadId === 'string'
? body.threadId
: undefined
if (!threadId) {
return new Response('Send a threadId.', { status: 400 })
}
const stream = chat({
adapter: grokBuildText('composer-2.5', { authMode }),
messages: [{ role: 'user', content: 'Report on this repo' }],
threadId,
stream: true,
middleware: [withSandbox(sandbox)],
})
return toServerSentEventsResponse(stream)
}The server accepts only 'host' or 'api-key'. Any other value falls back to 'api-key'.
To pick the agent, see Harnesses. For scrubEnv and other local-process options, see Providers (Advanced).