AI 代理通常需要查询或作结构化数据来回答问题、更新记录或创建数据管道。
Databricks 提供了多种方法,用于将代理连接到 Unity 目录表和外部数据存储中的结构化数据。 使用预配置的 MCP 服务器立即访问 Genie 空间和 SQL 仓库,或为专用工作流生成自定义工具。
本页演示如何:
查询 Unity 目录表中的数据
如果您的代理需要查询 Unity Catalog 表中的数据,Databricks 建议使用 Genie spaces。 Genie 空间是一个由最多 25 个 Unity Catalog 表组成的集合,Genie 可以使用自然语言进行上下文管理和查询。 代理可以使用预配置的 MCP URL 访问 Genie 空间。
连接到Genie空间:
- 使用要查询的表创建 Genie 空间,并与必须访问该空间的用户或服务主体共享空间。 请参阅 设置和管理 Genie 空间。
- 创建代理并将其连接到空间的预配置的托管 MCP URL:
https://<workspace-hostname>/api/2.0/mcp/genie/{genie_space_id}。
注释
Genie 的托管 MCP 服务器调用 Genie 作为 MCP 工具,这意味着调用 Genie API 时不会传递历史记录。
将 Genie 空间工具添加到代理系统
以下示例演示如何将代理连接到 Genie space MCP 服务器。 使用您的 Genie 空间的 ID 替换 <genie-space-id>
OpenAI 代理 SDK (应用)
from agents import Agent, Runner
from databricks.sdk import WorkspaceClient
from databricks_openai.agents import McpServer
workspace_client = WorkspaceClient()
host = workspace_client.config.host
async with McpServer(
url=f"{host}/api/2.0/mcp/genie/<genie-space-id>",
name="genie-space",
workspace_client=workspace_client,
) as genie_server:
agent = Agent(
name="Data analyst agent",
instructions="You are a data analyst. Use the Genie tool to query structured data and answer questions.",
model="databricks-claude-sonnet-4-5",
mcp_servers=[genie_server],
)
result = await Runner.run(agent, "What were the top 10 customers by revenue last quarter?")
print(result.final_output)
请在 databricks.yml 中为应用授予访问 Genie 空间的权限。
resources:
apps:
my_agent_app:
resources:
- name: 'my_genie_space'
genie_space:
space_id: '<genie-space-id>'
permission: 'CAN_RUN'
LangGraph (应用)
from databricks.sdk import WorkspaceClient
from databricks_langchain import ChatDatabricks, DatabricksMCPServer, DatabricksMultiServerMCPClient
from langgraph.prebuilt import create_react_agent
workspace_client = WorkspaceClient()
host = workspace_client.config.host
mcp_client = DatabricksMultiServerMCPClient([
DatabricksMCPServer(
name="genie-space",
url=f"{host}/api/2.0/mcp/genie/<genie-space-id>",
workspace_client=workspace_client,
),
])
async with mcp_client:
tools = await mcp_client.get_tools()
agent = create_react_agent(
ChatDatabricks(endpoint="databricks-claude-sonnet-4-5"),
tools=tools,
)
result = await agent.ainvoke(
{"messages": [{"role": "user", "content": "What were the top 10 customers by revenue last quarter?"}]}
)
print(result["messages"][-1].content)
授予应用对 databricks.yml 中 Genie 空间的访问权限:
resources:
apps:
my_agent_app:
resources:
- name: 'my_genie_space'
genie_space:
space_id: '<genie-space-id>'
permission: 'CAN_RUN'
模型服务
from databricks.sdk import WorkspaceClient
from databricks_mcp import DatabricksMCPClient
import mlflow
workspace_client = WorkspaceClient()
host = workspace_client.config.host
# Connect to the Genie space MCP server
mcp_client = DatabricksMCPClient(
server_url=f"{host}/api/2.0/mcp/genie/<genie-space-id>",
workspace_client=workspace_client,
)
# List available tools from the Genie space
tools = mcp_client.list_tools()
# Log the agent with the required resources for deployment
mlflow.pyfunc.log_model(
"agent",
python_model=my_agent,
resources=mcp_client.get_databricks_resources(),
)
若要部署代理,请参阅部署用于生成 AI 应用程序的代理(模型服务)。 有关使用 MCP 资源的日志记录代理的详细信息,请参阅 使用 Databricks 托管 MCP 服务器。
Genie 多代理系统
重要说明
此功能目前以公共预览版提供。
对于高级多代理系统,还可以使用 Genie 作为代理,而不是使用 MCP 集成它。 将 Genie 称为代理时,可以确定性地将现有聊天上下文传递给 Genie。
有关代码优先方法,请参阅在多代理系统中使用 Genie(模型服务)。 有关 UI 优先方法,请参阅 使用监督程序代理创建协调的多代理系统。
使用 Unity 目录 SQL 函数工具查询数据
当提前知道查询并且代理提供参数时,使用 Unity 目录 SQL 函数创建结构化检索工具。
以下示例创建一个名为lookup_customer_info的 Unity Catalog 函数,该函数允许 AI 代理从假设的 customer_data 表检索结构化数据。
在 SQL 编辑器中运行以下代码。
CREATE OR REPLACE FUNCTION main.default.lookup_customer_info(
customer_name STRING COMMENT 'Name of the customer whose info to look up'
)
RETURNS STRING
COMMENT 'Returns metadata about a particular customer, given the customer's name, including the customer's email and ID. The
customer ID can be used for other queries.'
RETURN SELECT CONCAT(
'Customer ID: ', customer_id, ', ',
'Customer Email: ', customer_email
)
FROM main.default.customer_data
WHERE customer_name = customer_name
LIMIT 1;
创建 Unity 目录工具后,将其添加到代理。 请参阅 “创建 Unity 目录”函数工具。