@@ -27,7 +27,7 @@ def _format_search_results_for_chatgpt(results: SearchResponse) -> List[Dict[str
2727 formatted_result = {
2828 "id" : result .permalink or f"doc-{ len (formatted_results )} " ,
2929 "title" : result .title if result .title and result .title .strip () else "Untitled" ,
30- "url" : result .permalink or ""
30+ "url" : result .permalink or "" ,
3131 }
3232 formatted_results .append (formatted_result )
3333
@@ -43,11 +43,11 @@ def _format_document_for_chatgpt(
4343 """
4444 # Extract title from markdown content if not provided
4545 if not title and isinstance (content , str ):
46- lines = content .split (' \n ' )
47- if lines and lines [0 ].startswith ('# ' ):
46+ lines = content .split (" \n " )
47+ if lines and lines [0 ].startswith ("# " ):
4848 title = lines [0 ][2 :].strip ()
4949 else :
50- title = identifier .split ('/' )[- 1 ].replace ('-' , ' ' ).title ()
50+ title = identifier .split ("/" )[- 1 ].replace ("-" , " " ).title ()
5151
5252 # Ensure title is never None
5353 if not title :
@@ -60,21 +60,19 @@ def _format_document_for_chatgpt(
6060 "title" : title or "Document Not Found" ,
6161 "text" : content ,
6262 "url" : identifier ,
63- "metadata" : {"error" : "Document not found" }
63+ "metadata" : {"error" : "Document not found" },
6464 }
6565
6666 return {
6767 "id" : identifier ,
6868 "title" : title or "Untitled Document" ,
6969 "text" : content ,
7070 "url" : identifier ,
71- "metadata" : {"format" : "markdown" }
71+ "metadata" : {"format" : "markdown" },
7272 }
7373
7474
75- @mcp .tool (
76- description = "Search for content across the knowledge base"
77- )
75+ @mcp .tool (description = "Search for content across the knowledge base" )
7876async def search (
7977 query : str ,
8078 context : Context | None = None ,
@@ -99,7 +97,7 @@ async def search(
9997 page = 1 ,
10098 page_size = 10 , # Reasonable default for ChatGPT consumption
10199 search_type = "text" , # Default to full-text search
102- context = context
100+ context = context ,
103101 )
104102
105103 # Handle string error responses from search_notes
@@ -108,44 +106,32 @@ async def search(
108106 search_results = {
109107 "results" : [],
110108 "error" : "Search failed" ,
111- "error_details" : results [:500 ] # Truncate long error messages
109+ "error_details" : results [:500 ], # Truncate long error messages
112110 }
113111 else :
114112 # Format successful results for ChatGPT
115113 formatted_results = _format_search_results_for_chatgpt (results )
116114 search_results = {
117115 "results" : formatted_results ,
118116 "total_count" : len (results .results ), # Use actual count from results
119- "query" : query
117+ "query" : query ,
120118 }
121119 logger .info (f"Search completed: { len (formatted_results )} results returned" )
122120
123121 # Return in MCP content array format as required by OpenAI
124- return [
125- {
126- "type" : "text" ,
127- "text" : json .dumps (search_results , ensure_ascii = False )
128- }
129- ]
122+ return [{"type" : "text" , "text" : json .dumps (search_results , ensure_ascii = False )}]
130123
131124 except Exception as e :
132125 logger .error (f"ChatGPT search failed for query '{ query } ': { e } " )
133126 error_results = {
134127 "results" : [],
135128 "error" : "Internal search error" ,
136- "error_message" : str (e )[:200 ]
129+ "error_message" : str (e )[:200 ],
137130 }
138- return [
139- {
140- "type" : "text" ,
141- "text" : json .dumps (error_results , ensure_ascii = False )
142- }
143- ]
131+ return [{"type" : "text" , "text" : json .dumps (error_results , ensure_ascii = False )}]
144132
145133
146- @mcp .tool (
147- description = "Fetch the full contents of a search result document"
148- )
134+ @mcp .tool (description = "Fetch the full contents of a search result document" )
149135async def fetch (
150136 id : str ,
151137 context : Context | None = None ,
@@ -169,7 +155,7 @@ async def fetch(
169155 project = None , # Let project resolution happen automatically
170156 page = 1 ,
171157 page_size = 10 , # Default pagination
172- context = context
158+ context = context ,
173159 )
174160
175161 # Format the document for ChatGPT
@@ -178,12 +164,7 @@ async def fetch(
178164 logger .info (f"Fetch completed: id='{ id } ', content_length={ len (document .get ('text' , '' ))} " )
179165
180166 # Return in MCP content array format as required by OpenAI
181- return [
182- {
183- "type" : "text" ,
184- "text" : json .dumps (document , ensure_ascii = False )
185- }
186- ]
167+ return [{"type" : "text" , "text" : json .dumps (document , ensure_ascii = False )}]
187168
188169 except Exception as e :
189170 logger .error (f"ChatGPT fetch failed for id '{ id } ': { e } " )
@@ -192,11 +173,6 @@ async def fetch(
192173 "title" : "Fetch Error" ,
193174 "text" : f"Failed to fetch document: { str (e )[:200 ]} " ,
194175 "url" : id ,
195- "metadata" : {"error" : "Fetch failed" }
176+ "metadata" : {"error" : "Fetch failed" },
196177 }
197- return [
198- {
199- "type" : "text" ,
200- "text" : json .dumps (error_document , ensure_ascii = False )
201- }
202- ]
178+ return [{"type" : "text" , "text" : json .dumps (error_document , ensure_ascii = False )}]
0 commit comments