# Vercel AI SDK + Eterna MCP Crypto Trading Integration

> Use `createMCPClient` from `@ai-sdk/mcp` with `type: 'http'` pointed at `https://mcp.eterna.exchange/mcp` inside a Next.js Route Handler or Server Action. Pass `await mcpClient.tools()` directly to `streamText` or `generateText` to build custom AI trading copilots across Claude, GPT, and Gemini models.

- Canonical URL: https://ai.eterna.exchange/integrations/vercel-ai-sdk
- Last updated: November 23, 2026
- Author: Eterna Engineering (MCP & Agent Infrastructure Team)
- MCP endpoint: https://mcp.eterna.exchange/mcp

Building a web-based crypto copilot, Telegram Mini App, or internal desk dashboard in TypeScript? The Vercel AI SDK pairs cleanly with Eterna's remote MCP endpoint so you can stream reasoning, tool outputs, and approval cards straight to React.

## Key Facts

| Metric | Value | Notes |
|--------|-------|-------|
| SDK Package | ai + @ai-sdk/mcp | Native HTTP MCP transport |
| Model Flexibility | Model agnostic | Swap Claude, OpenAI, Gemini in 1 line |
| Server-Only Auth | OAuth 2.1 Bearer | Tokens stay server-side, never in browser |
| Latency | <200ms execution | Single sandbox round-trip per turn |

## 1. Connect `@ai-sdk/mcp` in a Next.js Server Route

Initialize the MCP client per authenticated user session on the server, pass the Eterna tools to `streamText`, and close the client in `onFinish`.

### app/api/trade-chat/route.ts

```typescript
import { streamText, stepCountIs } from "ai";
import { createMCPClient } from "@ai-sdk/mcp";
import { anthropic } from "@ai-sdk/anthropic";

export async function POST(req: Request) {
  const { messages } = await req.json();
  const accessToken = process.env.ETERNA_ACCESS_TOKEN;

  const mcpClient = await createMCPClient({
    transport: {
      type: "http",
      url: "https://mcp.eterna.exchange/mcp",
      headers: { Authorization: `Bearer ${accessToken}` },
    },
  });

  const tools = await mcpClient.tools();

  const result = streamText({
    model: anthropic("claude-sonnet-4-5"),
    system:
      "You are a risk-disciplined trading assistant connected to Eterna MCP. Always inspect balance and positions before proposing trades.",
    messages,
    tools,
    stopWhen: stepCountIs(5),
    onFinish: async () => {
      await mcpClient.close();
    },
  });

  return result.toUIMessageStreamResponse();
}
```

*Always bind per-user OAuth tokens server-side and enforce human confirmation UI before invoking order mutations.*

## 2. Production Patterns for Web Trading Copilots

Keep custody isolation and auditability intact when exposing an AI trading agent to end users.

- Use per-user OAuth 2.1 tokens via `ai-auth.eterna.exchange` so each user trades inside their own isolated sub-account.
- Cap `stopWhen: stepCountIs(5)` to avoid infinite retry loops during exchange volatility.
- Render structured trade-ticket confirmation cards in React before permitting the model to call `eterna.placeOrder`.
- Log every `execute_code` snippet and returned JSON object for post-trade auditability.

## Frequently Asked Questions

### Should I call Eterna MCP directly from the browser?

No. Keep MCP client sessions and OAuth access tokens inside Next.js Route Handlers, Server Actions, or a backend service.

### Does `@ai-sdk/mcp` work with Eterna's `execute_code` sandbox?

Yes. `mcpClient.tools()` exposes `search_sdk`, `search_examples`, and `execute_code` automatically with full Zod/JSON schemas.

## References & Primary Sources

- [Eterna AI Building Guide](https://ai.eterna.exchange/ai-trading-agent): Step-by-step architecture for production AI trading agents.
- [Eterna MCP Server Card](https://ai.eterna.exchange/.well-known/mcp/server-card.json): Machine-readable server metadata and transport info.

## Related Guides

- [How to Build an AI Crypto Trading Agent with MCP (Step-by-Step)](https://ai.eterna.exchange/ai-trading-agent)
- [LangChain & LangGraph MCP Crypto Trading Agent](https://ai.eterna.exchange/integrations/langchain-langgraph)
- [OpenAI Agents SDK & Codex MCP Crypto Trading](https://ai.eterna.exchange/integrations/openai-agents-sdk)
- [Running Multiple AI Trading Agents Safely with Isolated Sub-Accounts](https://ai.eterna.exchange/guides/multi-agent-crypto-portfolio-isolation)
