Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
bde0cd6
stdio transport removed
aayush-kapoor Oct 1, 2025
f6d5478
cs
aayush-kapoor Oct 1, 2025
66a3b97
Merge branch 'main' into aayush/mcp-update
aayush-kapoor Oct 3, 2025
cb98c98
client oauth provider added
aayush-kapoor Oct 3, 2025
4027242
adding authProvider to transport config
aayush-kapoor Oct 3, 2025
66e9d37
example to iterate on
aayush-kapoor Oct 3, 2025
07a41a7
adding proper types and schema
aayush-kapoor Oct 6, 2025
7eb5aeb
Merge branch 'main' into aayush/mcp-update
aayush-kapoor Oct 6, 2025
5d4ca2c
build discovery URLs (for oauth 2.0 and oidc) to get auth server meta…
aayush-kapoor Oct 6, 2025
542943b
start auth flow with pkce generation
aayush-kapoor Oct 6, 2025
9cadd6b
Merge branch 'main' into aayush/mcp-update
aayush-kapoor Oct 6, 2025
cc82f15
exchange auth flow added
aayush-kapoor Oct 6, 2025
4b00d5a
refresh auth token flow added
aayush-kapoor Oct 6, 2025
6992cc2
register-client flow + ci fix
aayush-kapoor Oct 6, 2025
d64abe0
ci fix
aayush-kapoor Oct 6, 2025
173dc59
add final auth function
aayush-kapoor Oct 6, 2025
2e83527
correct auth function used
aayush-kapoor Oct 6, 2025
f6f180b
example updated to use new auth
aayush-kapoor Oct 6, 2025
a4cb0aa
pretty
aayush-kapoor Oct 6, 2025
d91e872
mcp client updated
aayush-kapoor Oct 7, 2025
87262ce
Merge branch 'main' into aayush/mcp-update
aayush-kapoor Oct 7, 2025
9f894ee
Merge branch 'main' into aayush/mcp-update
aayush-kapoor Oct 7, 2025
fe8045d
working example
aayush-kapoor Oct 7, 2025
b459de0
http transport method added
aayush-kapoor Oct 8, 2025
dae7e49
Merge branch 'main' into aayush/mcp-update
aayush-kapoor Oct 8, 2025
3706cee
proper imports
aayush-kapoor Oct 8, 2025
726b897
failing test for http transport
aayush-kapoor Oct 8, 2025
77689ba
fix for sse resumption
aayush-kapoor Oct 8, 2025
72c8db2
working example
aayush-kapoor Oct 8, 2025
9e2a153
working example pt2
aayush-kapoor Oct 8, 2025
d34c51e
pretty
aayush-kapoor Oct 8, 2025
26275dc
neater example
aayush-kapoor Oct 8, 2025
84a8c06
Merge branch 'main' into aayush/mcp-update
aayush-kapoor Oct 8, 2025
c5591af
Merge branch 'main' into aayush/mcp-update
aayush-kapoor Oct 9, 2025
798632a
example refined
aayush-kapoor Oct 9, 2025
509a1cc
updated example - works with most mcp servers
aayush-kapoor Oct 9, 2025
d75f279
refactor into new package
aayush-kapoor Oct 9, 2025
a4e3a3e
pretty
aayush-kapoor Oct 9, 2025
d58e6f1
rename
aayush-kapoor Oct 9, 2025
ff986f4
changeset updated
aayush-kapoor Oct 9, 2025
ed411a6
Merge branch 'main' into aayush/mcp-update
aayush-kapoor Oct 10, 2025
23bd8c3
remove pkce dependency
aayush-kapoor Oct 10, 2025
52f313b
moving pkce to prod dep
aayush-kapoor Oct 10, 2025
f5cc078
added mcp dependency to `ai`
aayush-kapoor Oct 10, 2025
22a4fe7
legacy imports still work
aayush-kapoor Oct 10, 2025
4e0d28c
Merge remote-tracking branch 'origin/main' into aayush/mcp-update
aayush-kapoor Oct 10, 2025
5ee1cb2
pretty
aayush-kapoor Oct 10, 2025
65a1367
build fix
aayush-kapoor Oct 10, 2025
60d359b
deprecated fucntion ref
aayush-kapoor Oct 10, 2025
ea65a78
documentation added
aayush-kapoor Oct 10, 2025
10b886e
pretty
aayush-kapoor Oct 10, 2025
86c42f5
Merge branch 'main' into aayush/mcp-update
aayush-kapoor Oct 10, 2025
4f1adaf
agent fix
aayush-kapoor Oct 10, 2025
faac7ba
Merge branch 'main' into aayush/mcp-update
aayush-kapoor Oct 10, 2025
a9fcd88
agent fix
aayush-kapoor Oct 10, 2025
036a3cf
Merge branch 'main' into aayush/mcp-update
aayush-kapoor Oct 14, 2025
0e5ff97
docs fixes
aayush-kapoor Oct 14, 2025
1ddbeae
bug fix
aayush-kapoor Oct 14, 2025
b9336bd
pretty
aayush-kapoor Oct 14, 2025
4e81c03
Merge branch 'main' into aayush/mcp-update
aayush-kapoor Oct 14, 2025
c700310
updated example
aayush-kapoor Oct 14, 2025
518331e
Merge branch 'aayush/mcp-update' of https://github.com/vercel/ai into…
aayush-kapoor Oct 14, 2025
492032f
updated zapier example for mcp
aayush-kapoor Oct 14, 2025
d9ab914
Merge branch 'main' into aayush/mcp-update
aayush-kapoor Oct 14, 2025
7a2e305
removing mcp export
aayush-kapoor Oct 14, 2025
f31b0f6
working e2e exmaple
aayush-kapoor Oct 15, 2025
c91b317
Merge branch 'main' into aayush/mcp-update
aayush-kapoor Oct 15, 2025
2608017
pretty + fx
aayush-kapoor Oct 15, 2025
ed2e04b
Merge branch 'main' into aayush/mcp-update
aayush-kapoor Oct 15, 2025
a71defa
agent fix
aayush-kapoor Oct 15, 2025
a89bc67
authFlow fix
aayush-kapoor Oct 15, 2025
aa44dbf
Merge branch 'main' into aayush/mcp-update
aayush-kapoor Oct 16, 2025
e61089a
throw error on using wrong transport
aayush-kapoor Oct 16, 2025
2c7bd9d
prettier + test fix
aayush-kapoor Oct 16, 2025
62a6757
Merge branch 'main' into aayush/mcp-update
aayush-kapoor Oct 16, 2025
f516db5
fx
aayush-kapoor Oct 16, 2025
12fdd4d
Merge branch 'main' into aayush/mcp-update
aayush-kapoor Oct 16, 2025
61386e8
Merge branch 'main' into aayush/mcp-update
aayush-kapoor Oct 16, 2025
8a1ba9d
Merge branch 'main' into aayush/mcp-update
aayush-kapoor Oct 20, 2025
952180b
Merge branch 'main' into aayush/mcp-update
aayush-kapoor Oct 21, 2025
ece5fa5
Merge branch 'main' into aayush/mcp-update
aayush-kapoor Oct 22, 2025
8f21666
updated example to open auth window on client side
aayush-kapoor Oct 22, 2025
8fe7475
Merge branch 'main' into aayush/mcp-update
aayush-kapoor Oct 22, 2025
a0250c3
pretty
aayush-kapoor Oct 22, 2025
17b7562
Merge branch 'aayush/mcp-update' of https://github.com/vercel/ai into…
aayush-kapoor Oct 22, 2025
90f2667
Merge branch 'main' into aayush/mcp-update
gr2m Oct 24, 2025
f07c958
docs(changeset): Add OAuth for MCP clients and refactor import path
gr2m Oct 24, 2025
2258ca0
Refactor import paths for MCP client and transport
gr2m Oct 24, 2025
e65e0b0
remove remaining artifacts of mcp from `ai` package
gr2m Oct 24, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .changeset/fluffy-poems-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
'ai': patch
'@ai-sdk/mcp': major
---

feat(ai): add OAuth for MCP clients + refactor to new package

This change replaces

```ts
import { experimental_createMCPClient } from 'ai';
import { Experimental_StdioMCPTransport } from 'ai/mcp-stdio';
```

with

```ts
import { experimental_createMCPClient } from '@ai-sdk/mcp';
import { Experimental_StdioMCPTransport } from '@ai-sdk/mcp/mcp-stdio';
```
55 changes: 37 additions & 18 deletions content/cookbook/01-next/73-mcp-tools.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,25 @@ The AI SDK supports Model Context Protocol (MCP) tools by offering a lightweight

Let's create a route handler for `/api/completion` that will generate text based on the input prompt and MCP tools that can be called at any time during a generation. The route will call the `streamText` function from the `ai` module, which will then generate text based on the input prompt and stream it to the client.

To use the `StreamableHTTPClientTransport`, you will need to install the official Typescript SDK for Model Context Protocol:
If you prefer to use the official transports (optional), install the official TypeScript SDK for Model Context Protocol:

<Snippet text="pnpm install @modelcontextprotocol/sdk" />

```ts filename="app/api/completion/route.ts"
import { experimental_createMCPClient, streamText } from 'ai';
import { Experimental_StdioMCPTransport } from 'ai/mcp-stdio';
import { experimental_createMCPClient, streamText } from '@ai-sdk/mcp';
import { Experimental_StdioMCPTransport } from '@ai-sdk/mcp/mcp-stdio';
import { openai } from '@ai-sdk/openai';
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio';
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp';
// Optional: Official transports if you prefer them
// import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio';
// import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse';
// import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp';

export async function POST(req: Request) {
const { prompt }: { prompt: string } = await req.json();

try {
// Initialize an MCP client to connect to a `stdio` MCP server:
const transport = new StdioClientTransport({
// Initialize an MCP client to connect to a `stdio` MCP server (local only):
const transport = new Experimental_StdioMCPTransport({
command: 'node',
args: ['src/stdio/dist/server.js'],
});
Expand All @@ -38,22 +39,40 @@ export async function POST(req: Request) {
transport,
});

// You can also connect to StreamableHTTP MCP servers
const httpTransport = new StreamableHTTPClientTransport(
new URL('http://localhost:3000/mcp'),
);
// Connect to an HTTP MCP server directly via the client transport config
const httpClient = await experimental_createMCPClient({
transport: httpTransport,
transport: {
type: 'http',
url: 'http://localhost:3000/mcp',

// optional: configure headers
// headers: { Authorization: 'Bearer my-api-key' },

// optional: provide an OAuth client provider for automatic authorization
// authProvider: myOAuthClientProvider,
},
});

// Alternatively, you can connect to a Server-Sent Events (SSE) MCP server:
const sseTransport = new SSEClientTransport(
new URL('http://localhost:3000/sse'),
);
// Connect to a Server-Sent Events (SSE) MCP server directly via the client transport config
const sseClient = await experimental_createMCPClient({
transport: sseTransport,
transport: {
type: 'sse',
url: 'http://localhost:3000/sse',

// optional: configure headers
// headers: { Authorization: 'Bearer my-api-key' },

// optional: provide an OAuth client provider for automatic authorization
// authProvider: myOAuthClientProvider,
},
});

// Alternatively, you can create transports with the official SDKs instead of direct config:
// const httpTransport = new StreamableHTTPClientTransport(new URL('http://localhost:3000/mcp'));
// const httpClient = await experimental_createMCPClient({ transport: httpTransport });
// const sseTransport = new SSEClientTransport(new URL('http://localhost:3000/sse'));
// const sseClient = await experimental_createMCPClient({ transport: sseTransport });

const toolSetOne = await stdioClient.tools();
const toolSetTwo = await httpClient.tools();
const toolSetThree = await sseClient.tools();
Expand Down
59 changes: 41 additions & 18 deletions content/cookbook/05-node/54-mcp-tools.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,30 @@ tags: ['node', 'tool use', 'agent', 'mcp']

The AI SDK supports Model Context Protocol (MCP) tools by offering a lightweight client that exposes a `tools` method for retrieving tools from a MCP server. After use, the client should always be closed to release resources.

Use the official Model Context Protocol Typescript SDK to create the connection to the MCP server.
If you prefer to use the official transports (optional), install the official Model Context Protocol TypeScript SDK.

<Snippet text="pnpm install @modelcontextprotocol/sdk" />

```ts
import { experimental_createMCPClient, generateText, stepCountIs } from 'ai';
import { Experimental_StdioMCPTransport } from 'ai/mcp-stdio';
import {
experimental_createMCPClient,
generateText,
stepCountIs,
} from '@ai-sdk/mcp';
import { Experimental_StdioMCPTransport } from '@ai-sdk/mcp/mcp-stdio';
import { openai } from '@ai-sdk/openai';
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio';
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp';
// Optional: Official transports if you prefer them
// import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio';
// import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse';
// import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp';

let clientOne;
let clientTwo;
let clientThree;

try {
// Initialize an MCP client to connect to a `stdio` MCP server:
const transport = new StdioClientTransport({
// Initialize an MCP client to connect to a `stdio` MCP server (local only):
const transport = new Experimental_StdioMCPTransport({
command: 'node',
args: ['src/stdio/dist/server.js'],
});
Expand All @@ -35,22 +40,40 @@ try {
transport,
});

// You can also connect to StreamableHTTP MCP servers
const httpTransport = new StreamableHTTPClientTransport(
new URL('http://localhost:3000/mcp'),
);
// Connect to an HTTP MCP server directly via the client transport config
const clientTwo = await experimental_createMCPClient({
transport: httpTransport,
transport: {
type: 'http',
url: 'http://localhost:3000/mcp',

// optional: configure headers
// headers: { Authorization: 'Bearer my-api-key' },

// optional: provide an OAuth client provider for automatic authorization
// authProvider: myOAuthClientProvider,
},
});

// Alternatively, you can connect to a Server-Sent Events (SSE) MCP server:
const sseTransport = new SSEClientTransport(
new URL('http://localhost:3000/sse'),
);
// Connect to a Server-Sent Events (SSE) MCP server directly via the client transport config
const clientThree = await experimental_createMCPClient({
transport: sseTransport,
transport: {
type: 'sse',
url: 'http://localhost:3000/sse',

// optional: configure headers
// headers: { Authorization: 'Bearer my-api-key' },

// optional: provide an OAuth client provider for automatic authorization
// authProvider: myOAuthClientProvider,
},
});

// Alternatively, you can create transports with the official SDKs instead of direct config:
// const httpTransport = new StreamableHTTPClientTransport(new URL('http://localhost:3000/mcp'));
// clientTwo = await experimental_createMCPClient({ transport: httpTransport });
// const sseTransport = new SSEClientTransport(new URL('http://localhost:3000/sse'));
// clientThree = await experimental_createMCPClient({ transport: sseTransport });

const toolSetOne = await clientOne.tools();
const toolSetTwo = await clientTwo.tools();
const toolSetThree = await clientThree.tools();
Expand Down
48 changes: 36 additions & 12 deletions content/docs/03-ai-sdk-core/16-mcp-tools.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,35 @@ We recommend using HTTP transport (like `StreamableHTTPClientTransport`) for pro

Create an MCP client using one of the following transport options:

- **HTTP transport (Recommended)**: Use transports from MCP's official TypeScript SDK like `StreamableHTTPClientTransport` for production deployments
- **HTTP transport (Recommended)**: Either configure HTTP directly via the client using `transport: { type: 'http', ... }`, or use MCP's official TypeScript SDK `StreamableHTTPClientTransport`
- SSE (Server-Sent Events): An alternative HTTP-based transport
- `stdio`: For local development only. Uses standard input/output streams for local MCP servers

### HTTP Transport (Recommended)

For production deployments, we recommend using HTTP transports like `StreamableHTTPClientTransport` from MCP's official TypeScript SDK:
For production deployments, we recommend using the HTTP transport. You can configure it directly on the client:

```typescript
import { experimental_createMCPClient as createMCPClient } from 'ai';
import { experimental_createMCPClient as createMCPClient } from '@ai-sdk/mcp';

const mcpClient = await createMCPClient({
transport: {
type: 'http',
url: 'https://your-server.com/mcp',

// optional: configure HTTP headers
headers: { Authorization: 'Bearer my-api-key' },

// optional: provide an OAuth client provider for automatic authorization
authProvider: myOAuthClientProvider,
},
});
```

Alternatively, you can use `StreamableHTTPClientTransport` from MCP's official TypeScript SDK:

```typescript
import { experimental_createMCPClient as createMCPClient } from '@ai-sdk/mcp';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';

const url = new URL('https://your-server.com/mcp');
Expand All @@ -40,20 +59,21 @@ const mcpClient = await createMCPClient({

### SSE Transport

SSE provides an alternative HTTP-based transport option. Configure it with a `type` and `url` property:
SSE provides an alternative HTTP-based transport option. Configure it with a `type` and `url` property. You can also provide an `authProvider` for OAuth:

```typescript
import { experimental_createMCPClient as createMCPClient } from 'ai';
import { experimental_createMCPClient as createMCPClient } from '@ai-sdk/mcp';

const mcpClient = await createMCPClient({
transport: {
type: 'sse',
url: 'https://my-server.com/sse',

// optional: configure HTTP headers, e.g. for authentication
headers: {
Authorization: 'Bearer my-api-key',
},
// optional: configure HTTP headers
headers: { Authorization: 'Bearer my-api-key' },

// optional: provide an OAuth client provider for automatic authorization
authProvider: myOAuthClientProvider,
},
});
```
Expand All @@ -67,10 +87,10 @@ const mcpClient = await createMCPClient({
The Stdio transport can be imported from either the MCP SDK or the AI SDK:

```typescript
import { experimental_createMCPClient as createMCPClient } from 'ai';
import { experimental_createMCPClient as createMCPClient } from '@ai-sdk/mcp';
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
// Or use the AI SDK's stdio transport:
// import { Experimental_StdioMCPTransport as StdioClientTransport } from 'ai/mcp-stdio';
// import { Experimental_StdioMCPTransport as StdioClientTransport } from '@ai-sdk/mcp/mcp-stdio';

const mcpClient = await createMCPClient({
transport: new StdioClientTransport({
Expand All @@ -87,8 +107,12 @@ You can also bring your own transport by implementing the `MCPTransport` interfa
<Note>
The client returned by the `experimental_createMCPClient` function is a
lightweight client intended for use in tool conversion. It currently does not
support all features of the full MCP client, such as: authorization, session
support all features of the full MCP client, such as: session
management, resumable streams, and receiving notifications.

Authorization via OAuth is supported when using the AI SDK MCP HTTP or SSE
transports by providing an `authProvider`.

</Note>

### Closing the MCP Client
Expand Down
17 changes: 12 additions & 5 deletions content/docs/07-reference/01-ai-sdk-core/23-create-mcp-client.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This feature is experimental and may change or be removed in the future.
## Import

<Snippet
text={`import { experimental_createMCPClient } from "ai"`}
text={`import { experimental_createMCPClient } from "@ai-sdk/mcp"`}
prompt={false}
/>

Expand Down Expand Up @@ -79,11 +79,11 @@ This feature is experimental and may change or be removed in the future.
],
},
{
type: 'McpSSEServerConfig',
type: 'MCPTransportConfig',
parameters: [
{
name: 'type',
type: "'sse'",
type: "'sse' | 'http",
description: 'Use Server-Sent Events for communication',
},
{
Expand All @@ -98,6 +98,13 @@ This feature is experimental and may change or be removed in the future.
description:
'Additional HTTP headers to be sent with requests.',
},
{
name: 'authProvider',
type: 'OAuthClientProvider',
isOptional: true,
description:
'Optional OAuth provider for authorization to access protected remote MCP servers.',
},
],
},
],
Expand Down Expand Up @@ -160,8 +167,8 @@ Returns a Promise that resolves to an `MCPClient` with the following methods:
## Example

```typescript
import { experimental_createMCPClient, generateText } from 'ai';
import { Experimental_StdioMCPTransport } from 'ai/mcp-stdio';
import { experimental_createMCPClient, generateText } from '@ai-sdk/mcp';
import { Experimental_StdioMCPTransport } from '@ai-sdk/mcp/mcp-stdio';
import { openai } from '@ai-sdk/openai';

let client;
Expand Down
3 changes: 3 additions & 0 deletions examples/mcp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"scripts": {
"sse:server": "tsx src/sse/server.ts",
"sse:client": "tsx src/sse/client.ts",
"sse-auth:server": "tsx src/mcp-with-auth/server.ts",
"sse-auth:client": "tsx src/mcp-with-auth/client.ts",
"stdio:build": "tsc src/stdio/server.ts --outDir src/stdio/dist --target es2023 --module nodenext",
"stdio:client": "tsx src/stdio/client.ts",
"http:server": "tsx src/http/server.ts",
Expand All @@ -22,6 +24,7 @@
"zod": "3.25.76"
},
"devDependencies": {
"@ai-sdk/mcp": "workspace:*",
"@types/express": "5.0.0",
"@types/node": "20.17.24",
"tsx": "4.19.2",
Expand Down
7 changes: 3 additions & 4 deletions examples/mcp/src/http/client.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { openai } from '@ai-sdk/openai';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
import { generateText, stepCountIs } from 'ai';
import 'dotenv/config';
import {
experimental_createMCPClient as createMCPClient,
experimental_MCPClient as MCPClient,
generateText,
stepCountIs,
} from 'ai';
import 'dotenv/config';
} from '@ai-sdk/mcp';

async function main() {
const transport = new StreamableHTTPClientTransport(
Expand Down
Loading
Loading