# @ai-sdk/mcp

Latest version **2.0.73** (published 2026-10-09) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @ai-sdk/mcp
pnpm add @ai-sdk/mcp
yarn add @ai-sdk/mcp
bun add @ai-sdk/mcp
```

## Health

**Score 80/100 (A)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score; popular repo.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.0.73 |
| Published | 2026-10-09 |
| First published | 2025-10-24 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=22 |
| Dependencies | 4 |
| Unpacked size | 768.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 27088 |
| Maintainers | vercel-release-bot, matheuss, matt.straka |
| Keywords | ai, mcp |

## Links

- npm: https://www.npmjs.com/package/@ai-sdk/mcp
- Repository: https://github.com/vercel/ai
- Homepage: https://ai-sdk.dev/docs
- Issues: https://github.com/vercel/ai/issues
- npm.io page: https://npm.io/package/@ai-sdk/mcp

## Dependencies (4)

- [cross-spawn](https://npm.io/package/cross-spawn.md) ^7.0.6
- [pkce-challenge](https://npm.io/package/pkce-challenge.md) ^5.0.1
- [@ai-sdk/provider](https://npm.io/package/@ai-sdk/provider.md) 4.0.26
- [@ai-sdk/provider-utils](https://npm.io/package/@ai-sdk/provider-utils.md) 5.0.58

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 2.0.73 (latest) — 2026-10-09
- 1.0.92 (ai-v6) — 2026-10-07
- 0.0.39 (ai-v5) — 2026-09-30
- 0.0.0-f1630544-20260811180950 (snapshot) — 2026-08-11
- 2.0.0-beta.67 (beta) — 2026-06-23
- 2.0.0-canary.65 (canary) — 2026-06-12
- 2.0.72 — 2026-10-08
- 2.0.71 — 2026-10-08
- 2.0.70 — 2026-10-07
- 2.0.69 — 2026-10-07
- 2.0.68 — 2026-10-06
- 1.0.91 — 2026-10-05
- 2.0.67 — 2026-10-05
- 2.0.66 — 2026-10-01
- 2.0.65 — 2026-09-30
- … 315 more at https://npm.io/package/@ai-sdk/mcp/versions

## README

# AI SDK - Model Context Protocol Client

The **Model Context Protocol (MCP) client** for the
[AI SDK](https://ai-sdk.dev/docs) lets you connect to MCP servers and use their
tools with AI SDK functions like `generateText` and `streamText`.

## Setup

The MCP client is available in the `@ai-sdk/mcp` module. You can install it with

```bash
npm i @ai-sdk/mcp ai zod
```

## Skill for Coding Agents

If you use coding agents such as Claude Code or Cursor, we highly recommend
adding the AI SDK skill to your repository:

```shell
npx skills add vercel/ai
```

## Usage

Create an MCP client with `createMCPClient()`, fetch the server tools with
`mcpClient.tools()`, and pass them to an AI SDK call:

```ts
import { createMCPClient } from '@ai-sdk/mcp';
import { generateText, isStepCount } from 'ai';

const mcpClient = await createMCPClient({
  transport: {
    type: 'http',
    url: 'https://your-server.com/mcp',
    headers: {
      Authorization: `Bearer ${process.env.MCP_API_KEY}`,
    },
  },
});

try {
  const tools = await mcpClient.tools();

  const { text } = await generateText({
    model: 'openai/gpt-6-astra',
    tools,
    stopWhen: isStepCount(10),
    prompt: 'Use the available tools to answer the user question.',
  });

  console.log(text);
} finally {
  await mcpClient.close();
}
```

The client converts MCP tool definitions into AI SDK tools, so model calls can
use them through the standard `tools` option.

## Experimental webhook events

Use `list`, `subscribe`, `refresh`, and `unsubscribe` through
`client.experimental_events` on the same `createMCPClient` instance. Configure
`experimental_events: { store }` with private durable storage shared with
`experimental_createMCPEventWebhook`, which you mount at your callback URL.

Subscription creation persists `delivery.url` and the generated signing secret
before contacting the server, enabling signed callback verification during the
subscribe request. The receiver verifies raw request bytes and checks event
envelopes before invoking your handler. Optional callbacks let your application
validate filters and event data. Agent execution and durable deduplication
remain application concerns; closing the client does not unsubscribe.

See the [MCP Events guide](https://ai-sdk.dev/docs/ai-sdk-core/mcp-events) and the
[local server/client example](../../examples/mcp/src/events/README.md).

## Protocol versions

The client supports legacy MCP protocol versions through the `initialize`
handshake and MCP `2026-07-28` through stateless protocol discovery. The
built-in stdio transport probes with `server/discover` and falls back to the
legacy handshake when connected to an older server.

Custom transports can opt into the same negotiation by setting
`supportsProtocolVersionDiscovery` to `true`. Modern requests include the
protocol version, client capabilities, and client information in `_meta`.

For streaming responses, close the MCP client when the stream finishes:

```ts
import { createMCPClient } from '@ai-sdk/mcp';
import { streamText } from 'ai';

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

const result = streamText({
  model: 'openai/gpt-6-astra',
  tools: await mcpClient.tools(),
  prompt: 'Use the available tools to answer the user question.',
  onEnd: async () => {
    await mcpClient.close();
  },
});

for await (const textPart of result.textStream) {
  process.stdout.write(textPart);
}
```

## Transports

HTTP is recommended for production deployments:

Session persistence applies only to legacy MCP protocol versions. MCP
`2026-07-28` is stateless and does not use session ids or cached initialize
results.

```ts
import { createMCPClient } from '@ai-sdk/mcp';

const savedSession = await loadMcpSession();
let currentSessionId = savedSession?.sessionId;

const mcpClient = await createMCPClient({
  transport: {
    type: 'http',
    url: 'https://your-server.com/mcp',
    initialSessionId: savedSession?.sessionId,
    initialProtocolVersion: savedSession?.initializeResult.protocolVersion,
    terminateSessionOnClose: false,
    onSessionIdChange: sessionId => {
      currentSessionId = sessionId;
    },
    onSessionExpired: sessionId => {
      if (currentSessionId === sessionId) {
        currentSessionId = undefined;
        void clearMcpSession();
      }
    },
  },
  initialInitializeResult: savedSession?.initializeResult,
});

if (currentSessionId) {
  await saveMcpSession({
    sessionId: currentSessionId,
    initializeResult: mcpClient.initializeResult,
  });
}
```

SSE is also supported for MCP servers that use Server-Sent Events:

```ts
const mcpClient = await createMCPClient({
  transport: {
    type: 'sse',
    url: 'https://your-server.com/sse',
  },
});
```

For local MCP servers, you can use stdio transport from the `@ai-sdk/mcp/mcp-stdio`
subpath:

```ts
import { createMCPClient } from '@ai-sdk/mcp';
import { Experimental_StdioMCPTransport } from '@ai-sdk/mcp/mcp-stdio';

const mcpClient = await createMCPClient({
  transport: new Experimental_StdioMCPTransport({
    command: 'node',
    args: ['server.js'],
  }),
});
```

## Documentation

Please check out the
[AI SDK MCP documentation](https://ai-sdk.dev/docs/ai-sdk-core/mcp-tools) for
more information.

Managed backends can implement `Experimental_MCPEventAdapter` and configure
`experimental_events: { adapter }` instead of a store. Its `createAdapter()`
method receives `{ transport }` metadata (HTTP/SSE type and URL, or
`{ type: 'custom' }` for custom transports) and returns `Experimental_MCPEventOperations`
bound to the authorized account. Catalog discovery still
uses the authenticated MCP transport; subscribe/get/list/unsubscribe delegate
to the backend, which owns renewal and webhook delivery. Managed clients do not
expose `refresh()`. See the [managed subscription reference](https://ai-sdk.dev/docs/reference/ai-sdk-core/mcp-events#managed-subscriptions).

---
_Source: https://npm.io/package/@ai-sdk/mcp · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
