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.
Install
Section titled “Install”npm install @scalekit-sdk/node @mastra/core @mastra/mcp @ai-sdk/openaiGet a per-user MCP URL
Section titled “Get a per-user MCP URL”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 sessioninst_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 onlySee Virtual MCP Servers to set up the config and generate the URL.
Build the agent
Section titled “Build the agent”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 secretconst 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();