|
| 1 | +import os |
| 2 | +from mcp.server.fastmcp import FastMCP |
| 3 | +from openai import OpenAI |
| 4 | +from agents import set_tracing_export_api_key |
| 5 | + |
| 6 | +# Create server |
| 7 | +mcp = FastMCP("Search Server") |
| 8 | +_vector_store_id = "" |
| 9 | + |
| 10 | +def _run_rag(query: str) -> str: |
| 11 | + """Do a search for answers within the knowledge base and internal documents of the user. |
| 12 | + Args: |
| 13 | + query: The user query |
| 14 | + """ |
| 15 | + results = client.vector_stores.search( |
| 16 | + vector_store_id=_vector_store_id, |
| 17 | + query=query, |
| 18 | + rewrite_query=True, # Query rewriting generally improves results |
| 19 | + ) |
| 20 | + return results.data[0].content[0].text |
| 21 | + |
| 22 | + |
| 23 | +def _summarize_rag_response(rag_output: str) -> str: |
| 24 | + """Summarize the RAG response using GPT-4 |
| 25 | + Args: |
| 26 | + rag_output: The RAG response |
| 27 | + """ |
| 28 | + response = client.responses.create( |
| 29 | + model="gpt-4.1-mini", |
| 30 | + tools=[{"type": "web_search_preview"}], |
| 31 | + input="Summarize the following text concisely: \n\n" + rag_output, |
| 32 | + ) |
| 33 | + return response.output_text |
| 34 | + |
| 35 | + |
| 36 | +@mcp.tool() |
| 37 | +def generate_rag_output(query: str) -> str: |
| 38 | + """Generate a summarized RAG output for a given query. |
| 39 | + Args: |
| 40 | + query: The user query |
| 41 | + """ |
| 42 | + print("[debug-server] generate_rag_output: ", query) |
| 43 | + rag_output = _run_rag(query) |
| 44 | + return _summarize_rag_response(rag_output) |
| 45 | + |
| 46 | + |
| 47 | +@mcp.tool() |
| 48 | +def run_web_search(query: str) -> str: |
| 49 | + """Run a web search for the given query. |
| 50 | + Args: |
| 51 | + query: The user query |
| 52 | + """ |
| 53 | + print("[debug-server] run_web_search:", query) |
| 54 | + response = client.responses.create( |
| 55 | + model="gpt-4.1-mini", |
| 56 | + tools=[{"type": "web_search_preview"}], |
| 57 | + input=query, |
| 58 | + ) |
| 59 | + return response.output_text |
| 60 | + |
| 61 | + |
| 62 | +def index_documents(directory: str): |
| 63 | + """Index the documents in the given directory to the vector store |
| 64 | + Args: |
| 65 | + directory: The directory to index the documents from |
| 66 | + """ |
| 67 | + # OpenAI supported file extensions for retrieval (see docs) |
| 68 | + SUPPORTED_EXTENSIONS = {'.pdf', '.txt', '.md', '.docx', '.pptx', '.csv', '.rtf', '.html', '.json', '.xml'} |
| 69 | + # Collect all files in the specified directory |
| 70 | + files = [os.path.join(directory, f) for f in os.listdir(directory)] |
| 71 | + # Filter files for supported extensions only |
| 72 | + supported_files = [] |
| 73 | + for file_path in files: |
| 74 | + _, ext = os.path.splitext(file_path) |
| 75 | + if ext.lower() in SUPPORTED_EXTENSIONS: |
| 76 | + supported_files.append(file_path) |
| 77 | + else: |
| 78 | + print(f"[warning] Skipping unsupported file for retrieval: {file_path}") |
| 79 | + |
| 80 | + vector_store = client.vector_stores.create( # Create vector store |
| 81 | + name="Support FAQ", |
| 82 | + ) |
| 83 | + global _vector_store_id |
| 84 | + _vector_store_id = vector_store.id |
| 85 | + |
| 86 | + for file_path in supported_files: |
| 87 | + # Upload each file to the vector store, ensuring the file handle is closed |
| 88 | + with open(file_path, "rb") as fp: |
| 89 | + client.vector_stores.files.upload_and_poll( |
| 90 | + vector_store_id=vector_store.id, |
| 91 | + file=fp |
| 92 | + ) |
| 93 | + print(f"[debug-server] uploading file: {file_path}") |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + oai_api_key = os.environ.get("OPENAI_API_KEY") |
| 98 | + if not oai_api_key: |
| 99 | + raise ValueError("OPENAI_API_KEY environment variable is not set") |
| 100 | + set_tracing_export_api_key(oai_api_key) |
| 101 | + client = OpenAI(api_key=oai_api_key) |
| 102 | + |
| 103 | + current_dir = os.path.dirname(os.path.abspath(__file__)) |
| 104 | + samples_dir = os.path.join(current_dir, "sample_files") |
| 105 | + index_documents(samples_dir) |
| 106 | + |
| 107 | + mcp.run(transport="sse") |
0 commit comments