共用方式為


Databricks 向量搜尋 MCP 伺服器的元參數

這很重要

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

當您建置使用 Databricks 受控向量搜尋 MCP 伺服器的 AI 代理程式時,請使用參數 _meta 來決定性地控制工具行為,例如傳回和套用搜尋篩選的結果數目上限。

主要優點是您可以預設搜尋行為 (例如結果限制和篩選器),同時保持實際搜尋查詢的彈性,以便客服專員動態產生。

_meta 參數是官方 MCP 規範的一部分。

工具呼叫引數與 _meta 參數

向量搜尋 MCP 伺服器會以兩種方式處理參數:

  • 工具呼叫引數:通常由 LLM 根據使用者輸入動態產生的參數
  • _meta parameters:您可以在代理程式程式碼中預設的組態參數,以確定性地控制搜尋行為

支援的 _meta 參數

向量搜尋支援下列 _meta 參數:

參數名稱 類型 Description
num_results int 要傳回的結果數目。
預設值:5
filters str JSON 字串,其中包含要套用至搜尋的篩選條件。 必須是有效的 JSON 字串格式。
範例:'{"updated_after": "2024-01-01"}'
query_type str 用於擷取結果的搜尋演算法。
支援的值: "ANN" (近似最近鄰,預設)或 "HYBRID" (結合向量和關鍵字搜尋)
預設:"ANN"

如需這些參數的詳細資訊,請參閱 向量搜尋 API 文件

範例:設定向量搜尋擷取的最大結果和篩選器

此範例示範如何使用 _meta 參數來控制向量搜尋行為,同時允許來自 AI 代理程式的動態查詢。 它使用官方的 Python MCP SDK。

在此案例中,您想要:

  • 始終將搜尋結果限制為 3 個項目,以獲得一致的回應時間
  • 僅搜尋最近的文件(2024-01-01 之後更新)以確保相關性
  • 使用混合式搜尋可取得比純向量搜尋更高的準確度

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

# 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_vector_search_tool_call_with_meta():
    # Initialize Databricks workspace client for authentication
    workspace_client = WorkspaceClient()

    # Construct the MCP server URL for your specific catalog and schema
    # Replace <workspace-hostname>, YOUR_CATALOG, and YOUR_SCHEMA with your values
    mcp_server_url = "https://<workspace-hostname>/api/2.0/mcp/vector-search/YOUR_CATALOG/YOUR_SCHEMA"

    # 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 both dynamic and preset parameters
            request = CallToolRequest(
                method="tools/call",
                params={
                    # Tool name follows the pattern: CATALOG__SCHEMA__INDEX_NAME
                    "name": "YOUR_CATALOG__YOUR_SCHEMA__YOUR_INDEX_NAME",

                    # Dynamic arguments - typically provided by your AI agent or user input
                    "arguments": {
                        "query": "How do I reset my password?"  # This comes from your agent
                    },

                    # Meta parameters - preset configuration to control search behavior
                    "_meta": {
                        "num_results": 3,  # Limit to 3 results for consistent performance
                        "filters": '{"updated_after": "2024-01-01"}',  # JSON string for date filtering
                        "query_type": "HYBRID"  # Use hybrid search for better relevance
                    }
                }
            )

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

後續步驟