Skip to main content
Glama
baidu

Baidu Vector Database MCP Server

Official
by baidu

create_vector_index

Create a vector index on a vector field in Baidu Vector Database to enable efficient similarity searches for AI applications.

Instructions

Create a vector index on a vector type field in the Mochow instance.

Args:
    table_name (str): Name of the table.
    index_name (str): Name of the index.
    field_name (str): Name of the vector field.
    index_type (str): Type of vector index. Supported values are "HNSW", "HNSWPQ", "HNSWSQ".
    metric_type (str): Distance metric. Supported values are "L2", "COSINE", "IP".
    params (Optional[dict[str, Any]]): Additional vector index parameters.

Returns:
    str: A message indicating the success of index creation.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
table_nameYes
index_nameYes
field_nameYes
index_typeNoHNSW
metric_typeNoL2
paramsNo

Implementation Reference

  • The MCP tool handler function decorated with @mcp.tool(), which registers and implements the 'create_vector_index' tool by delegating to the MochowConnector.
    @mcp.tool()
    async def create_vector_index(
        table_name: str,
        index_name: str,
        field_name: str,
        index_type: str = "HNSW",
        metric_type: str = "L2",
        params: Optional[dict[str, Any]] = None,
        ctx: Context = None) -> str:
        """
        Create a vector index on a vector type field in the Mochow instance.
    
        Args:
            table_name (str): Name of the table.
            index_name (str): Name of the index.
            field_name (str): Name of the vector field.
            index_type (str): Type of vector index. Supported values are "HNSW", "HNSWPQ", "HNSWSQ".
            metric_type (str): Distance metric. Supported values are "L2", "COSINE", "IP".
            params (Optional[dict[str, Any]]): Additional vector index parameters.
    
        Returns:
            str: A message indicating the success of index creation.
        """
        connector = ctx.request_context.lifespan_context.connector
        await connector.create_vector_index(table_name, index_name, field_name, index_type, metric_type, params)
        return f"Vector index '{index_name}' created successfully"
  • The helper method in MochowConnector class that performs the actual vector index creation using the MochowClient, including validation, parameter mapping, and index creation.
    async def create_vector_index(
        self,
        table_name: str,
        index_name: str,
        field_name: str,
        index_type: str = "HNSW",
        metric_type: str = "L2",
        params: Optional[dict[str, Any]] = None,
    ) -> bool:
        """
        Create a vector index on a given vector field.
    
        Args:
            table_name (str): Name of the table.
            index_name (str): Name of the index.
            field_name (str): Name of the vector field.
            index_type (str): Type of vector index. Supported values are "HNSW", "HNSWPQ", "PUCK".
            metric_type (str): Distance metric. Supported values are "L2", "COSINE", "IP".
            params (Optional[dict[str, Any]]): Additional vector index parameters.
    
        Returns:
            bool: True if the index is created successfully, False otherwise.
        """
        if self.database is None:
            raise ValueError("Switch to the database before create vector index")
    
        # check vector index
        index_existed = True
        try:
            self.database.table(table_name).describe_index(index_name)
        except ServerError as e:
            if e.code == ServerErrCode.INDEX_NOT_EXIST:
                index_existed = False
            else:
                raise ValueError(f"Failed to get index detail: {str(e)}")
    
        # index already existed with same name
        if index_existed:
            raise ValueError(f"Index already existed with same name '{index_name}'")
    
        # create vector index
        index_metric_type = None
        for k, v in MetricType.__members__.items():
            if k == metric_type:
                index_metric_type = v
        if index_metric_type is None:
            raise ValueError("Only the three metric types of L2, COSINE, and IP are supported.")
    
        indexes = []
        if index_type == "HNSW":
            indexes.append(
                VectorIndex(
                    index_name=index_name, index_type=IndexType.HNSW, field=field_name,
                    metric_type=index_metric_type, auto_build=False,
                    params=HNSWParams(m=params.get("M", 16), efconstruction=params.get("efConstruction", 200))))
        elif index_type == "HNSWPQ":
            indexes.append(
                VectorIndex(
                    index_name=index_name, index_type=IndexType.HNSW, field=field_name,
                    metric_type=index_metric_type, auto_build=False,
                    params=HNSWPQParams(
                        m=params.get("M", 16), efconstruction=params.get("efConstruction", 200),
                        NSQ=params.get("NSQ", 8), samplerate=params.get("sampleRate", 1.0))))
        elif index_type == "PUCK":
            indexes.append(
                VectorIndex(
                    index_name=index_name, index_type=IndexType.HNSW, field=field_name,
                    metric_type=index_metric_type, auto_build=False,
                    params=PUCKParams(
                        coarseClusterCount=params.get("coarseClusterCount", 5),
                        fineClusterCount=params.get("fineClusterCount", 5))))
        else:
            raise ValueError("Only the three vector index types of HNSW, HNSWPQ, PUCK are supported.")
    
        try:
            self.database.table(table_name).create_indexes(indexes)
            return True
        except Exception as e:
            raise ValueError(f"Failed to create vector index: {str(e)}")
  • The docstring in the handler function defining the input schema, supported values, and output format for the create_vector_index tool.
    """
    Create a vector index on a vector type field in the Mochow instance.
    
    Args:
        table_name (str): Name of the table.
        index_name (str): Name of the index.
        field_name (str): Name of the vector field.
        index_type (str): Type of vector index. Supported values are "HNSW", "HNSWPQ", "HNSWSQ".
        metric_type (str): Distance metric. Supported values are "L2", "COSINE", "IP".
        params (Optional[dict[str, Any]]): Additional vector index parameters.
    
    Returns:
        str: A message indicating the success of index creation.
    """

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/baidu/mochow-mcp-server-python'

If you have feedback or need assistance with the MCP directory API, please join our Discord server