# Connect Vercel AI SDK to SAM.gov data

To use SAM.gov data in the Vercel AI SDK, create a client with createMCPClient({ transport: { type: "http", url: "https://samgovapi.io/api/mcp", headers: { Authorization: "Bearer <key>" } } }) from @ai-sdk/mcp, pass await client.tools() to generateText or streamText, and close the client when the response ends.

The AI SDK is the common choice for TypeScript apps that answer users in a chat UI. Running the MCP client on the server side keeps the key out of the browser.

- Connects with: MCP
- MCP server: https://samgovapi.io/api/mcp
- REST base: https://samgovapi.io/api/v1
- Auth: Authorization: Bearer <key>, kept in SAMGOVAPI_API_KEY
- Tested: @ai-sdk/mcp 2.0.99 with ai 7.0.172: tools() returned every tool and the search tool executed (2026-11-13)

## Get a key

Free key at https://samgovapi.io/sign-up, or have the agent call start_signup and approve the link it shows you. Read process.env.SAMGOVAPI_API_KEY on the server only.

## Set up Vercel AI SDK

1. Install: createMCPClient lives in @ai-sdk/mcp, not in the ai package.

Terminal:

```bash
npm install ai @ai-sdk/mcp
```

2. Call a model with the tools: Close the client in a finally block, or in onEnd when streaming.

route.ts:

```ts
import { createMCPClient } from "@ai-sdk/mcp";
import { generateText, isStepCount } from "ai";

const mcp = await createMCPClient({
  transport: { type: "http", url: "https://samgovapi.io/api/mcp", headers: { Authorization: `Bearer ${process.env.SAMGOVAPI_API_KEY}` } },
});
try {
  const { text } = await generateText({
    model: "openai/gpt-5",
    tools: await mcp.tools(),
    stopWhen: isStepCount(5),
    prompt: "Is UEI ZXBCU6K5KL99 registered in SAM.gov, and when does its registration expire?",
  });
  console.log(text);
} finally {
  await mcp.close();
}
```

## Finish account setup once

Data tools unlock after the account answers its onboarding questions: ask the agent to set up your samgovapi account (get_onboarding_questions, then answer_onboarding). Free.

## Check that it works

Ask: "Is UEI ZXBCU6K5KL99 registered in SAM.gov, and when does its registration expire?"

Expected tool call:

```json
{"name":"registration_status","arguments":{"id":"ZXBCU6K5KL99","user_request":"check a registration and its expiry"}}
```

## Things to know

- AI SDK 7 calls the step limit isStepCount; version 5 and 6 code used stepCountIs.
- The model id is an example.

## Troubleshooting

- Edge runtime errors: Run the MCP client in a Node.js route; it holds a connection for the request.
- INVALID_API_KEY: The variable is not set in the deployment's environment.
- ONBOARDING_REQUIRED: Answer the onboarding questions once for the account.

## Questions

### Is createMCPClient still experimental?

No. experimental_createMCPClient remains as an alias in @ai-sdk/mcp.

## Official documentation

- AI SDK: MCP tools: https://ai-sdk.dev/docs/ai-sdk-core/mcp-tools

Checked 2026-11-13. All guides: https://samgovapi.io/agents.md

Independent. Not affiliated with, endorsed by or sponsored by GSA, SAM.gov or any U.S. government agency. Registering in SAM.gov is free at sam.gov.
