共用方式為


使用外部 MCP 伺服器

這很重要

這項功能位於 測試版 (Beta) 中。 工作區管理員可以從 「預覽 」頁面控制對此功能的存取。 請參閱 管理 Azure Databricks 預覽。

將 Databricks 連線到外部模型內容通訊協定 (MCP) 伺服器,讓您的代理程式存取裝載在 Databricks 外部的更廣泛工具。 外部 MCP 伺服器是裝載在 Databricks 外部的第三方 MCP 伺服器,您可以透過 Databricks 管理的 Proxy 連線到這些伺服器。

驗證和安全性

Databricks 會使用受控 MCP Proxy 和 Unity 目錄 HTTP 連線 來安全地處理工作區的驗證:

  • 安全權杖管理:Databricks 會自動處理所有 OAuth 流程和權杖重新整理
  • 沒有公開的認證:權杖永遠不會公開給最終使用者
  • 集中式驗證:使用 Unity 目錄連線的一致驗證模式

需求

安裝外部 MCP 伺服器

你可以選擇三種方式安裝外部 MCP 伺服器來搭配代理程式使用。 所有方法都會建立 Unity 目錄連線,使 MCP 伺服器能安全且經過認證。

根據您的 MCP 伺服器選擇您的安裝方法:

來自 Databricks Marketplace

這很重要

這項功能目前處於 公開預覽版

直接從 Azure Databricks Marketplace 安裝策劃和預先設定的 MCP 伺服器:

  1. 在您的 Azure Databricks 工作區中,移至 Marketplace>代理程式>MCP 伺服器索引標籤。

  2. 找到您要安裝的 MCP 伺服器,然後按一下安裝。

  3. 在安裝對話方塊中,設定連線:

    • 連線名稱:輸入 Unity 目錄連線的名稱 (例如 github_connection)。
    • 主機名稱:主機網域已預先填入經精選的伺服器。
    • 基本路徑:基本路徑已預先填入已管理的伺服器清單。

    認證:輸入您的驗證認證。 必填欄位取決於伺服器的認證型別:

    • 對於 持有者權杖 驗證:提供您的持有者權杖。
    • 對於 OAuth U2M 驗證:提供您的用戶端 ID 和用戶端密碼。

    請參閱 MCP 伺服器提供者的文件,以取得這些認證的指示。

  4. 按一下 [安裝] 以建立連線。

如需 Marketplace 安裝的完整詳細資料,請參閱取得 外部 MCP 伺服器的存取權

自訂 HTTP 連線

建立 Unity 目錄 HTTP 連線以安裝任何 MCP 伺服器,包括 Marketplace 中無法使用的自我裝載或協力廠商伺服器。

  1. 建立 HTTP 連線。 請參閱 HTTP 連線

  2. 建立 HTTP 連線時,請選取 是 mcp 連線 核取方塊以啟用 MCP 功能。

  3. 確認連線 URL 指向有效的 MCP 伺服器端點。

動態客戶端註冊(實驗性)

這很重要

這項功能目前處於 公開預覽版

使用動態用戶端註冊(DCR)自動註冊並安裝支援 OAuth 2.0 DCR(RFC7591)的 MCP 伺服器。

DCR 自動處理 OAuth 發現、客戶端註冊及 Unity 目錄連線建立,無需手動設定憑證。

使用 DCR 必須符合以下條件:

  • MCP 伺服器必須支援 DCR 與 OAuth 2.0 授權。
  • MCP 伺服器必須公開 OAuth 元資料端點。
  1. 安裝或升級 databricks-mcp 套件以取得最新版本:

    %pip install -U databricks-mcp
    %restart_python
    
  2. 使用 DCR 註冊功能建立 Unity 目錄連線:

    from databricks.sdk import WorkspaceClient
    from databricks_mcp import register_mcp_server_via_dcr
    
    # Initialize workspace client
    workspace_client = WorkspaceClient()
    
    # Register MCP server via DCR
    connection_url = register_mcp_server_via_dcr(
        connection_name="my_mcp_server",
        mcp_url="https://mcp.example.com/api",
        workspace_client=workspace_client
    )
    
    print(f"Connection created: {connection_url}")
    

完成任何安裝方法後:

  • 該 Unity Catalog 連線是使用您提供的 MCP 伺服器詳細資料建立的。

  • Azure Databricks 會佈建受控 Proxy 端點,以安全地處理驗證和權杖管理

  • MCP 伺服器可在 AI Playground 中使用,並可在代理程式中以程式設計方式使用

  • 您可以透過 Unity 目錄許可權授與其他使用者連線的存取權

  • Proxy端點URL遵循以下格式:

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

若要檢視您的 MCP 伺服器,請移至您的工作區 >代理程式>MCP 伺服器

代理程式 MCP 伺服器索引標籤

共用 MCP 伺服器連線

USE CONNECTION 權限授與需要連接 MCP 伺服器的身分主體:

  1. 在您的工作區中,前往 目錄>連線> 您的連線>權限
  2. 授與身分識別 Unity 目錄連線的適當存取權。

使用 AI Playground 測試 MCP 伺服器

直接在 AI Playground 中測試 MCP 伺服器,無需編寫任何程式碼:

  1. 前往 Databricks 工作區中的 AI Playground。

  2. 選擇具有 [已啟用工具] 標籤的模型。

  3. 按一下工具 + 新增工具,>然後從可用的工具選項中選取 MCP 伺服器

  4. MCP 伺服器區 段中,選取 外部 MCP 伺服器以 瀏覽可用的連線。

  5. 選擇您稍早安裝的 Unity 目錄連線 (例如 github_connection)。

  6. 與 LLM 聊天以測試它如何與您的 MCP 伺服器工具互動。 AI Playground 會自動從您的 MCP 伺服器中發現可用的工具,並將它們提供給 LLM。

這可讓您在建置完整代理程式或部署至生產環境之前,快速建立原型並測試 MCP 伺服器整合。

以程式設計方式使用 MCP 伺服器

安裝 MCP 伺服器後,透過連線到 Proxy URL,在代理程式程式碼中以程式設計方式使用它。 Databricks Proxy 會讓外部伺服器的行為類似於受控 MCP 伺服器,處理驗證和權杖管理。

Databricks MCP 用戶端

建議的方法會將外部 MCP 伺服器視為 Databricks 受控伺服器,方法是將 Proxy 端點新增至您的 MANAGED_MCP_SERVER_URLS 清單。

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_connection"  # External MCP proxy
]

若要在代理程式中使用 MCP 伺服器,請將 Proxy 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_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_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 Proxy 端點存取的外部 MCP 伺服器。

LangGraph MCP 工具呼叫代理程式

拿筆記本

OpenAI MCP 工具呼叫代理程式

拿筆記本

局限性

  • 不支援使用 Private Link 與 VPC 中資源的私有連線。 如果您需要此功能,請聯絡您的支援團隊。

後續步驟