Databricks SQL

Importante

Questa funzionalità è in Anteprima Pubblica.

Il server SQL MCP di Databricks è un server MCP gestito da Azure Databricks che permette agli agenti di eseguire SQL generato dall'IA sulle tabelle del tuo Catalogo Unity per leggere e scrivere dati, con accesso governato dai permessi del Catalogo Unity. Usalo per costruire pipeline di dati con strumenti di codifica AI. Le query vengono eseguite in modo asincrono: l'agente chiama lo strumento per avviare una query, poi interroga fino al completamento della risposta.

Modello URL Ambito OAuth
https://<workspace-hostname>/api/2.0/mcp/sql sql

_meta Parametri

_meta i parametri sono valori di configurazione che preimposta nel codice dell'agente per impostare il comportamento del server MCP in modo deterministico, invece di lasciare che l'LLM li generi dinamicamente al momento della chiamata dello strumento. Il server SQL MCP di Databricks supporta il seguente _meta parametro:

Nome del parametro Type Description
warehouse_id str ID del warehouse SQL da usare per l'esecuzione di query.
Esempio: "a1b2c3d4e5f67890"
Se non specificato, il sistema seleziona automaticamente un magazzino in base alle risorse e alle autorizzazioni.

Esempio: specificare un'istanza di SQL Warehouse per le query SQL di Databricks

Questo esempio illustra come usare il warehouse_id_meta parametro per specificare quale SQL Warehouse esegue query dal server SQL MCP di Databricks usando il Python MCP SDK ufficiale.

In questo scenario si vuole:

  • Usare un'istanza specifica di SQL Warehouse per l'esecuzione di query anziché lasciare che il sistema ne selezioni automaticamente uno
  • Verificare prestazioni coerenti in base al routing delle query in un warehouse dedicato

Per eseguire questo esempio, configurare l'ambiente Python per lo sviluppo MCP gestito:

Per trovare l'ID del tuo SQL Warehouse, vedere Connetti a un SQL Warehouse.

# 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>/api/2.0/mcp/sql"

    # 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())