Back to Llama Index

Import dependencies for Model Context Protocol (MCP) fastMCP server

docs/examples/tools/mcp_agent_tools.ipynb

0.14.211.5 KB
Original Source

Use LlamaIndex agent tools as MCP tools

We have dozens of agent tools in LlamaHub and they can all be instantly used as MCP tools! This notebook shows how exactly that's done, using the Notion Tool as an example.

First we install our tool, and our MCP server:

python
!pip install llama-index-tools-notion mcp fastmcp

Bring in our dependencies:

python
# Import dependencies for Model Context Protocol (MCP) fastMCP server
from typing import Any, Dict, List, Optional
from fastmcp import FastMCP

print("MCP fastMCP server dependencies imported successfully!")

Instantiate our tools using an API key:

python
# Import and configure LlamaIndex Notion Tool Spec
from llama_index.tools.notion import NotionToolSpec

notion_token = "xxxx"
tool_spec = NotionToolSpec(integration_token=notion_token)

Let's see what tools are available:

python
tools = tool_spec.to_tool_list()

for i, tool in enumerate(tools):
    print(f"Tool {i+1}: {tool.metadata.name}")

Now we create and configure the fastMCP server, and register each tool:

python
mcp_server = FastMCP("MCP Agent Tools Server")

# Register the tools from the Notion ToolSpec
for tool in tools:
    mcp_server.tool(
        name=tool.metadata.name, description=tool.metadata.description
    )(tool.real_fn)

Now we can run our MCP server complete with our tools!

python
await mcp_server.run_async(transport="streamable-http")