lemma-mcp 0.7.0

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 (68) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +474 -0
  3. package/README.tr.md +384 -0
  4. package/dist/guides/core.d.ts +33 -0
  5. package/dist/guides/core.d.ts.map +1 -0
  6. package/dist/guides/core.js +424 -0
  7. package/dist/guides/core.js.map +1 -0
  8. package/dist/guides/index.d.ts +2 -0
  9. package/dist/guides/index.d.ts.map +1 -0
  10. package/dist/guides/index.js +2 -0
  11. package/dist/guides/index.js.map +1 -0
  12. package/dist/guides/task-map.d.ts +3 -0
  13. package/dist/guides/task-map.d.ts.map +1 -0
  14. package/dist/guides/task-map.js +107 -0
  15. package/dist/guides/task-map.js.map +1 -0
  16. package/dist/index.d.ts +3 -0
  17. package/dist/index.d.ts.map +1 -0
  18. package/dist/index.js +7 -0
  19. package/dist/index.js.map +1 -0
  20. package/dist/memory/config.d.ts +6 -0
  21. package/dist/memory/config.d.ts.map +1 -0
  22. package/dist/memory/config.js +69 -0
  23. package/dist/memory/config.js.map +1 -0
  24. package/dist/memory/core.d.ts +27 -0
  25. package/dist/memory/core.d.ts.map +1 -0
  26. package/dist/memory/core.js +415 -0
  27. package/dist/memory/core.js.map +1 -0
  28. package/dist/memory/index.d.ts +2 -0
  29. package/dist/memory/index.d.ts.map +1 -0
  30. package/dist/memory/index.js +2 -0
  31. package/dist/memory/index.js.map +1 -0
  32. package/dist/server/handlers.d.ts +131 -0
  33. package/dist/server/handlers.d.ts.map +1 -0
  34. package/dist/server/handlers.js +694 -0
  35. package/dist/server/handlers.js.map +1 -0
  36. package/dist/server/hooks.d.ts +16 -0
  37. package/dist/server/hooks.d.ts.map +1 -0
  38. package/dist/server/hooks.js +77 -0
  39. package/dist/server/hooks.js.map +1 -0
  40. package/dist/server/index.d.ts +5 -0
  41. package/dist/server/index.d.ts.map +1 -0
  42. package/dist/server/index.js +322 -0
  43. package/dist/server/index.js.map +1 -0
  44. package/dist/server/system-prompt.d.ts +5 -0
  45. package/dist/server/system-prompt.d.ts.map +1 -0
  46. package/dist/server/system-prompt.js +143 -0
  47. package/dist/server/system-prompt.js.map +1 -0
  48. package/dist/server/tools.d.ts +22 -0
  49. package/dist/server/tools.d.ts.map +1 -0
  50. package/dist/server/tools.js +443 -0
  51. package/dist/server/tools.js.map +1 -0
  52. package/dist/sessions/core.d.ts +17 -0
  53. package/dist/sessions/core.d.ts.map +1 -0
  54. package/dist/sessions/core.js +162 -0
  55. package/dist/sessions/core.js.map +1 -0
  56. package/dist/sessions/index.d.ts +2 -0
  57. package/dist/sessions/index.d.ts.map +1 -0
  58. package/dist/sessions/index.js +2 -0
  59. package/dist/sessions/index.js.map +1 -0
  60. package/dist/sessions/virtual.d.ts +23 -0
  61. package/dist/sessions/virtual.d.ts.map +1 -0
  62. package/dist/sessions/virtual.js +157 -0
  63. package/dist/sessions/virtual.js.map +1 -0
  64. package/dist/types.d.ts +143 -0
  65. package/dist/types.d.ts.map +1 -0
  66. package/dist/types.js +2 -0
  67. package/dist/types.js.map +1 -0
  68. package/package.json +62 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Lemma Project Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,474 @@
1
+ <p align="center">
2
+ <img src="assets/logo.png" width="200" alt="Lemma Logo">
3
+ </p>
4
+
5
+ # Lemma - Persistent Memory for LLMs via MCP
6
+
7
+ [English](README.md) | [Türkçe](README.tr.md)
8
+
9
+ Lemma is a Model Context Protocol (MCP) server that provides a persistent memory layer for Large Language Models. It enables LLMs to remember facts, preferences, and context across sessions through a biological memory model with automatic decay, learning, and universal injection.
10
+
11
+ ## What is Lemma?
12
+
13
+ Lemma acts as an external hippocampus for AI assistants. The human brain does not record everything — it synthesizes, distills, and leaves behind fragments. Frequently accessed knowledge grows stronger; unused knowledge fades and is forgotten.
14
+
15
+ Lemma operates on the same principle:
16
+
17
+ - **Raw conversations are never stored** — only synthesized fragments
18
+ - **Fragments decay over time** — frequently accessed ones strengthen
19
+ - **Used knowledge gains context** — tags and associations are built automatically
20
+ - **Memories are injected automatically** — LLM sees them without calling tools
21
+
22
+ ## How It Works
23
+
24
+ ### Universal Memory Injection
25
+
26
+ Lemma injects memories directly into tool descriptions via `tools/list`. This works on **every MCP client** — Claude Desktop, Cursor, VS Code, opencode, Gemini CLI, and others.
27
+
28
+ ```
29
+ tools/list → memory_read description includes:
30
+ "YOUR MEMORIES (you already know these):
31
+ [m8f728] React Architecture (95%)
32
+ Full content here...
33
+ [m1bbea] Clean Code Research (77%)
34
+ Full content here..."
35
+ ```
36
+
37
+ The LLM starts every session already knowing its most important memories. No explicit tool call needed.
38
+
39
+ **Dual injection:**
40
+ 1. **Tool descriptions** — universal, works everywhere
41
+ 2. **`instructions` field** — for clients that support MCP initialize instructions
42
+
43
+ **3-layer architecture:**
44
+ - Layer 1: Full content for top memories (~3000 tokens, configurable)
45
+ - Layer 2: Summary index for remaining memories
46
+ - Layer 3: Active guides with descriptions and learnings
47
+
48
+ ### Memory Structure
49
+
50
+ Each memory fragment has:
51
+
52
+ | Field | Type | Description |
53
+ |-------|------|-------------|
54
+ | `id` | string | Unique identifier (`m` + 12 hex chars from crypto.randomUUID) |
55
+ | `title` | string | Short title for quick scanning |
56
+ | `fragment` | string | Synthesized memory text |
57
+ | `project` | string | Project scope (`null` for global) |
58
+ | `confidence` | float | Reliability 0.0-1.0 (decays and boosts over time) |
59
+ | `source` | string | `"user"` or `"ai"` |
60
+ | `created` | string | Creation date (YYYY-MM-DD) |
61
+ | `lastAccessed` | string | ISO timestamp of last read |
62
+ | `accessed` | int | Access count in current decay cycle |
63
+ | `tags` | string[] | Context tags from usage (e.g., "debugging", "refactoring") |
64
+ | `associatedWith` | string[] | IDs of fragments accessed in the same session |
65
+ | `negativeHits` | int | Times this memory was marked unhelpful (resets per session) |
66
+
67
+ ### Learning System
68
+
69
+ Unlike static memory, Lemma uses a biological model where knowledge evolves through use:
70
+
71
+ **Boost (on access):**
72
+ ```
73
+ confidence = min(1.0, confidence + 0.1)
74
+ tags += context_tag (e.g., "debugging")
75
+ associatedWith += co_accessed_fragment_ids
76
+ ```
77
+
78
+ **Decay (per session):**
79
+ ```
80
+ decay = max(0.005, 0.05 - (accessed * 0.005))
81
+ confidence = confidence - decay
82
+ ```
83
+
84
+ - **Frequency**: Frequently accessed items decay slower (min 0.005 per session)
85
+ - **Unused items** decay at the base rate of 0.05 per session
86
+ - **Associations**: Fragments used together build cross-references for future recall
87
+
88
+ ### Deduplication
89
+
90
+ Lemma uses **Fuse.js fuzzy matching** (not Jaccard) for dedup:
91
+ - "Use React hooks" vs "Don't use React hooks" — correctly detected as different
92
+ - "react", "reactjs", "React.js" — correctly detected as same (for guides)
93
+ - Applies to both user and AI sourced memories
94
+
95
+ ### Virtual Sessions
96
+
97
+ Tool calls are automatically correlated into virtual sessions:
98
+ - Auto-starts on first tool call
99
+ - Auto-finalizes after 30 minutes of inactivity
100
+ - Tracks technologies seen, guides used, memories created
101
+ - No explicit `session_start`/`session_end` required
102
+ - Sessions stored in `~/.lemma/sessions/`
103
+
104
+ ### Data Safety
105
+
106
+ - **Cumulative backup**: `.bak` files are ID-based merges — never overwrites existing entries
107
+ - **File locking**: Module-level write lock prevents concurrent data corruption
108
+ - **Safe I/O**: Empty/null arrays rejected before writing
109
+ - **No implicit deletion**: Decay only reduces confidence, never removes fragments
110
+
111
+ ### Configuration
112
+
113
+ Optional config file at `~/.lemma/config.json`:
114
+
115
+ ```json
116
+ {
117
+ "token_budget": {
118
+ "full_content": 3000,
119
+ "summary_index": 1000,
120
+ "guides_detail": 1000
121
+ },
122
+ "injection": {
123
+ "max_full_content_fragments": 15,
124
+ "max_summary_fragments": 30,
125
+ "max_guides": 20,
126
+ "max_guide_detail": 3
127
+ },
128
+ "virtual_session": {
129
+ "timeout_minutes": 30
130
+ }
131
+ }
132
+ ```
133
+
134
+ ### File Locations
135
+
136
+ | OS | Path |
137
+ |---|---|
138
+ | **Windows** | `C:\Users\{username}\.lemma\` |
139
+ | **macOS** | `/Users/{username}/.lemma/` |
140
+ | **Linux** | `/home/{username}/.lemma/` |
141
+
142
+ Files:
143
+ - `memory.jsonl` — memory fragments
144
+ - `guides.jsonl` — experience guides
145
+ - `config.json` — user configuration (optional)
146
+ - `sessions/` — virtual session logs
147
+ - `.bak` files — cumulative backups
148
+
149
+ ## Quick Start
150
+
151
+ Add Lemma to your MCP client configuration:
152
+
153
+ **Claude Desktop (Windows):** `%APPDATA%\Claude\claude_desktop_config.json`
154
+ **Claude Desktop (macOS):** `~/Library/Application Support/Claude/claude_desktop_config.json`
155
+
156
+ ```json
157
+ {
158
+ "mcpServers": {
159
+ "lemma": {
160
+ "command": "npx",
161
+ "args": ["-y", "github:xenitV1/lemma"]
162
+ }
163
+ }
164
+ }
165
+ ```
166
+
167
+ ---
168
+
169
+ ## Hook System
170
+
171
+ Lemma provides a pluggable hook system for extending server behavior:
172
+
173
+ ### Lifecycle Hooks
174
+
175
+ ```javascript
176
+ import { registerHook, HookTypes } from "@lemma/lemma/server";
177
+
178
+ registerHook(HookTypes.ON_START, async (context) => {
179
+ console.log("Server started!", context);
180
+ });
181
+
182
+ registerHook(HookTypes.ON_PROJECT_CHANGE, async (context) => {
183
+ console.log(`Project changed to: ${context.project}`);
184
+ });
185
+ ```
186
+
187
+ ### Prompt Modifiers
188
+
189
+ Extend the system prompt generation with custom transformations:
190
+
191
+ ```javascript
192
+ import { registerPromptModifier } from "@lemma/lemma/server";
193
+
194
+ registerPromptModifier(async (prompt, context) => {
195
+ if (context.project === "my-app") {
196
+ return prompt + "\n\n<custom>Note: Using experimental features.</custom>";
197
+ }
198
+ return prompt;
199
+ });
200
+ ```
201
+
202
+ ---
203
+
204
+ ## Manual Installation
205
+
206
+ ```bash
207
+ git clone https://github.com/xenitV1/lemma
208
+ cd Lemma
209
+ npm install
210
+ ```
211
+
212
+ **Requirements:** Node.js 18.0.0 or higher
213
+
214
+ ### Local Configuration
215
+
216
+ ```json
217
+ {
218
+ "mcpServers": {
219
+ "lemma": {
220
+ "command": "node",
221
+ "args": ["C:\\path\\to\\Lemma\\src\\index.js"]
222
+ }
223
+ }
224
+ }
225
+ ```
226
+
227
+ ---
228
+
229
+ ## Available Tools (20)
230
+
231
+ ### Memory Tools (10)
232
+
233
+ #### `memory_read`
234
+
235
+ Read memory fragments. SUMMARY MODE shows title + description; use `id` for full detail.
236
+
237
+ **Parameters:**
238
+ - `project` (string, optional): Project name to filter
239
+ - `query` (string, optional): Semantic search keyword
240
+ - `id` (string, optional): Get full detail for a specific fragment
241
+ - `ids` (string[], optional): Get full details for multiple fragments at once
242
+ - `context` (string, optional): Tag this access with a context (e.g., "debugging")
243
+ - `all` (boolean, optional): Show fragments from all projects (default: false)
244
+
245
+ #### `memory_add`
246
+
247
+ **MANDATORY:** Call AFTER completing analysis to save findings.
248
+
249
+ **Parameters:**
250
+ - `fragment` (string, required): The memory text to store
251
+ - `title` (string, optional): Short title
252
+ - `description` (string, optional): Short summary
253
+ - `project` (string, optional): Project scope (null = global)
254
+ - `source` (string, optional): "user" or "ai", default "ai"
255
+
256
+ #### `memory_update`
257
+
258
+ Update an existing fragment by ID.
259
+
260
+ **Parameters:**
261
+ - `id` (string, required): Fragment ID
262
+ - `title` (string, optional): New title
263
+ - `fragment` (string, optional): New text
264
+ - `confidence` (number, optional): New confidence 0-1
265
+
266
+ #### `memory_feedback`
267
+
268
+ Provide feedback on a memory fragment after use. Positive boosts confidence; negative reduces by -0.1.
269
+
270
+ **Parameters:**
271
+ - `id` (string, required): Fragment ID
272
+ - `useful` (boolean, required): `true` if helpful, `false` if not
273
+
274
+ #### `memory_forget`
275
+
276
+ Remove a memory fragment by ID.
277
+
278
+ **Parameters:**
279
+ - `id` (string, required): Fragment ID
280
+
281
+ #### `memory_merge`
282
+
283
+ Merge multiple fragments into one. Creates new ID, deletes originals.
284
+
285
+ **Parameters:**
286
+ - `ids` (string[], required): Fragment IDs to merge
287
+ - `title` (string, required): Title for merged fragment
288
+ - `fragment` (string, required): Merged content
289
+ - `project` (string, optional): Project scope
290
+
291
+ #### `memory_stats`
292
+
293
+ Get memory store statistics: fragment counts, average confidence, project breakdown.
294
+
295
+ **Parameters:**
296
+ - `project` (string, optional): Filter by project
297
+
298
+ #### `memory_audit`
299
+
300
+ Audit memory store for integrity issues: orphan references, duplicate IDs, confidence anomalies.
301
+
302
+ ### Guide Tools (8)
303
+
304
+ #### `guide_get`
305
+
306
+ Get tracked guides with usage statistics. Returns guides sorted by usage count (most used first).
307
+
308
+ **Parameters:**
309
+ - `category` (string, optional): Filter by category
310
+ - `guide` (string, optional): Get detail for specific guide
311
+ - `task` (string, optional): Task description to get relevant guide suggestions
312
+
313
+ #### `guide_practice`
314
+
315
+ **MANDATORY:** Record guide usage when you use a guide during work.
316
+
317
+ **Parameters:**
318
+ - `guide` (string, required): Guide name
319
+ - `category` (string, required): Category
320
+ - `description` (string, optional): Detailed manual/protocols
321
+ - `contexts` (string[], required): Contexts where used
322
+ - `learnings` (string[], required): New learnings discovered
323
+ - `outcome` (string, optional): "success" or "failure" — tracks success rate
324
+
325
+ #### `guide_create`
326
+
327
+ Create a guide with a detailed manual.
328
+
329
+ **Parameters:**
330
+ - `guide` (string, required): Guide name
331
+ - `category` (string, required): Category
332
+ - `description` (string, required): Full manual/protocols
333
+ - `contexts` (string[], optional): Initial contexts
334
+ - `learnings` (string[], optional): Initial learnings
335
+
336
+ #### `guide_distill`
337
+
338
+ Transform a memory fragment into a guide's learning.
339
+
340
+ **Parameters:**
341
+ - `memory_id` (string, required): Memory fragment ID
342
+ - `guide` (string, required): Target guide name
343
+ - `category` (string, optional): Category (required if creating new guide)
344
+
345
+ #### `guide_update`
346
+
347
+ Update an existing guide's properties.
348
+
349
+ **Parameters:**
350
+ - `guide` (string, required): Current guide name
351
+ - `new_name` (string, optional): New name
352
+ - `category` (string, optional): New category
353
+ - `description` (string, optional): New description/manual
354
+ - `add_anti_patterns` (string[], optional): Add anti-patterns
355
+ - `add_pitfalls` (string[], optional): Add known pitfalls
356
+ - `superseded_by` (string, optional): Mark as superseded by another guide
357
+ - `deprecated` (boolean, optional): Mark as deprecated
358
+
359
+ #### `guide_forget`
360
+
361
+ Remove a guide.
362
+
363
+ **Parameters:**
364
+ - `guide` (string, required): Guide name
365
+
366
+ #### `guide_merge`
367
+
368
+ Merge multiple guides into one. Usage counts are summed.
369
+
370
+ **Parameters:**
371
+ - `guides` (string[], required): Guide names to merge
372
+ - `guide` (string, required): Name for merged guide
373
+ - `category` (string, required): Category
374
+ - `description` (string, optional): Merged description
375
+ - `contexts` (string[], optional): Merged contexts
376
+ - `learnings` (string[], optional): Merged learnings
377
+
378
+ ### Session Tools (2)
379
+
380
+ #### `session_start`
381
+
382
+ Start a traced work session. Records task metadata and returns relevant guides.
383
+
384
+ **Parameters:**
385
+ - `task_type` (string, required): "debugging", "implementation", "refactoring", "testing", "research", "documentation", "optimization", or "other"
386
+ - `technologies` (string[], optional): Technologies involved
387
+ - `initial_approach` (string, optional): Initial plan
388
+
389
+ #### `session_end`
390
+
391
+ End the current session. Records outcome and updates guide success tracking.
392
+
393
+ **Parameters:**
394
+ - `outcome` (string, required): "success", "partial", "failure", or "abandoned"
395
+ - `final_approach` (string, optional): What approach worked
396
+ - `lessons` (string[], optional): What was learned
397
+
398
+ #### `session_stats`
399
+
400
+ Get virtual session statistics: recent tool usage patterns and technologies.
401
+
402
+ **Parameters:**
403
+ - `count` (number, optional): Number of recent sessions (default 10)
404
+
405
+ ## Philosophy
406
+
407
+ ### What Should Be Stored
408
+
409
+ **User Layer:**
410
+ - User preferences (communication style, format, language)
411
+ - Project context (technology stack, folder structure, conventions)
412
+ - Explicitly requested memories
413
+
414
+ **Capability Layer:**
415
+ - Successful solutions and approaches used
416
+ - Shortcuts discovered for recurring tasks
417
+ - Approaches that were tried and failed
418
+
419
+ ### What Should NOT Be Stored
420
+
421
+ - Raw conversation content
422
+ - One-off questions that won't recur
423
+ - Temporary or highly context-specific information
424
+ - Personal or sensitive data
425
+
426
+ ## Development
427
+
428
+ ### Running Tests
429
+
430
+ ```bash
431
+ npm test
432
+ ```
433
+
434
+ 110 tests covering memory core, guides core, handlers, learning lifecycle, hook system, dynamic prompt generation, and virtual sessions. All I/O is isolated to temp directories.
435
+
436
+ ### Project Structure
437
+
438
+ ```
439
+ Lemma/
440
+ ├── src/
441
+ │ ├── index.js # MCP server entry point
442
+ │ ├── memory/
443
+ │ │ ├── index.js # Memory module re-exports
444
+ │ │ ├── core.js # Core memory logic, decay, search, dedup
445
+ │ │ └── config.js # User configuration loader
446
+ │ ├── guides/
447
+ │ │ ├── index.js # Guides module re-exports
448
+ │ │ ├── core.js # Core guides logic, fuzzy dedup
449
+ │ │ └── task-map.js # Task-to-guide mapping
450
+ │ ├── server/
451
+ │ │ ├── index.js # Server setup, injection, notifications
452
+ │ │ ├── handlers.js # Tool handlers (20 tools)
453
+ │ │ ├── tools.js # Tool definitions
454
+ │ │ ├── hooks.js # Hook system & prompt modifiers
455
+ │ │ └── system-prompt.js # Dynamic system prompt
456
+ │ └── sessions/
457
+ │ ├── index.js # Sessions module re-exports
458
+ │ ├── core.js # Session lifecycle
459
+ │ └── virtual.js # Virtual session tracking
460
+ ├── tests/
461
+ │ └── test.js # Test suite (110 tests)
462
+ ├── docs/ # Research papers and references
463
+ ├── package.json
464
+ ├── CHANGELOG.md
465
+ └── README.md
466
+ ```
467
+
468
+ ## Security
469
+
470
+ All data is stored locally in `~/.lemma/`. Nothing is ever sent to external servers. Users can inspect, edit, or clear data at any time via the MCP tools or directly.
471
+
472
+ ## License
473
+
474
+ MIT License