MCP cover image
See in Github
2025-03-28

5

Github Watches

15

Github Forks

82

Github Stars

LlamaIndex MCP demos

This repo demonstrates both how to create an MCP server using LlamaCloud and how to use LlamaIndex as an MCP client.

LlamaCloud as an MCP server

To provide a local MCP server that can be used by a client like Claude Desktop, you can use mcp-server.py. You can use this to provide a tool that will use RAG to provide Claude with up-to-the-second private information that it can use to answer questions. You can provide as many of these tools as you want.

Set up your LlamaCloud index

  1. Get a LlamaCloud account
  2. Create a new index with any data source you want. In our case we used Google Drive and provided a subset of the LlamaIndex documentation as a source. You could also upload documents directly to the index if you just want to test it out.
  3. Get an API key from the LlamaCloud UI

Set up your MCP server

  1. Clone this repository
  2. Create a .env file and add two environment variables:
    • LLAMA_CLOUD_API_KEY - The API key you got in the previous step
    • OPENAI_API_KEY - An OpenAI API key. This is used to power the RAG query. You can use any other LLM if you don't want to use OpenAI.

Now let's look at the code. First you instantiate an MCP server:

mcp = FastMCP('llama-index-server')

Then you define your tool using the @mcp.tool() decorator:

@mcp.tool()
def llama_index_documentation(query: str) -> str:
    """Search the llama-index documentation for the given query."""

    index = LlamaCloudIndex(
        name="mcp-demo-2",
        project_name="Rando project",
        organization_id="e793a802-cb91-4e6a-bd49-61d0ba2ac5f9",
        api_key=os.getenv("LLAMA_CLOUD_API_KEY"),
    )

    response = index.as_query_engine().query(query + " Be verbose and include code examples.")

    return str(response)

Here our tool is called llama_index_documentation; it instantiates a LlamaCloud index called mcp-demo-2 and then uses it as a query engine to answer the query, including some extra instructions in the prompt. You'll get instructions on how to set up your LlamaCloud index in the next section.

Finally, you run the server:

if __name__ == "__main__":
    mcp.run(transport="stdio")

Note the stdio transport, used for communicating to Claude Desktop.

Configure Claude Desktop

  1. Install Claude Desktop
  2. In the menu bar choose Claude -> Settings -> Developer -> Edit Config. This will show up a config file that you can edit in your preferred text editor.
  3. You'll want your config to look something like this (make sure to replace $YOURPATH with the path to the repository):
{
    "mcpServers": {
        "llama_index_docs_server": {
            "command": "poetry",
            "args": [
                "--directory",
                "$YOURPATH/llamacloud-mcp",
                "run",
                "python",
                "$YOURPATH/llamacloud-mcp/mcp-server.py"
            ]
        }
    }
}

Make sure to restart Claude Desktop after configuring the file.

Now you're ready to query! You should see a tool icon with your server listed underneath the query box in Claude Desktop, like this:

LlamaIndex as an MCP client

LlamaIndex also has an MCP client integration, meaning you can turn any MCP server into a set of tools that can be used by an agent. You can see this in mcp-client.py, where we use the BasicMCPClient to connect to our local MCP server.

For simplicity of demo, we are using the same MCP server we just set up above. Ordinarily, you would not use MCP to connect LlamaCloud to a LlamaIndex agent, you would use QueryEngineTool and pass it directly to the agent.

Set up your MCP server

To provide a local MCP server that can be used by an HTTP client, we need to slightly modify mcp-server.py to use the run_sse_async method instead of run. You can find this in mcp-http-server.py.

mcp = FastMCP('llama-index-server',port=8000)

asyncio.run(mcp.run_sse_async())

Get your tools from the MCP server

mcp_client = BasicMCPClient("http://localhost:8000/sse")
mcp_tool_spec = McpToolSpec(
    client=mcp_client,
    # Optional: Filter the tools by name
    # allowed_tools=["tool1", "tool2"],
)

tools = mcp_tool_spec.to_tool_list()

Create an agent and ask a question

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

agent = FunctionAgent(
    tools=tools,
    llm=llm,
    system_prompt="You are an agent that knows how to build agents in LlamaIndex.",
)

async def run_agent():
    response = await agent.run("How do I instantiate an agent in LlamaIndex?")
    print(response)

if __name__ == "__main__":
    asyncio.run(run_agent())

You're all set! You can now use the agent to answer questions from your LlamaCloud index.

相关推荐

  • https://suefel.com
  • Latest advice and best practices for custom GPT development.

  • Yusuf Emre Yeşilyurt
  • I find academic articles and books for research and literature reviews.

  • https://maiplestudio.com
  • Find Exhibitors, Speakers and more

  • Carlos Ferrin
  • Encuentra películas y series en plataformas de streaming.

  • Joshua Armstrong
  • Confidential guide on numerology and astrology, based of GG33 Public information

  • Emmet Halm
  • Converts Figma frames into front-end code for various mobile frameworks.

  • Elijah Ng Shi Yi
  • Advanced software engineer GPT that excels through nailing the basics.

  • Alexandru Strujac
  • Efficient thumbnail creator for YouTube videos

  • lumpenspace
  • Take an adjectivised noun, and create images making it progressively more adjective!

  • Lists Tailwind CSS classes in monospaced font

  • https://appia.in
  • Siri Shortcut Finder – your go-to place for discovering amazing Siri Shortcuts with ease

  • apappascs
  • 发现市场上最全面,最新的MCP服务器集合。该存储库充当集中式枢纽,提供了广泛的开源和专有MCP服务器目录,并提供功能,文档链接和贡献者。

  • ShrimpingIt
  • MCP系列GPIO Expander的基于Micropython I2C的操作,源自ADAFRUIT_MCP230XX

  • jae-jae
  • MCP服务器使用剧作《无头浏览器》获取网页内容。

  • Mintplex-Labs
  • 带有内置抹布,AI代理,无代理构建器,MCP兼容性等的多合一桌面和Docker AI应用程序。

  • ravitemer
  • 一个功能强大的Neovim插件,用于管理MCP(模型上下文协议)服务器

  • patruff
  • Ollama和MCP服务器之间的桥梁,使本地LLMS可以使用模型上下文协议工具

  • pontusab
  • 光标与风浪冲浪社区,查找规则和MCP

  • WangRongsheng
  • 🧑‍🚀 llm 资料总结(数据处理、模型训练、模型部署、 o1 模型、mcp 、小语言模型、视觉语言模型)|摘要世界上最好的LLM资源。

  • n8n-io
  • 具有本机AI功能的公平代码工作流程自动化平台。将视觉构建与自定义代码,自宿主或云相结合,400+集成。

  • av
  • 毫不费力地使用一个命令运行LLM后端,API,前端和服务。

    Reviews

    2 (1)
    Avatar
    user_Y2eaz2HZ
    2025-04-17

    As a loyal user of llamacloud-mcp, I am incredibly impressed with its performance and versatility. It seamlessly integrates into my workflows, enhancing productivity and efficiency. The user-friendly interface and robust features made it a game-changer for my projects. Hats off to run-llama for creating such an exceptional tool! Highly recommend checking it out: https://github.com/run-llama/llamacloud-mcp.