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 and pass the URL to your Mastra application:
# 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# Pass mcp_url to your Mastra app (e.g. via environment variable or API response)See Configure an MCP server and Generate user MCP URLs to set up the config and generate the URL.
Build the agent
Section titled “Build the agent”Pass the 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';
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();