MCPcopy
hub / github.com/TanStack/ai

github.com/TanStack/ai @main sqlite

repository ↗ · DeepWiki ↗
7,507 symbols 21,728 edges 1,767 files 989 documented · 13%
README

TanStack AI

NPM downloads GitHub stars Release Bundle size Follow @TanStack

TanStack AI

Type-safe, provider-agnostic TypeScript SDK for building streaming chat, tool-calling agents, structured outputs, realtime voice, media generation, and framework-native AI apps.

TanStack AI is built from composable activities and provider adapters. Use one provider or switch between many. Import only chat, or add image, audio, video, speech, transcription, summarization, realtime, Code Mode, devtools, and framework bindings as your app needs them.

Read the docs ->

Start Here

What You Can Build

  • Streaming chat experiences with typed messages, tool calls, reasoning parts, and configurable connection adapters.
  • Type-safe tools that can run on the server or client from one shared toolDefinition() contract.
  • Structured output flows backed by JSON Schema, Zod, ArkType, Valibot, or plain JSON Schema.
  • Multimodal prompts and responses that include text, images, audio, video, and documents.
  • Image, audio, video, speech, transcription, and summarization workflows using a shared generation client pattern.
  • Realtime voice chat with provider adapters for realtime sessions and token minting.
  • Code Mode agents that let an LLM write and execute TypeScript in an isolated sandbox to orchestrate tools with loops, branches, and parallel calls.
  • Devtools and observability pipelines for inspecting messages, tool calls, stream chunks, errors, usage, and OpenTelemetry traces.
  • Framework-native clients for React, Solid, Vue, Svelte, and Preact, plus a headless client for custom runtimes.

Install

Install the core package and the provider/framework packages your app uses:

pnpm add @tanstack/ai @tanstack/ai-openai

For a React chat UI:

pnpm add @tanstack/ai @tanstack/ai-client @tanstack/ai-react @tanstack/ai-openai

OpenRouter is also a good starting point if you want access to many providers through one API key:

pnpm add @tanstack/ai @tanstack/ai-openrouter

Streaming Chat

import { chat, toServerSentEventsResponse } from '@tanstack/ai'
import { openaiText } from '@tanstack/ai-openai'

export async function POST(request: Request) {
  const body = await request.json()

  const stream = chat({
    adapter: openaiText('gpt-5.2'),
    messages: body.messages,
  })

  return toServerSentEventsResponse(stream)
}

Learn more in the Chat & Streaming docs and Connection Adapters docs.

Type-Safe Tools

Define a tool once, then attach a server or client implementation with the same input and output types:

import { toolDefinition } from '@tanstack/ai'
import { z } from 'zod'

const getProducts = toolDefinition({
  name: 'getProducts',
  description: 'Search the product catalog',
  inputSchema: z.object({ query: z.string() }),
  outputSchema: z.array(
    z.object({
      id: z.string(),
      name: z.string(),
    }),
  ),
}).server(async ({ query }) => {
  return db.products.search(query)
})

Learn more in the Tools docs, Tool Approval Flow docs, and Lazy Tool Discovery docs.

Structured Outputs

Use outputSchema when you need typed objects instead of freeform text:

import { chat } from '@tanstack/ai'
import { openaiText } from '@tanstack/ai-openai'
import { z } from 'zod'

const Person = z.object({
  name: z.string(),
  age: z.number(),
})

const person = await chat({
  adapter: openaiText('gpt-5.2'),
  messages: [{ role: 'user', content: 'Ada Lovelace, 36' }],
  outputSchema: Person,
})

Learn more in the Structured Outputs docs.

Media, Realtime, and Code Mode

  • Generations - one pattern for image generation, text-to-speech, transcription, summarization, audio generation, and video generation.
  • Realtime Voice Chat - build low-latency realtime voice experiences.
  • Code Mode - let models write and execute TypeScript inside a secure isolate.
  • Code Mode with Skills - give Code Mode reusable runtime capabilities.

Providers

Official adapters include:

Package Use it for
@tanstack/ai-openrouter 300+ models through one OpenRouter API, with per-request cost tracking
@tanstack/ai-openai OpenAI chat, image, video, speech, transcription, realtime, and provider tools
@tanstack/ai-anthropic Anthropic Claude chat, thinking, tools, and structured outputs
@tanstack/ai-gemini Google Gemini chat, image, speech, and audio generation
@tanstack/ai-ollama Local Ollama models
@tanstack/ai-grok xAI Grok chat, images, and realtime
@tanstack/ai-groq Groq low-latency inference
@tanstack/ai-elevenlabs ElevenLabs realtime voice, speech, transcription, music, and sound effects
@tanstack/ai-fal fal.ai image, video, audio, speech, and transcription models

The adapter system is tree-shakeable by activity. Import openaiText for chat, openaiImage for images, falVideo for video, geminiSpeech for TTS, and so on.

Framework Packages

Package What it provides
@tanstack/ai-client Headless chat, realtime, and generation clients
@tanstack/ai-react React hooks including useChat, useRealtimeChat, and generation hooks
@tanstack/ai-solid Solid hooks for chat and generations
@tanstack/ai-vue Vue composables for chat and generations
@tanstack/ai-svelte Svelte 5 factories for chat and generations
@tanstack/ai-preact Preact hooks for chat
@tanstack/ai-react-ui, @tanstack/ai-solid-ui, @tanstack/ai-vue-ui Headless UI components for chat interfaces

Advanced Docs

  • Middleware - hook into chat configuration, chunks, tool calls, usage, errors, and structured outputs.
  • OpenTelemetry - emit vendor-neutral GenAI traces and metrics.
  • Observability - subscribe to typed TanStack AI events.
  • Per-Model Type Safety - narrow model options and content modalities to the selected model.
  • Runtime Adapter Switching - switch providers at runtime.
  • Tree-Shaking - ship only the activities and adapters you use.
  • Agent Skills - install TanStack AI skills into Claude Code, Cursor, GitHub Copilot, Codex, and other coding agents with TanStack Intent.

Get Involved

Partners

CodeRabbit Cloudflare

AI and you?

We're looking for TanStack AI partners to join our mission. Partner with us
to push the boundaries of TanStack AI and build amazing things together.

LET'S CHAT

Explore the TanStack Ecosystem

Extension points exported contracts — how you extend this code

SendEmail (Interface)
* A binding that allows a Worker to send email messages. [5 implementers]
examples/sandbox-cloudflare/worker-configuration.d.ts
SandboxHandle (Interface)
(no doc) [6 implementers]
packages/ai-sandbox/src/contracts.ts
ChunkStrategy (Interface)
(no doc) [5 implementers]
packages/ai/src/activities/chat/stream/types.ts
IsolateContext (Interface)
(no doc) [3 implementers]
packages/ai-code-mode/src/types.ts
WorkerLoaderEntrypoint (Interface)
* Worker Loader binding type. * * Provides dynamic-code execution by loading a module into a fresh V8 * isolate. Conf
packages/ai-isolate-cloudflare/src/worker/index.ts
ModelMeta (Interface)
* Internal metadata structure describing a Mistral model's capabilities * and approximate pricing (USD per million toke
packages/ai-mistral/src/model-meta.ts
ImageValidationOptions (Interface)
* Internal options interface for validation
packages/ai-grok/src/image/image-provider-options.ts
OpencodeMcpServer (Interface)
An OpenCode `remote`-type MCP server entry (mirrors OPENCODE_CONFIG_CONTENT shape).
packages/ai-opencode/src/adapters/projection.ts

Core symbols most depended-on inside this repo

now
called by 1141
packages/ai-sandbox/src/run-log.ts
push
called by 867
packages/ai-acp/src/stream/queue.ts
chat
called by 503
packages/ai/src/activities/chat/index.ts
processChunk
called by 498
packages/ai/src/activities/chat/stream/processor.ts
get
called by 482
examples/sandbox-cloudflare/worker-configuration.d.ts
resolve
called by 472
packages/ai-sandbox-local-process/src/handle.ts
createMockConnectionAdapter
called by 442
packages/ai-client/tests/test-utils.ts
log
called by 437
examples/sandbox-cloudflare/worker-configuration.d.ts

Shape

Function 3,884
Interface 1,845
Method 1,312
Class 465
Enum 1

Languages

TypeScript100%

Modules by API surface

examples/sandbox-cloudflare/worker-configuration.d.ts782 symbols
packages/ai-client/src/devtools.ts140 symbols
packages/ai-event-client/src/index.ts88 symbols
packages/ai/src/types.ts71 symbols
packages/ai/src/activities/chat/index.ts66 symbols
packages/ai/src/activities/chat/stream/processor.ts63 symbols
packages/ai-client/src/devtools-noop.ts57 symbols
packages/ai-devtools/src/store/ai-context.tsx55 symbols
packages/ai-client/src/chat-client.ts53 symbols
packages/ai-devtools/src/components/conversation/IterationCard.tsx52 symbols
packages/ai-devtools/src/components/hooks/HookDetails.tsx51 symbols
packages/ai-sandbox-sprites/src/client.ts44 symbols

Dependencies from manifests, versioned

@ag-ui/core0.0.52 · 1×
@agentclientprotocol/sdk0.25.0 · 1×
@analogjs/vite-plugin-angular1.10.0 · 1×
@angular/build21.2.0 · 1×
@angular/common21.2.0 · 1×
@angular/compiler21.2.0 · 1×
@angular/core21.2.0 · 1×
@angular/forms21.2.0 · 1×
@angular/platform-browser21.2.0 · 1×
@angular/platform-browser-dynamic21.2.0 · 1×
@anthropic-ai/sdk0.97.1 · 1×
@aws-crypto/sha256-js5.2.0 · 1×

For agents

$ claude mcp add ai \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact