> ## Documentation Index
> Fetch the complete documentation index at: https://docs.secapi.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Connect SEC API to Vercel AI SDK

> Load SEC API MCP tools into a server-side Vercel AI SDK route, close the client cleanly, and verify a source-backed filing result.

Use `@ai-sdk/mcp` when a server-side AI SDK route needs SEC API tools. Create
the client in a route handler, server action, worker, or backend service. The
API key authorizes access to SEC API, so keep it in the hosting environment or
secret manager instead of sending it to a client component.

## Make one server-side filing call

```bash theme={null}
npm install ai @ai-sdk/mcp
```

Load the tools and verify `filings.latest` before using a language model:

```typescript theme={null}
import { createMCPClient } from "@ai-sdk/mcp";

let mcp: Awaited<ReturnType<typeof createMCPClient>> | undefined;

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

  const tools = await mcp.tools();
  const filing = await tools["filings.latest"].execute(
    {
      ticker: "AAPL",
      form: "10-K",
    },
    { messages: [], toolCallId: "verify-filings-latest" },
  );
  console.log(filing);
} finally {
  await mcp?.close();
}
```

The AI SDK HTTP transport accepts request headers and exposes MCP tools as an
AI SDK tool set. Closing the MCP client is part of the lifecycle: it releases
the connection after a one-shot request, including an error path. For a
streaming route, keep the client alive for the request and close it when the
stream settles; do not create a fresh client for each token.

The [AI SDK MCP guide](https://ai-sdk.dev/docs/ai-sdk-core/mcp-tools) is the
source for transport and close behavior when you upgrade `@ai-sdk/mcp`.

## Add it to a model turn after it works

Once the direct filing call works, pass `await mcp.tools()` to `generateText` or
`streamText` in the same server-side request lifecycle. Use a small, reviewed
tool subset where your application does not need the whole catalog. Put issuer,
form, date range, and list-limit constraints in your application policy before
the model begins a multi-step plan.

## Keep the filing trail in the response

`filings.latest` provides `accessionNumber`, `filingDate`, and `filingUrl` for
the source record. The MCP envelope includes `requestId` and `traceparent`.
Persist those fields beside an answer, cache entry, or audit record. Preserve
provenance, freshness, and source metadata from search and extraction tools;
do not collapse them into unsupported model prose.

## Errors and limits

Catch errors around both client creation and tool execution. Repair `401`
credentials, resolve `402` account state, and on `429` or temporary `503`
honor `Retry-After` before a bounded retry. For `mcp_tool_timeout`, lower the
scope or result limit. Log the MCP JSON-RPC error, SEC API `error.data.code`,
and request ID without logging the API key.

## Next step

Use the verified tool set in your server-side AI SDK handler, and require the
source filing identity and request metadata in every SEC API-backed response.
