11"""ChatGPT-compatible MCP tools for Basic Memory.
22
3- This module provides simplified 'search' and 'fetch' tools that adapt Basic Memory's
4- rich MCP interface to ChatGPT's expected naming conventions and MCP content array format.
5-
6- ChatGPT's MCP integration expects:
7- - search(query: str) -> MCP content array with JSON-encoded results
8- - fetch(id: str) -> MCP content array with JSON-encoded document
9-
10- These tools wrap our existing search_notes and read_note functions and format
11- responses according to OpenAI's MCP specification.
3+ These adapters expose Basic Memory's search/fetch functionality using the exact
4+ tool names and response structure OpenAI's MCP clients expect: each call returns
5+ a list containing a single `{"type": "text", "text": "{...json...}"}` item.
126"""
137
148import json
@@ -83,27 +77,15 @@ async def search(
8377 query : str ,
8478 context : Context | None = None ,
8579) -> List [Dict [str , Any ]]:
86- """Search for content across the knowledge base.
87-
88- This tool provides a simplified search interface optimized for ChatGPT's
89- MCP integration. It returns results in the required MCP content array format.
80+ """ChatGPT/OpenAI MCP search adapter returning a single text content item.
9081
9182 Args:
92- query: Search query string (supports boolean operators, phrases )
93- context: MCP context for authentication and routing
83+ query: Search query (full-text syntax supported by `search_notes` )
84+ context: Optional FastMCP context passed through for auth/session data
9485
9586 Returns:
96- MCP content array with exactly one item containing JSON-encoded search results
97-
98- The response follows OpenAI's MCP specification:
99- {
100- "content": [
101- {
102- "type": "text",
103- "text": "{\" results\" :[{\" id\" :\" doc-1\" ,\" title\" :\" ...\" ,\" url\" :\" ...\" }]}"
104- }
105- ]
106- }
87+ List with one dict: `{ "type": "text", "text": "{...JSON...}" }`
88+ where the JSON body contains `results`, `total_count`, and echo of `query`.
10789 """
10890 logger .info (f"ChatGPT search request: query='{ query } '" )
10991
@@ -165,27 +147,15 @@ async def fetch(
165147 id : str ,
166148 context : Context | None = None ,
167149) -> List [Dict [str , Any ]]:
168- """Fetch the full content of a specific document.
169-
170- This tool provides a simplified fetch interface optimized for ChatGPT's
171- MCP integration. It returns document content in the required MCP content array format.
150+ """ChatGPT/OpenAI MCP fetch adapter returning a single text content item.
172151
173152 Args:
174- id: Document identifier (permalink, title, or memory:// URL)
175- context: MCP context for authentication and routing
153+ id: Document identifier (permalink, title, or memory URL)
154+ context: Optional FastMCP context passed through for auth/session data
176155
177156 Returns:
178- MCP content array with exactly one item containing JSON-encoded document
179-
180- The response follows OpenAI's MCP specification:
181- {
182- "content": [
183- {
184- "type": "text",
185- "text": "{\" id\" :\" doc-1\" ,\" title\" :\" ...\" ,\" text\" :\" full text...\" ,\" url\" :\" ...\" ,\" metadata\" :{}}"
186- }
187- ]
188- }
157+ List with one dict: `{ "type": "text", "text": "{...JSON...}" }`
158+ where the JSON body includes `id`, `title`, `text`, `url`, and metadata.
189159 """
190160 logger .info (f"ChatGPT fetch request: id='{ id } '" )
191161
@@ -225,4 +195,4 @@ async def fetch(
225195 "type" : "text" ,
226196 "text" : json .dumps (error_document , ensure_ascii = False )
227197 }
228- ]
198+ ]
0 commit comments