Skip to content

Commit 0367b67

Browse files
aayush-kapoorvercel-ai-sdk[bot]
authored andcommitted
Backport conflicts for PR #9127 to release-v5.0
1 parent 55cdf7a commit 0367b67

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+5627
-166
lines changed

.changeset/fluffy-poems-live.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
'ai': patch
3+
'@ai-sdk/mcp': major
4+
---
5+
6+
feat(ai): add OAuth for MCP clients + refactor to new package
7+
8+
This change replaces
9+
10+
```ts
11+
import { experimental_createMCPClient } from 'ai';
12+
import { Experimental_StdioMCPTransport } from 'ai/mcp-stdio';
13+
```
14+
15+
with
16+
17+
```ts
18+
import { experimental_createMCPClient } from '@ai-sdk/mcp';
19+
import { Experimental_StdioMCPTransport } from '@ai-sdk/mcp/mcp-stdio';
20+
```

content/cookbook/01-next/73-mcp-tools.mdx

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,25 @@ The AI SDK supports Model Context Protocol (MCP) tools by offering a lightweight
1212

1313
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.
1414

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

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

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

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

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

41-
// You can also connect to StreamableHTTP MCP servers
42-
const httpTransport = new StreamableHTTPClientTransport(
43-
new URL('http://localhost:3000/mcp'),
44-
);
42+
// Connect to an HTTP MCP server directly via the client transport config
4543
const httpClient = await experimental_createMCPClient({
46-
transport: httpTransport,
44+
transport: {
45+
type: 'http',
46+
url: 'http://localhost:3000/mcp',
47+
48+
// optional: configure headers
49+
// headers: { Authorization: 'Bearer my-api-key' },
50+
51+
// optional: provide an OAuth client provider for automatic authorization
52+
// authProvider: myOAuthClientProvider,
53+
},
4754
});
4855

49-
// Alternatively, you can connect to a Server-Sent Events (SSE) MCP server:
50-
const sseTransport = new SSEClientTransport(
51-
new URL('http://localhost:3000/sse'),
52-
);
56+
// Connect to a Server-Sent Events (SSE) MCP server directly via the client transport config
5357
const sseClient = await experimental_createMCPClient({
54-
transport: sseTransport,
58+
transport: {
59+
type: 'sse',
60+
url: 'http://localhost:3000/sse',
61+
62+
// optional: configure headers
63+
// headers: { Authorization: 'Bearer my-api-key' },
64+
65+
// optional: provide an OAuth client provider for automatic authorization
66+
// authProvider: myOAuthClientProvider,
67+
},
5568
});
5669

70+
// Alternatively, you can create transports with the official SDKs instead of direct config:
71+
// const httpTransport = new StreamableHTTPClientTransport(new URL('http://localhost:3000/mcp'));
72+
// const httpClient = await experimental_createMCPClient({ transport: httpTransport });
73+
// const sseTransport = new SSEClientTransport(new URL('http://localhost:3000/sse'));
74+
// const sseClient = await experimental_createMCPClient({ transport: sseTransport });
75+
5776
const toolSetOne = await stdioClient.tools();
5877
const toolSetTwo = await httpClient.tools();
5978
const toolSetThree = await sseClient.tools();

content/cookbook/05-node/54-mcp-tools.mdx

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,30 @@ tags: ['node', 'tool use', 'agent', 'mcp']
88

99
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.
1010

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

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

1515
```ts
16-
import { experimental_createMCPClient, generateText, stepCountIs } from 'ai';
17-
import { Experimental_StdioMCPTransport } from 'ai/mcp-stdio';
16+
import {
17+
experimental_createMCPClient,
18+
generateText,
19+
stepCountIs,
20+
} from '@ai-sdk/mcp';
21+
import { Experimental_StdioMCPTransport } from '@ai-sdk/mcp/mcp-stdio';
1822
import { openai } from '@ai-sdk/openai';
19-
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio';
20-
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse';
21-
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp';
23+
// Optional: Official transports if you prefer them
24+
// import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio';
25+
// import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse';
26+
// import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp';
2227

2328
let clientOne;
2429
let clientTwo;
2530
let clientThree;
2631

2732
try {
28-
// Initialize an MCP client to connect to a `stdio` MCP server:
29-
const transport = new StdioClientTransport({
33+
// Initialize an MCP client to connect to a `stdio` MCP server (local only):
34+
const transport = new Experimental_StdioMCPTransport({
3035
command: 'node',
3136
args: ['src/stdio/dist/server.js'],
3237
});
@@ -35,22 +40,40 @@ try {
3540
transport,
3641
});
3742

38-
// You can also connect to StreamableHTTP MCP servers
39-
const httpTransport = new StreamableHTTPClientTransport(
40-
new URL('http://localhost:3000/mcp'),
41-
);
43+
// Connect to an HTTP MCP server directly via the client transport config
4244
const clientTwo = await experimental_createMCPClient({
43-
transport: httpTransport,
45+
transport: {
46+
type: 'http',
47+
url: 'http://localhost:3000/mcp',
48+
49+
// optional: configure headers
50+
// headers: { Authorization: 'Bearer my-api-key' },
51+
52+
// optional: provide an OAuth client provider for automatic authorization
53+
// authProvider: myOAuthClientProvider,
54+
},
4455
});
4556

46-
// Alternatively, you can connect to a Server-Sent Events (SSE) MCP server:
47-
const sseTransport = new SSEClientTransport(
48-
new URL('http://localhost:3000/sse'),
49-
);
57+
// Connect to a Server-Sent Events (SSE) MCP server directly via the client transport config
5058
const clientThree = await experimental_createMCPClient({
51-
transport: sseTransport,
59+
transport: {
60+
type: 'sse',
61+
url: 'http://localhost:3000/sse',
62+
63+
// optional: configure headers
64+
// headers: { Authorization: 'Bearer my-api-key' },
65+
66+
// optional: provide an OAuth client provider for automatic authorization
67+
// authProvider: myOAuthClientProvider,
68+
},
5269
});
5370

71+
// Alternatively, you can create transports with the official SDKs instead of direct config:
72+
// const httpTransport = new StreamableHTTPClientTransport(new URL('http://localhost:3000/mcp'));
73+
// clientTwo = await experimental_createMCPClient({ transport: httpTransport });
74+
// const sseTransport = new SSEClientTransport(new URL('http://localhost:3000/sse'));
75+
// clientThree = await experimental_createMCPClient({ transport: sseTransport });
76+
5477
const toolSetOne = await clientOne.tools();
5578
const toolSetTwo = await clientTwo.tools();
5679
const toolSetThree = await clientThree.tools();

content/docs/03-ai-sdk-core/16-mcp-tools.mdx

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,35 @@ We recommend using HTTP transport (like `StreamableHTTPClientTransport`) for pro
1818

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

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

2525
### HTTP Transport (Recommended)
2626

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

2929
```typescript
30-
import { experimental_createMCPClient as createMCPClient } from 'ai';
30+
import { experimental_createMCPClient as createMCPClient } from '@ai-sdk/mcp';
31+
32+
const mcpClient = await createMCPClient({
33+
transport: {
34+
type: 'http',
35+
url: 'https://your-server.com/mcp',
36+
37+
// optional: configure HTTP headers
38+
headers: { Authorization: 'Bearer my-api-key' },
39+
40+
// optional: provide an OAuth client provider for automatic authorization
41+
authProvider: myOAuthClientProvider,
42+
},
43+
});
44+
```
45+
46+
Alternatively, you can use `StreamableHTTPClientTransport` from MCP's official TypeScript SDK:
47+
48+
```typescript
49+
import { experimental_createMCPClient as createMCPClient } from '@ai-sdk/mcp';
3150
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
3251

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

4160
### SSE Transport
4261

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

4564
```typescript
46-
import { experimental_createMCPClient as createMCPClient } from 'ai';
65+
import { experimental_createMCPClient as createMCPClient } from '@ai-sdk/mcp';
4766

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

53-
// optional: configure HTTP headers, e.g. for authentication
54-
headers: {
55-
Authorization: 'Bearer my-api-key',
56-
},
72+
// optional: configure HTTP headers
73+
headers: { Authorization: 'Bearer my-api-key' },
74+
75+
// optional: provide an OAuth client provider for automatic authorization
76+
authProvider: myOAuthClientProvider,
5777
},
5878
});
5979
```
@@ -67,10 +87,10 @@ const mcpClient = await createMCPClient({
6787
The Stdio transport can be imported from either the MCP SDK or the AI SDK:
6888

6989
```typescript
70-
import { experimental_createMCPClient as createMCPClient } from 'ai';
90+
import { experimental_createMCPClient as createMCPClient } from '@ai-sdk/mcp';
7191
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
7292
// Or use the AI SDK's stdio transport:
73-
// import { Experimental_StdioMCPTransport as StdioClientTransport } from 'ai/mcp-stdio';
93+
// import { Experimental_StdioMCPTransport as StdioClientTransport } from '@ai-sdk/mcp/mcp-stdio';
7494

7595
const mcpClient = await createMCPClient({
7696
transport: new StdioClientTransport({
@@ -87,8 +107,12 @@ You can also bring your own transport by implementing the `MCPTransport` interfa
87107
<Note>
88108
The client returned by the `experimental_createMCPClient` function is a
89109
lightweight client intended for use in tool conversion. It currently does not
90-
support all features of the full MCP client, such as: authorization, session
110+
support all features of the full MCP client, such as: session
91111
management, resumable streams, and receiving notifications.
112+
113+
Authorization via OAuth is supported when using the AI SDK MCP HTTP or SSE
114+
transports by providing an `authProvider`.
115+
92116
</Note>
93117

94118
### Closing the MCP Client

content/docs/07-reference/01-ai-sdk-core/23-create-mcp-client.mdx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ This feature is experimental and may change or be removed in the future.
1414
## Import
1515

1616
<Snippet
17-
text={`import { experimental_createMCPClient } from "ai"`}
17+
text={`import { experimental_createMCPClient } from "@ai-sdk/mcp"`}
1818
prompt={false}
1919
/>
2020

@@ -79,11 +79,11 @@ This feature is experimental and may change or be removed in the future.
7979
],
8080
},
8181
{
82-
type: 'McpSSEServerConfig',
82+
type: 'MCPTransportConfig',
8383
parameters: [
8484
{
8585
name: 'type',
86-
type: "'sse'",
86+
type: "'sse' | 'http",
8787
description: 'Use Server-Sent Events for communication',
8888
},
8989
{
@@ -98,6 +98,13 @@ This feature is experimental and may change or be removed in the future.
9898
description:
9999
'Additional HTTP headers to be sent with requests.',
100100
},
101+
{
102+
name: 'authProvider',
103+
type: 'OAuthClientProvider',
104+
isOptional: true,
105+
description:
106+
'Optional OAuth provider for authorization to access protected remote MCP servers.',
107+
},
101108
],
102109
},
103110
],
@@ -160,8 +167,8 @@ Returns a Promise that resolves to an `MCPClient` with the following methods:
160167
## Example
161168

162169
```typescript
163-
import { experimental_createMCPClient, generateText } from 'ai';
164-
import { Experimental_StdioMCPTransport } from 'ai/mcp-stdio';
170+
import { experimental_createMCPClient, generateText } from '@ai-sdk/mcp';
171+
import { Experimental_StdioMCPTransport } from '@ai-sdk/mcp/mcp-stdio';
165172
import { openai } from '@ai-sdk/openai';
166173

167174
let client;

examples/mcp/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"scripts": {
66
"sse:server": "tsx src/sse/server.ts",
77
"sse:client": "tsx src/sse/client.ts",
8+
"sse-auth:server": "tsx src/mcp-with-auth/server.ts",
9+
"sse-auth:client": "tsx src/mcp-with-auth/client.ts",
810
"stdio:build": "tsc src/stdio/server.ts --outDir src/stdio/dist --target es2023 --module nodenext",
911
"stdio:client": "tsx src/stdio/client.ts",
1012
"http:server": "tsx src/http/server.ts",
@@ -22,6 +24,7 @@
2224
"zod": "3.25.76"
2325
},
2426
"devDependencies": {
27+
"@ai-sdk/mcp": "workspace:*",
2528
"@types/express": "5.0.0",
2629
"@types/node": "20.17.24",
2730
"tsx": "4.19.2",

examples/mcp/src/http/client.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { openai } from '@ai-sdk/openai';
22
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
3+
import { generateText, stepCountIs } from 'ai';
4+
import 'dotenv/config';
35
import {
46
experimental_createMCPClient as createMCPClient,
57
experimental_MCPClient as MCPClient,
6-
generateText,
7-
stepCountIs,
8-
} from 'ai';
9-
import 'dotenv/config';
8+
} from '@ai-sdk/mcp';
109

1110
async function main() {
1211
const transport = new StreamableHTTPClientTransport(

0 commit comments

Comments
 (0)