> **Building with AI coding agents?** Install the authstack plugin with one command. This equips your agent with accurate Scalekit implementation patterns.
>
> **Recommended**:
> ```bash
> npx @scalekit-inc/cli setup
> ```
>
> Global:
> ```bash
> npm install -g @scalekit-inc/cli
> scalekit setup
> ```
>
> Supports Claude Code, Cursor, GitHub Copilot, Codex + skills for 40+ agents.
> Features: full-stack-auth, agent-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-authenticated tools using MCP. Mastra's native MCP client connects directly to a Scalekit-generated MCP URL.
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.

> note: 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 [Virtual MCP Servers](/agentkit/mcp/overview/) 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

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 |
