Open History to see the last commit
1---
2title: MCP Server Tools
3id: mcp
4order: 8
5description: "Connect TanStack AI to a Model Context Protocol server with createMCPClient, and give the model the tools of that server in chat()."
6keywords:
7 - tanstack ai
8 - mcp
9 - model context protocol
10 - mcp tools
11 - mcp client
12 - server tools
13 - createMCPClient
14 - createMCPClients
15 - type safety
16---
17
18`createMCPClient` connects to an MCP server. Pass that client to `chat()` in the `mcp` option.
19
20> [!NOTE]
21> MCP tools run on the server. Call `createMCPClient` in a server route or a serverless function, never in browser code.
22
23Servers can also push signed webhooks through the experimental MCP Events extension. See [MCP Events](./mcp-events). A Start app that subscribes and receives a delivery is in [Subscribe to MCP Events](../tutorials/mcp-events).
24
25## Quick setup
26
27You need a working chat first. If you do not have one, start with the [Quick Start](../getting-started/quick-start).
28
291. Install the MCP client:
30
31<!-- ::start:tabs variant="package-manager" mode="install" -->
32
33react: @tanstack/ai-mcp @modelcontextprotocol/client
34vue: @tanstack/ai-mcp @modelcontextprotocol/client
35solid: @tanstack/ai-mcp @modelcontextprotocol/client
36svelte: @tanstack/ai-mcp @modelcontextprotocol/client
37preact: @tanstack/ai-mcp @modelcontextprotocol/client
38angular: @tanstack/ai-mcp @modelcontextprotocol/client
39vanilla: @tanstack/ai-mcp @modelcontextprotocol/client
40octane: @tanstack/ai-mcp @modelcontextprotocol/client
41
42<!-- ::end:tabs -->
43
442. In your chat route, connect to the server and pass the client to `chat()`. Change the URL to the URL of your MCP server:
45
46```ts ignore
47// src/routes/api.chat.ts (TanStack Start)
48import { createFileRoute } from '@tanstack/react-router'
49import { chat, toServerSentEventsResponse } from '@tanstack/ai'
50import { openaiText } from '@tanstack/ai-openai'
51import { createMCPClient } from '@tanstack/ai-mcp'
52
53export const Route = createFileRoute('/api/chat')({
54 server: {
55 handlers: {
56 POST: async ({ request }) => {
57 const { messages, threadId } = await request.json()
58
59 const mcp = await createMCPClient({
60 transport: {
61 type: 'http',
62 url: 'https://my-mcp-server.example.com/mcp',
63 },
64 })
65
66 // chat() finds the tools and closes the client when the run ends.
67 const stream = chat({
68 adapter: openaiText('gpt-6.1-sol'),
69 messages,
70 threadId,
71 mcp: { clients: [mcp] },
72 })
73
74 return toServerSentEventsResponse(stream)
75 },
76 },
77 },
78})
79```
80
813. Keep your client code. `useChat` reads this endpoint the same way as any other chat endpoint:
82
83```tsx
84// src/components/Chat.tsx
85import { useChat } from '@tanstack/ai-react'
86import { fetchServerSentEvents } from '@tanstack/ai-client'
87
88export function Chat() {
89 const { messages, sendMessage, status } = useChat({
90 connection: fetchServerSentEvents('/api/chat'),
91 threadId: 'support-chat',
92 })
93
94 return (
95 <div>
96 {messages.map((m) => (
97 <div key={m.id}>
98 <strong>{m.role}:</strong>{' '}
99 {m.parts.find((p) => p.type === 'text')?.content}
100 </div>
101 ))}
102 <button
103 onClick={() => sendMessage({ content: 'Hello' })}
104 disabled={status === 'streaming'}
105 >
106 Send
107 </button>
108 </div>
109 )
110}
111```
112
113Now ask the model something that needs one of the server tools. The model calls the tool, your server runs it on the MCP server, and the answer streams back.
114
115## How it works
116
117- `createMCPClient` connects to the server. It tries protocol `2026-07-28` first. If the server does not support it, the client uses the 2025 handshake.
118- `chat({ mcp })` asks each client for its tools before the first model call.
119- When the model calls a tool, `chat()` sends the call to the MCP server and gives the result back to the model.
120- When the run ends, `chat()` closes the client. This happens on success, on error, and on abort.
121
122> [!NOTE]
123> MCP tool arguments have the type `unknown` at compile time. The client checks them against the JSON Schema of the server at runtime.
124
125## Connect to your server
126
127Pick the transport that your server uses.
128
129HTTP (Streamable HTTP) is the usual choice for a remote server. Add `headers` when the server needs a token:
130
131```ts
132import { createMCPClient } from '@tanstack/ai-mcp'
133
134const mcp = await createMCPClient({
135 transport: {
136 type: 'http',
137 url: 'https://my-mcp-server.example.com/mcp',
138 headers: { Authorization: `Bearer ${process.env.MCP_TOKEN}` },
139 },
140})
141```
142
143SSE is for servers that use the older SSE transport:
144
145```ts
146import { createMCPClient } from '@tanstack/ai-mcp'
147
148const mcp = await createMCPClient({
149 transport: {
150 type: 'sse',
151 url: 'https://my-mcp-server.example.com/sse',
152 headers: { Authorization: `Bearer ${process.env.MCP_TOKEN}` },
153 },
154})
155```
156
157stdio starts a local MCP process. It works in Node.js only. It has its own import path, so edge bundles do not include Node modules:
158
159```ts
160import { stdioTransport } from '@tanstack/ai-mcp/stdio'
161import { createMCPClient } from '@tanstack/ai-mcp'
162
163const mcp = await createMCPClient({
164 transport: stdioTransport({
165 command: 'node',
166 args: ['./my-mcp-server.js'],
167 env: { API_KEY: process.env.API_KEY ?? '' },
168 }),
169})
170```
171
172For OAuth, custom transports, and timeouts, see [MCP Server Tools: Advanced](./mcp-advanced).
173
174## What to read next
175
176- **Hide tools or ask the user first.** A server can expose tools that write or delete data. See [Limit and Gate Tools](./mcp-advanced#limit-and-gate-tools).
177- **Many servers, warm connections, or a flaky server.** See [Managed MCP with chat()](./mcp-managed).
178- **Typed tool arguments, resources, and prompts.** See [Manual MCP](./mcp-manual).
179- **Compile-checked tool names.** See [MCP Type Generation](./mcp-codegen).
180- **The server asks the user for input during a tool call.** See [MCP Client Input](./mcp-input).
181
182> [!NOTE]
183> This page is about MCP on your server. To expose browser actions to a browser agent, see [WebMCP Tools](./webmcp). WebMCP calls go to the browser agent, and they do not become tool results in a TanStack chat run.
184
185Package names for an `@modelcontextprotocol/sdk` import are in [MCP SDK packages](../migration/mcp-sdk).
186
187## Advanced
188
189Every option of the client is on [MCP Server Tools: Advanced](./mcp-advanced): OAuth, timeouts, the three modes of type safety, tool annotations, pools, lifecycle, tool names, lazy discovery, and the error reference.