Skip to main content

Install SEC API in Vercel AI SDK

Use @ai-sdk/mcp to register SEC API as a streamable HTTP MCP server with an x-api-key header. The returned tools can be passed to your AI SDK model call.

Prerequisites

  • SEC API key. Sign up at secapi.ai. Set SECAPI_API_KEY.
  • Node.js plus ai, @ai-sdk/mcp, and a model provider package.

Install

npm install ai @ai-sdk/anthropic @ai-sdk/mcp
import { createMCPClient } from "@ai-sdk/mcp";
import { generateText } from "ai";
import { anthropic } from "@ai-sdk/anthropic";

const mcp = await createMCPClient({
  transport: {
    type: "http",
    url: "https://api.secapi.ai/mcp",
    headers: { "x-api-key": process.env.SECAPI_API_KEY! },
  },
});

try {
  const tools = await mcp.tools();
  const { text } = await generateText({
    model: anthropic("claude-sonnet-4-5"),
    tools,
    prompt: "Show me Apple's latest 8-K filing.",
  });
  console.log(text);
} finally {
  await mcp.close();
}

Verify

Run the snippet. The agent should call the filings.latest tool with ticker: AAPL, form: 8-K and print the filing URL plus a short summary.

Troubleshooting

  • 401 Unauthorizedx-api-key is missing or wrong. Sanity check: curl https://api.secapi.ai/v1/limits -H "x-api-key: $SECAPI_API_KEY".
  • 429 Too Many Requests — rate limit hit. See GET https://api.secapi.ai/v1/billing for your plan’s cap.
  • Process hangs after first call — always await mcp.close() in a finally block (or in onFinish and onError if you use streaming). Open SSE connections will keep the event loop alive.
  • Cannot find module '@ai-sdk/mcp' — install the MCP package or check your AI SDK version’s MCP client import path.

Streaming variant

For streamText, close the client in both callbacks:
const { textStream } = await streamText({
  model: anthropic("claude-sonnet-4-5"),
  tools: await mcp.tools(),
  prompt: "Show me Apple's latest 8-K filing.",
  onFinish: () => mcp.close(),
  onError: () => mcp.close(),
});

Next