LlamaCloud MCP Server

MIT License
42

Integrations

  • Allows retrieving and indexing documents from Google Drive to provide up-to-date private information for RAG queries.

  • Powers the RAG query functionality, enabling the retrieval of relevant information from indexed documents.

LlamaIndex MCP 演示

该 repo 演示了如何使用 LlamaCloud 创建 MCP 服务器以及如何使用 LlamaIndex 作为 MCP 客户端。

LlamaCloud 作为 MCP 服务器

要提供可供 Claude Desktop 等客户端使用的本地 MCP 服务器,您可以使用mcp-server.py 。您可以使用它来提供一个工具,该工具将使用 RAG 为 Claude 提供最新的隐私信息,以便 Claude 回答问题。您可以根据需要提供任意数量的此类工具。

设置您的 LlamaCloud 索引

  1. 获取LlamaCloud帐户
  2. 使用您想要的任何数据源创建一个新的索引。在我们的例子中,我们使用了Google Drive ,并提供了 LlamaIndex 文档的子集作为数据源。如果您只是想测试一下,也可以直接将文档上传到索引。
  3. LlamaCloud UI获取 API 密钥

设置您的 MCP 服务器

  1. 克隆此存储库
  2. 创建一个.env文件并添加两个环境变量:
    • LLAMA_CLOUD_API_KEY - 您在上一步中获得的 API 密钥
    • OPENAI_API_KEY - OpenAI API 密钥。用于支持 RAG 查询。如果您不想使用 OpenAI,可以使用任何其他 LLM

现在我们来看一下代码。首先实例化一个 MCP 服务器:

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

然后使用@mcp.tool()装饰器定义您的工具:

@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)

这里我们的工具名为llama_index_documentation ;它会实例化一个名为mcp-demo-2的 LlamaCloud 索引,然后将其用作查询引擎来响应查询,并在提示符中包含一些额外的说明。您将在下一节中获得有关如何设置 LlamaCloud 索引的说明。

最后,运行服务器:

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

注意stdio传输,用于与 Claude Desktop 进行通信。

配置 Claude 桌面

  1. 安装Claude Desktop
  2. 在菜单栏中选择Claude -> Settings -> Developer -> Edit Config 。这将显示一个配置文件,您可以在您喜欢的文本编辑器中编辑它。
  3. 您希望您的配置看起来像这样(确保用存储库的路径替换$YOURPATH ):
{ "mcpServers": { "llama_index_docs_server": { "command": "poetry", "args": [ "--directory", "$YOURPATH/llamacloud-mcp", "run", "python", "$YOURPATH/llamacloud-mcp/mcp-server.py" ] } } }

配置文件后,请确保重新启动 Claude Desktop

现在您可以开始查询了!您应该会在 Claude Desktop 的查询框下方看到一个工具图标,其中列出了您的服务器,如下所示:

LlamaIndex 作为 MCP 客户端

LlamaIndex 还集成了 MCP 客户端,这意味着您可以将任何 MCP 服务器变成一套可供代理使用的工具。您可以在mcp-client.py中看到这一点,其中我们使用BasicMCPClient连接到本地 MCP 服务器。

为了演示的简便,我们使用上面刚刚设置的 MCP 服务器。通常情况下,您不会使用 MCP 将 LlamaCloud 连接到 LlamaIndex 代理,而是使用QueryEngineTool并将其直接传递给代理。

设置您的 MCP 服务器

为了提供一个可供 HTTP 客户端使用的本地 MCP 服务器,我们需要稍微修改mcp-server.py ,使用run_sse_async方法代替run方法。您可以在mcp-http-server.py中找到它。

mcp = FastMCP('llama-index-server',port=8000) asyncio.run(mcp.run_sse_async())

从 MCP 服务器获取工具

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()

创建代理并提出问题

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())

一切就绪!现在,您可以使用代理来回答 LlamaCloud 索引中的问题。

-
security - not tested
A
license - permissive license
-
quality - not tested

与 Claude Desktop 集成的本地 MCP 服务器,使 RAG 功能能够为 Claude 提供来自自定义 LlamaCloud 索引的最新私人信息。

  1. LlamaCloud as an MCP server
    1. Set up your LlamaCloud index
    2. Set up your MCP server
    3. Configure Claude Desktop
  2. LlamaIndex as an MCP client
    1. Set up your MCP server
    2. Get your tools from the MCP server
    3. Create an agent and ask a question
ID: 836roj5xrj