使用外部 MCP 服务器

重要

此功能在 Beta 版中。

将 Databricks 连接到外部模型上下文协议 (MCP) 服务器,使代理能够访问 Databricks 外部托管的更广泛的工具。 外部 MCP 服务器是托管在 Databricks 外部的第三方 MCP 服务器,通过 Databricks 托管的代理进行连接。

身份验证和安全性

Databricks 使用托管的 MCP 代理和 Unity Catalog HTTP 连接以安全方式处理工作区的身份验证。

  • 安全令牌管理:Databricks 自动处理所有 OAuth 流和令牌刷新
  • 未公开凭据:永远不会向最终用户公开令牌
  • 集中式身份验证:使用 Unity 目录连接的一致身份验证模式

要求

创建 Unity 目录 HTTP 连接

若要连接到外部 MCP 服务器,必须创建与服务器的 Unity 目录 HTTP 连接。

  1. 创建 HTTP 连接,请参阅 HTTP 连接
  2. 创建 HTTP 连接时,选中 “是 mcp 连接 ”复选框以启用 MCP 功能。
  3. 验证连接 URL 是否指向有效的 MCP 服务器终结点。

创建 UC 连接后,Databricks 会自动预配充当 Databricks 托管 MCP 服务器的代理终结点。 此终结点安全地将流量代理到外部 MCP 服务器,管理令牌注入和续订。

代理终结点 URL 采用以下格式:

https://<workspace-hostname>/api/2.0/mcp/external/{connection_name}

使用 AI Playground 进行连接

可以直接在 AI Playground 中测试外部 MCP 服务器,而无需编写任何代码:

  1. 导航到 Databricks 工作区中的 AI 游乐场。

  2. 选择带有“工具已启用”标签的模型。

  3. 单击 “工具 > + 添加”工具 ,然后从可用工具选项中选择 MCP 服务器

  4. “MCP 服务器” 部分中,选择“ 外部 MCP 服务器 ”以浏览可用的外部连接。

  5. 选择之前创建的 Unity 目录 HTTP 连接(例如)。 github_u2m_connection

  6. 与 LLM 聊天以测试它如何与外部 MCP 服务器工具交互。 AI Playground 会自动从外部 MCP 服务器发现可用的工具,并使其可供 LLM 使用。

这样,就可以在生成完整代理或部署到生产环境之前快速原型和测试外部 MCP 服务器集成。

连接到外部 MCP 服务器

使用代理 URL 连接到外部 MCP 服务器。 Databricks 代理使外部服务器的行为类似于托管 MCP 服务器。

Databricks MCP 客户端

建议的方法通过将代理终结点添加到 MANAGED_MCP_SERVER_URLS 列表中,将外部 MCP 服务器视为 Databricks 托管服务器。

from databricks.sdk import WorkspaceClient
from databricks_mcp import DatabricksMCPClient

# Initialize workspace client
workspace_client = WorkspaceClient()
host = workspace_client.config.host

# External MCP servers are proxied as managed servers, allowing you
# to use the same API for both managed and external servers
MANAGED_MCP_SERVER_URLS = [
    f"{host}/api/2.0/mcp/functions/system/ai",  # Default managed MCP
    f"{host}/api/2.0/mcp/external/github_u2m_connection"  # External MCP proxy
]

若要在代理中使用外部 MCP 服务器,请将代理 URL 传递给 managed_server_urls 参数:

# Use with agents - external servers work just like managed ones
import asyncio
from your_agent_code import create_mcp_tools  # Your agent's tool creation function

# Create tools from both managed and external (proxied) servers
mcp_tools = asyncio.run(
    create_mcp_tools(
        ws=workspace_client,
        managed_server_urls=MANAGED_MCP_SERVER_URLS
    )
)

还可以使用 Databricks MCP 客户端直接调用该工具:

# Direct tool call using DatabricksMCPClient
mcp_client = DatabricksMCPClient(
    server_url=f"{host}/api/2.0/mcp/external/github_u2m_connection",
    workspace_client=workspace_client
)

# List available tools
tools = mcp_client.list_tools()
print(f"Available tools: {[tool.name for tool in tools]}")

# Call a tool
response = mcp_client.call_tool(
    "list_commits",
    {"owner": "mlflow", "repo": "mlflow", "sha": "master"}
)
print(response.content[0].text)

标准 MCP SDK

使用标准 MCP SDK 和 async/await 连接到外部 MCP 服务器:

%pip install -U databricks-sdk databricks_mcp tabulate databricks_ai_bridge
%restart_python

import json
from databricks.sdk import WorkspaceClient
from databricks_mcp import DatabricksOAuthClientProvider
from databricks.sdk.credentials_provider import ModelServingUserCredentials
from mcp.client.streamable_http import streamablehttp_client as connect
from mcp import ClientSession
from tabulate import tabulate


async def main():
    app_url = "https://<workspace-hostname>/api/2.0/mcp/external/github_u2m_connection"
    client = WorkspaceClient()

    async with connect(app_url, auth=DatabricksOAuthClientProvider(client)) as (
        read_stream,
        write_stream,
        _,
    ):
        async with ClientSession(read_stream, write_stream) as session:
            init = await session.initialize()
            print(json.dumps(init.model_dump(), indent=2))
            tools = await session.list_tools()
            print(json.dumps(tools.model_dump(), indent=2))

            arguments = {
                "owner": "mlflow",
                "repo": "mlflow",
                "sha": "master"
            }
            response = await session.call_tool(name="list_commits", arguments=arguments)

            data = json.loads(response.content[0].text)
            rows = []
            for commit in data:
                author = commit.get("commit", {}).get("author", {}).get("name")
                message = commit.get("commit", {}).get("message", "").split("\n")[0]
                html_url = commit.get("html_url", "")
                rows.append([author, message, html_url])

            # Print as table
            print(tabulate(rows, headers=["Author", "Message", "Commit URL"], tablefmt="github"))

await main()

示例笔记本:使用 Databricks MCP 服务器生成代理

以下笔记本演示如何创作调用 MCP 工具的 LangGraph 和 OpenAI 代理,包括通过 Databricks 代理终结点访问的外部 MCP 服务器。

LangGraph MCP 工具调用代理

获取笔记本

OpenAI MCP 工具调用代理

获取笔记本

局限性

  • 不支持使用专用链接与您的VPC中的资源建立专用连接。 如果需要此功能,请联系支持团队。

后续步骤