A Mastra agent that reads Gmail using Scalekit AgentKit for OAuth-managed tool access. Discovers tools dynamically from Scalekit and wraps them as native Mastra tools.
Ask the agent to manage your Gmail. It discovers available tools from Scalekit, handles authorization, and executes tool calls on your behalf:
Prompt: Fetch my last 5 unread emails and summarize them.
Connected account for user_123 is active.
Discovered 8 tools: gmail_fetch_mails, gmail_send_mail, ...
Created 8 Mastra tools.
Here are your 5 most recent unread emails:
1. "Q1 roadmap feedback needed" — Sarah Chen (1h ago)
2. "Deploy failed: production" — GitHub Actions (2h ago)
3. "New PR review requested" — Lin Feng (3h ago)
...
| Pattern | What it shows |
|---|---|
| Dynamic tool discovery | Lists tools from Scalekit at runtime — no hardcoded tool definitions |
| Magic link authorization | Generates an auth URL if the user hasn't connected their account |
| Automatic token refresh | Scalekit manages OAuth tokens; your code never handles refresh |
Mastra createTool wrapping |
Converts Scalekit tool definitions into native Mastra tools |
- A Scalekit account with a
gmailconnection configured under AgentKit → Connections - An OpenAI API key
- Node.js 18+ and pnpm
1. Add credentials
cp .env.example .envFill in your credentials:
SCALEKIT_ENV_URL=https://your-env.scalekit.dev
SCALEKIT_CLIENT_ID=skc_...
SCALEKIT_CLIENT_SECRET=...
OPENAI_API_KEY=sk-...Get your Scalekit credentials at app.scalekit.com → select your environment → Settings → API Credentials
2. Install and run
pnpm install
pnpm startOn the first run, if the user hasn't authorized Gmail, you'll see an authorization link:
[gmail] Authorization required.
Open this link:
https://auth.scalekit.dev/connect/...
Press Enter once you have completed the OAuth flow...
After authorization, the agent runs and prints its response.
3. Custom prompts
Pass a custom prompt as an argument:
pnpm start "Search for emails from GitHub and list the subjects"- Initialize Scalekit —
ScalekitClientauthenticates with your environment credentials - Ensure connected account — Checks if the user has an active Gmail connection; generates a magic link if not
- Discover tools —
scalekit.tools.listTools()returns tool definitions (name, description, input schema) for the connected account - Create Mastra tools — Each Scalekit tool is wrapped with
createTool()from@mastra/core/tools, wiringexecutetoscalekit.tools.executeTool() - Run the agent — A Mastra
Agentwith GPT-4o receives the tools and generates a response
- AgentKit overview — how Scalekit manages OAuth for agents
- AgentKit quickstart — connect your first user in minutes
- All supported connectors — Gmail, Slack, Notion, and 60+ more
- Mastra documentation — the Mastra framework