|
| 1 | +# MCP for Deep Research |
| 2 | + |
| 3 | +This is a minimal example of a Deep Research style MCP server for searching and fetching files from the OpenAI file storage service. |
| 4 | + |
| 5 | +For a reference of _how_ to call this service from the Responses API, with Deep Research see [this cookbook](https://cookbook.openai.com/examples/deep_research_api/introduction_to_deep_research_api). To see how to call the MCP server with the Agents SDK, checkout [this cookbook](https://cookbook.openai.com/examples/deep_research_api/how_to_use_deep_research_API_agents)! |
| 6 | + |
| 7 | +The Deep Research agent relies specifically on Search and Fetch tools. Search should look through your object store for a set of specfic, top-k IDs. Fetch, is a tool that takes objectIds as arguments and pulls back the relevant resources. |
| 8 | + |
| 9 | +## Set up & run |
| 10 | + |
| 11 | +Store your internal file(s) in [OpenAI Vector Storage](https://platform.openai.com/storage/vector_stores/) |
| 12 | + |
| 13 | +Python setup: |
| 14 | + |
| 15 | +```shell |
| 16 | +python3 -m venv env |
| 17 | +source env/bin/activate |
| 18 | +pip install -r requirements.txt |
| 19 | +``` |
| 20 | + |
| 21 | +Run the server: |
| 22 | + |
| 23 | +```shell |
| 24 | +python main.py |
| 25 | +``` |
| 26 | + |
| 27 | +The server will start on `http://0.0.0.0:8000/sse/` using SSE transport. If you want to reach the server from the public internet, there are a variety of ways to do that including with ngrok: |
| 28 | + |
| 29 | +```shell |
| 30 | +brew install ngrok |
| 31 | +ngrok config add-authtoken <your_token> |
| 32 | +ngrok http 8000 |
| 33 | +``` |
| 34 | + |
| 35 | +You should now be able to reach your local server from your client. |
| 36 | + |
| 37 | +## Files |
| 38 | + |
| 39 | +- `main.py`: [Main server code](https://github.com/openai/openai-cookbook/blob/main/examples/deep_research_api/how_to_build_a_deep_research_mcp_server/main.py) |
| 40 | + |
| 41 | +## Example Flow diagram for MCP Server |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +## Example request |
| 46 | + |
| 47 | +```python |
| 48 | +# system_message includes reference to internal file lookups for MCP. |
| 49 | +system_message = """ |
| 50 | +You are a professional researcher preparing a structured, data-driven report on behalf of a global health economics team. Your task is to analyze the health question the user poses. |
| 51 | +
|
| 52 | +Do: |
| 53 | +- Focus on data-rich insights: include specific figures, trends, statistics, and measurable outcomes (e.g., reduction in hospitalization costs, market size, pricing trends, payer adoption). |
| 54 | +- When appropriate, summarize data in a way that could be turned into charts or tables, and call this out in the response (e.g., "this would work well as a bar chart comparing per-patient costs across regions"). |
| 55 | +- Prioritize reliable, up-to-date sources: peer-reviewed research, health organizations (e.g., WHO, CDC), regulatory agencies, or pharmaceutical earnings reports. |
| 56 | +- Include an internal file lookup tool to retrieve information from our own internal data sources. If you've already retrieved a file, do not call fetch again for that same file. Prioritize inclusion of that data. |
| 57 | +- Include inline citations and return all source metadata. |
| 58 | +
|
| 59 | +Be analytical, avoid generalities, and ensure that each section supports data-backed reasoning that could inform healthcare policy or financial modeling. |
| 60 | +""" |
| 61 | + |
| 62 | +user_query = "Research the economic impact of semaglutide on global healthcare systems." |
| 63 | + |
| 64 | +response = client.responses.create( |
| 65 | + model="o3-deep-research-2025-06-26", |
| 66 | + input=[ |
| 67 | + { |
| 68 | + "role": "developer", |
| 69 | + "content": [ |
| 70 | + { |
| 71 | + "type": "input_text", |
| 72 | + "text": system_message, |
| 73 | + } |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "role": "user", |
| 78 | + "content": [ |
| 79 | + { |
| 80 | + "type": "input_text", |
| 81 | + "text": user_query, |
| 82 | + } |
| 83 | + ] |
| 84 | + } |
| 85 | + ], |
| 86 | + reasoning={ |
| 87 | + "summary": "auto" |
| 88 | + }, |
| 89 | + tools=[ |
| 90 | + { |
| 91 | + "type": "web_search_preview" |
| 92 | + }, |
| 93 | + { # ADD MCP TOOL SUPPORT |
| 94 | + "type": "mcp", |
| 95 | + "server_label": "internal_file_lookup", |
| 96 | + "server_url": "http://0.0.0.0:8000/sse/", # Update to the location of *your* MCP server |
| 97 | + "require_approval": "never" |
| 98 | + } |
| 99 | + ] |
| 100 | +) |
0 commit comments