调用索引MCP演示
llamacloud-mcp 是一个允许您将LlamaCloud用作MCP服务器的工具。它可用于查询LlamaCloud索引并从文件中提取数据。
它允许:
- 指定用于上下文检索的一个或多个索引。
- 指定一个或多个提取代理用于数据提取
- 配置项目和组织id
- 配置用于MCP服务器的传输(stdio、sse、可流式传输http)
入门指南
- 安装 紫外线
- 跑
uvx llamacloud-mcp@latest --help查看可用选项。 - 配置您的MCP客户端以使用
llamacloud-mcp服务器。您可以直接使用以下命令启动服务器uvx llamacloud-mcp@latest或使用aclaude_desktop_config.json用于连接claude桌面的文件。
用法
% uvx llamacloud-mcp@latest --help
Usage: llamacloud-mcp [OPTIONS]
Options:
--index TEXT Index definition in the format
name:description. Can be used multiple
times.
--extract-agent TEXT Extract agent definition in the format
name:description. Can be used multiple
times.
--project-id TEXT Project ID for LlamaCloud
--org-id TEXT Organization ID for LlamaCloud
--transport [stdio|sse|streamable-http]
Transport to run the MCP server on. One of
"stdio", "sse", "streamable-http".
--api-key TEXT API key for LlamaCloud
--help Show this message and exit.配置Claude桌面
- 安装 克劳德桌面
- 在菜单栏中选择
Claude->Settings->Developer->Edit Config。这将显示一个配置文件,您可以在首选文本编辑器中编辑该文件。 - 在配置文件中添加以下“mcpServers”,其中每个
--index是您定义的新索引工具,每个--extract-agent是一种提取剂工具。 - 您希望您的配置看起来像这样(确保替换
$YOURPATH带有存储库的路径):
{
"mcpServers": {
"llama_index_docs_server": {
"command": "uvx",
"args": [
"llamacloud-mcp@latest",
"--index",
"your-index-name:Description of your index",
"--index",
"your-other-index-name:Description of your other index",
"--extract-agent",
"extract-agent-name:Description of your extract agent",
"--project-name",
"",
"--org-id",
"",
"--api-key",
""
]
},
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
""
]
}
}
}确保 重新启动克劳德桌面 配置文件后。
现在,您已经准备好进行查询了!您应该在Claude Desktop的查询框下方看到一个工具图标,其中列出了您的服务器,如下所示:

LlamaCloud作为MCP服务器从零开始
要提供可供Claude Desktop等客户端使用的本地MCP服务器,您可以使用 mcp-server.py。您可以使用此工具提供一个工具,该工具将使用RAG向Claude提供最多第二个私人信息,以便其回答问题。您可以根据需要提供尽可能多的这些工具。
设置您的LlamaCloud索引
- 得到一个 LlamaCloud 账户
- 创建新索引 使用您想要的任何数据源。在我们的案例中,我们使用 Google 云端硬盘 并提供了LlamaIndex文档的一个子集作为来源。如果你只是想测试一下,你也可以直接将文档上传到索引。
- 从获取API密钥 LlamaCloud用户界面
设置您的MCP服务器
- 克隆此存储库
- 创建一个
.env文件并添加两个环境变量:
- LLAMA_CLOUD_API_KEY -您在上一步中获得的API密钥 - OPENAI_API_KEY -一个OpenAI API密钥。这用于为RAG查询供电。您可以使用 任何其他LLM 如果你不想使用OpenAI。
现在让我们看看代码。首先实例化一个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;它实例化一个名为的LlamaCloud索引 mcp-demo-2 然后将其用作查询引擎来回答查询,包括提示中的一些额外指令。在下一节中,您将获得有关如何设置LlamaCloud索引的说明。
最后,运行服务器:
if __name__ == "__main__":
mcp.run(transport="stdio")注意 stdio 传输,用于与Claude Desktop通信。
LlamaIndex作为MCP客户端
LlamaIndex还具有MCP客户端集成,这意味着您可以将任何MCP服务器转换为一组可供代理使用的工具。你可以在 mcp-client.py,我们在哪里使用 BasicMCPClient 连接到我们的本地MCP服务器。
为了简化演示,我们使用的是与上面设置的MCP服务器相同的服务器。通常,您不会使用MCP将LlamaCloud连接到LlamaIndex代理,您会使用 查询排序 并将其直接传递给代理。
设置您的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索引中的问题。
