Enhance your AutoGen 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:
  • Python 3.10 or higher installed
  • A Membit account with API access
  • Basic familiarity with AutoGen agents
  • Node.js installed (for MCP remote client)
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 AutoGen and MCP integration:
# Install AutoGen with MCP tools
pip install autogenstudio

# Install MCP remote client (requires Node.js)
npm install -g mcp-remote
We recommend using a virtual environment to manage your Python dependencies and avoid conflicts.

Quick Start

1

Import required modules

Import the necessary components for AutoGen and MCP integration:
import asyncio

from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_ext.tools.mcp import McpWorkbench, StdioServerParams
2

Configure MCP server parameters

Set up the connection to Membit’s MCP server:
params = StdioServerParams(
    command="npx",
    args=[
        "mcp-remote",
        "https://mcp.membit.ai/mcp",
        "--header",
        "X-Membit-Api-Key:${MEMBIT_API_KEY}",
    ],
    env={
        "MEMBIT_API_KEY": <your-api-key>,
    },
    read_timeout_seconds=60,
)
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

Initialize your model client

Set up your language model client:
model_client = OpenAIChatCompletionClient(model="gpt-4o-mini")
4

Create your Membit-powered agent

Use McpWorkbench to connect Membit tools to your AutoGen agent:
async with McpWorkbench(server_params=params) as workbench:
    # List available tools
    tools = await workbench.list_tools()

    # Create assistant agent with Membit tools
    agent = AssistantAgent(
        "membit_assistant",
        model_client=model_client,
        workbench=workbench,
        reflect_on_tool_use=True,
        model_client_stream=True,
        system_message="You are a social media analyst with access to real-time data. Use Membit tools to analyze trending discussions and provide insights.",
    )
5

Run your agent

Execute your agent with a social media analysis task:
    await Console(
        agent.run_stream(
            task="What are the most trending discussions about AI today? Use cluster_search to find hot topics and provide insights with URLs."
        )
    )
Your AutoGen 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 asyncio

from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_ext.tools.mcp import McpWorkbench, StdioServerParams

async def main() -> None: # Configure MCP server connection
params = StdioServerParams(
command="npx",
args=[
"mcp-remote",
"https://mcp.membit.ai/mcp",
"--header",
"X-Membit-Api-Key:${MEMBIT_API_KEY}",
],
env={ # Replace `<your-api-key>` with your actual Membit API key.
"MEMBIT_API_KEY": <your-api-key>,
},
read_timeout_seconds=60,
)

    # Initialize model client
    model_client = OpenAIChatCompletionClient(model="gpt-4o-mini")

    # Create and run agent with Membit tools
    async with McpWorkbench(server_params=params) as workbench:
        tools = await workbench.list_tools()
        print("Available Membit Tools:")
        for tool in tools:
            print(f"- {tool.name}: {tool.description}")

        agent = AssistantAgent(
            "membit_assistant",
            model_client=model_client,
            workbench=workbench,
            reflect_on_tool_use=True,
            model_client_stream=True,
            system_message="You are a helpful social media analyst. Use cluster_search to find trending topics, cluster_info for detailed analysis, and post_search for specific posts.",
        )

        await Console(
            agent.run_stream(
                task="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."
            )
        )

if **name** == "**main**":
asyncio.run(main())

Troubleshooting