跳转到内容

将 MCP 工具箱与 LlamaIndex 结合使用

使用MCP工具箱将您的数据库与LlamaIndex智能体集成。

数据库MCP工具箱是一个开源的数据库MCP服务器。它的设计着眼于企业级和生产质量。通过处理连接池、身份验证等复杂问题,它使您能够更轻松、更快速、更安全地开发工具。

工具箱工具可以与LlamaIndex应用程序无缝集成。有关入门指南配置MCP工具箱的更多信息,请参阅文档

architecture

工具箱是一个开源服务器,由您自行部署和管理。有关部署和配置的更多说明,请参阅官方工具箱文档:

在开始之前,请先安装与LlamaIndex兼容的MCP工具箱SDK包:

pip install toolbox-llamaindex

一旦您的工具箱服务器配置完成并运行起来,您就可以从服务器加载工具:

import asyncio
import os
from llama_index.core.agent.workflow import AgentWorkflow
from llama_index.core.workflow import Context
from llama_index.llms.google_genai import GoogleGenAI
from toolbox_llamaindex import ToolboxClient
prompt = """
You're a helpful hotel assistant. You handle hotel searching, booking and
cancellations. When the user searches for a hotel, mention it's name, id,
location and price tier. Always mention hotel ids while performing any
searches. This is very important for any operations. For any bookings or
cancellations, please provide the appropriate confirmation. Be sure to
update checkin or checkout dates if mentioned by the user.
Don't ask for confirmations from the user.
"""
queries = [
"Find hotels in Basel with Basel in it's name.",
"Can you book the Hilton Basel for me?",
"Oh wait, this is too expensive. Please cancel it and book the Hyatt Regency instead.",
"My check in dates would be from April 10, 2024 to April 19, 2024.",
]
async def run_application():
llm = GoogleGenAI(
api_key=os.getenv("GOOGLE_API_KEY"),
model="gemini-2.0-flash-001",
)
# llm = GoogleGenAI(
# model="gemini-2.0-flash-001",
# vertexai_config={"project": "project-id", "location": "us-central1"},
# )
# Load the tools from the Toolbox server
async with ToolboxClient("http://127.0.0.1:5000") as client:
tools = await client.aload_toolset()
agent = AgentWorkflow.from_tools_or_functions(
tools,
llm=llm,
)
for tool in tools:
print(tool.metadata)
ctx = Context(agent)
for query in queries:
response = await agent.run(user_msg=query, ctx=ctx)
print()
print(f"---- {query} ----")
print(str(response))
await run_application()
ToolMetadata(description='Book a hotel by its ID. If the hotel is successfully booked, returns a NULL, raises an error if not.\n\nArgs:\n hotel_id (str): The ID of the hotel to book.', name='book-hotel', fn_schema=<class 'toolbox_core.utils.book-hotel'>, return_direct=False)
ToolMetadata(description='Cancel a hotel by its ID.\n\nArgs:\n hotel_id (str): The ID of the hotel to cancel.', name='cancel-hotel', fn_schema=<class 'toolbox_core.utils.cancel-hotel'>, return_direct=False)
ToolMetadata(description='Search for hotels based on location.\n\nArgs:\n location (str): The location of the hotel.', name='search-hotels-by-location', fn_schema=<class 'toolbox_core.utils.search-hotels-by-location'>, return_direct=False)
ToolMetadata(description='Search for hotels based on name.\n\nArgs:\n name (str): The name of the hotel.', name='search-hotels-by-name', fn_schema=<class 'toolbox_core.utils.search-hotels-by-name'>, return_direct=False)
ToolMetadata(description="Update a hotel's check-in and check-out dates by its ID. Returns a message indicating whether the hotel was successfully updated or not.\n\nArgs:\n hotel_id (str): The ID of the hotel to update.\n checkin_date (str): The new check-in date of the hotel.\n checkout_date (str): The new check-out date of the hotel.", name='update-hotel', fn_schema=<class 'toolbox_core.utils.update-hotel'>, return_direct=False)
---- Find hotels in Basel with Basel in it's name. ----
OK. I found three hotels in Basel with Basel in the name: Holiday Inn Basel, Hilton Basel, and Hyatt Regency Basel.
---- Can you book the Hilton Basel for me? ----
OK. I have booked the Hilton Basel for you.
---- Oh wait, this is too expensive. Please cancel it and book the Hyatt Regency instead. ----
OK. I have booked the Hyatt Regency Basel for you.
---- My check in dates would be from April 10, 2024 to April 19, 2024. ----
OK. I have updated your check-in date to April 10, 2024 and your check-out date to April 19, 2024 for the Hyatt Regency Basel.

工具箱拥有多种功能,便于开发面向数据库的生成式人工智能工具。 如需了解更多信息,请阅读以下功能详情:

  • 认证参数: 自动将工具输入与OIDC令牌中的值绑定,使得运行敏感查询时不会潜在泄露数据
  • 授权调用: 根据用户认证令牌限制对工具的使用权限
  • OpenTelemetry: 通过 OpenTelemetry 从 Toolbox 获取指标和追踪数据