> ## Documentation Index
> Fetch the complete documentation index at: https://docs.membit.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# AutoGen

> Integrate real-time context to your AutoGen AI agents via Membit MCP.

![Cover Image](https://mintcdn.com/membit/4JB7OCBLDkpjkRT-/images/cover/autogen.png?fit=max\&auto=format\&n=4JB7OCBLDkpjkRT-\&q=85\&s=59afbc3384f1a399d15a9b57cf6e45c2)

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)

> [!WARNING]
>
> You'll need valid Membit API credentials and the MCP remote URL. If you don't
> have access yet, [get your API key](https://docs.membit.ai/access-and-auth.md) to get started.

## Installation

Install the required packages for AutoGen and MCP integration:

```bash theme={null}
# Install AutoGen with MCP tools
pip install autogenstudio

# Install MCP remote client (requires Node.js)
npm install -g mcp-remote
```

> [!TIP]
>
> 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:

```python theme={null}
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:

```python theme={null}
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,
)
```

> [!NOTE]
>
> Make sure you have `mcp-remote` installed globally via npm for this to work.

> [!WARNING]
>
> 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:

```python theme={null}
model_client = OpenAIChatCompletionClient(model="gpt-4o-mini")
```

**4. Create your Membit-powered agent**

Use McpWorkbench to connect Membit tools to your AutoGen agent:

```python theme={null}
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:

```python theme={null}
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."
    )
)
```

> [!TIP]
>
> 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:

```python theme={null}
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

> [accordion] MCP Connection Issues
>
> **Problem**: Cannot connect to Membit MCP server
>
> **Solutions**:
>
> * Verify `mcp-remote` is installed: `npm list -g mcp-remote`
> * Check your API key is correct
> * Ensure Node.js is properly installed and accessible
> * Increase `read_timeout_seconds` if experiencing timeouts

> [accordion] Workbench Tool Loading
>
> **Problem**: McpWorkbench fails to load tools
>
> **Solutions**:
>
> * Verify MCP server parameters are correct
> * Check network connectivity and firewall settings
> * Ensure your Membit API credentials are valid
> * Try listing tools first to verify connection: `await workbench.list_tools()`

> [accordion] Agent Tool Usage Issues
>
> **Problem**: Agents don't use Membit tools effectively
>
> **Solutions**:
>
> * Make system messages more explicit about tool usage
> * Enable `reflect_on_tool_use=True` for better tool reasoning
> * Use `model_client_stream=True` for real-time feedback
> * Test with simpler, more direct task descriptions

```
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.