Skip to main content

Mastra

Connect a Mastra agent to Civic using MCPClient with Streamable HTTP transport. Mastra's MCP client exposes all discovered tools directly to a Mastra Agent.

Prerequisites

  • Node.js 20+
  • A Civic account at app.civic.com with a configured toolkit
  • A Civic token and an Anthropic API key

Installation

pnpm add @mastra/mcp @mastra/core @ai-sdk/anthropic

Environment Variables

CIVIC_URL=https://app.civic.com/hub/mcp?profile=your-toolkit
CIVIC_TOKEN=your-civic-token
ANTHROPIC_API_KEY=your-anthropic-key
Get Your Credentials

How to generate a Civic token and configure toolkit URL parameters

Connecting to Civic

Use MCPClient with the servers config, then call listToolsets() to get all tools:

import { MCPClient } from "@mastra/mcp";
import { Agent } from "@mastra/core/agent";
import { anthropic } from "@ai-sdk/anthropic";

const civicMcp = new MCPClient({
servers: {
civic: {
url: new URL(process.env.CIVIC_URL!),
requestInit: {
headers: { Authorization: `Bearer ${process.env.CIVIC_TOKEN}` },
},
},
},
});

// Use listToolsets() — not getTools()
const toolsets = await civicMcp.listToolsets();
const allTools = Object.values(toolsets).reduce(
(acc, ts) => ({ ...acc, ...ts }),
{}
);
console.log(`${Object.keys(allTools).length} tools loaded`);

const agent = new Agent({
name: "Civic Assistant",
model: anthropic("claude-sonnet-4-6"),
instructions: "You are a helpful assistant with access to Civic tools.",
tools: allTools,
});
note

Use civicMcp.listToolsets() — not civicMcp.getTools(). Merge the returned toolsets object with Object.values(toolsets).reduce((acc, ts) => ({ ...acc, ...ts }), {}) before passing to the agent.

Running the Agent

async function main() {
const response = await agent.generate("What events do I have today?");
console.log(response.text);
await civicMcp.disconnect();
}

main();

Production Configuration

For production agents, lock to a specific toolkit using the profile URL parameter:

CIVIC_URL=https://app.civic.com/hub/mcp?profile=your-production-toolkit

Reference Implementation

mastra-reference-implementation-civic

Complete implementation with Fastify chat UI and deployment guide

Next Steps