Connect to MCP Servers and Access Tools
Connect a LlamaIndex agent to any MCP server and call its tools, resources, and prompts.
Why it matters
Integrate your AI agent with MCP Servers to leverage their tools and resources. This asset enables seamless communication, allowing agents to call remote functions and access data.
Outcomes
What it gets done
Connect to MCP servers via HTTP, SSE, or stdio.
List and call tools exposed by MCP servers.
Access and read resources managed by MCP servers.
Convert LlamaIndex Workflows into MCP applications.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/li-tool-tools-mcp | bash Capabilities
Tools your agent gets
List available tools from an MCP server.
Call a tool on an MCP server with specified parameters.
List available resources from an MCP server.
Read a resource from an MCP server and get its content.
List available prompts from an MCP server.
Get a prompt from an MCP server with optional parameters.
Overview
MCP ToolSpec
A LlamaIndex tool spec that connects agents to MCP servers, and a set of helpers to convert workflows into MCP apps and fetch tools by URL. Use to call tools from an existing MCP server, or to expose a LlamaIndex workflow as its own MCP server.
What it does
The MCP ToolSpec connects a LlamaIndex agent to Model Context Protocol servers, letting the agent call the tools those servers provide. The idea is migrated from an existing writeup on integrating MCP tools into LlamaIndex, and it wraps MCP server access as ordinary LlamaIndex tools an agent can use directly.
Using it is a two-step process: create a BasicMCPClient pointed at a running MCP server (for example over SSE at a local address), then wrap it in McpToolSpec, optionally filtering to specific tools by name via allowed_tools or including MCP resources in the tool list via include_resources. to_tool_list() (or the async to_tool_list_async()) converts the MCP server's tools into LlamaIndex FunctionTools that can be attached to an agent exactly like any other tool.
Beyond the basic client-to-tools flow, the package includes helper functions. workflow_as_mcp converts a LlamaIndex Workflow into an MCP app, so a workflow you've built can itself be exposed as an MCP server and launched (for example via mcp dev script.py, assuming the mcp[cli] extra is installed). get_tools_from_mcp_url and its async counterpart aget_tools_from_mcp_url fetch a list of FunctionTools directly from an MCP server URL or command in one call. The underlying BasicMCPClient supports multiple transports - Streamable HTTP, Server-Sent Events, and local stdio via a command and args - plus operations beyond tool calling: listing and reading resources, and listing and getting prompts. It also supports OAuth 2.0 authentication for connecting to protected MCP servers, via a with_oauth constructor.
When to use - and when NOT to
Use it when you want a LlamaIndex agent to call tools exposed by any existing MCP server, or when you want to turn your own LlamaIndex workflow into an MCP server other clients can call. Use get_tools_from_mcp_url for a quick one-shot tool list without managing a client object yourself, and BasicMCPClient directly when you also need resource or prompt access, not just tools. Do not use it expecting a bundled MCP server - this package is a client and a workflow-to-MCP converter, not a specific server implementation.
Capabilities
McpToolSpec converts an MCP server's tools into LlamaIndex FunctionTools, with optional name filtering and resource inclusion. workflow_as_mcp exposes a LlamaIndex Workflow as an MCP app. get_tools_from_mcp_url/aget_tools_from_mcp_url fetch tools directly from a server URL or command. BasicMCPClient supports HTTP, SSE, and stdio transports, tool calling, resource listing/reading, prompt listing/getting, and OAuth 2.0 authentication.
How to install
pip install llama-index-tools-mcp
Who it's for
Developers building LlamaIndex agents that need to call tools from existing MCP servers, or who want to expose their own LlamaIndex workflows as MCP servers for other clients to use.
Source README
MCP ToolSpec
This tool connects to MCP Servers and allows an Agent to call the tools provided by MCP Servers.
This idea is migrated from Integrate MCP Tools into LlamaIndex.
Installation
pip install llama-index-tools-mcp
Usage
Usage is as simple as connecting to an MCP Server and getting the tools.
from llama_index.tools.mcp import BasicMCPClient, McpToolSpec
### We consider there is a mcp server running on 127.0.0.1:8000, or you can use the mcp client to connect to your own mcp server.
mcp_client = BasicMCPClient("http://127.0.0.1:8000/sse")
mcp_tool_spec = McpToolSpec(
client=mcp_client,
# Optional: Filter the tools by name
# allowed_tools=["tool1", "tool2"],
# Optional: Include resources in the tool list
# include_resources=True,
)
### sync
tools = mcp_tool_spec.to_tool_list()
### async
tools = await mcp_tool_spec.to_tool_list_async()
Then you can use the tools in your agent!
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.openai import OpenAI
agent = FunctionAgent(
name="Agent",
description="Some description",
llm=OpenAI(model="gpt-4o"),
tools=tools,
system_prompt="You are a helpful assistant.",
)
resp = await agent.run("What is the weather in Tokyo?")
Helper Functions
This package also includes several helper functions for working with MCP Servers.
workflow_as_mcp
This function converts a Workflow to an MCP app.
from llama_index.core.workflow import (
Context,
Workflow,
Event,
StartEvent,
StopEvent,
step,
)
from llama_index.tools.mcp import workflow_as_mcp
class RunEvent(StartEvent):
msg: str
class InfoEvent(Event):
msg: str
class LoudWorkflow(Workflow):
"""Useful for converting strings to uppercase and making them louder."""
@step
def step_one(self, ctx: Context, ev: RunEvent) -> StopEvent:
ctx.write_event_to_stream(InfoEvent(msg="Hello, world!"))
return StopEvent(result=ev.msg.upper() + "!")
workflow = LoudWorkflow()
mcp = workflow_as_mcp(workflow, start_event_model=RunEvent)
Then, you can launch the MCP server (assuming you have the mcp[cli] extra installed):
mcp dev script.py
get_tools_from_mcp_url / aget_tools_from_mcp_url
This function get a list of FunctionTools from an MCP server or command.
from llama_index.tools.mcp import (
get_tools_from_mcp_url,
aget_tools_from_mcp_url,
)
tools = get_tools_from_mcp_url("http://127.0.0.1:8000/sse")
### async
tools = await get_tools_from_mcp_url("http://127.0.0.1:8000/sse")
MCP Client Usage
The BasicMCPClient provides comprehensive access to MCP server capabilities beyond just tools.
Basic Client Operations
from llama_index.tools.mcp import BasicMCPClient
### Connect to an MCP server using different transports
http_client = BasicMCPClient("https://example.com/mcp") # Streamable HTTP
sse_client = BasicMCPClient("https://example.com/sse") # Server-Sent Events
local_client = BasicMCPClient("python", args=["server.py"]) # stdio
### List available tools
tools = await http_client.list_tools()
### Call a tool
result = await http_client.call_tool("calculate", {"x": 5, "y": 10})
### List available resources
resources = await http_client.list_resources()
### Read a resource
content, mime_type = await http_client.read_resource("config://app")
### List available prompts
prompts = await http_client.list_prompts()
### Get a prompt
prompt_result = await http_client.get_prompt("greet", {"name": "World"})
OAuth Authentication
The client supports OAuth 2.0 authentication for connecting to protected MCP servers:
from llama_index.tools.mcp import BasicMCPClient
#### Simple authentication with in-memory token storage
client = BasicMCPClient.with_oauth(
"https://api.example.com/mcp",
client_name="My App",
redirec
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.