
Connexion avec Vercel AI SDK
Vercel AI SDK — NewAlgorithm tools loaded directly into your TypeScript agent.
Ce contenu est disponible en anglais.
Quel MCP allez-vous connecter ? Les commandes ci-dessous s'adaptent à votre choix.
- 1
Generate your API key
Sign in to app.newalgorithm.io with your account, open Keys & apps and generate a key. It is shown only once: copy it and store it somewhere safe.
Key formatna_live_xxxxxxxxxxxxxxxxx - 2
Install the dependencies
The AI SDK's MCP client uses the protocol's official transport:
npm install ai @ai-sdk/anthropic @modelcontextprotocol/sdk - 3
Store the key in the environment
In .env.local (and in the project's environment variables on Vercel). No NEXT_PUBLIC_ prefix — the key must never end up in the browser bundle.
.env.localNA_API_KEY=na_live_xxxxxxxxxxxxxxxxx - 4
Connect and load the tools
The client returns the tools already in the format that generateText and streamText expect:
app/api/chat/route.tsimport { anthropic } from "@ai-sdk/anthropic" import { experimental_createMCPClient as createMCPClient, generateText, stepCountIs } from "ai" import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js" const na = await createMCPClient({ transport: new StreamableHTTPClientTransport(new URL("https://totvs.mcp.newalgorithm.io/mcp"), { requestInit: { headers: { Authorization: `Bearer ${process.env.NA_API_KEY}` }, }, }), }) try { const { text } = await generateText({ model: anthropic("claude-opus-5"), tools: await na.tools(), stopWhen: stepCountIs(5), system: "Query NewAlgorithm before answering about SAP. Let the Manager Agent choose the Specialist.", prompt: "How is cost center allocation done in SAP S/4HANA?", }) console.log(text) } finally { await na.close() } - 5
Confirm
Before plugging it into the agent, print the list of tools — it's the quickest test that the key and the endpoint are right.
console.log(Object.keys(await na.tools()))Résultat attendu : [ 'na_search', 'na_route', 'na_specialists', 'na_version' ]
Close the client at the end of the request (na.close()) to avoid leaking connections in serverless environments. On AI SDK 4.x, replace stopWhen: stepCountIs(5) with maxSteps: 5.
Détails sur l'authentification et les outils dans la Documentation.