Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

provider-tools.md

178 lines7.6 KB

1---
2title: Provider Tools
3id: provider-tools
4order: 2
5---
6​
7Most providers expose native tools beyond user-defined function calls: web
8search, code execution, computer use, hosted retrieval, and more. TanStack AI
9exports each provider's native tools from a dedicated `/tools` subpath per
10adapter package.
11​
12You have an adapter already wired up. You want to give the model access to a
13provider-native capability (e.g. Anthropic web search) and be sure you never
14pair a tool with a model that doesn't support it. By the end of this page,
15you'll have imported the factory, added it to `chat({ tools: [...] })`, and
16understood the compile-time guard that will catch unsupported combinations.
17​
18## Import
19​
20Every adapter ships provider tools on a `/tools` subpath:
21​
22```typescript
23import { webSearchTool } from '@tanstack/ai-anthropic/tools'
24import { codeInterpreterTool } from '@tanstack/ai-openai/tools'
25import { googleSearchTool } from '@tanstack/ai-gemini/tools'
26```
27​
28## Use in `chat({ tools })`
29​
30```typescript
31import { chat } from '@tanstack/ai'
32import { anthropicText } from '@tanstack/ai-anthropic'
33import { webSearchTool } from '@tanstack/ai-anthropic/tools'
34​
35const stream = chat({
36 adapter: anthropicText('claude-opus-4-6'),
37 messages: [{ role: 'user', content: "Summarize today's AI news." }],
38 tools: [
39 webSearchTool({
40 name: 'web_search',
41 type: 'web_search_20250305',
42 max_uses: 3,
43 }),
44 ],
45})
46```
47​
48The factory is what selects native provider behavior, not the public name. A plain function named `web_search`, `google_search`, or `code_execution` stays a custom function. Do not hand-build `{ name: 'web_search', metadata: ... }` and expect a native payload.
49​
50You can put a factory tool and your own function in the same `chat({ tools })` call if the names differ. Tool names in one `tools` array must be unique. If you pass both `webSearchTool()` and your own function named `web_search`, `chat()` throws `DuplicateToolNameError` before it talks to the provider.
51​
52## Multi-turn persistence
53​
54Provider tools run on the provider's own infrastructure, so their results
55(e.g. Anthropic `web_search` sources, `web_fetch` page contents) come back
56embedded in the assistant turn rather than as a separate tool message. TanStack
57AI preserves those results on the assistant message, so when you feed the prior
58conversation back into the next `chat()` call the model still sees the earlier
59evidence — no special handling required:
60​
61```typescript
62import { chat, StreamProcessor } from '@tanstack/ai'
63import { anthropicText } from '@tanstack/ai-anthropic'
64import { webSearchTool } from '@tanstack/ai-anthropic/tools'
65​
66const adapter = anthropicText('claude-opus-4-6')
67const tools = [webSearchTool({ name: 'web_search', type: 'web_search_20250305' })]
68​
69const processor = new StreamProcessor()
70for await (const chunk of chat({
71 adapter,
72 tools,
73 messages: [{ role: 'user', content: 'Find two sources on the drone market.' }],
74})) {
75 processor.processChunk(chunk)
77processor.finalizeStream()
78​
79// The follow-up turn can still cite the previous search results.
80const followUp = chat({
81 adapter,
82 tools,
83 messages: [
84 ...processor.getMessages(),
85 { role: 'user', content: 'List the exact sources you used.' },
86 ],
87})
88```
89​
90The search/fetch call surfaces as a provider-executed `tool-call` part on the
91assistant message; the agent loop never tries to run it client-side.
92​
93OpenAI `applyPatchTool`, `localShellTool`, and `shellTool({ environment: { type: "local" } })` are different. The model returns the call, and your app runs it. See [OpenAI adapter](../adapters/openai.md#applypatchtool).
94​
95### Read web search sources
96​
97OpenAI and Gemini web search calls expose a common `metadata.sources` array on
98the assistant tool-call part. Each source has a `url` and can also include a
99`title` or `pageAge`. Use this list to render source links in your UI. The
100provider still owns search execution, so do not execute the tool call in your
101application.
102​
103```typescript
104import { getProviderExecutedMetadata } from '@tanstack/ai'
105import type { UIMessage } from '@tanstack/ai'
106​
107function logSources(message: UIMessage) {
108 for (const part of message.parts) {
109 if (part.type !== 'tool-call') continue
110 const sources = getProviderExecutedMetadata(part)?.sources ?? []
111 for (const source of sources) {
112 console.log(source.title ?? source.url, source.url)
113 }
114 }
116```
117​
118The raw provider response stays available for provider-specific handling under
119`metadata.openai` or `metadata.gemini`.
120​
121## Type-level guard
122​
123Every provider-specific tool factory (e.g. `webSearchTool`, `computerUseTool`)
124returns a `ProviderTool<TProvider, TKind>` brand. The adapter's
125`toolCapabilities` (derived from each model's `supports.tools` list) gates
126which brands are assignable to `tools`.
127​
128Paste a `computerUseTool(...)` into a model that doesn't expose it, and
129TypeScript reports an error on that array element — not on the factory call,
130not at runtime. User-defined `toolDefinition()` tools stay unbranded and
131always assignable. The `customTool` factories exported from `ai-anthropic` and
132`ai-openai` also return a plain `Tool` (not a `ProviderTool` brand) and are
133therefore universally accepted by any chat model, just like `toolDefinition()`.
134​
135## Available tools
136​
137| Provider | Tools |
138|---|---|
139| Anthropic | `webSearchTool`, `webFetchTool`, `codeExecutionTool`, `computerUseTool`, `bashTool`, `textEditorTool`, `memoryTool` — see [Anthropic adapter](../adapters/anthropic.md#provider-tools). |
140| OpenAI | `webSearchTool`, `webSearchPreviewTool`, `fileSearchTool`, `imageGenerationTool`, `codeInterpreterTool`, `mcpTool`, `computerUseTool`, `localShellTool`, `shellTool`, `applyPatchTool` — see [OpenAI adapter](../adapters/openai.md#provider-tools). |
141| Gemini | `codeExecutionTool`, `fileSearchTool`, `googleSearchTool`, `googleSearchRetrievalTool`, `googleMapsTool`, `urlContextTool`, `computerUseTool` — see [Gemini adapter](../adapters/gemini.md#provider-tools). |
142| OpenRouter | `webSearchTool`, `webFetchTool` — see [OpenRouter adapter](../adapters/openrouter.md#provider-tools). |
143| Grok | function tools only (no provider-specific tools). |
144| Groq | function tools only (no provider-specific tools). |
145​
146## Which models support which tools?
147​
148Each adapter's `supports.tools` array is the source of truth. The comparison
149matrix is maintained alongside `model-meta.ts` and reflected here:
150​
151- **Anthropic**: every registered model supports the full tool superset
152 (the retired Claude 3.x models with narrower support were removed).
153- **OpenAI**: GPT-5 family and reasoning models (O-series) support the full
154 superset. GPT-6 Astra, Sol, and Luna support every tool except
155 `webSearchPreviewTool` and `localShellTool`. GPT-4-series supports
156 web/file/image/code/mcp but not preview/shell variants. GPT-3.5 and
157 audio-focused models: none.
158- **Gemini**: 3.x Pro/Flash models support the full tool set. Lite and
159 image/video variants have narrower support.
160- **OpenRouter**: every chat model supports `webSearchTool` and
161 `webFetchTool` via the gateway.
162​
163For the exact per-model list, open the adapter page or read the model's
164`supports.tools` array directly from `model-meta.ts`.
165​
166## Provider Skills
167​
168Anthropic and OpenAI support hosted, provider-managed skill bundles that run
169inside the provider's server-side sandbox. Skills attach to an execution tool
170(`codeExecutionTool` for Anthropic, `shellTool` for OpenAI) and are referenced
171by ID — the provider handles installation and execution.
172​
173See [Provider Skills](./provider-skills.md) for full setup steps and examples.
174​
175## Migrating from earlier versions
176​
177If you were using `createWebSearchTool` from `@tanstack/ai-openrouter`, see
178[Migration Guide §6](../migration/migration.md#6-provider-tools-moved-to-tools-subpath).