Using MCP Tools with LlamaIndex
LlamaIndex provides robust support for consuming MCP servers through the llama-index-tools-mcp
package.
Installation
Section titled “Installation”pip install llama-index-tools-mcp
Basic Usage
Section titled “Basic Usage”The most common usage will be converting an MCP server into a list of llama-index
tool definitions:
from llama_index.tools.mcp import BasicMCPClient, McpToolSpec
# Connect to MCP servermcp_client = BasicMCPClient("http://127.0.0.1:8000/sse")mcp_tool_spec = McpToolSpec(client=mcp_client)
# Get toolstools = await mcp_tool_spec.to_tool_list_async()
Using with Agents
Section titled “Using with Agents”Once you have a list of tools, you can plug this into any existing agent:
from llama_index.core.agent.workflow import FunctionAgentfrom 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")
You can read more about agents in the Agents Guide.
Connection Types
Section titled “Connection Types”The BasicMCPClient
supports multiple transport methods:
# Server-Sent Eventssse_client = BasicMCPClient("https://example.com/sse")
# Streamable HTTPhttp_client = BasicMCPClient("https://example.com/mcp")
# Local processlocal_client = BasicMCPClient("python", args=["server.py"])