|
| 1 | +""" |
| 2 | +Semantic Search exposed as an MCP tool |
| 3 | +
|
| 4 | +Author: L. Saetta |
| 5 | +License: MIT |
| 6 | +""" |
| 7 | + |
| 8 | +from typing import Annotated |
| 9 | +from pydantic import Field |
| 10 | +import oracledb |
| 11 | +from fastmcp import FastMCP |
| 12 | +from langchain_community.vectorstores.utils import DistanceStrategy |
| 13 | +from langchain_community.embeddings import OCIGenAIEmbeddings |
| 14 | +from langchain_community.vectorstores.oraclevs import OracleVS |
| 15 | +from utils import get_console_logger |
| 16 | + |
| 17 | +from config import DEBUG |
| 18 | +from config import AUTH, EMBED_MODEL_ID, SERVICE_ENDPOINT, COMPARTMENT_ID |
| 19 | +from config import TRANSPORT, HOST, PORT |
| 20 | +from config_private import CONNECT_ARGS |
| 21 | + |
| 22 | +logger = get_console_logger() |
| 23 | + |
| 24 | +mcp = FastMCP("Demo Semantic Search as MCP server") |
| 25 | + |
| 26 | + |
| 27 | +# |
| 28 | +# Helper functions |
| 29 | +# |
| 30 | +def get_connection(): |
| 31 | + """ |
| 32 | + get a connection to the DB |
| 33 | + """ |
| 34 | + return oracledb.connect(**CONNECT_ARGS) |
| 35 | + |
| 36 | + |
| 37 | +def get_embedding_model(): |
| 38 | + """ |
| 39 | + Create the Embedding Model |
| 40 | + """ |
| 41 | + embed_model = OCIGenAIEmbeddings( |
| 42 | + auth_type=AUTH, |
| 43 | + model_id=EMBED_MODEL_ID, |
| 44 | + service_endpoint=SERVICE_ENDPOINT, |
| 45 | + compartment_id=COMPARTMENT_ID, |
| 46 | + ) |
| 47 | + return embed_model |
| 48 | + |
| 49 | + |
| 50 | +@mcp.tool |
| 51 | +def semantic_search( |
| 52 | + query: Annotated[ |
| 53 | + str, Field(description="The search query to find relevant documents.") |
| 54 | + ], |
| 55 | + top_k: Annotated[int, Field(description="TOP_K parameter for search")] = 5, |
| 56 | + collection_name: Annotated[ |
| 57 | + str, Field(description="The name of DB table") |
| 58 | + ] = "BOOKS", |
| 59 | +) -> dict: |
| 60 | + """ |
| 61 | + Perform a semantic search based on the provided query. |
| 62 | + Args: |
| 63 | + query (str): The search query. |
| 64 | + top_k (int): The number of top results to return. |
| 65 | + Returns: |
| 66 | + dict: a dictionary containing the relevant documents. |
| 67 | + """ |
| 68 | + try: |
| 69 | + # must be the same embedding model used during load in the Vector Store |
| 70 | + embed_model = get_embedding_model() |
| 71 | + |
| 72 | + # get a connection to the DB and init VS |
| 73 | + with get_connection() as conn: |
| 74 | + v_store = OracleVS( |
| 75 | + client=conn, |
| 76 | + table_name=collection_name, |
| 77 | + distance_strategy=DistanceStrategy.COSINE, |
| 78 | + embedding_function=embed_model, |
| 79 | + ) |
| 80 | + |
| 81 | + relevant_docs = v_store.similarity_search(query=query, k=top_k) |
| 82 | + |
| 83 | + if DEBUG: |
| 84 | + logger.info("Result from similarity search:") |
| 85 | + logger.info(relevant_docs) |
| 86 | + |
| 87 | + except Exception as e: |
| 88 | + logger.error("Error in vector_store.invoke: %s", e) |
| 89 | + error = str(e) |
| 90 | + return {"error": error} |
| 91 | + |
| 92 | + result = {"relevant_docs": relevant_docs} |
| 93 | + |
| 94 | + return result |
| 95 | + |
| 96 | +@mcp.tool |
| 97 | +def get_collections() -> list: |
| 98 | + """ |
| 99 | + Get the list of collections (DB tables) available in the Oracle Vector Store. |
| 100 | + Returns: |
| 101 | + list: A list of collection names. |
| 102 | + """ |
| 103 | + with get_connection() as conn: |
| 104 | + cursor = conn.cursor() |
| 105 | + |
| 106 | + cursor.execute( |
| 107 | + """SELECT DISTINCT utc.table_name |
| 108 | + FROM user_tab_columns utc |
| 109 | + WHERE utc.data_type = 'VECTOR' |
| 110 | + ORDER BY 1 ASC""" |
| 111 | + ) |
| 112 | + collections = [row[0] for row in cursor.fetchall()] |
| 113 | + return collections |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + mcp.run( |
| 117 | + transport=TRANSPORT, |
| 118 | + # Bind to all interfaces |
| 119 | + host=HOST, |
| 120 | + port=PORT, |
| 121 | + log_level="INFO", |
| 122 | + ) |
0 commit comments