> ## 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.

# LlamaIndex

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

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

Enhance your LlamaIndex 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 LlamaIndex agents

> [!WARNING]
>
> You'll need valid Membit API credentials. 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 LlamaIndex MCP tools package:

```bash theme={null}
pip install llama-index-tools-mcp
```

> [!TIP]
>
> We recommend using a virtual environment to manage your dependencies and avoid
> conflicts.

## Quick Start

**1. Import the required modules**

First, import the necessary components from LlamaIndex:

```python theme={null}
from llama_index.tools.mcp import (
    BasicMCPClient,
    get_tools_from_mcp_url,
    aget_tools_from_mcp_url,
)
from llama_index.core.agent import ReActAgent
from llama_index.core.memory import ChatMemoryBuffer
from llama_index.llms.openai import OpenAI
```

**2. Get Membit tools from MCP server**

Connect to Membit's MCP server to retrieve available tools:

> [tab] Async
>
> ```python theme={null}
> # Create MCP Client
> mcp_client = BasicMCPClient(
>     command_or_url="npx",
>     args=[
>         "mcp-remote",
>         "https://mcp.membit.ai/mcp",
>         "--header",
>         "X-Membit-Api-Key:${MEMBIT_API_KEY}",
>     ],
>     env={
>         "MEMBIT_API_KEY": <your-api-key>,
>     },
> )
> # Async method
> tools = await aget_tools_from_mcp_url(
>     "",
>     client=mcp_client,
> )
> ```

> [tab] Sync
>
> ```python theme={null}
> # Create MCP Client
> mcp_client = BasicMCPClient(
>     command_or_url="npx",
>     args=[
>         "mcp-remote",
>         "https://mcp.membit.ai/mcp",
>         "--header",
>         "X-Membit-Api-Key:${MEMBIT_API_KEY}",
>     ],
>     env={
>         "MEMBIT_API_KEY": <your-api-key>,
>     },
> )
> # Synchronous method
> tools = get_tools_from_mcp_url(
>     "",
>     client=mcp_client,
> )
> ```

> [!WARNING]
>
> Replace `<your-api-key>` with your actual Membit API key. Keep this credential secure and don't share it with unauthorized users.

**3. Create and configure your agent**

Set up a LlamaIndex ReAct agent with Membit tools:

```python theme={null}
# Initialize your LLM
llm = OpenAI(model="gpt-4o-mini")

# Create the agent with Membit tools
agent = ReActAgent(
    tools=tools,
    llm=llm,
    memory=ChatMemoryBuffer.from_defaults(),
    verbose=True,
)
```

> [!TIP]
>
> Your agent is now ready to access real-time social media context through Membit!

**4. Query your agent**

Start asking questions that leverage real-time social data:

```python theme={null}
response = agent.query(
    "Use cluster_search to find trending Bitcoin discussions today, "
    "then use cluster_info to get details about the most interesting cluster."
)

print(response)
```

## Complete Example

Here's a full working example that demonstrates the integration:

```python theme={null}
import asyncio

from llama_index.core.agent import ReActAgent
from llama_index.core.memory import ChatMemoryBuffer
from llama_index.llms.openai import OpenAI
from llama_index.tools.mcp import BasicMCPClient, aget_tools_from_mcp_url

async def main():
    # Create MCP client
    mcp_client = BasicMCPClient(
        command_or_url="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>,
        },
    )

    # Get Membit tools
    tools = await aget_tools_from_mcp_url(
        "",
        client=mcp_client,
    )

    # Initialize LLM
    llm = OpenAI(model="gpt-4o-mini")

    # Create agent
    agent = ReActAgent(
        tools=tools,
        llm=llm,
        memory=ChatMemoryBuffer.from_defaults(),
        verbose=True,
    )

    # Query for trending crypto discussions
    response = await agent.aquery(
        "What are the most trending discussions about Bitcoin today? "
        "Use cluster_search to find the hottest topics and give me insights."
    )

    print(response)

if __name__ == "__main__":
    asyncio.run(main())
```

## Troubleshooting

> [accordion] Connection Issues
>
> **Problem**: Cannot connect to Membit MCP server
>
> **Solutions**:
>
> * Verify your Membit MCP URL is correct
> * Check your network connection and firewall settings
> * Ensure your API credentials are valid and not expired
> * Try the synchronous method if async isn't working

> [accordion] No Results Returned
>
> **Problem**: Queries return empty or limited results
>
> **Solutions**:
>
> * Make your search terms more general or use synonyms
> * Check if the topic is currently being discussed on social media
> * Verify your search timeframe isn't too restrictive
> * Try different query phrasings

> [accordion] Performance Issues
>
> **Problem**: Slow response times or timeouts
>
> **Solutions**:
>
> * Use async methods for better performance
> * Implement caching for frequently requested data
> * Optimize your query complexity
> * Check your internet connection stability


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