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

provider-tools.md

210 lines9.2 KB

1---
2title: Provider Tools
3id: provider-tools
4order: 2
5description: "Use the built-in tools of a provider, such as web search, code execution, and computer use. Import the factory from the /tools subpath of the adapter and add it to chat()."
6---
7​
8A provider tool runs on the provider's servers. Use it for web search or code execution that you do not host.
9​
10Import the factory of the provider tool from the `/tools` subpath of your adapter, and add it to `chat({ tools })`. The provider runs the tool. TypeScript stops you if the model does not support that tool.
11​
12## Add a provider tool
13​
14### 1. Import the factory
15​
16Each adapter has its provider tools on a `/tools` subpath:
17​
18```typescript
19import { webSearchTool } from '@tanstack/ai-anthropic/tools'
20import { codeInterpreterTool } from '@tanstack/ai-openai/tools'
21import { googleSearchTool } from '@tanstack/ai-gemini/tools'
22```
23​
24### 2. Add it to `chat({ tools })`
25​
26This route gives Claude web search, with at most 3 searches for each request:
27​
28```typescript
29// api/chat.ts
30import {
31 chat,
32 chatParamsFromRequest,
33 toServerSentEventsResponse,
34} from '@tanstack/ai'
35import { anthropicText } from '@tanstack/ai-anthropic'
36import { webSearchTool } from '@tanstack/ai-anthropic/tools'
37​
38export async function POST(request: Request) {
39 const { messages, threadId, runId } = await chatParamsFromRequest(request)
40​
41 const stream = chat({
42 adapter: anthropicText('claude-opus-5-5'),
43 messages,
44 threadId,
45 runId,
46 tools: [
47 webSearchTool({
48 name: 'web_search',
49 type: 'web_search_20250305',
50 max_uses: 3,
51 }),
52 ],
53 })
54​
55 return toServerSentEventsResponse(stream)
57```
58​
59### 3. Try it
60​
61Ask your chat "Summarize today's AI news." The model searches the web on Anthropic's servers and answers with what it found. You wrote no search code.
62​
63## How it works
64​
65- The factory marks the tool as a native tool of the provider. The adapter sends it in the format of the provider.
66- The provider runs the tool on its own servers. The result comes back inside the assistant turn, not as a separate tool message.
67- The call shows up as a provider-executed `tool-call` part. The agent loop never tries to run it in your app.
68- Each factory returns a typed `ProviderTool`. If the model does not support the tool, TypeScript shows an error on that item of the `tools` array.
69​
70## Available tools
71​
72| Provider | Tools |
73|---|---|
74| Anthropic | `webSearchTool`, `webFetchTool`, `codeExecutionTool`, `computerUseTool`, `bashTool`, `textEditorTool`, `memoryTool`. See [Anthropic adapter](../adapters/anthropic.md#provider-tools). |
75| OpenAI | `webSearchTool`, `webSearchPreviewTool`, `fileSearchTool`, `imageGenerationTool`, `codeInterpreterTool`, `mcpTool`, `computerUseTool`, `localShellTool`, `shellTool`, `applyPatchTool`. See [OpenAI adapter](../adapters/openai.md#provider-tools). |
76| Gemini | `codeExecutionTool`, `fileSearchTool`, `googleSearchTool`, `googleSearchRetrievalTool`, `googleMapsTool`, `urlContextTool`, `computerUseTool`. See [Gemini adapter](../adapters/gemini.md#provider-tools). |
77| OpenRouter | `webSearchTool`, `webFetchTool`. See [OpenRouter adapter](../adapters/openrouter.md#provider-tools). |
78| Grok | `grokWebSearchTool`, `grokXSearchTool`, `grokFileSearchTool`, `grokMCPTool`. See [Grok adapter](../adapters/grok.md#provider-tools). |
79| Groq | Function tools only. No provider tools. |
80​
81Anthropic and OpenAI can also load hosted skills into their code sandbox. See [Provider Skills](./provider-skills.md).
82​
83## Which models support which tools?
84​
85The `supports.tools` array of each model in `model-meta.ts` is the source of truth:
86​
87- **Anthropic**: every registered model supports all tools, except `claude-opus-5-fast`, which supports none.
88 - For `computerUseTool`, each model takes only some versions. The Claude 5.5 models take only `computer_toolset_20260801`. See the version table in [`computerUseTool`](../adapters/anthropic-advanced.md#computerusetool).
89- **OpenAI**:
90 - GPT-6.1 Sol, the GPT-5 family, and the reasoning models (O-series) support all tools.
91 - GPT-6 Astra, Sol, and Luna support every tool except `webSearchPreviewTool` and `localShellTool`.
92 - The GPT-4 series supports web, file, image, code, and MCP tools, but not the preview or shell tools.
93 - GPT-3.5 and the audio models support none.
94- **Gemini**: the 3.x Pro and Flash models support all tools. The Lite, image, and video models support fewer.
95- **OpenRouter**: every chat model supports `webSearchTool` and `webFetchTool` through the gateway.
96​
97For the exact list of a model, open the adapter page, or read the `supports.tools` array of the model in `model-meta.ts`.
98​
99## Advanced
100​
101### Provider tools next to your own tools
102​
103If the names are different, you can put a factory tool and your own function in the same `tools` array.
104​
105- The factory selects the native behavior, not the name. A plain function named `web_search`, `google_search`, or `code_execution` stays a custom function.
106- Do not build `{ name: 'web_search', metadata: ... }` by hand. You do not get a native tool that way.
107- Tool names in one `tools` array must be unique. If you pass `webSearchTool()` and your own function named `web_search`, `chat()` throws `DuplicateToolNameError` before it calls the provider.
108​
109### Results stay in the conversation
110​
111Provider tool results, such as Anthropic `web_search` sources and `web_fetch` page contents, stay on the assistant message. When you send the earlier conversation into the next `chat()` call, the model still sees them. You do not need extra code:
112​
113```typescript
114import { chat, StreamProcessor } from '@tanstack/ai'
115import { anthropicText } from '@tanstack/ai-anthropic'
116import { webSearchTool } from '@tanstack/ai-anthropic/tools'
117​
118const adapter = anthropicText('claude-opus-5-5')
119const tools = [webSearchTool({ name: 'web_search', type: 'web_search_20250305' })]
120​
121const processor = new StreamProcessor()
122for await (const chunk of chat({
123 adapter,
124 tools,
125 messages: [{ role: 'user', content: 'Find two sources on the drone market.' }],
126 threadId: 'thread-1',
127})) {
128 processor.processChunk(chunk)
130processor.finalizeStream()
131​
132// The follow-up turn can still cite the previous search results.
133const followUp = chat({
134 adapter,
135 tools,
136 messages: [
137 ...processor.getMessages(),
138 { role: 'user', content: 'List the exact sources you used.' },
139 ],
140 threadId: 'thread-1',
141})
142```
143​
144### Show web search sources
145​
146OpenAI and Gemini web search calls put a `metadata.sources` array on the assistant `tool-call` part. Each source has a `url`, and it can also have a `title` or a `pageAge`. Use the list to show source links:
147​
148```typescript
149import { getProviderExecutedMetadata } from '@tanstack/ai'
150import type { UIMessage } from '@tanstack/ai'
151​
152function logSources(message: UIMessage) {
153 for (const part of message.parts) {
154 if (part.type !== 'tool-call') continue
155 const sources = getProviderExecutedMetadata(part)?.sources ?? []
156 for (const source of sources) {
157 console.log(source.title ?? source.url, source.url)
158 }
159 }
161```
162​
163The provider runs the search, so do not run this tool call in your app. The raw provider response stays in `metadata.openai` or `metadata.gemini`.
164​
165### Tools that your app runs
166​
167These OpenAI tools work the other way: the model returns the call, and your app runs it.
168​
169- `applyPatchTool`
170- `localShellTool`
171- `computerUseTool`
172- `shellTool({ environment: { type: "local" } })`
173​
174See [OpenAI: Advanced](../adapters/openai-advanced.md#applypatchtool) and [`computerUseTool`](../adapters/openai-advanced.md#computerusetool).
175​
176To 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).
177​
178```typescript
179import { chat } from '@tanstack/ai'
180import { openaiText } from '@tanstack/ai-openai'
181import { createComputerUse } from '@tanstack/ai-computer-use'
182import { playwrightComputer } from '@tanstack/ai-computer-playwright'
183​
184declare const threadId: string
185​
186const computerUse = createComputerUse({
187 driver: playwrightComputer({ startUrl: 'https://tanstack.com' }),
188})
189​
190const stream = chat({
191 adapter: openaiText('gpt-6.1-sol'),
192 messages: [{ role: 'user', content: 'Find the TanStack AI docs' }],
193 threadId,
194 tools: computerUse.tools,
195 middleware: [computerUse.middleware],
196 interrupts: [computerUse.interrupt],
197})
198```
199​
200### How the type guard works
201​
202- Each provider tool factory, such as `webSearchTool` or `computerUseTool`, returns a `ProviderTool<TProvider, TKind>` brand.
203- The `toolCapabilities` of the adapter come from the `supports.tools` list of each model. They decide which brands you can put in `tools`.
204- Put `computerUseTool(...)` on a model that does not support it, and TypeScript shows an error on that array item. The error is not on the factory call, and it is not at runtime.
205- Your `toolDefinition()` tools have no brand, so every model accepts them.
206- The `customTool` factories of `ai-anthropic` and `ai-openai` also return a plain `Tool`, not a `ProviderTool`. Every chat model accepts them too.
207​
208### Upgrade from 0.x
209​
210Did you use `createWebSearchTool` from `@tanstack/ai-openrouter`? See [Migration Guide section 5](../migration/migration.md#5-provider-tools-moved-to-tools).