← Todas las conexiones

Conectar con Vercel AI SDK

Vercel AI SDK — NewAlgorithm tools loaded directly into your TypeScript agent.

Antes de empezar, crea tu cuenta gratuita.Crear cuenta

Este contenido está disponible en inglés.

¿Qué MCP vas a conectar? Los comandos de abajo cambian según tu elección.

  1. 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 format
    na_live_xxxxxxxxxxxxxxxxx
  2. 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. 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.local
    NA_API_KEY=na_live_xxxxxxxxxxxxxxxxx
  4. 4

    Connect and load the tools

    The client returns the tools already in the format that generateText and streamText expect:

    app/api/chat/route.ts
    import { 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. 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()))

    Resultado esperado: [ '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.

Detalles de autenticación y herramientas en la Documentación.