Skip to content
Scalekit Docs
Talk to an EngineerDashboard

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.

Terminal window
npm install @scalekit-sdk/node @mastra/core @mastra/mcp @ai-sdk/openai

Generate a Scalekit MCP URL for the user. This requires the Python SDK. Call this from your backend for the current user, then pass that URL into your Mastra app (API response, session store, or request-scoped config).

# 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
# Return mcp_url to the Mastra app for this user only

See Virtual MCP Servers to set up the config and generate the URL.

Pass the current user’s MCP URL to MCPClient. Mastra fetches the tool list and schemas automatically:

import { Agent } from '@mastra/core/agent';
import { MCPClient } from '@mastra/mcp';
import { openai } from '@ai-sdk/openai';
// From your backend for the authenticated user — not a shared process-wide secret
const mcpUrl = await getMcpUrlForUser(currentUserId);
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();