Databricks SQL MCP 伺服器

Databricks SQL MCP 伺服器讓代理程式能對你的 Unity Catalog 資料表執行 AI 生成的 SQL 來讀寫資料,存取權限受 Unity Catalog 權限管理。 查詢以非同步方式執行:代理呼叫工具開始查詢,然後輪詢直到回應完成。

Important

要使用 system.ai.dbsql、system.ai.sandbox 或 system.ai.web_search,帳號管理員必須從帳號主控台的 預覽 頁面啟用 Unity Gateway 測試版。 請參閱 管理帳戶預覽功能。

使用這台伺服器進行開發與資料工程:執行你或你的程式代理撰寫的特定查詢、檢查結構、驗證 SQL 語法,以及從 AI 編碼工具撰寫資料管線。 它讓你能確定性地控制執行的精確 SQL。

MCP 預設允許讀寫。 若要將其設為唯讀,請在內建的 true 政策中,將 system.ai.dbsql_policy 設為 disallow_writes。 請參閱套用原則。

連接您的代理程式

請使用這個網址,搭配你的程式碼代理設定指南或 Python 代理設定:

URL 模式 OAuth 範圍
https://<workspace-hostname>/ai-gateway/mcp-services/system.ai.dbsql ai-gateway

請你的代理人執行SELECT 1 AS result。 確認它呼叫 MCP 工具並回傳 1。

_meta 參數

_meta 參數是你在代理程式碼中預先設定的設定值,用來確定性地設定 MCP 伺服器的行為,而不是讓 LLM 在工具呼叫時動態產生參數。 Databricks SQL MCP 伺服器支援以下 _meta 參數:

參數名稱 類型 Description
warehouse_id str 用於執行查詢的 SQL 倉庫 ID。
範例:"a1b2c3d4e5f67890"
若未指定,系統會根據資源與權限自動選擇倉庫。

範例:指定一個 SQL 倉庫用於 Databricks SQL 查詢

此範例展示了如何使用warehouse_id_meta參數指定哪個 SQL 倉庫會使用官方 Python MCP SDK 從 Databricks SQL MCP 伺服器執行查詢。

在此案例中,您想要:

  • 使用特定的 SQL 倉庫來執行查詢,而不是讓系統自動選擇一個
  • 透過將查詢路由到專用倉庫來驗證穩定的效能

若要執行此範例,請 設定 Python 環境以進行受管 MCP 開發:

要找到你的 SQL 倉庫 ID,請參見 「連接 SQL 倉庫」。

# Import required libraries for MCP client and Databricks authentication
import asyncio
from databricks.sdk import WorkspaceClient
from databricks_mcp.oauth_provider import DatabricksOAuthClientProvider
from mcp.client.streamable_http import streamablehttp_client
from mcp.client.session import ClientSession
from mcp.types import CallToolRequest, CallToolResult

async def run_dbsql_tool_call_with_meta():
    # Initialize Databricks workspace client for authentication
    workspace_client = WorkspaceClient()

    # Construct the MCP server URL for DBSQL
    # Replace <workspace-hostname> with your workspace hostname
    mcp_server_url = "https://<workspace-hostname>/ai-gateway/mcp-services/system.ai.dbsql"

    # Establish connection to the MCP server with OAuth authentication
    async with streamablehttp_client(
        url=mcp_server_url,
        auth=DatabricksOAuthClientProvider(workspace_client),
    ) as (read_stream, write_stream, _):

        # Create an MCP session for making tool calls
        async with ClientSession(read_stream, write_stream) as session:
            # Initialize the session before making requests
            await session.initialize()

            # Create the tool call request with warehouse_id in _meta
            request = CallToolRequest(
                method="tools/call",
                params={
                    # Tool name for executing SQL queries
                    "name": "execute_sql",

                    # Dynamic arguments - typically provided by your AI agent
                    "arguments": {
                        "query": "SELECT * FROM my_catalog.my_schema.my_table LIMIT 10"
                    },

                    # Meta parameters - specify which warehouse to use
                    "_meta": {
                        "warehouse_id": "a1b2c3d4e5f67890"  # Your SQL warehouse ID
                    }
                }
            )

            # Send the request and get the response
            response = await session.send_request(request, CallToolResult)
            return response

# Execute the async function and get results
response = asyncio.run(run_dbsql_tool_call_with_meta())

Limitations

  • 沒有語意上下文。 伺服器執行它所提供的 SQL。 它無法解析商業術語、度量定義或資料表關係,因此代理必須僅從結構推斷出這些。 對於以自然語言提出的分析問題,請使用 Genie One MCP 伺服器,該伺服器的答案以 Genie 本體論為基礎。
  • 結果大小。 伺服器會在工具回應中截斷大量結果集,以避免耗盡模型的上下文視窗。 回傳較少的列數,或用 SQL 聚合,以保持結果在限制內。
  • 非同步執行。 查詢不會同步回傳。 代理程式先開始查詢,然後輪詢直到查詢完成,因此必須處理進行中的狀態。

舊版工作區端點

對於現有的整合,請使用 https://<workspace-hostname>/api/2.0/mcp/sql 搭配 sql OAuth 範圍。 此端點使用 受管 MCP 伺服器工作空間預覽及底層資源權限。