From 51eecd2830208dd4ae89da9830f17db331e263bf Mon Sep 17 00:00:00 2001 From: ThomasTaroni Date: Sat, 26 Apr 2025 19:28:43 +0200 Subject: [PATCH] Add CustomLogsHandler for JSON logging in GPTResearcher Introduced a CustomLogsHandler class to handle and log JSON data during research or search operations. Updated GPTResearcher initialization to include the CustomLogsHandler for improved logging and debugging. --- src/server.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/server.py b/src/server.py index 3de1191..0e7e169 100644 --- a/src/server.py +++ b/src/server.py @@ -44,6 +44,16 @@ if not hasattr(mcp, "researchers"): mcp.researchers = {} +class CustomLogsHandler: + """A custom Logs handler class to handle JSON data.""" + def __init__(self): + self.logs = [] # Initialize logs to store data + + async def send_json(self, data: Dict[str, Any]) -> None: + """Send JSON data and log it.""" + self.logs.append(data) # Append data to logs + print(f"MCP Log: {data}") # For demonstration, print the log + @mcp.resource("research://{topic}") async def research_resource(topic: str) -> str: """ @@ -64,9 +74,10 @@ async def research_resource(topic: str) -> str: # If not, conduct the research logger.info(f"Conducting new research for resource on topic: {topic}") + custom_logs_handler = CustomLogsHandler() # Initialize GPT Researcher - researcher = GPTResearcher(topic, report_type=research_type) + researcher = GPTResearcher(query=topic, report_type=research_type, websocket=custom_logs_handler) try: # Conduct the research @@ -105,9 +116,10 @@ async def deep_research(query: str) -> Dict[str, Any]: # Generate a unique ID for this research session research_id = str(uuid.uuid4()) + custom_logs_handler = CustomLogsHandler() # Initialize GPT Researcher - researcher = GPTResearcher(query, report_type=research_type) + researcher = GPTResearcher(query=query, report_type=research_type, websocket=custom_logs_handler) # Start research try: @@ -152,9 +164,9 @@ async def quick_search(query: str) -> Dict[str, Any]: # Generate a unique ID for this search session search_id = str(uuid.uuid4()) - + custom_logs_handler = CustomLogsHandler() # Initialize GPT Researcher - researcher = GPTResearcher(query, report_type=research_type) + researcher = GPTResearcher(query=query, report_type=research_type, websocket=custom_logs_handler) try: # Perform quick search