{"_id":"@ai-sdk/mcp@2.0.78","bugs":{"url":"https://github.com/vercel/ai/issues"},"dist":{"shasum":"7a9ebed400f6429713f7bd202720625a8022267e","tarball":"http://registry-npm.ywwl.com/@ai-sdk/mcp/download/@ai-sdk/mcp-2.0.78.tgz","fileCount":39,"integrity":"sha512-8kwCNA/+WXgYJCoEWl/NDlV5e8bkIjAxhBT3jWD7zrdI47Uep8AV5fsGqy7fzl4xs6fgNri4iqGbZQkvyTuOPA==","signatures":[{"sig":"MEQCIHqIPeSxpHPitfEqpenIr74JAG5IL060K04grk+nVz7YAiBX107lj+x0uzrWlshBcGsFZRtua8vYUhAif1lZj993hA==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"},{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQDh9L6HavMJdi3xpw2bFJibuRa3hNw0pgt9nWq+OFNETwIhAMQgdMLko7Wb3XCouC4BcJnc6ZUz9H10AvO9Pp6jH+V6"}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/@ai-sdk%2fmcp@2.0.78","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":788080,"size":175640,"noattachment":false,"key":"/@ai-sdk/mcp/-/@ai-sdk/mcp-2.0.78.tgz"},"main":"./dist/index.js","name":"@ai-sdk/mcp","type":"module","types":"./dist/index.d.ts","engines":{"node":">=22"},"exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.js","default":"./dist/index.js"},"./mcp-stdio":{"types":"./dist/mcp-stdio/index.d.ts","import":"./dist/mcp-stdio/index.js","default":"./dist/mcp-stdio/index.js"},"./package.json":"./package.json"},"license":"Apache-2.0","scripts":{"test":"pnpm test:node && pnpm test:edge","build":"pnpm clean && tsdown","clean":"del-cli *.tsbuildinfo","test:edge":"vitest --config vitest.edge.config.js --run","test:node":"vitest --config vitest.node.config.js --run","test:watch":"vitest --config vitest.node.config.js","type-check":"tsc --build","build:watch":"pnpm clean && tsdown --watch","test:update":"pnpm test:node -u"},"version":"2.0.78","_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"f54ba994-4951-4247-8d06-59680158fd55"}},"homepage":"https://ai-sdk.dev/docs","keywords":["ai","mcp"],"repository":{"url":"https://github.com/vercel/ai","type":"git","directory":"packages/mcp"},"directories":{},"maintainers":[{"name":"vercel-release-bot","email":"infra+release@vercel.com"},{"name":"matheuss","email":"matheus.frndes@gmail.com"},{"name":"matt.straka","email":"matt.j.straka@gmail.com"}],"sideEffects":false,"_nodeVersion":"24.21.0","dependencies":{"cross-spawn":"^7.0.6","pkce-challenge":"^5.0.1","@ai-sdk/provider":"4.0.29","@ai-sdk/provider-utils":"5.0.62"},"publishConfig":{"access":"public","provenance":true},"_hasShrinkwrap":false,"devDependencies":{"zod":"3.25.76","tsdown":"^0.23.0","vitest":"^4.1.11","typescript":"6.0.3","@types/node":"22.19.19","@types/cross-spawn":"^6.0.6","@ai-sdk/test-server":"2.0.4","@vercel/ai-tsconfig":"0.0.0"},"peerDependencies":{"zod":"^3.25.76 || ^4.1.8"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/mcp_2.0.78_1792181463251_0.3167116119949913"},"readme":"# AI SDK - Model Context Protocol Client\n\nThe **Model Context Protocol (MCP) client** for the\n[AI SDK](https://ai-sdk.dev/docs) lets you connect to MCP servers and use their\ntools with AI SDK functions like `generateText` and `streamText`.\n\n## Setup\n\nThe MCP client is available in the `@ai-sdk/mcp` module. You can install it with\n\n```bash\nnpm i @ai-sdk/mcp ai zod\n```\n\n## Skill for Coding Agents\n\nIf you use coding agents such as Claude Code or Cursor, we highly recommend\nadding the AI SDK skill to your repository:\n\n```shell\nnpx skills add vercel/ai\n```\n\n## Usage\n\nCreate an MCP client with `createMCPClient()`, fetch the server tools with\n`mcpClient.tools()`, and pass them to an AI SDK call:\n\n```ts\nimport { createMCPClient } from '@ai-sdk/mcp';\nimport { generateText, isStepCount } from 'ai';\n\nconst mcpClient = await createMCPClient({\n  transport: {\n    type: 'http',\n    url: 'https://your-server.com/mcp',\n    headers: {\n      Authorization: `Bearer ${process.env.MCP_API_KEY}`,\n    },\n  },\n});\n\ntry {\n  const tools = await mcpClient.tools();\n\n  const { text } = await generateText({\n    model: 'openai/gpt-6-astra',\n    tools,\n    stopWhen: isStepCount(10),\n    prompt: 'Use the available tools to answer the user question.',\n  });\n\n  console.log(text);\n} finally {\n  await mcpClient.close();\n}\n```\n\nThe client converts MCP tool definitions into AI SDK tools, so model calls can\nuse them through the standard `tools` option.\n\n## Experimental webhook events\n\nUse `list`, `subscribe`, `refresh`, and `unsubscribe` through\n`client.experimental_events` on the same `createMCPClient` instance. Configure\n`experimental_events: { store }` with private durable storage shared with\n`experimental_createMCPEventWebhook`, which you mount at your callback URL.\n\nSubscription creation persists `delivery.url` and the generated signing secret\nbefore contacting the server, enabling signed callback verification during the\nsubscribe request. The receiver verifies raw request bytes and checks event\nenvelopes before invoking your handler. Optional callbacks let your application\nvalidate filters and event data. Agent execution and durable deduplication\nremain application concerns; closing the client does not unsubscribe.\n\nSee the [MCP Events guide](https://ai-sdk.dev/docs/ai-sdk-core/mcp-events) and the\n[local server/client example](../../examples/mcp/src/events/README.md).\n\n## Protocol versions\n\nThe client supports legacy MCP protocol versions through the `initialize`\nhandshake and MCP `2026-07-28` through stateless protocol discovery. The\nbuilt-in stdio transport probes with `server/discover` and falls back to the\nlegacy handshake when connected to an older server.\n\nCustom transports can opt into the same negotiation by setting\n`supportsProtocolVersionDiscovery` to `true`. Modern requests include the\nprotocol version, client capabilities, and client information in `_meta`.\n\nFor streaming responses, close the MCP client when the stream finishes:\n\n```ts\nimport { createMCPClient } from '@ai-sdk/mcp';\nimport { streamText } from 'ai';\n\nconst mcpClient = await createMCPClient({\n  transport: {\n    type: 'http',\n    url: 'https://your-server.com/mcp',\n  },\n});\n\nconst result = streamText({\n  model: 'openai/gpt-6-astra',\n  tools: await mcpClient.tools(),\n  prompt: 'Use the available tools to answer the user question.',\n  onEnd: async () => {\n    await mcpClient.close();\n  },\n});\n\nfor await (const textPart of result.textStream) {\n  process.stdout.write(textPart);\n}\n```\n\n## Transports\n\nHTTP is recommended for production deployments:\n\nSession persistence applies only to legacy MCP protocol versions. MCP\n`2026-07-28` is stateless and does not use session ids or cached initialize\nresults.\n\n```ts\nimport { createMCPClient } from '@ai-sdk/mcp';\n\nconst savedSession = await loadMcpSession();\nlet currentSessionId = savedSession?.sessionId;\n\nconst mcpClient = await createMCPClient({\n  transport: {\n    type: 'http',\n    url: 'https://your-server.com/mcp',\n    initialSessionId: savedSession?.sessionId,\n    initialProtocolVersion: savedSession?.initializeResult.protocolVersion,\n    terminateSessionOnClose: false,\n    onSessionIdChange: sessionId => {\n      currentSessionId = sessionId;\n    },\n    onSessionExpired: sessionId => {\n      if (currentSessionId === sessionId) {\n        currentSessionId = undefined;\n        void clearMcpSession();\n      }\n    },\n  },\n  initialInitializeResult: savedSession?.initializeResult,\n});\n\nif (currentSessionId) {\n  await saveMcpSession({\n    sessionId: currentSessionId,\n    initializeResult: mcpClient.initializeResult,\n  });\n}\n```\n\nSSE is also supported for MCP servers that use Server-Sent Events:\n\n```ts\nconst mcpClient = await createMCPClient({\n  transport: {\n    type: 'sse',\n    url: 'https://your-server.com/sse',\n  },\n});\n```\n\nFor local MCP servers, you can use stdio transport from the `@ai-sdk/mcp/mcp-stdio`\nsubpath:\n\n```ts\nimport { createMCPClient } from '@ai-sdk/mcp';\nimport { Experimental_StdioMCPTransport } from '@ai-sdk/mcp/mcp-stdio';\n\nconst mcpClient = await createMCPClient({\n  transport: new Experimental_StdioMCPTransport({\n    command: 'node',\n    args: ['server.js'],\n  }),\n});\n```\n\n## Documentation\n\nPlease check out the\n[AI SDK MCP documentation](https://ai-sdk.dev/docs/ai-sdk-core/mcp-tools) for\nmore information.\n\nManaged backends can implement `Experimental_MCPEventAdapter` and configure\n`experimental_events: { adapter }` instead of a store. Its `createAdapter()`\nmethod receives `{ transport }` metadata (HTTP/SSE type and URL, or\n`{ type: 'custom' }` for custom transports) and returns `Experimental_MCPEventOperations`\nbound to the authorized account. Catalog discovery still\nuses the authenticated MCP transport; subscribe/get/list/unsubscribe delegate\nto the backend, which owns renewal and webhook delivery. Managed clients do not\nexpose `refresh()`. See the [managed subscription reference](https://ai-sdk.dev/docs/reference/ai-sdk-core/mcp-events#managed-subscriptions).\n","publish_time":1792181463368,"_cnpm_publish_time":1792181463368,"dist-tags":{"ai-v5":"0.0.43","ai-v6":"1.0.101","beta":"2.0.0-beta.67","canary":"2.0.0-canary.65","latest":"2.0.91","snapshot":"0.0.0-f1630544-20260811180950"}}