ai 6.0.31 → 6.0.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/dist/index.js +12 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +12 -2
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +1 -1
- package/dist/internal/index.mjs +1 -1
- package/docs/00-introduction/index.mdx +76 -0
- package/docs/02-foundations/01-overview.mdx +43 -0
- package/docs/02-foundations/02-providers-and-models.mdx +163 -0
- package/docs/02-foundations/03-prompts.mdx +620 -0
- package/docs/02-foundations/04-tools.mdx +160 -0
- package/docs/02-foundations/05-streaming.mdx +62 -0
- package/docs/02-foundations/index.mdx +43 -0
- package/docs/02-getting-started/00-choosing-a-provider.mdx +110 -0
- package/docs/02-getting-started/01-navigating-the-library.mdx +85 -0
- package/docs/02-getting-started/02-nextjs-app-router.mdx +556 -0
- package/docs/02-getting-started/03-nextjs-pages-router.mdx +542 -0
- package/docs/02-getting-started/04-svelte.mdx +627 -0
- package/docs/02-getting-started/05-nuxt.mdx +566 -0
- package/docs/02-getting-started/06-nodejs.mdx +512 -0
- package/docs/02-getting-started/07-expo.mdx +766 -0
- package/docs/02-getting-started/08-tanstack-start.mdx +583 -0
- package/docs/02-getting-started/index.mdx +44 -0
- package/docs/03-agents/01-overview.mdx +96 -0
- package/docs/03-agents/02-building-agents.mdx +367 -0
- package/docs/03-agents/03-workflows.mdx +370 -0
- package/docs/03-agents/04-loop-control.mdx +350 -0
- package/docs/03-agents/05-configuring-call-options.mdx +286 -0
- package/docs/03-agents/index.mdx +40 -0
- package/docs/03-ai-sdk-core/01-overview.mdx +33 -0
- package/docs/03-ai-sdk-core/05-generating-text.mdx +600 -0
- package/docs/03-ai-sdk-core/10-generating-structured-data.mdx +662 -0
- package/docs/03-ai-sdk-core/15-tools-and-tool-calling.mdx +1102 -0
- package/docs/03-ai-sdk-core/16-mcp-tools.mdx +375 -0
- package/docs/03-ai-sdk-core/20-prompt-engineering.mdx +144 -0
- package/docs/03-ai-sdk-core/25-settings.mdx +198 -0
- package/docs/03-ai-sdk-core/30-embeddings.mdx +247 -0
- package/docs/03-ai-sdk-core/31-reranking.mdx +218 -0
- package/docs/03-ai-sdk-core/35-image-generation.mdx +341 -0
- package/docs/03-ai-sdk-core/36-transcription.mdx +173 -0
- package/docs/03-ai-sdk-core/37-speech.mdx +167 -0
- package/docs/03-ai-sdk-core/40-middleware.mdx +480 -0
- package/docs/03-ai-sdk-core/45-provider-management.mdx +349 -0
- package/docs/03-ai-sdk-core/50-error-handling.mdx +149 -0
- package/docs/03-ai-sdk-core/55-testing.mdx +218 -0
- package/docs/03-ai-sdk-core/60-telemetry.mdx +313 -0
- package/docs/03-ai-sdk-core/65-devtools.mdx +107 -0
- package/docs/03-ai-sdk-core/index.mdx +88 -0
- package/docs/04-ai-sdk-ui/01-overview.mdx +44 -0
- package/docs/04-ai-sdk-ui/02-chatbot.mdx +1313 -0
- package/docs/04-ai-sdk-ui/03-chatbot-message-persistence.mdx +535 -0
- package/docs/04-ai-sdk-ui/03-chatbot-resume-streams.mdx +263 -0
- package/docs/04-ai-sdk-ui/03-chatbot-tool-usage.mdx +682 -0
- package/docs/04-ai-sdk-ui/04-generative-user-interfaces.mdx +389 -0
- package/docs/04-ai-sdk-ui/05-completion.mdx +186 -0
- package/docs/04-ai-sdk-ui/08-object-generation.mdx +344 -0
- package/docs/04-ai-sdk-ui/20-streaming-data.mdx +397 -0
- package/docs/04-ai-sdk-ui/21-error-handling.mdx +190 -0
- package/docs/04-ai-sdk-ui/21-transport.mdx +174 -0
- package/docs/04-ai-sdk-ui/24-reading-ui-message-streams.mdx +104 -0
- package/docs/04-ai-sdk-ui/25-message-metadata.mdx +152 -0
- package/docs/04-ai-sdk-ui/50-stream-protocol.mdx +477 -0
- package/docs/04-ai-sdk-ui/index.mdx +64 -0
- package/docs/05-ai-sdk-rsc/01-overview.mdx +45 -0
- package/docs/05-ai-sdk-rsc/02-streaming-react-components.mdx +209 -0
- package/docs/05-ai-sdk-rsc/03-generative-ui-state.mdx +279 -0
- package/docs/05-ai-sdk-rsc/03-saving-and-restoring-states.mdx +105 -0
- package/docs/05-ai-sdk-rsc/04-multistep-interfaces.mdx +282 -0
- package/docs/05-ai-sdk-rsc/05-streaming-values.mdx +158 -0
- package/docs/05-ai-sdk-rsc/06-loading-state.mdx +273 -0
- package/docs/05-ai-sdk-rsc/08-error-handling.mdx +96 -0
- package/docs/05-ai-sdk-rsc/09-authentication.mdx +42 -0
- package/docs/05-ai-sdk-rsc/10-migrating-to-ui.mdx +722 -0
- package/docs/05-ai-sdk-rsc/index.mdx +58 -0
- package/docs/06-advanced/01-prompt-engineering.mdx +96 -0
- package/docs/06-advanced/02-stopping-streams.mdx +184 -0
- package/docs/06-advanced/03-backpressure.mdx +173 -0
- package/docs/06-advanced/04-caching.mdx +169 -0
- package/docs/06-advanced/05-multiple-streamables.mdx +68 -0
- package/docs/06-advanced/06-rate-limiting.mdx +60 -0
- package/docs/06-advanced/07-rendering-ui-with-language-models.mdx +213 -0
- package/docs/06-advanced/08-model-as-router.mdx +120 -0
- package/docs/06-advanced/09-multistep-interfaces.mdx +115 -0
- package/docs/06-advanced/09-sequential-generations.mdx +55 -0
- package/docs/06-advanced/10-vercel-deployment-guide.mdx +117 -0
- package/docs/06-advanced/index.mdx +11 -0
- package/docs/07-reference/01-ai-sdk-core/01-generate-text.mdx +2142 -0
- package/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx +3215 -0
- package/docs/07-reference/01-ai-sdk-core/03-generate-object.mdx +780 -0
- package/docs/07-reference/01-ai-sdk-core/04-stream-object.mdx +1140 -0
- package/docs/07-reference/01-ai-sdk-core/05-embed.mdx +190 -0
- package/docs/07-reference/01-ai-sdk-core/06-embed-many.mdx +171 -0
- package/docs/07-reference/01-ai-sdk-core/06-rerank.mdx +309 -0
- package/docs/07-reference/01-ai-sdk-core/10-generate-image.mdx +227 -0
- package/docs/07-reference/01-ai-sdk-core/11-transcribe.mdx +138 -0
- package/docs/07-reference/01-ai-sdk-core/12-generate-speech.mdx +214 -0
- package/docs/07-reference/01-ai-sdk-core/15-agent.mdx +203 -0
- package/docs/07-reference/01-ai-sdk-core/16-tool-loop-agent.mdx +449 -0
- package/docs/07-reference/01-ai-sdk-core/17-create-agent-ui-stream.mdx +148 -0
- package/docs/07-reference/01-ai-sdk-core/18-create-agent-ui-stream-response.mdx +168 -0
- package/docs/07-reference/01-ai-sdk-core/18-pipe-agent-ui-stream-to-response.mdx +144 -0
- package/docs/07-reference/01-ai-sdk-core/20-tool.mdx +196 -0
- package/docs/07-reference/01-ai-sdk-core/22-dynamic-tool.mdx +175 -0
- package/docs/07-reference/01-ai-sdk-core/23-create-mcp-client.mdx +410 -0
- package/docs/07-reference/01-ai-sdk-core/24-mcp-stdio-transport.mdx +68 -0
- package/docs/07-reference/01-ai-sdk-core/25-json-schema.mdx +94 -0
- package/docs/07-reference/01-ai-sdk-core/26-zod-schema.mdx +109 -0
- package/docs/07-reference/01-ai-sdk-core/27-valibot-schema.mdx +55 -0
- package/docs/07-reference/01-ai-sdk-core/28-output.mdx +342 -0
- package/docs/07-reference/01-ai-sdk-core/30-model-message.mdx +415 -0
- package/docs/07-reference/01-ai-sdk-core/31-ui-message.mdx +246 -0
- package/docs/07-reference/01-ai-sdk-core/32-validate-ui-messages.mdx +101 -0
- package/docs/07-reference/01-ai-sdk-core/33-safe-validate-ui-messages.mdx +113 -0
- package/docs/07-reference/01-ai-sdk-core/40-provider-registry.mdx +182 -0
- package/docs/07-reference/01-ai-sdk-core/42-custom-provider.mdx +121 -0
- package/docs/07-reference/01-ai-sdk-core/50-cosine-similarity.mdx +52 -0
- package/docs/07-reference/01-ai-sdk-core/60-wrap-language-model.mdx +59 -0
- package/docs/07-reference/01-ai-sdk-core/61-wrap-image-model.mdx +64 -0
- package/docs/07-reference/01-ai-sdk-core/65-language-model-v2-middleware.mdx +46 -0
- package/docs/07-reference/01-ai-sdk-core/66-extract-reasoning-middleware.mdx +68 -0
- package/docs/07-reference/01-ai-sdk-core/67-simulate-streaming-middleware.mdx +71 -0
- package/docs/07-reference/01-ai-sdk-core/68-default-settings-middleware.mdx +80 -0
- package/docs/07-reference/01-ai-sdk-core/69-add-tool-input-examples-middleware.mdx +155 -0
- package/docs/07-reference/01-ai-sdk-core/70-extract-json-middleware.mdx +147 -0
- package/docs/07-reference/01-ai-sdk-core/70-step-count-is.mdx +84 -0
- package/docs/07-reference/01-ai-sdk-core/71-has-tool-call.mdx +120 -0
- package/docs/07-reference/01-ai-sdk-core/75-simulate-readable-stream.mdx +94 -0
- package/docs/07-reference/01-ai-sdk-core/80-smooth-stream.mdx +145 -0
- package/docs/07-reference/01-ai-sdk-core/90-generate-id.mdx +43 -0
- package/docs/07-reference/01-ai-sdk-core/91-create-id-generator.mdx +89 -0
- package/docs/07-reference/01-ai-sdk-core/index.mdx +159 -0
- package/docs/07-reference/02-ai-sdk-ui/01-use-chat.mdx +446 -0
- package/docs/07-reference/02-ai-sdk-ui/02-use-completion.mdx +179 -0
- package/docs/07-reference/02-ai-sdk-ui/03-use-object.mdx +178 -0
- package/docs/07-reference/02-ai-sdk-ui/31-convert-to-model-messages.mdx +230 -0
- package/docs/07-reference/02-ai-sdk-ui/32-prune-messages.mdx +108 -0
- package/docs/07-reference/02-ai-sdk-ui/40-create-ui-message-stream.mdx +151 -0
- package/docs/07-reference/02-ai-sdk-ui/41-create-ui-message-stream-response.mdx +113 -0
- package/docs/07-reference/02-ai-sdk-ui/42-pipe-ui-message-stream-to-response.mdx +73 -0
- package/docs/07-reference/02-ai-sdk-ui/43-read-ui-message-stream.mdx +57 -0
- package/docs/07-reference/02-ai-sdk-ui/46-infer-ui-tools.mdx +99 -0
- package/docs/07-reference/02-ai-sdk-ui/47-infer-ui-tool.mdx +75 -0
- package/docs/07-reference/02-ai-sdk-ui/50-direct-chat-transport.mdx +333 -0
- package/docs/07-reference/02-ai-sdk-ui/index.mdx +89 -0
- package/docs/07-reference/03-ai-sdk-rsc/01-stream-ui.mdx +767 -0
- package/docs/07-reference/03-ai-sdk-rsc/02-create-ai.mdx +90 -0
- package/docs/07-reference/03-ai-sdk-rsc/03-create-streamable-ui.mdx +91 -0
- package/docs/07-reference/03-ai-sdk-rsc/04-create-streamable-value.mdx +48 -0
- package/docs/07-reference/03-ai-sdk-rsc/05-read-streamable-value.mdx +78 -0
- package/docs/07-reference/03-ai-sdk-rsc/06-get-ai-state.mdx +50 -0
- package/docs/07-reference/03-ai-sdk-rsc/07-get-mutable-ai-state.mdx +70 -0
- package/docs/07-reference/03-ai-sdk-rsc/08-use-ai-state.mdx +26 -0
- package/docs/07-reference/03-ai-sdk-rsc/09-use-actions.mdx +42 -0
- package/docs/07-reference/03-ai-sdk-rsc/10-use-ui-state.mdx +35 -0
- package/docs/07-reference/03-ai-sdk-rsc/11-use-streamable-value.mdx +46 -0
- package/docs/07-reference/03-ai-sdk-rsc/20-render.mdx +262 -0
- package/docs/07-reference/03-ai-sdk-rsc/index.mdx +67 -0
- package/docs/07-reference/04-stream-helpers/01-ai-stream.mdx +89 -0
- package/docs/07-reference/04-stream-helpers/02-streaming-text-response.mdx +79 -0
- package/docs/07-reference/04-stream-helpers/05-stream-to-response.mdx +108 -0
- package/docs/07-reference/04-stream-helpers/07-openai-stream.mdx +77 -0
- package/docs/07-reference/04-stream-helpers/08-anthropic-stream.mdx +79 -0
- package/docs/07-reference/04-stream-helpers/09-aws-bedrock-stream.mdx +91 -0
- package/docs/07-reference/04-stream-helpers/10-aws-bedrock-anthropic-stream.mdx +96 -0
- package/docs/07-reference/04-stream-helpers/10-aws-bedrock-messages-stream.mdx +96 -0
- package/docs/07-reference/04-stream-helpers/11-aws-bedrock-cohere-stream.mdx +93 -0
- package/docs/07-reference/04-stream-helpers/12-aws-bedrock-llama-2-stream.mdx +93 -0
- package/docs/07-reference/04-stream-helpers/13-cohere-stream.mdx +78 -0
- package/docs/07-reference/04-stream-helpers/14-google-generative-ai-stream.mdx +85 -0
- package/docs/07-reference/04-stream-helpers/15-hugging-face-stream.mdx +84 -0
- package/docs/07-reference/04-stream-helpers/16-langchain-adapter.mdx +98 -0
- package/docs/07-reference/04-stream-helpers/16-llamaindex-adapter.mdx +70 -0
- package/docs/07-reference/04-stream-helpers/17-mistral-stream.mdx +81 -0
- package/docs/07-reference/04-stream-helpers/18-replicate-stream.mdx +83 -0
- package/docs/07-reference/04-stream-helpers/19-inkeep-stream.mdx +80 -0
- package/docs/07-reference/04-stream-helpers/index.mdx +103 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-api-call-error.mdx +30 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-download-error.mdx +27 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-empty-response-body-error.mdx +24 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-argument-error.mdx +26 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-data-content-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-data-content.mdx +26 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-message-role-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-prompt-error.mdx +47 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-response-data-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-tool-approval-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-invalid-tool-input-error.mdx +27 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-json-parse-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-load-api-key-error.mdx +24 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-load-setting-error.mdx +24 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-message-conversion-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-content-generated-error.mdx +24 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-image-generated-error.mdx +36 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-object-generated-error.mdx +43 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-speech-generated-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-such-model-error.mdx +26 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-such-provider-error.mdx +28 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-such-tool-error.mdx +26 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-no-transcript-generated-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-retry-error.mdx +27 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-too-many-embedding-values-for-call-error.mdx +27 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-tool-call-not-found-for-approval-error.mdx +26 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-tool-call-repair-error.mdx +28 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-type-validation-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/ai-unsupported-functionality-error.mdx +25 -0
- package/docs/07-reference/05-ai-sdk-errors/index.mdx +38 -0
- package/docs/07-reference/index.mdx +34 -0
- package/docs/08-migration-guides/00-versioning.mdx +46 -0
- package/docs/08-migration-guides/24-migration-guide-6-0.mdx +823 -0
- package/docs/08-migration-guides/25-migration-guide-5-0-data.mdx +882 -0
- package/docs/08-migration-guides/26-migration-guide-5-0.mdx +3427 -0
- package/docs/08-migration-guides/27-migration-guide-4-2.mdx +99 -0
- package/docs/08-migration-guides/28-migration-guide-4-1.mdx +14 -0
- package/docs/08-migration-guides/29-migration-guide-4-0.mdx +1157 -0
- package/docs/08-migration-guides/36-migration-guide-3-4.mdx +14 -0
- package/docs/08-migration-guides/37-migration-guide-3-3.mdx +64 -0
- package/docs/08-migration-guides/38-migration-guide-3-2.mdx +46 -0
- package/docs/08-migration-guides/39-migration-guide-3-1.mdx +168 -0
- package/docs/08-migration-guides/index.mdx +22 -0
- package/docs/09-troubleshooting/01-azure-stream-slow.mdx +33 -0
- package/docs/09-troubleshooting/02-client-side-function-calls-not-invoked.mdx +22 -0
- package/docs/09-troubleshooting/03-server-actions-in-client-components.mdx +40 -0
- package/docs/09-troubleshooting/04-strange-stream-output.mdx +36 -0
- package/docs/09-troubleshooting/05-streamable-ui-errors.mdx +16 -0
- package/docs/09-troubleshooting/05-tool-invocation-missing-result.mdx +106 -0
- package/docs/09-troubleshooting/06-streaming-not-working-when-deployed.mdx +31 -0
- package/docs/09-troubleshooting/06-streaming-not-working-when-proxied.mdx +31 -0
- package/docs/09-troubleshooting/06-timeout-on-vercel.mdx +60 -0
- package/docs/09-troubleshooting/07-unclosed-streams.mdx +34 -0
- package/docs/09-troubleshooting/08-use-chat-failed-to-parse-stream.mdx +26 -0
- package/docs/09-troubleshooting/09-client-stream-error.mdx +25 -0
- package/docs/09-troubleshooting/10-use-chat-tools-no-response.mdx +32 -0
- package/docs/09-troubleshooting/11-use-chat-custom-request-options.mdx +149 -0
- package/docs/09-troubleshooting/12-typescript-performance-zod.mdx +46 -0
- package/docs/09-troubleshooting/12-use-chat-an-error-occurred.mdx +59 -0
- package/docs/09-troubleshooting/13-repeated-assistant-messages.mdx +73 -0
- package/docs/09-troubleshooting/14-stream-abort-handling.mdx +73 -0
- package/docs/09-troubleshooting/14-tool-calling-with-structured-outputs.mdx +48 -0
- package/docs/09-troubleshooting/15-abort-breaks-resumable-streams.mdx +55 -0
- package/docs/09-troubleshooting/15-stream-text-not-working.mdx +33 -0
- package/docs/09-troubleshooting/16-streaming-status-delay.mdx +63 -0
- package/docs/09-troubleshooting/17-use-chat-stale-body-data.mdx +141 -0
- package/docs/09-troubleshooting/18-ontoolcall-type-narrowing.mdx +66 -0
- package/docs/09-troubleshooting/19-unsupported-model-version.mdx +50 -0
- package/docs/09-troubleshooting/20-no-object-generated-content-filter.mdx +72 -0
- package/docs/09-troubleshooting/30-model-is-not-assignable-to-type.mdx +21 -0
- package/docs/09-troubleshooting/40-typescript-cannot-find-namespace-jsx.mdx +24 -0
- package/docs/09-troubleshooting/50-react-maximum-update-depth-exceeded.mdx +39 -0
- package/docs/09-troubleshooting/60-jest-cannot-find-module-ai-rsc.mdx +22 -0
- package/docs/09-troubleshooting/index.mdx +11 -0
- package/package.json +8 -4
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Model Context Protocol (MCP)
|
|
3
|
+
description: Learn how to connect to Model Context Protocol (MCP) servers and use their tools with AI SDK Core.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Model Context Protocol (MCP)
|
|
7
|
+
|
|
8
|
+
The AI SDK supports connecting to [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) servers to access their tools, resources, and prompts.
|
|
9
|
+
This enables your AI applications to discover and use capabilities across various services through a standardized interface.
|
|
10
|
+
|
|
11
|
+
<Note>
|
|
12
|
+
If you're using OpenAI's Responses API, you can also use the built-in
|
|
13
|
+
`openai.tools.mcp` tool, which provides direct MCP server integration without
|
|
14
|
+
needing to convert tools. See the [OpenAI provider
|
|
15
|
+
documentation](/providers/ai-sdk-providers/openai#mcp-tool) for details.
|
|
16
|
+
</Note>
|
|
17
|
+
|
|
18
|
+
## Initializing an MCP Client
|
|
19
|
+
|
|
20
|
+
We recommend using HTTP transport (like `StreamableHTTPClientTransport`) for production deployments. The stdio transport should only be used for connecting to local servers as it cannot be deployed to production environments.
|
|
21
|
+
|
|
22
|
+
Create an MCP client using one of the following transport options:
|
|
23
|
+
|
|
24
|
+
- **HTTP transport (Recommended)**: Either configure HTTP directly via the client using `transport: { type: 'http', ... }`, or use MCP's official TypeScript SDK `StreamableHTTPClientTransport`
|
|
25
|
+
- SSE (Server-Sent Events): An alternative HTTP-based transport
|
|
26
|
+
- `stdio`: For local development only. Uses standard input/output streams for local MCP servers
|
|
27
|
+
|
|
28
|
+
### HTTP Transport (Recommended)
|
|
29
|
+
|
|
30
|
+
For production deployments, we recommend using the HTTP transport. You can configure it directly on the client:
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import { createMCPClient } from '@ai-sdk/mcp';
|
|
34
|
+
|
|
35
|
+
const mcpClient = await createMCPClient({
|
|
36
|
+
transport: {
|
|
37
|
+
type: 'http',
|
|
38
|
+
url: 'https://your-server.com/mcp',
|
|
39
|
+
|
|
40
|
+
// optional: configure HTTP headers
|
|
41
|
+
headers: { Authorization: 'Bearer my-api-key' },
|
|
42
|
+
|
|
43
|
+
// optional: provide an OAuth client provider for automatic authorization
|
|
44
|
+
authProvider: myOAuthClientProvider,
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Alternatively, you can use `StreamableHTTPClientTransport` from MCP's official TypeScript SDK:
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import { createMCPClient } from '@ai-sdk/mcp';
|
|
53
|
+
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
54
|
+
|
|
55
|
+
const url = new URL('https://your-server.com/mcp');
|
|
56
|
+
const mcpClient = await createMCPClient({
|
|
57
|
+
transport: new StreamableHTTPClientTransport(url, {
|
|
58
|
+
sessionId: 'session_123',
|
|
59
|
+
}),
|
|
60
|
+
});
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### SSE Transport
|
|
64
|
+
|
|
65
|
+
SSE provides an alternative HTTP-based transport option. Configure it with a `type` and `url` property. You can also provide an `authProvider` for OAuth:
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
import { createMCPClient } from '@ai-sdk/mcp';
|
|
69
|
+
|
|
70
|
+
const mcpClient = await createMCPClient({
|
|
71
|
+
transport: {
|
|
72
|
+
type: 'sse',
|
|
73
|
+
url: 'https://my-server.com/sse',
|
|
74
|
+
|
|
75
|
+
// optional: configure HTTP headers
|
|
76
|
+
headers: { Authorization: 'Bearer my-api-key' },
|
|
77
|
+
|
|
78
|
+
// optional: provide an OAuth client provider for automatic authorization
|
|
79
|
+
authProvider: myOAuthClientProvider,
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Stdio Transport (Local Servers)
|
|
85
|
+
|
|
86
|
+
<Note type="warning">
|
|
87
|
+
The stdio transport should only be used for local servers.
|
|
88
|
+
</Note>
|
|
89
|
+
|
|
90
|
+
The Stdio transport can be imported from either the MCP SDK or the AI SDK:
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
import { createMCPClient } from '@ai-sdk/mcp';
|
|
94
|
+
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
|
|
95
|
+
// Or use the AI SDK's stdio transport:
|
|
96
|
+
// import { Experimental_StdioMCPTransport as StdioClientTransport } from '@ai-sdk/mcp/mcp-stdio';
|
|
97
|
+
|
|
98
|
+
const mcpClient = await createMCPClient({
|
|
99
|
+
transport: new StdioClientTransport({
|
|
100
|
+
command: 'node',
|
|
101
|
+
args: ['src/stdio/dist/server.js'],
|
|
102
|
+
}),
|
|
103
|
+
});
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Custom Transport
|
|
107
|
+
|
|
108
|
+
You can also bring your own transport by implementing the `MCPTransport` interface for specific requirements not covered by the standard transports.
|
|
109
|
+
|
|
110
|
+
<Note>
|
|
111
|
+
The client returned by the `createMCPClient` function is a
|
|
112
|
+
lightweight client intended for use in tool conversion. It currently does not
|
|
113
|
+
support all features of the full MCP client, such as: session
|
|
114
|
+
management, resumable streams, and receiving notifications.
|
|
115
|
+
|
|
116
|
+
Authorization via OAuth is supported when using the AI SDK MCP HTTP or SSE
|
|
117
|
+
transports by providing an `authProvider`.
|
|
118
|
+
|
|
119
|
+
</Note>
|
|
120
|
+
|
|
121
|
+
### Closing the MCP Client
|
|
122
|
+
|
|
123
|
+
After initialization, you should close the MCP client based on your usage pattern:
|
|
124
|
+
|
|
125
|
+
- For short-lived usage (e.g., single requests), close the client when the response is finished
|
|
126
|
+
- For long-running clients (e.g., command line apps), keep the client open but ensure it's closed when the application terminates
|
|
127
|
+
|
|
128
|
+
When streaming responses, you can close the client when the LLM response has finished. For example, when using `streamText`, you should use the `onFinish` callback:
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
const mcpClient = await createMCPClient({
|
|
132
|
+
// ...
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
const tools = await mcpClient.tools();
|
|
136
|
+
|
|
137
|
+
const result = await streamText({
|
|
138
|
+
model: __MODEL__,
|
|
139
|
+
tools,
|
|
140
|
+
prompt: 'What is the weather in Brooklyn, New York?',
|
|
141
|
+
onFinish: async () => {
|
|
142
|
+
await mcpClient.close();
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
When generating responses without streaming, you can use try/finally or cleanup functions in your framework:
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
let mcpClient: MCPClient | undefined;
|
|
151
|
+
|
|
152
|
+
try {
|
|
153
|
+
mcpClient = await createMCPClient({
|
|
154
|
+
// ...
|
|
155
|
+
});
|
|
156
|
+
} finally {
|
|
157
|
+
await mcpClient?.close();
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Using MCP Tools
|
|
162
|
+
|
|
163
|
+
The client's `tools` method acts as an adapter between MCP tools and AI SDK tools. It supports two approaches for working with tool schemas:
|
|
164
|
+
|
|
165
|
+
### Schema Discovery
|
|
166
|
+
|
|
167
|
+
With schema discovery, all tools offered by the server are automatically listed, and input parameter types are inferred based on the schemas provided by the server:
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
const tools = await mcpClient.tools();
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
This approach is simpler to implement and automatically stays in sync with server changes. However, you won't have TypeScript type safety during development, and all tools from the server will be loaded
|
|
174
|
+
|
|
175
|
+
### Schema Definition
|
|
176
|
+
|
|
177
|
+
For better type safety and control, you can define the tools and their input schemas explicitly in your client code:
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
import { z } from 'zod';
|
|
181
|
+
|
|
182
|
+
const tools = await mcpClient.tools({
|
|
183
|
+
schemas: {
|
|
184
|
+
'get-data': {
|
|
185
|
+
inputSchema: z.object({
|
|
186
|
+
query: z.string().describe('The data query'),
|
|
187
|
+
format: z.enum(['json', 'text']).optional(),
|
|
188
|
+
}),
|
|
189
|
+
},
|
|
190
|
+
// For tools with zero inputs, you should use an empty object:
|
|
191
|
+
'tool-with-no-args': {
|
|
192
|
+
inputSchema: z.object({}),
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
This approach provides full TypeScript type safety and IDE autocompletion, letting you catch parameter mismatches during development. When you define `schemas`, the client only pulls the explicitly defined tools, keeping your application focused on the tools it needs
|
|
199
|
+
|
|
200
|
+
### Typed Tool Outputs
|
|
201
|
+
|
|
202
|
+
When MCP servers return `structuredContent` (per the [MCP specification](https://modelcontextprotocol.io/specification/2025-06-18/server/tools#structured-content)), you can define an `outputSchema` to get typed tool results:
|
|
203
|
+
|
|
204
|
+
```typescript
|
|
205
|
+
import { z } from 'zod';
|
|
206
|
+
|
|
207
|
+
const tools = await mcpClient.tools({
|
|
208
|
+
schemas: {
|
|
209
|
+
'get-weather': {
|
|
210
|
+
inputSchema: z.object({
|
|
211
|
+
location: z.string(),
|
|
212
|
+
}),
|
|
213
|
+
// Define outputSchema for typed results
|
|
214
|
+
outputSchema: z.object({
|
|
215
|
+
temperature: z.number(),
|
|
216
|
+
conditions: z.string(),
|
|
217
|
+
humidity: z.number(),
|
|
218
|
+
}),
|
|
219
|
+
},
|
|
220
|
+
},
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
const result = await tools['get-weather'].execute(
|
|
224
|
+
{ location: 'New York' },
|
|
225
|
+
{ messages: [], toolCallId: 'weather-1' },
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
console.log(`Temperature: ${result.temperature}°C`);
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
When `outputSchema` is provided:
|
|
232
|
+
|
|
233
|
+
- The client extracts `structuredContent` from the tool result
|
|
234
|
+
- The output is validated against your schema at runtime
|
|
235
|
+
- You get full TypeScript type safety for the result
|
|
236
|
+
|
|
237
|
+
If the server doesn't return `structuredContent`, the client falls back to parsing JSON from the text content. If neither is available or validation fails, an error is thrown.
|
|
238
|
+
|
|
239
|
+
<Note>
|
|
240
|
+
Without `outputSchema`, the tool returns the raw `CallToolResult` object
|
|
241
|
+
containing `content` and optional `isError` fields.
|
|
242
|
+
</Note>
|
|
243
|
+
|
|
244
|
+
## Using MCP Resources
|
|
245
|
+
|
|
246
|
+
According to the [MCP specification](https://modelcontextprotocol.io/docs/learn/server-concepts#resources), resources are **application-driven** data sources that provide context to the model. Unlike tools (which are model-controlled), your application decides when to fetch and pass resources as context.
|
|
247
|
+
|
|
248
|
+
The MCP client provides three methods for working with resources:
|
|
249
|
+
|
|
250
|
+
### Listing Resources
|
|
251
|
+
|
|
252
|
+
List all available resources from the MCP server:
|
|
253
|
+
|
|
254
|
+
```typescript
|
|
255
|
+
const resources = await mcpClient.listResources();
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
### Reading Resource Contents
|
|
259
|
+
|
|
260
|
+
Read the contents of a specific resource by its URI:
|
|
261
|
+
|
|
262
|
+
```typescript
|
|
263
|
+
const resourceData = await mcpClient.readResource({
|
|
264
|
+
uri: 'file:///example/document.txt',
|
|
265
|
+
});
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
### Listing Resource Templates
|
|
269
|
+
|
|
270
|
+
Resource templates are dynamic URI patterns that allow flexible queries. List all available templates:
|
|
271
|
+
|
|
272
|
+
```typescript
|
|
273
|
+
const templates = await mcpClient.listResourceTemplates();
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
## Using MCP Prompts
|
|
277
|
+
|
|
278
|
+
<Note type="warning">
|
|
279
|
+
MCP Prompts is an experimental feature and may change in the future.
|
|
280
|
+
</Note>
|
|
281
|
+
|
|
282
|
+
According to the MCP specification, prompts are user-controlled templates that servers expose for clients to list and retrieve with optional arguments.
|
|
283
|
+
|
|
284
|
+
### Listing Prompts
|
|
285
|
+
|
|
286
|
+
```typescript
|
|
287
|
+
const prompts = await mcpClient.experimental_listPrompts();
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
### Getting a Prompt
|
|
291
|
+
|
|
292
|
+
Retrieve prompt messages, optionally passing arguments defined by the server:
|
|
293
|
+
|
|
294
|
+
```typescript
|
|
295
|
+
const prompt = await mcpClient.experimental_getPrompt({
|
|
296
|
+
name: 'code_review',
|
|
297
|
+
arguments: { code: 'function add(a, b) { return a + b; }' },
|
|
298
|
+
});
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
## Handling Elicitation Requests
|
|
302
|
+
|
|
303
|
+
Elicitation is a mechanism where MCP servers can request additional information from the client during tool execution. For example, a server might need user input to complete a registration form or confirmation for a sensitive operation.
|
|
304
|
+
|
|
305
|
+
<Note type="warning">
|
|
306
|
+
It is up to the client application to handle elicitation requests properly.
|
|
307
|
+
The MCP client simply surfaces these requests from the server to your
|
|
308
|
+
application code.
|
|
309
|
+
</Note>
|
|
310
|
+
|
|
311
|
+
### Enabling Elicitation Support
|
|
312
|
+
|
|
313
|
+
To enable elicitation, you need to advertise the capability when creating the MCP client:
|
|
314
|
+
|
|
315
|
+
```typescript
|
|
316
|
+
const mcpClient = await createMCPClient({
|
|
317
|
+
transport: {
|
|
318
|
+
type: 'sse',
|
|
319
|
+
url: 'https://your-server.com/sse',
|
|
320
|
+
},
|
|
321
|
+
capabilities: {
|
|
322
|
+
elicitation: {},
|
|
323
|
+
},
|
|
324
|
+
});
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
### Registering an Elicitation Handler
|
|
328
|
+
|
|
329
|
+
Use the `onElicitationRequest` method to register a handler that will be called when the server requests input:
|
|
330
|
+
|
|
331
|
+
```typescript
|
|
332
|
+
import { ElicitationRequestSchema } from '@ai-sdk/mcp';
|
|
333
|
+
|
|
334
|
+
mcpClient.onElicitationRequest(ElicitationRequestSchema, async request => {
|
|
335
|
+
// request.params.message: A message describing what input is needed
|
|
336
|
+
// request.params.requestedSchema: JSON schema defining the expected input structure
|
|
337
|
+
|
|
338
|
+
// Get input from the user (implement according to your application's needs)
|
|
339
|
+
const userInput = await getInputFromUser(
|
|
340
|
+
request.params.message,
|
|
341
|
+
request.params.requestedSchema,
|
|
342
|
+
);
|
|
343
|
+
|
|
344
|
+
// Return the result with one of three actions:
|
|
345
|
+
return {
|
|
346
|
+
action: 'accept', // or 'decline' or 'cancel'
|
|
347
|
+
content: userInput, // only required when action is 'accept'
|
|
348
|
+
};
|
|
349
|
+
});
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
### Elicitation Response Actions
|
|
353
|
+
|
|
354
|
+
Your handler must return an object with an `action` field that can be one of:
|
|
355
|
+
|
|
356
|
+
- `'accept'`: User provided the requested information. Must include `content` with the data.
|
|
357
|
+
- `'decline'`: User chose not to provide the information.
|
|
358
|
+
- `'cancel'`: User cancelled the operation entirely.
|
|
359
|
+
|
|
360
|
+
## Examples
|
|
361
|
+
|
|
362
|
+
You can see MCP in action in the following examples:
|
|
363
|
+
|
|
364
|
+
<ExampleLinks
|
|
365
|
+
examples={[
|
|
366
|
+
{
|
|
367
|
+
title: 'Learn to use MCP tools in Node.js',
|
|
368
|
+
link: '/cookbook/node/mcp-tools',
|
|
369
|
+
},
|
|
370
|
+
{
|
|
371
|
+
title: 'Learn to handle MCP elicitation requests in Node.js',
|
|
372
|
+
link: '/cookbook/node/mcp-elicitation',
|
|
373
|
+
},
|
|
374
|
+
]}
|
|
375
|
+
/>
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Prompt Engineering
|
|
3
|
+
description: Learn how to develop prompts with AI SDK Core.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Prompt Engineering
|
|
7
|
+
|
|
8
|
+
## Tips
|
|
9
|
+
|
|
10
|
+
### Prompts for Tools
|
|
11
|
+
|
|
12
|
+
When you create prompts that include tools, getting good results can be tricky as the number and complexity of your tools increases.
|
|
13
|
+
|
|
14
|
+
Here are a few tips to help you get the best results:
|
|
15
|
+
|
|
16
|
+
1. Use a model that is strong at tool calling, such as `gpt-5` or `gpt-4.1`. Weaker models will often struggle to call tools effectively and flawlessly.
|
|
17
|
+
1. Keep the number of tools low, e.g. to 5 or less.
|
|
18
|
+
1. Keep the complexity of the tool parameters low. Complex Zod schemas with many nested and optional elements, unions, etc. can be challenging for the model to work with.
|
|
19
|
+
1. Use semantically meaningful names for your tools, parameters, parameter properties, etc. The more information you pass to the model, the better it can understand what you want.
|
|
20
|
+
1. Add `.describe("...")` to your Zod schema properties to give the model hints about what a particular property is for.
|
|
21
|
+
1. When the output of a tool might be unclear to the model and there are dependencies between tools, use the `description` field of a tool to provide information about the output of the tool execution.
|
|
22
|
+
1. You can include example input/outputs of tool calls in your prompt to help the model understand how to use the tools. Keep in mind that the tools work with JSON objects, so the examples should use JSON.
|
|
23
|
+
|
|
24
|
+
In general, the goal should be to give the model all information it needs in a clear way.
|
|
25
|
+
|
|
26
|
+
### Tool & Structured Data Schemas
|
|
27
|
+
|
|
28
|
+
The mapping from Zod schemas to LLM inputs (typically JSON schema) is not always straightforward, since the mapping is not one-to-one.
|
|
29
|
+
|
|
30
|
+
#### Zod Dates
|
|
31
|
+
|
|
32
|
+
Zod expects JavaScript Date objects, but models return dates as strings.
|
|
33
|
+
You can specify and validate the date format using `z.string().datetime()` or `z.string().date()`,
|
|
34
|
+
and then use a Zod transformer to convert the string to a Date object.
|
|
35
|
+
|
|
36
|
+
```ts highlight="7-10"
|
|
37
|
+
const result = await generateObject({
|
|
38
|
+
model: __MODEL__,
|
|
39
|
+
schema: z.object({
|
|
40
|
+
events: z.array(
|
|
41
|
+
z.object({
|
|
42
|
+
event: z.string(),
|
|
43
|
+
date: z
|
|
44
|
+
.string()
|
|
45
|
+
.date()
|
|
46
|
+
.transform(value => new Date(value)),
|
|
47
|
+
}),
|
|
48
|
+
),
|
|
49
|
+
}),
|
|
50
|
+
prompt: 'List 5 important events from the year 2000.',
|
|
51
|
+
});
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
#### Optional Parameters
|
|
55
|
+
|
|
56
|
+
When working with tools that have optional parameters, you may encounter compatibility issues with certain providers that use strict schema validation.
|
|
57
|
+
|
|
58
|
+
<Note>
|
|
59
|
+
This is particularly relevant for OpenAI models with structured outputs
|
|
60
|
+
(strict mode).
|
|
61
|
+
</Note>
|
|
62
|
+
|
|
63
|
+
For maximum compatibility, optional parameters should use `.nullable()` instead of `.optional()`:
|
|
64
|
+
|
|
65
|
+
```ts highlight="6,7,16,17"
|
|
66
|
+
// This may fail with strict schema validation
|
|
67
|
+
const failingTool = tool({
|
|
68
|
+
description: 'Execute a command',
|
|
69
|
+
inputSchema: z.object({
|
|
70
|
+
command: z.string(),
|
|
71
|
+
workdir: z.string().optional(), // This can cause errors
|
|
72
|
+
timeout: z.string().optional(),
|
|
73
|
+
}),
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// This works with strict schema validation
|
|
77
|
+
const workingTool = tool({
|
|
78
|
+
description: 'Execute a command',
|
|
79
|
+
inputSchema: z.object({
|
|
80
|
+
command: z.string(),
|
|
81
|
+
workdir: z.string().nullable(), // Use nullable instead
|
|
82
|
+
timeout: z.string().nullable(),
|
|
83
|
+
}),
|
|
84
|
+
});
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
#### Temperature Settings
|
|
88
|
+
|
|
89
|
+
For tool calls and object generation, it's recommended to use `temperature: 0` to ensure deterministic and consistent results:
|
|
90
|
+
|
|
91
|
+
```ts highlight="3"
|
|
92
|
+
const result = await generateText({
|
|
93
|
+
model: __MODEL__,
|
|
94
|
+
temperature: 0, // Recommended for tool calls
|
|
95
|
+
tools: {
|
|
96
|
+
myTool: tool({
|
|
97
|
+
description: 'Execute a command',
|
|
98
|
+
inputSchema: z.object({
|
|
99
|
+
command: z.string(),
|
|
100
|
+
}),
|
|
101
|
+
}),
|
|
102
|
+
},
|
|
103
|
+
prompt: 'Execute the ls command',
|
|
104
|
+
});
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Lower temperature values reduce randomness in model outputs, which is particularly important when the model needs to:
|
|
108
|
+
|
|
109
|
+
- Generate structured data with specific formats
|
|
110
|
+
- Make precise tool calls with correct parameters
|
|
111
|
+
- Follow strict schemas consistently
|
|
112
|
+
|
|
113
|
+
## Debugging
|
|
114
|
+
|
|
115
|
+
### Inspecting Warnings
|
|
116
|
+
|
|
117
|
+
Not all providers support all AI SDK features.
|
|
118
|
+
Providers either throw exceptions or return warnings when they do not support a feature.
|
|
119
|
+
To check if your prompt, tools, and settings are handled correctly by the provider, you can check the call warnings:
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
const result = await generateText({
|
|
123
|
+
model: __MODEL__,
|
|
124
|
+
prompt: 'Hello, world!',
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
console.log(result.warnings);
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### HTTP Request Bodies
|
|
131
|
+
|
|
132
|
+
You can inspect the raw HTTP request bodies for models that expose them, e.g. [OpenAI](/providers/ai-sdk-providers/openai).
|
|
133
|
+
This allows you to inspect the exact payload that is sent to the model provider in the provider-specific way.
|
|
134
|
+
|
|
135
|
+
Request bodies are available via the `request.body` property of the response:
|
|
136
|
+
|
|
137
|
+
```ts highlight="6"
|
|
138
|
+
const result = await generateText({
|
|
139
|
+
model: __MODEL__,
|
|
140
|
+
prompt: 'Hello, world!',
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
console.log(result.request.body);
|
|
144
|
+
```
|