forked from assafelovic/gptr-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
323 lines (251 loc) · 9.87 KB
/
server.py
File metadata and controls
323 lines (251 loc) · 9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
"""
GPT Researcher MCP Server
This script implements an MCP server for GPT Researcher, allowing AI assistants
to conduct web research and generate reports via the MCP protocol.
"""
import os
import sys
import uuid
import logging
from typing import Dict, Any, Optional, List
from dotenv import load_dotenv
from fastapi.responses import JSONResponse
from fastmcp import FastMCP
from gpt_researcher import GPTResearcher
# Load environment variables
load_dotenv()
from utils import (
research_store,
create_success_response,
handle_exception,
get_researcher_by_id,
format_sources_for_response,
format_context_with_sources,
store_research_results,
create_research_prompt
)
logging.basicConfig(
level=logging.INFO,
format='[%(asctime)s][%(levelname)s] - %(message)s',
)
logger = logging.getLogger(__name__)
# Initialize FastMCP server
mcp = FastMCP(
name="GPT Researcher"
)
# Initialize researchers dictionary
if not hasattr(mcp, "researchers"):
mcp.researchers = {}
@mcp.resource("research://{topic}")
async def research_resource(topic: str) -> str:
"""
Provide research context for a given topic directly as a resource.
This allows LLMs to access web-sourced information without explicit function calls.
Args:
topic: The research topic or query
Returns:
String containing the research context with source information
"""
# Check if we've already researched this topic
if topic in research_store:
logger.info(f"Returning cached research for topic: {topic}")
return research_store[topic]["context"]
# If not, conduct the research
logger.info(f"Conducting new research for resource on topic: {topic}")
# Initialize GPT Researcher
researcher = GPTResearcher(topic)
try:
# Conduct the research
await researcher.conduct_research()
# Get the context and sources
context = researcher.get_research_context()
sources = researcher.get_research_sources()
source_urls = researcher.get_source_urls()
# Format with sources included
formatted_context = format_context_with_sources(topic, context, sources)
# Store for future use
store_research_results(topic, context, sources, source_urls, formatted_context)
return formatted_context
except Exception as e:
return f"Error conducting research on '{topic}': {str(e)}"
@mcp.tool()
async def deep_research(query: str) -> Dict[str, Any]:
"""
Conduct a web deep research on a given query using GPT Researcher.
Use this tool when you need time-sensitive, real-time information like stock prices, news, people, specific knowledge, etc.
Args:
query: The research query or topic
Returns:
Dict containing research status, ID, and the actual research context and sources
that can be used directly by LLMs for context enrichment
"""
logger.info(f"Conducting research on query: {query}...")
# Generate a unique ID for this research session
research_id = str(uuid.uuid4())
# Initialize GPT Researcher
researcher = GPTResearcher(query)
# Start research
try:
await researcher.conduct_research()
mcp.researchers[research_id] = researcher
logger.info(f"Research completed for ID: {research_id}")
# Get the research context and sources
context = researcher.get_research_context()
sources = researcher.get_research_sources()
source_urls = researcher.get_source_urls()
# Store in the research store for the resource API
store_research_results(query, context, sources, source_urls)
return create_success_response({
"research_id": research_id,
"query": query,
"source_count": len(sources),
"context": context,
"sources": format_sources_for_response(sources),
"source_urls": source_urls
})
except Exception as e:
return handle_exception(e, "Research")
@mcp.tool()
async def quick_search(query: str) -> Dict[str, Any]:
"""
Perform a quick web search on a given query and return search results with snippets.
This optimizes for speed over quality and is useful when an LLM doesn't need in-depth
information on a topic.
Args:
query: The search query
Returns:
Dict containing search results and snippets
"""
logger.info(f"Performing quick search on query: {query}...")
# Generate a unique ID for this search session
search_id = str(uuid.uuid4())
# Initialize GPT Researcher
researcher = GPTResearcher(query)
try:
# Perform quick search
search_results = await researcher.quick_search(query=query)
mcp.researchers[search_id] = researcher
logger.info(f"Quick search completed for ID: {search_id}")
return create_success_response({
"search_id": search_id,
"query": query,
"result_count": len(search_results) if search_results else 0,
"search_results": search_results
})
except Exception as e:
return handle_exception(e, "Quick search")
@mcp.tool()
async def write_report(research_id: str, custom_prompt: Optional[str] = None) -> Dict[str, Any]:
"""
Generate a report based on previously conducted research.
Args:
research_id: The ID of the research session from deep_research
custom_prompt: Optional custom prompt for report generation
Returns:
Dict containing the report content and metadata
"""
success, researcher, error = get_researcher_by_id(mcp.researchers, research_id)
if not success:
return error
logger.info(f"Generating report for research ID: {research_id}")
try:
# Generate report
report = await researcher.write_report(custom_prompt=custom_prompt)
# Get additional information
sources = researcher.get_research_sources()
costs = researcher.get_costs()
return create_success_response({
"report": report,
"source_count": len(sources),
"costs": costs
})
except Exception as e:
return handle_exception(e, "Report generation")
@mcp.tool()
async def get_research_sources(research_id: str) -> Dict[str, Any]:
"""
Get the sources used in the research.
Args:
research_id: The ID of the research session
Returns:
Dict containing the research sources
"""
success, researcher, error = get_researcher_by_id(mcp.researchers, research_id)
if not success:
return error
sources = researcher.get_research_sources()
source_urls = researcher.get_source_urls()
return create_success_response({
"sources": format_sources_for_response(sources),
"source_urls": source_urls
})
@mcp.tool()
async def get_research_context(research_id: str) -> Dict[str, Any]:
"""
Get the full context of the research.
Args:
research_id: The ID of the research session
Returns:
Dict containing the research context
"""
success, researcher, error = get_researcher_by_id(mcp.researchers, research_id)
if not success:
return error
context = researcher.get_research_context()
return create_success_response({
"context": context
})
@mcp.prompt()
def research_query(topic: str, goal: str, report_format: str = "research_report") -> str:
"""
Create a research query prompt for GPT Researcher.
Args:
topic: The topic to research
goal: The goal or specific question to answer
report_format: The format of the report to generate
Returns:
A formatted prompt for research
"""
return create_research_prompt(topic, goal, report_format)
@mcp.custom_route("/health", methods=["GET"])
async def health_check(request):
return JSONResponse({"status": "healthy", "service": "mcp-server"})
def run_server():
"""Run the MCP server using FastMCP's built-in event loop handling."""
# Check if API keys are set
if not os.getenv("OPENAI_API_KEY"):
logger.error("OPENAI_API_KEY not found. Please set it in your .env file.")
return
# Determine transport based on environment
transport = os.getenv("MCP_TRANSPORT", "stdio").lower()
# Auto-detect Docker environment
if os.path.exists("/.dockerenv") or os.getenv("DOCKER_CONTAINER"):
transport = "sse"
logger.info("Docker environment detected, using SSE transport")
# Add startup message
logger.info(f"Starting GPT Researcher MCP Server with {transport} transport...")
print(f"🚀 GPT Researcher MCP Server starting with {transport} transport...")
print(" Check researcher_mcp_server.log for details")
# Let FastMCP handle the event loop
try:
if transport == "stdio":
logger.info("Using STDIO transport (Claude Desktop compatible)")
mcp.run(transport="stdio")
elif transport == "sse":
mcp.run(transport="sse", host="0.0.0.0", port=8000)
elif transport == "streamable-http":
mcp.run(transport="streamable-http", host="0.0.0.0", port=8000)
else:
raise ValueError(f"Unsupported transport: {transport}")
# Note: If we reach here, the server has stopped
logger.info("MCP Server is running...")
while True:
pass # Keep the process alive
except Exception as e:
logger.error(f"Error running MCP server: {str(e)}")
print(f"❌ MCP Server error: {str(e)}")
return
print("✅ MCP Server stopped")
if __name__ == "__main__":
# Use the non-async approach to avoid asyncio nesting issues
run_server()