LlamaIndex MCP 데모
이 저장소는 LlamaCloud를 사용하여 MCP 서버를 만드는 방법과 LlamaIndex를 MCP 클라이언트로 사용하는 방법을 모두 보여줍니다.
MCP 서버로서의 LlamaCloud
Claude Desktop과 같은 클라이언트에서 사용할 수 있는 로컬 MCP 서버를 제공하려면 mcp-server.py 사용할 수 있습니다. 이 파일을 사용하면 RAG를 사용하여 Claude에게 질문에 답변하는 데 사용할 수 있는 최신 개인 정보를 제공하는 도구를 제공할 수 있습니다. 이러한 도구는 원하는 만큼 제공할 수 있습니다.
LlamaCloud 인덱스 설정
LlamaCloud 계정 받기
원하는 데이터 소스를 사용하여 새 인덱스를 만드세요 . 이 경우에는 Google Drive를 사용하고 LlamaIndex 문서의 일부를 소스로 제공했습니다. 테스트용으로만 사용하고 싶다면 인덱스에 문서를 직접 업로드할 수도 있습니다.
LlamaCloud UI 에서 API 키 가져오기
MCP 서버 설정
이 저장소를 복제하세요
.env파일을 만들고 두 개의 환경 변수를 추가합니다.LLAMA_CLOUD_API_KEY- 이전 단계에서 얻은 API 키OPENAI_API_KEY- OpenAI API 키입니다. RAG 쿼리를 실행하는 데 사용됩니다. OpenAI를 사용하지 않으려면 다른 LLM을 사용할 수 있습니다.
이제 코드를 살펴보겠습니다. 먼저 MCP 서버를 인스턴스화합니다.
지엑스피1
그런 다음 @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")Claude Desktop과 통신하는 데 사용되는 stdio 전송에 주목하세요.
Claude Desktop 구성
메뉴 막대에서
Claude->Settings->Developer->Edit Config선택하세요. 원하는 텍스트 편집기에서 편집할 수 있는 구성 파일이 표시됩니다.구성은 다음과 같아야 합니다(
$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의 쿼리 상자 아래에 서버가 나열된 도구 아이콘이 다음과 같이 표시됩니다.

Related MCP server: Choose MCP Server
MCP 클라이언트로서의 LlamaIndex
LlamaIndex에는 MCP 클라이언트 통합 기능이 있어 모든 MCP 서버를 에이전트가 사용할 수 있는 도구 세트로 변환할 수 있습니다. mcp-client.py 파일에서 BasicMCPClient 를 사용하여 로컬 MCP 서버에 연결하는 것을 확인할 수 있습니다.
데모의 편의를 위해 위에서 설정한 것과 동일한 MCP 서버를 사용합니다. 일반적으로 LlamaCloud를 LlamaIndex 에이전트에 연결할 때 MCP를 사용하지 않고, QueryEngineTool을 사용하여 에이전트에 직접 전달합니다.
MCP 서버 설정
HTTP 클라이언트에서 사용할 수 있는 로컬 MCP 서버를 제공하려면 mcp-server.py 약간 수정하여 run 대신 run_sse_async 메서드를 사용해야 합니다. 이 내용은 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 인덱스의 질문에 답변할 수 있습니다.
Resources
Looking for Admin?
Admins can modify the Dockerfile, update the server description, and track usage metrics. If you are the server author, to access the admin panel.