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

provider-tools.md

205 lines8.8 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`, `computerUseTool`, and `shellTool({ environment: { type: "local" } })` are different. The model returns the call, and your app runs it. See [OpenAI adapter](../adapters/openai.md#applypatchtool) and [`computerUseTool`](../adapters/openai.md#computerusetool).
94​
95To run computer use actions on a browser or a desktop without your own action code, use `createComputerUse` from `@tanstack/ai-computer-use`. The same `tools` work with OpenAI and Anthropic, and each thread gets its own machine. See [Computer use](../computer-use/computer-use.md).
96​
97```typescript
98import { chat } from '@tanstack/ai'
99import { openaiText } from '@tanstack/ai-openai'
100import { createComputerUse } from '@tanstack/ai-computer-use'
101import { playwrightComputer } from '@tanstack/ai-computer-playwright'
102​
103declare const threadId: string
104​
105const computerUse = createComputerUse({
106 driver: playwrightComputer({ startUrl: 'https://tanstack.com' }),
107})
108​
109const stream = chat({
110 adapter: openaiText('gpt-6.1-sol'),
111 messages: [{ role: 'user', content: 'Find the TanStack AI docs' }],
112 threadId,
113 tools: computerUse.tools,
114 middleware: [computerUse.middleware],
115 interrupts: [computerUse.interrupt],
116})
117```
118​
119### Read web search sources
120​
121OpenAI and Gemini web search calls expose a common `metadata.sources` array on
122the assistant tool-call part. Each source has a `url` and can also include a
123`title` or `pageAge`. Use this list to render source links in your UI. The
124provider still owns search execution, so do not execute the tool call in your
125application.
126​
127```typescript
128import { getProviderExecutedMetadata } from '@tanstack/ai'
129import type { UIMessage } from '@tanstack/ai'
130​
131function logSources(message: UIMessage) {
132 for (const part of message.parts) {
133 if (part.type !== 'tool-call') continue
134 const sources = getProviderExecutedMetadata(part)?.sources ?? []
135 for (const source of sources) {
136 console.log(source.title ?? source.url, source.url)
137 }
138 }
140```
141​
142The raw provider response stays available for provider-specific handling under
143`metadata.openai` or `metadata.gemini`.
144​
145## Type-level guard
146​
147Every provider-specific tool factory (e.g. `webSearchTool`, `computerUseTool`)
148returns a `ProviderTool<TProvider, TKind>` brand. The adapter's
149`toolCapabilities` (derived from each model's `supports.tools` list) gates
150which brands are assignable to `tools`.
151​
152Paste a `computerUseTool(...)` into a model that doesn't expose it, and
153TypeScript reports an error on that array element — not on the factory call,
154not at runtime. User-defined `toolDefinition()` tools stay unbranded and
155always assignable. The `customTool` factories exported from `ai-anthropic` and
156`ai-openai` also return a plain `Tool` (not a `ProviderTool` brand) and are
157therefore universally accepted by any chat model, just like `toolDefinition()`.
158​
159## Available tools
160​
161| Provider | Tools |
162|---|---|
163| Anthropic | `webSearchTool`, `webFetchTool`, `codeExecutionTool`, `computerUseTool`, `bashTool`, `textEditorTool`, `memoryTool` — see [Anthropic adapter](../adapters/anthropic.md#provider-tools). |
164| OpenAI | `webSearchTool`, `webSearchPreviewTool`, `fileSearchTool`, `imageGenerationTool`, `codeInterpreterTool`, `mcpTool`, `computerUseTool`, `localShellTool`, `shellTool`, `applyPatchTool` — see [OpenAI adapter](../adapters/openai.md#provider-tools). |
165| Gemini | `codeExecutionTool`, `fileSearchTool`, `googleSearchTool`, `googleSearchRetrievalTool`, `googleMapsTool`, `urlContextTool`, `computerUseTool` — see [Gemini adapter](../adapters/gemini.md#provider-tools). |
166| OpenRouter | `webSearchTool`, `webFetchTool` — see [OpenRouter adapter](../adapters/openrouter.md#provider-tools). |
167| Grok | function tools only (no provider-specific tools). |
168| Groq | function tools only (no provider-specific tools). |
169​
170## Which models support which tools?
171​
172Each adapter's `supports.tools` array is the source of truth. The comparison
173matrix is maintained alongside `model-meta.ts` and reflected here:
174​
175- **Anthropic**: every registered model supports the full tool superset,
176 except `claude-opus-5-fast`, which supports none. For `computerUseTool`,
177 each model takes only some versions. The Claude 5.5 models take only
178 `computer_toolset_20260801`. See the version table in
179 [`computerUseTool`](../adapters/anthropic.md#computerusetool).
180- **OpenAI**: GPT-5 family and reasoning models (O-series) support the full
181 superset. GPT-6 Astra, Sol, and Luna support every tool except
182 `webSearchPreviewTool` and `localShellTool`. GPT-4-series supports
183 web/file/image/code/mcp but not preview/shell variants. GPT-3.5 and
184 audio-focused models: none.
185- **Gemini**: 3.x Pro/Flash models support the full tool set. Lite and
186 image/video variants have narrower support.
187- **OpenRouter**: every chat model supports `webSearchTool` and
188 `webFetchTool` via the gateway.
189​
190For the exact per-model list, open the adapter page or read the model's
191`supports.tools` array directly from `model-meta.ts`.
192​
193## Provider Skills
194​
195Anthropic and OpenAI support hosted, provider-managed skill bundles that run
196inside the provider's server-side sandbox. Skills attach to an execution tool
197(`codeExecutionTool` for Anthropic, `shellTool` for OpenAI) and are referenced
198by ID — the provider handles installation and execution.
199​
200See [Provider Skills](./provider-skills.md) for full setup steps and examples.
201​
202## Migrating from earlier versions
203​
204If you were using `createWebSearchTool` from `@tanstack/ai-openrouter`, see
205[Migration Guide §6](../migration/migration.md#6-provider-tools-moved-to-tools-subpath).