> **Building with AI coding agents?** If you're using an AI coding agent, install the official Scalekit plugin. It gives your agent full awareness of the Scalekit API — reducing hallucinations and enabling faster, more accurate code generation.
>
> - **Claude Code**: `/plugin marketplace add scalekit-inc/claude-code-authstack` then `/plugin install <auth-type>@scalekit-auth-stack`
> - **GitHub Copilot CLI**: `copilot plugin marketplace add scalekit-inc/github-copilot-authstack` then `copilot plugin install <auth-type>@scalekit-auth-stack`
> - **Codex**: run the bash installer, restart, then open Plugin Directory and enable `<auth-type>`
> - **Skills CLI** (Windsurf, Cline, 40+ agents): `npx skills add scalekit-inc/skills --list` then `--skill <skill-name>`
>
> `<auth-type>` / `<skill-name>`: `agentkit`, `full-stack-auth`, `mcp-auth`, `modular-sso`, `modular-scim` — [Full setup guide](https://docs.scalekit.com/dev-kit/build-with-ai/)

---

# Mastra

Connect a Mastra agent to Scalekit tools using MCP. Mastra has native MCP support via `@mastra/mcp`. Pass a Scalekit-generated URL and Mastra handles tool discovery automatically.
**Why MCP for Mastra:** Mastra's tool system uses Zod schemas internally. The MCP path skips manual schema conversion. Mastra discovers tools and their schemas directly from the Scalekit MCP server.

## Install

```sh
npm install @scalekit-sdk/node @mastra/core @mastra/mcp @ai-sdk/openai
```

## Get a per-user MCP URL

Generate a Scalekit MCP URL for the user. This requires the Python SDK. Call this from your backend and pass the URL to your Mastra application:

```python
# Backend (Python): generate once per user session
inst_response = actions.mcp.ensure_instance(
    config_name="your-mcp-config",
    user_identifier="user_123",
)
mcp_url = inst_response.instance.url
# Pass mcp_url to your Mastra app (e.g. via environment variable or API response)
```

See [Configure an MCP server](/agentkit/mcp/configure-mcp-server/) and [Generate user MCP URLs](/agentkit/mcp/generate-user-urls/) to set up the config and generate the URL.

## Build the agent

Pass the MCP URL to `MCPClient`. Mastra fetches the tool list and schemas automatically:

```typescript
import { Agent } from '@mastra/core/agent';
import { MCPClient } from '@mastra/mcp';
import { openai } from '@ai-sdk/openai';

const mcpUrl = process.env.SCALEKIT_MCP_URL!; // set from your backend

const mcp = new MCPClient({
  servers: {
    scalekit: { url: new URL(mcpUrl) },
  },
});

const tools = await mcp.getTools();

const agent = new Agent({
  name: 'gmail_assistant',
  instructions: 'You are a helpful Gmail assistant.',
  model: openai('gpt-4o'),
  tools,
});

const result = await agent.generate('Fetch my last 5 unread emails and summarize them');
console.log(result.text);

await mcp.disconnect();
```

---

## More Scalekit documentation

| Resource | What it contains | When to use it |
|----------|-----------------|----------------|
| [/llms.txt](/llms.txt) | Structured index with routing hints per product area | Start here — find which documentation set covers your topic before loading full content |
| [/llms-full.txt](/llms-full.txt) | Complete documentation for all Scalekit products in one file | Use when you need exhaustive context across multiple products or when the topic spans several areas |
| [sitemap-0.xml](https://docs.scalekit.com/sitemap-0.xml) | Full URL list of every documentation page | Use to discover specific page URLs you can fetch for targeted, page-level answers |
