主题
LlamaIndex 通过 llama-index-tools-mcp 包为「使用 MCP 服务端」提供了完善的支持。
安装
终端窗口
pip install llama-index-tools-mcp基本用法
最常见的用法是把一个 MCP 服务端转换成一串 llama-index 工具定义:
from llama_index.tools.mcp import BasicMCPClient, McpToolSpec
# Connect to MCP server
mcp_client = BasicMCPClient("http://127.0.0.1:8000/sse")
mcp_tool_spec = McpToolSpec(client=mcp_client)
# Get tools
tools = await mcp_tool_spec.to_tool_list_async()与智能体一起使用
拿到工具列表之后,就可以把它插进任意现有的 agent:
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI
agent = FunctionAgent(
tools=tools,
llm=OpenAI(model="gpt-5-mini"),
system_prompt="You are a helpful assistant.",
)
response = await agent.run("Your query here")关于 agent 的更多内容,可阅读智能体指南。
连接类型
BasicMCPClient 支持多种传输方式:
# Server-Sent Events
sse_client = BasicMCPClient("https://example.com/sse")
# Streamable HTTP
http_client = BasicMCPClient("https://example.com/mcp")
# Local process
local_client = BasicMCPClient("python", args=["server.py"])