# Install Page Systems in an AI SDK agent

Two ways to give an [AI SDK](https://ai-sdk.dev) agent Page Systems tools: the `@pagesystems/ai-sdk` package, or the Page Systems MCP server through `@ai-sdk/mcp`. Both give the model the same calls: list the page types, read one type's schema, create a page, read the answer.

Requirements: `ai` 7 and Node 22 or newer. Tested with `ai` 7.0.137 and `@ai-sdk/mcp` 2.0.73.

## 1. Set your API key

Create a key in the [dashboard](https://app.pagesystems.ai/dashboard/keys) (it starts with `ap_live_` and is shown once) and set it where your agent runs. Never commit it.

```bash
export PAGE_SYSTEMS_API_KEY=ap_live_...
```

## 2a. The package

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

```ts
import { pageSystemsTools, getPageSystemsSkill } from '@pagesystems/ai-sdk';
import { generateText, isStepCount } from 'ai';

const skill = await getPageSystemsSkill(); // once, when the agent starts

const result = await generateText({
  model,
  system: [yourInstructions, skill].join('\n\n'),
  messages,
  tools: pageSystemsTools(),
  // Several steps: list_page_types, get_page_type_schema, then create_page.
  stopWhen: isStepCount(6),
});
```

`pageSystemsTools()` reads `PAGE_SYSTEMS_API_KEY`, or pass `{ apiKey }`. The tools are `list_page_types`, `get_page_type_schema`, `create_page` and `get_page_result`. `create_page` does not type the page's fields: the model reads them from `get_page_type_schema`. If your model keeps calling `create_page` with only `page.type` and gets `invalid_spec` back (we have seen this with OpenAI GPT models), use MCP instead: its `create_page` carries every page type's full schema.

## 2b. MCP

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

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

const skill = await fetch('https://pagesystems.ai/v1/skill').then((r) => r.text()); // once, when the agent starts
const pageSystems = await createMCPClient({
  transport: {
    type: 'http',
    url: 'https://pagesystems.ai/mcp',
    headers: { Authorization: `Bearer ${process.env.PAGE_SYSTEMS_API_KEY}` },
  },
});

try {
  const result = await generateText({
    model,
    system: [yourInstructions, skill].join('\n\n'),
    messages,
    tools: await pageSystems.tools(),
    stopWhen: isStepCount(6),
  });
} finally {
  await pageSystems.close();
}
```

The MCP server also offers `validate_spec` and `cancel_page`.

## 3. Keep the conversation

The answer comes back in a later turn, so keep the messages: append `result.response.messages` to your history after each call. When the person says they answered, or when your own callback fires, the model calls `get_page_result` with the page id it already has.

## 4. Check it works

Ask your agent something a page answers better than chat, for example "Ask Sam which of 9:00, 11:00 or 14:00 next Tuesday works for a call." It should send a link. Open it, pick a time, submit, then ask the agent what Sam picked: it reads the answer with `get_page_result`.
