basic-memory 0.17.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (171) hide show
  1. basic_memory/__init__.py +7 -0
  2. basic_memory/alembic/alembic.ini +119 -0
  3. basic_memory/alembic/env.py +185 -0
  4. basic_memory/alembic/migrations.py +24 -0
  5. basic_memory/alembic/script.py.mako +26 -0
  6. basic_memory/alembic/versions/314f1ea54dc4_add_postgres_full_text_search_support_.py +131 -0
  7. basic_memory/alembic/versions/3dae7c7b1564_initial_schema.py +93 -0
  8. basic_memory/alembic/versions/502b60eaa905_remove_required_from_entity_permalink.py +51 -0
  9. basic_memory/alembic/versions/5fe1ab1ccebe_add_projects_table.py +120 -0
  10. basic_memory/alembic/versions/647e7a75e2cd_project_constraint_fix.py +112 -0
  11. basic_memory/alembic/versions/9d9c1cb7d8f5_add_mtime_and_size_columns_to_entity_.py +49 -0
  12. basic_memory/alembic/versions/a1b2c3d4e5f6_fix_project_foreign_keys.py +49 -0
  13. basic_memory/alembic/versions/a2b3c4d5e6f7_add_search_index_entity_cascade.py +56 -0
  14. basic_memory/alembic/versions/b3c3938bacdb_relation_to_name_unique_index.py +44 -0
  15. basic_memory/alembic/versions/cc7172b46608_update_search_index_schema.py +113 -0
  16. basic_memory/alembic/versions/e7e1f4367280_add_scan_watermark_tracking_to_project.py +37 -0
  17. basic_memory/alembic/versions/f8a9b2c3d4e5_add_pg_trgm_for_fuzzy_link_resolution.py +239 -0
  18. basic_memory/api/__init__.py +5 -0
  19. basic_memory/api/app.py +131 -0
  20. basic_memory/api/routers/__init__.py +11 -0
  21. basic_memory/api/routers/directory_router.py +84 -0
  22. basic_memory/api/routers/importer_router.py +152 -0
  23. basic_memory/api/routers/knowledge_router.py +318 -0
  24. basic_memory/api/routers/management_router.py +80 -0
  25. basic_memory/api/routers/memory_router.py +90 -0
  26. basic_memory/api/routers/project_router.py +448 -0
  27. basic_memory/api/routers/prompt_router.py +260 -0
  28. basic_memory/api/routers/resource_router.py +249 -0
  29. basic_memory/api/routers/search_router.py +36 -0
  30. basic_memory/api/routers/utils.py +169 -0
  31. basic_memory/api/template_loader.py +292 -0
  32. basic_memory/api/v2/__init__.py +35 -0
  33. basic_memory/api/v2/routers/__init__.py +21 -0
  34. basic_memory/api/v2/routers/directory_router.py +93 -0
  35. basic_memory/api/v2/routers/importer_router.py +182 -0
  36. basic_memory/api/v2/routers/knowledge_router.py +413 -0
  37. basic_memory/api/v2/routers/memory_router.py +130 -0
  38. basic_memory/api/v2/routers/project_router.py +342 -0
  39. basic_memory/api/v2/routers/prompt_router.py +270 -0
  40. basic_memory/api/v2/routers/resource_router.py +286 -0
  41. basic_memory/api/v2/routers/search_router.py +73 -0
  42. basic_memory/cli/__init__.py +1 -0
  43. basic_memory/cli/app.py +84 -0
  44. basic_memory/cli/auth.py +277 -0
  45. basic_memory/cli/commands/__init__.py +18 -0
  46. basic_memory/cli/commands/cloud/__init__.py +6 -0
  47. basic_memory/cli/commands/cloud/api_client.py +112 -0
  48. basic_memory/cli/commands/cloud/bisync_commands.py +110 -0
  49. basic_memory/cli/commands/cloud/cloud_utils.py +101 -0
  50. basic_memory/cli/commands/cloud/core_commands.py +195 -0
  51. basic_memory/cli/commands/cloud/rclone_commands.py +371 -0
  52. basic_memory/cli/commands/cloud/rclone_config.py +110 -0
  53. basic_memory/cli/commands/cloud/rclone_installer.py +263 -0
  54. basic_memory/cli/commands/cloud/upload.py +233 -0
  55. basic_memory/cli/commands/cloud/upload_command.py +124 -0
  56. basic_memory/cli/commands/command_utils.py +77 -0
  57. basic_memory/cli/commands/db.py +44 -0
  58. basic_memory/cli/commands/format.py +198 -0
  59. basic_memory/cli/commands/import_chatgpt.py +84 -0
  60. basic_memory/cli/commands/import_claude_conversations.py +87 -0
  61. basic_memory/cli/commands/import_claude_projects.py +86 -0
  62. basic_memory/cli/commands/import_memory_json.py +87 -0
  63. basic_memory/cli/commands/mcp.py +76 -0
  64. basic_memory/cli/commands/project.py +889 -0
  65. basic_memory/cli/commands/status.py +174 -0
  66. basic_memory/cli/commands/telemetry.py +81 -0
  67. basic_memory/cli/commands/tool.py +341 -0
  68. basic_memory/cli/main.py +28 -0
  69. basic_memory/config.py +616 -0
  70. basic_memory/db.py +394 -0
  71. basic_memory/deps.py +705 -0
  72. basic_memory/file_utils.py +478 -0
  73. basic_memory/ignore_utils.py +297 -0
  74. basic_memory/importers/__init__.py +27 -0
  75. basic_memory/importers/base.py +79 -0
  76. basic_memory/importers/chatgpt_importer.py +232 -0
  77. basic_memory/importers/claude_conversations_importer.py +180 -0
  78. basic_memory/importers/claude_projects_importer.py +148 -0
  79. basic_memory/importers/memory_json_importer.py +108 -0
  80. basic_memory/importers/utils.py +61 -0
  81. basic_memory/markdown/__init__.py +21 -0
  82. basic_memory/markdown/entity_parser.py +279 -0
  83. basic_memory/markdown/markdown_processor.py +160 -0
  84. basic_memory/markdown/plugins.py +242 -0
  85. basic_memory/markdown/schemas.py +70 -0
  86. basic_memory/markdown/utils.py +117 -0
  87. basic_memory/mcp/__init__.py +1 -0
  88. basic_memory/mcp/async_client.py +139 -0
  89. basic_memory/mcp/project_context.py +141 -0
  90. basic_memory/mcp/prompts/__init__.py +19 -0
  91. basic_memory/mcp/prompts/ai_assistant_guide.py +70 -0
  92. basic_memory/mcp/prompts/continue_conversation.py +62 -0
  93. basic_memory/mcp/prompts/recent_activity.py +188 -0
  94. basic_memory/mcp/prompts/search.py +57 -0
  95. basic_memory/mcp/prompts/utils.py +162 -0
  96. basic_memory/mcp/resources/ai_assistant_guide.md +283 -0
  97. basic_memory/mcp/resources/project_info.py +71 -0
  98. basic_memory/mcp/server.py +81 -0
  99. basic_memory/mcp/tools/__init__.py +48 -0
  100. basic_memory/mcp/tools/build_context.py +120 -0
  101. basic_memory/mcp/tools/canvas.py +152 -0
  102. basic_memory/mcp/tools/chatgpt_tools.py +190 -0
  103. basic_memory/mcp/tools/delete_note.py +242 -0
  104. basic_memory/mcp/tools/edit_note.py +324 -0
  105. basic_memory/mcp/tools/list_directory.py +168 -0
  106. basic_memory/mcp/tools/move_note.py +551 -0
  107. basic_memory/mcp/tools/project_management.py +201 -0
  108. basic_memory/mcp/tools/read_content.py +281 -0
  109. basic_memory/mcp/tools/read_note.py +267 -0
  110. basic_memory/mcp/tools/recent_activity.py +534 -0
  111. basic_memory/mcp/tools/search.py +385 -0
  112. basic_memory/mcp/tools/utils.py +540 -0
  113. basic_memory/mcp/tools/view_note.py +78 -0
  114. basic_memory/mcp/tools/write_note.py +230 -0
  115. basic_memory/models/__init__.py +15 -0
  116. basic_memory/models/base.py +10 -0
  117. basic_memory/models/knowledge.py +226 -0
  118. basic_memory/models/project.py +87 -0
  119. basic_memory/models/search.py +85 -0
  120. basic_memory/repository/__init__.py +11 -0
  121. basic_memory/repository/entity_repository.py +503 -0
  122. basic_memory/repository/observation_repository.py +73 -0
  123. basic_memory/repository/postgres_search_repository.py +379 -0
  124. basic_memory/repository/project_info_repository.py +10 -0
  125. basic_memory/repository/project_repository.py +128 -0
  126. basic_memory/repository/relation_repository.py +146 -0
  127. basic_memory/repository/repository.py +385 -0
  128. basic_memory/repository/search_index_row.py +95 -0
  129. basic_memory/repository/search_repository.py +94 -0
  130. basic_memory/repository/search_repository_base.py +241 -0
  131. basic_memory/repository/sqlite_search_repository.py +439 -0
  132. basic_memory/schemas/__init__.py +86 -0
  133. basic_memory/schemas/base.py +297 -0
  134. basic_memory/schemas/cloud.py +50 -0
  135. basic_memory/schemas/delete.py +37 -0
  136. basic_memory/schemas/directory.py +30 -0
  137. basic_memory/schemas/importer.py +35 -0
  138. basic_memory/schemas/memory.py +285 -0
  139. basic_memory/schemas/project_info.py +212 -0
  140. basic_memory/schemas/prompt.py +90 -0
  141. basic_memory/schemas/request.py +112 -0
  142. basic_memory/schemas/response.py +229 -0
  143. basic_memory/schemas/search.py +117 -0
  144. basic_memory/schemas/sync_report.py +72 -0
  145. basic_memory/schemas/v2/__init__.py +27 -0
  146. basic_memory/schemas/v2/entity.py +129 -0
  147. basic_memory/schemas/v2/resource.py +46 -0
  148. basic_memory/services/__init__.py +8 -0
  149. basic_memory/services/context_service.py +601 -0
  150. basic_memory/services/directory_service.py +308 -0
  151. basic_memory/services/entity_service.py +864 -0
  152. basic_memory/services/exceptions.py +37 -0
  153. basic_memory/services/file_service.py +541 -0
  154. basic_memory/services/initialization.py +216 -0
  155. basic_memory/services/link_resolver.py +121 -0
  156. basic_memory/services/project_service.py +880 -0
  157. basic_memory/services/search_service.py +404 -0
  158. basic_memory/services/service.py +15 -0
  159. basic_memory/sync/__init__.py +6 -0
  160. basic_memory/sync/background_sync.py +26 -0
  161. basic_memory/sync/sync_service.py +1259 -0
  162. basic_memory/sync/watch_service.py +510 -0
  163. basic_memory/telemetry.py +249 -0
  164. basic_memory/templates/prompts/continue_conversation.hbs +110 -0
  165. basic_memory/templates/prompts/search.hbs +101 -0
  166. basic_memory/utils.py +468 -0
  167. basic_memory-0.17.1.dist-info/METADATA +617 -0
  168. basic_memory-0.17.1.dist-info/RECORD +171 -0
  169. basic_memory-0.17.1.dist-info/WHEEL +4 -0
  170. basic_memory-0.17.1.dist-info/entry_points.txt +3 -0
  171. basic_memory-0.17.1.dist-info/licenses/LICENSE +661 -0
@@ -0,0 +1,267 @@
1
+ """Read note tool for Basic Memory MCP server."""
2
+
3
+ from textwrap import dedent
4
+ from typing import Optional
5
+
6
+ from loguru import logger
7
+ from fastmcp import Context
8
+
9
+ from basic_memory.mcp.async_client import get_client
10
+ from basic_memory.mcp.project_context import get_active_project
11
+ from basic_memory.mcp.server import mcp
12
+ from basic_memory.mcp.tools.search import search_notes
13
+ from basic_memory.mcp.tools.utils import call_get, resolve_entity_id
14
+ from basic_memory.telemetry import track_mcp_tool
15
+ from basic_memory.schemas.memory import memory_url_path
16
+ from basic_memory.utils import validate_project_path
17
+
18
+
19
+ @mcp.tool(
20
+ description="Read a markdown note by title or permalink.",
21
+ )
22
+ async def read_note(
23
+ identifier: str,
24
+ project: Optional[str] = None,
25
+ page: int = 1,
26
+ page_size: int = 10,
27
+ context: Context | None = None,
28
+ ) -> str:
29
+ """Return the raw markdown for a note, or guidance text if no match is found.
30
+
31
+ Finds and retrieves a note by its title, permalink, or content search,
32
+ returning the raw markdown content including observations, relations, and metadata.
33
+
34
+ Project Resolution:
35
+ Server resolves projects in this order: Single Project Mode → project parameter → default project.
36
+ If project unknown, use list_memory_projects() or recent_activity() first.
37
+
38
+ This tool will try multiple lookup strategies to find the most relevant note:
39
+ 1. Direct permalink lookup
40
+ 2. Title search fallback
41
+ 3. Text search as last resort
42
+
43
+ Args:
44
+ project: Project name to read from. Optional - server will resolve using the
45
+ hierarchy above. If unknown, use list_memory_projects() to discover
46
+ available projects.
47
+ identifier: The title or permalink of the note to read
48
+ Can be a full memory:// URL, a permalink, a title, or search text
49
+ page: Page number for paginated results (default: 1)
50
+ page_size: Number of items per page (default: 10)
51
+ context: Optional FastMCP context for performance caching.
52
+
53
+ Returns:
54
+ The full markdown content of the note if found, or helpful guidance if not found.
55
+ Content includes frontmatter, observations, relations, and all markdown formatting.
56
+
57
+ Examples:
58
+ # Read by permalink
59
+ read_note("my-research", "specs/search-spec")
60
+
61
+ # Read by title
62
+ read_note("work-project", "Search Specification")
63
+
64
+ # Read with memory URL
65
+ read_note("my-research", "memory://specs/search-spec")
66
+
67
+ # Read with pagination
68
+ read_note("work-project", "Project Updates", page=2, page_size=5)
69
+
70
+ # Read recent meeting notes
71
+ read_note("team-docs", "Weekly Standup")
72
+
73
+ Raises:
74
+ HTTPError: If project doesn't exist or is inaccessible
75
+ SecurityError: If identifier attempts path traversal
76
+
77
+ Note:
78
+ If the exact note isn't found, this tool provides helpful suggestions
79
+ including related notes, search commands, and note creation templates.
80
+ """
81
+ track_mcp_tool("read_note")
82
+ async with get_client() as client:
83
+ # Get and validate the project
84
+ active_project = await get_active_project(client, project, context)
85
+
86
+ # Validate identifier to prevent path traversal attacks
87
+ # We need to check both the raw identifier and the processed path
88
+ processed_path = memory_url_path(identifier)
89
+ project_path = active_project.home
90
+
91
+ if not validate_project_path(identifier, project_path) or not validate_project_path(
92
+ processed_path, project_path
93
+ ):
94
+ logger.warning(
95
+ "Attempted path traversal attack blocked",
96
+ identifier=identifier,
97
+ processed_path=processed_path,
98
+ project=active_project.name,
99
+ )
100
+ return f"# Error\n\nIdentifier '{identifier}' is not allowed - paths must stay within project boundaries"
101
+
102
+ # Get the file via REST API - first try direct identifier resolution
103
+ entity_path = memory_url_path(identifier)
104
+ logger.info(
105
+ f"Attempting to read note from Project: {active_project.name} identifier: {entity_path}"
106
+ )
107
+
108
+ try:
109
+ # Try to resolve identifier to entity ID
110
+ entity_id = await resolve_entity_id(client, active_project.id, entity_path)
111
+
112
+ # Fetch content using entity ID
113
+ response = await call_get(
114
+ client,
115
+ f"/v2/projects/{active_project.id}/resource/{entity_id}",
116
+ params={"page": page, "page_size": page_size},
117
+ )
118
+
119
+ # If successful, return the content
120
+ if response.status_code == 200:
121
+ logger.info("Returning read_note result from resource: {path}", path=entity_path)
122
+ return response.text
123
+ except Exception as e: # pragma: no cover
124
+ logger.info(f"Direct lookup failed for '{entity_path}': {e}")
125
+ # Continue to fallback methods
126
+
127
+ # Fallback 1: Try title search via API
128
+ logger.info(f"Search title for: {identifier}")
129
+ title_results = await search_notes.fn(
130
+ query=identifier, search_type="title", project=project, context=context
131
+ )
132
+
133
+ # Handle both SearchResponse object and error strings
134
+ if title_results and hasattr(title_results, "results") and title_results.results:
135
+ result = title_results.results[0] # Get the first/best match
136
+ if result.permalink:
137
+ try:
138
+ # Resolve the permalink to entity ID
139
+ entity_id = await resolve_entity_id(client, active_project.id, result.permalink)
140
+
141
+ # Fetch content using the entity ID
142
+ response = await call_get(
143
+ client,
144
+ f"/v2/projects/{active_project.id}/resource/{entity_id}",
145
+ params={"page": page, "page_size": page_size},
146
+ )
147
+
148
+ if response.status_code == 200:
149
+ logger.info(f"Found note by title search: {result.permalink}")
150
+ return response.text
151
+ except Exception as e: # pragma: no cover
152
+ logger.info(
153
+ f"Failed to fetch content for found title match {result.permalink}: {e}"
154
+ )
155
+ else:
156
+ logger.info(
157
+ f"No results in title search for: {identifier} in project {active_project.name}"
158
+ )
159
+
160
+ # Fallback 2: Text search as a last resort
161
+ logger.info(f"Title search failed, trying text search for: {identifier}")
162
+ text_results = await search_notes.fn(
163
+ query=identifier, search_type="text", project=project, context=context
164
+ )
165
+
166
+ # We didn't find a direct match, construct a helpful error message
167
+ # Handle both SearchResponse object and error strings
168
+ if not text_results or not hasattr(text_results, "results") or not text_results.results:
169
+ # No results at all
170
+ return format_not_found_message(active_project.name, identifier)
171
+ else:
172
+ # We found some related results
173
+ return format_related_results(active_project.name, identifier, text_results.results[:5])
174
+
175
+
176
+ def format_not_found_message(project: str | None, identifier: str) -> str:
177
+ """Format a helpful message when no note was found."""
178
+ return dedent(f"""
179
+ # Note Not Found in {project}: "{identifier}"
180
+
181
+ I couldn't find any notes matching "{identifier}". Here are some suggestions:
182
+
183
+ ## Check Identifier Type
184
+ - If you provided a title, try using the exact permalink instead
185
+ - If you provided a permalink, check for typos or try a broader search
186
+
187
+ ## Search Instead
188
+ Try searching for related content:
189
+ ```
190
+ search_notes(project="{project}", query="{identifier}")
191
+ ```
192
+
193
+ ## Recent Activity
194
+ Check recently modified notes:
195
+ ```
196
+ recent_activity(timeframe="7d")
197
+ ```
198
+
199
+ ## Create New Note
200
+ This might be a good opportunity to create a new note on this topic:
201
+ ```
202
+ write_note(
203
+ project="{project}",
204
+ title="{identifier.capitalize()}",
205
+ content='''
206
+ # {identifier.capitalize()}
207
+
208
+ ## Overview
209
+ [Your content here]
210
+
211
+ ## Observations
212
+ - [category] [Observation about {identifier}]
213
+
214
+ ## Relations
215
+ - relates_to [[Related Topic]]
216
+ ''',
217
+ folder="notes"
218
+ )
219
+ ```
220
+ """)
221
+
222
+
223
+ def format_related_results(project: str | None, identifier: str, results) -> str:
224
+ """Format a helpful message with related results when an exact match wasn't found."""
225
+ message = dedent(f"""
226
+ # Note Not Found in {project}: "{identifier}"
227
+
228
+ I couldn't find an exact match for "{identifier}", but I found some related notes:
229
+
230
+ """)
231
+
232
+ for i, result in enumerate(results):
233
+ message += dedent(f"""
234
+ ## {i + 1}. {result.title}
235
+ - **Type**: {result.type.value}
236
+ - **Permalink**: {result.permalink}
237
+
238
+ You can read this note with:
239
+ ```
240
+ read_note(project="{project}", {result.permalink}")
241
+ ```
242
+
243
+ """)
244
+
245
+ message += dedent(f"""
246
+ ## Try More Specific Lookup
247
+ For exact matches, try using the full permalink from one of the results above.
248
+
249
+ ## Search For More Results
250
+ To see more related content:
251
+ ```
252
+ search_notes(project="{project}", query="{identifier}")
253
+ ```
254
+
255
+ ## Create New Note
256
+ If none of these match what you're looking for, consider creating a new note:
257
+ ```
258
+ write_note(
259
+ project="{project}",
260
+ title="[Your title]",
261
+ content="[Your content]",
262
+ folder="notes"
263
+ )
264
+ ```
265
+ """)
266
+
267
+ return message