Enhance your Mastra applications with real-time social media context from Membit. This integration allows your AI agents to access up-to-the-minute discussions, trends, and insights from platforms like X (Twitter), Farcaster, and more.

Prerequisites

Before you begin, ensure you have:
  • Node.js 20 or higher installed
  • A Membit account with API access
  • Basic familiarity with Mastra agents
  • TypeScript knowledge (recommended)
You’ll need valid Membit API credentials and the MCP remote URL. If you don’t have access yet, get your API key to get started.

Installation

Install the required packages for Mastra and MCP integration:
# Install Mastra with MCP support
npm install @mastra/mcp@latest

# Install MCP remote client
npm install -g mcp-remote
Mastra works best with TypeScript. Make sure your project is configured for TypeScript development.

Quick Start

1

Import required modules

Import the necessary components for Mastra and MCP integration:
import { openai } from "@ai-sdk/openai";
import { Agent } from "@mastra/core/agent";
import { LibSQLStore } from "@mastra/libsql";
import { MCPClient } from "@mastra/mcp";
2

Initialize MCP client

Create an MCP client to connect to Membit’s tools:
export const mcp = new MCPClient({
  servers: {
    membit: {
      command: "npx",
      env: {
        MEMBIT_API_KEY: <your-api-key>,
      },
      args: [
        "mcp-remote",
        "https://mcp.membit.ai/mcp",
        "--header",
        "X-Membit-Api-Key:${MEMBIT_API_KEY}",
      ],
    },
  },
});
Make sure you have mcp-remote installed globally via npm for this to work.
Replace <your-api-key> with your actual Membit API key. Keep this credential secure and don’t share it with unauthorized users.
3

Create your Membit-powered agent

Create a Mastra agent with Membit tools and memory:
export const membitAgent = new Agent({
  name: "Social Media Analyst",
  instructions: `You are a social media analyst with access to real-time data. Use cluster_search to find trending discussions, cluster_info for detailed analysis, and post_search for specific posts.`,
  model: openai("gpt-4o-mini"),
  tools: await mcp.getTools(),
});
4

Use your agent

Execute your agent with social media analysis tasks:
const result = await membitAgent.generate(
  "What are the most trending discussions about AI today? Use cluster_search to find hot topics and provide insights with URLs."
);

console.log(result.text);
Your Mastra agent is now powered by real-time social media context from Membit!

Complete Example

Here’s a full working example that demonstrates the integration:
import { openai } from "@ai-sdk/openai";
import { Agent } from '@mastra/core/agent';
import { LibSQLStore } from '@mastra/libsql';
import { MCPClient } from "@mastra/mcp";

// Initialize MCP client for Membit
export const mcp = new MCPClient({
servers: {
membit: {
command: "npx",
env: {
// Replace `<your-api-key>` with your actual Membit API key.
MEMBIT_API_KEY: <your-api-key>,
},
args: [
"mcp-remote",
"https://mcp.membit.ai/mcp",
"--header",
"X-Membit-Api-Key:${MEMBIT_API_KEY}",
],
},
},
});

// Create Membit-powered agent
export const membitAgent = new Agent({
name: 'Membit Social Analyst',
instructions: `You are an expert social media analyst with access to real-time trending data. 
  Use cluster_search to find trending discussions, cluster_info to dive deeper into specific conversations, 
  and post_search to find individual posts. Always provide context and source URLs.`,
model: openrouter("anthropic/claude-3-5-sonnet-20241022"),
tools: await mcp.getTools(),
});

async function main() {
try {
const result = await membitAgent.generate(
"What are the most trending discussions today related to Bitcoin? Use cluster_search to find hot topics and provide the best conversations to follow with URLs."
);

    console.log("Analysis Results:");
    console.log(result.text);

} catch (error) {
console.error("Error during analysis:", error);
}
}

main();

Troubleshooting