alma-memory 0.5.0__py3-none-any.whl → 0.7.0__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.
- alma/__init__.py +296 -194
- alma/compression/__init__.py +33 -0
- alma/compression/pipeline.py +980 -0
- alma/confidence/__init__.py +47 -47
- alma/confidence/engine.py +540 -540
- alma/confidence/types.py +351 -351
- alma/config/loader.py +157 -157
- alma/consolidation/__init__.py +23 -23
- alma/consolidation/engine.py +678 -678
- alma/consolidation/prompts.py +84 -84
- alma/core.py +1189 -322
- alma/domains/__init__.py +30 -30
- alma/domains/factory.py +359 -359
- alma/domains/schemas.py +448 -448
- alma/domains/types.py +272 -272
- alma/events/__init__.py +75 -75
- alma/events/emitter.py +285 -284
- alma/events/storage_mixin.py +246 -246
- alma/events/types.py +126 -126
- alma/events/webhook.py +425 -425
- alma/exceptions.py +49 -49
- alma/extraction/__init__.py +31 -31
- alma/extraction/auto_learner.py +265 -264
- alma/extraction/extractor.py +420 -420
- alma/graph/__init__.py +106 -81
- alma/graph/backends/__init__.py +32 -18
- alma/graph/backends/kuzu.py +624 -0
- alma/graph/backends/memgraph.py +432 -0
- alma/graph/backends/memory.py +236 -236
- alma/graph/backends/neo4j.py +417 -417
- alma/graph/base.py +159 -159
- alma/graph/extraction.py +198 -198
- alma/graph/store.py +860 -860
- alma/harness/__init__.py +35 -35
- alma/harness/base.py +386 -386
- alma/harness/domains.py +705 -705
- alma/initializer/__init__.py +37 -37
- alma/initializer/initializer.py +418 -418
- alma/initializer/types.py +250 -250
- alma/integration/__init__.py +62 -62
- alma/integration/claude_agents.py +444 -432
- alma/integration/helena.py +423 -423
- alma/integration/victor.py +471 -471
- alma/learning/__init__.py +101 -86
- alma/learning/decay.py +878 -0
- alma/learning/forgetting.py +1446 -1446
- alma/learning/heuristic_extractor.py +390 -390
- alma/learning/protocols.py +374 -374
- alma/learning/validation.py +346 -346
- alma/mcp/__init__.py +123 -45
- alma/mcp/__main__.py +156 -156
- alma/mcp/resources.py +122 -122
- alma/mcp/server.py +955 -591
- alma/mcp/tools.py +3254 -511
- alma/observability/__init__.py +91 -0
- alma/observability/config.py +302 -0
- alma/observability/guidelines.py +170 -0
- alma/observability/logging.py +424 -0
- alma/observability/metrics.py +583 -0
- alma/observability/tracing.py +440 -0
- alma/progress/__init__.py +21 -21
- alma/progress/tracker.py +607 -607
- alma/progress/types.py +250 -250
- alma/retrieval/__init__.py +134 -53
- alma/retrieval/budget.py +525 -0
- alma/retrieval/cache.py +1304 -1061
- alma/retrieval/embeddings.py +202 -202
- alma/retrieval/engine.py +850 -366
- alma/retrieval/modes.py +365 -0
- alma/retrieval/progressive.py +560 -0
- alma/retrieval/scoring.py +344 -344
- alma/retrieval/trust_scoring.py +637 -0
- alma/retrieval/verification.py +797 -0
- alma/session/__init__.py +19 -19
- alma/session/manager.py +442 -399
- alma/session/types.py +288 -288
- alma/storage/__init__.py +101 -61
- alma/storage/archive.py +233 -0
- alma/storage/azure_cosmos.py +1259 -1048
- alma/storage/base.py +1083 -525
- alma/storage/chroma.py +1443 -1443
- alma/storage/constants.py +103 -0
- alma/storage/file_based.py +614 -619
- alma/storage/migrations/__init__.py +21 -0
- alma/storage/migrations/base.py +321 -0
- alma/storage/migrations/runner.py +323 -0
- alma/storage/migrations/version_stores.py +337 -0
- alma/storage/migrations/versions/__init__.py +11 -0
- alma/storage/migrations/versions/v1_0_0.py +373 -0
- alma/storage/migrations/versions/v1_1_0_workflow_context.py +551 -0
- alma/storage/pinecone.py +1080 -1080
- alma/storage/postgresql.py +1948 -1452
- alma/storage/qdrant.py +1306 -1306
- alma/storage/sqlite_local.py +3041 -1358
- alma/testing/__init__.py +46 -0
- alma/testing/factories.py +301 -0
- alma/testing/mocks.py +389 -0
- alma/types.py +292 -264
- alma/utils/__init__.py +19 -0
- alma/utils/tokenizer.py +521 -0
- alma/workflow/__init__.py +83 -0
- alma/workflow/artifacts.py +170 -0
- alma/workflow/checkpoint.py +311 -0
- alma/workflow/context.py +228 -0
- alma/workflow/outcomes.py +189 -0
- alma/workflow/reducers.py +393 -0
- {alma_memory-0.5.0.dist-info → alma_memory-0.7.0.dist-info}/METADATA +244 -72
- alma_memory-0.7.0.dist-info/RECORD +112 -0
- alma_memory-0.5.0.dist-info/RECORD +0 -76
- {alma_memory-0.5.0.dist-info → alma_memory-0.7.0.dist-info}/WHEEL +0 -0
- {alma_memory-0.5.0.dist-info → alma_memory-0.7.0.dist-info}/top_level.txt +0 -0
alma/mcp/server.py
CHANGED
|
@@ -1,591 +1,955 @@
|
|
|
1
|
-
"""
|
|
2
|
-
ALMA MCP Server Implementation.
|
|
3
|
-
|
|
4
|
-
Provides the main server class that handles MCP protocol communication.
|
|
5
|
-
Supports both stdio (for Claude Code) and HTTP modes.
|
|
6
|
-
"""
|
|
7
|
-
|
|
8
|
-
import asyncio
|
|
9
|
-
import json
|
|
10
|
-
import logging
|
|
11
|
-
import sys
|
|
12
|
-
from typing import Any, Dict, List, Optional
|
|
13
|
-
|
|
14
|
-
from alma import ALMA
|
|
15
|
-
from alma.mcp.resources import (
|
|
16
|
-
get_agents_resource,
|
|
17
|
-
get_config_resource,
|
|
18
|
-
list_resources,
|
|
19
|
-
)
|
|
20
|
-
from alma.mcp.tools import (
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
""
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
"
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
"
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
"
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
"
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
"
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
"
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
"
|
|
212
|
-
"
|
|
213
|
-
},
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
"
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
"description": "
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
"
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
"description": "
|
|
267
|
-
"default":
|
|
268
|
-
},
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
"
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
"
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
"
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
"
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
1
|
+
"""
|
|
2
|
+
ALMA MCP Server Implementation.
|
|
3
|
+
|
|
4
|
+
Provides the main server class that handles MCP protocol communication.
|
|
5
|
+
Supports both stdio (for Claude Code) and HTTP modes.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import asyncio
|
|
9
|
+
import json
|
|
10
|
+
import logging
|
|
11
|
+
import sys
|
|
12
|
+
from typing import Any, Dict, List, Optional
|
|
13
|
+
|
|
14
|
+
from alma import ALMA
|
|
15
|
+
from alma.mcp.resources import (
|
|
16
|
+
get_agents_resource,
|
|
17
|
+
get_config_resource,
|
|
18
|
+
list_resources,
|
|
19
|
+
)
|
|
20
|
+
from alma.mcp.tools import (
|
|
21
|
+
alma_cleanup_checkpoints,
|
|
22
|
+
alma_consolidate,
|
|
23
|
+
alma_get_artifacts,
|
|
24
|
+
alma_health,
|
|
25
|
+
alma_merge_states,
|
|
26
|
+
async_alma_add_knowledge,
|
|
27
|
+
async_alma_add_preference,
|
|
28
|
+
# Async workflow variants
|
|
29
|
+
async_alma_checkpoint,
|
|
30
|
+
async_alma_forget,
|
|
31
|
+
async_alma_health,
|
|
32
|
+
async_alma_learn,
|
|
33
|
+
async_alma_link_artifact,
|
|
34
|
+
async_alma_resume,
|
|
35
|
+
async_alma_retrieve,
|
|
36
|
+
async_alma_retrieve_scoped,
|
|
37
|
+
async_alma_stats,
|
|
38
|
+
async_alma_workflow_learn,
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
logger = logging.getLogger(__name__)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class ALMAMCPServer:
|
|
45
|
+
"""
|
|
46
|
+
MCP Server for ALMA.
|
|
47
|
+
|
|
48
|
+
Exposes ALMA functionality via the Model Context Protocol,
|
|
49
|
+
allowing any MCP-compatible client (like Claude Code) to
|
|
50
|
+
interact with the memory system.
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
def __init__(
|
|
54
|
+
self,
|
|
55
|
+
alma: ALMA,
|
|
56
|
+
server_name: str = "alma-memory",
|
|
57
|
+
server_version: str = "0.6.0",
|
|
58
|
+
):
|
|
59
|
+
"""
|
|
60
|
+
Initialize the MCP server.
|
|
61
|
+
|
|
62
|
+
Args:
|
|
63
|
+
alma: Configured ALMA instance
|
|
64
|
+
server_name: Server identifier
|
|
65
|
+
server_version: Server version
|
|
66
|
+
"""
|
|
67
|
+
self.alma = alma
|
|
68
|
+
self.server_name = server_name
|
|
69
|
+
self.server_version = server_version
|
|
70
|
+
|
|
71
|
+
# Register tools
|
|
72
|
+
self.tools = self._register_tools()
|
|
73
|
+
|
|
74
|
+
# Register resources
|
|
75
|
+
self.resources = list_resources()
|
|
76
|
+
|
|
77
|
+
def _register_tools(self) -> List[Dict[str, Any]]:
|
|
78
|
+
"""Register available MCP tools."""
|
|
79
|
+
return [
|
|
80
|
+
{
|
|
81
|
+
"name": "alma_retrieve",
|
|
82
|
+
"description": "Retrieve relevant memories for a task. Returns heuristics, domain knowledge, anti-patterns, and user preferences.",
|
|
83
|
+
"inputSchema": {
|
|
84
|
+
"type": "object",
|
|
85
|
+
"properties": {
|
|
86
|
+
"task": {
|
|
87
|
+
"type": "string",
|
|
88
|
+
"description": "Description of the task to perform",
|
|
89
|
+
},
|
|
90
|
+
"agent": {
|
|
91
|
+
"type": "string",
|
|
92
|
+
"description": "Name of the agent requesting memories (e.g., 'helena', 'victor')",
|
|
93
|
+
},
|
|
94
|
+
"user_id": {
|
|
95
|
+
"type": "string",
|
|
96
|
+
"description": "Optional user ID for preference retrieval",
|
|
97
|
+
},
|
|
98
|
+
"top_k": {
|
|
99
|
+
"type": "integer",
|
|
100
|
+
"description": "Maximum items per memory type (default: 5)",
|
|
101
|
+
"default": 5,
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
"required": ["task", "agent"],
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
"name": "alma_learn",
|
|
109
|
+
"description": "Record a task outcome for learning. Use after completing a task to help improve future performance.",
|
|
110
|
+
"inputSchema": {
|
|
111
|
+
"type": "object",
|
|
112
|
+
"properties": {
|
|
113
|
+
"agent": {
|
|
114
|
+
"type": "string",
|
|
115
|
+
"description": "Name of the agent that executed the task",
|
|
116
|
+
},
|
|
117
|
+
"task": {
|
|
118
|
+
"type": "string",
|
|
119
|
+
"description": "Description of the task",
|
|
120
|
+
},
|
|
121
|
+
"outcome": {
|
|
122
|
+
"type": "string",
|
|
123
|
+
"enum": ["success", "failure"],
|
|
124
|
+
"description": "Whether the task succeeded or failed",
|
|
125
|
+
},
|
|
126
|
+
"strategy_used": {
|
|
127
|
+
"type": "string",
|
|
128
|
+
"description": "What approach was taken",
|
|
129
|
+
},
|
|
130
|
+
"task_type": {
|
|
131
|
+
"type": "string",
|
|
132
|
+
"description": "Category of task (for grouping)",
|
|
133
|
+
},
|
|
134
|
+
"duration_ms": {
|
|
135
|
+
"type": "integer",
|
|
136
|
+
"description": "How long the task took in milliseconds",
|
|
137
|
+
},
|
|
138
|
+
"error_message": {
|
|
139
|
+
"type": "string",
|
|
140
|
+
"description": "Error details if failed",
|
|
141
|
+
},
|
|
142
|
+
"feedback": {
|
|
143
|
+
"type": "string",
|
|
144
|
+
"description": "User feedback if provided",
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
"required": ["agent", "task", "outcome", "strategy_used"],
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
"name": "alma_add_preference",
|
|
152
|
+
"description": "Add a user preference to memory. Preferences persist across sessions.",
|
|
153
|
+
"inputSchema": {
|
|
154
|
+
"type": "object",
|
|
155
|
+
"properties": {
|
|
156
|
+
"user_id": {
|
|
157
|
+
"type": "string",
|
|
158
|
+
"description": "User identifier",
|
|
159
|
+
},
|
|
160
|
+
"category": {
|
|
161
|
+
"type": "string",
|
|
162
|
+
"description": "Category (communication, code_style, workflow)",
|
|
163
|
+
},
|
|
164
|
+
"preference": {
|
|
165
|
+
"type": "string",
|
|
166
|
+
"description": "The preference text",
|
|
167
|
+
},
|
|
168
|
+
"source": {
|
|
169
|
+
"type": "string",
|
|
170
|
+
"description": "How this was learned (default: explicit_instruction)",
|
|
171
|
+
"default": "explicit_instruction",
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
"required": ["user_id", "category", "preference"],
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
"name": "alma_add_knowledge",
|
|
179
|
+
"description": "Add domain knowledge within agent's scope. Knowledge is facts, not strategies.",
|
|
180
|
+
"inputSchema": {
|
|
181
|
+
"type": "object",
|
|
182
|
+
"properties": {
|
|
183
|
+
"agent": {
|
|
184
|
+
"type": "string",
|
|
185
|
+
"description": "Agent this knowledge belongs to",
|
|
186
|
+
},
|
|
187
|
+
"domain": {
|
|
188
|
+
"type": "string",
|
|
189
|
+
"description": "Knowledge domain",
|
|
190
|
+
},
|
|
191
|
+
"fact": {
|
|
192
|
+
"type": "string",
|
|
193
|
+
"description": "The fact to remember",
|
|
194
|
+
},
|
|
195
|
+
"source": {
|
|
196
|
+
"type": "string",
|
|
197
|
+
"description": "How this was learned (default: user_stated)",
|
|
198
|
+
"default": "user_stated",
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
"required": ["agent", "domain", "fact"],
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
"name": "alma_forget",
|
|
206
|
+
"description": "Prune stale or low-confidence memories to keep the system clean.",
|
|
207
|
+
"inputSchema": {
|
|
208
|
+
"type": "object",
|
|
209
|
+
"properties": {
|
|
210
|
+
"agent": {
|
|
211
|
+
"type": "string",
|
|
212
|
+
"description": "Specific agent to prune, or omit for all",
|
|
213
|
+
},
|
|
214
|
+
"older_than_days": {
|
|
215
|
+
"type": "integer",
|
|
216
|
+
"description": "Remove outcomes older than this (default: 90)",
|
|
217
|
+
"default": 90,
|
|
218
|
+
},
|
|
219
|
+
"below_confidence": {
|
|
220
|
+
"type": "number",
|
|
221
|
+
"description": "Remove heuristics below this confidence (default: 0.3)",
|
|
222
|
+
"default": 0.3,
|
|
223
|
+
},
|
|
224
|
+
},
|
|
225
|
+
},
|
|
226
|
+
},
|
|
227
|
+
{
|
|
228
|
+
"name": "alma_stats",
|
|
229
|
+
"description": "Get memory statistics for monitoring and debugging.",
|
|
230
|
+
"inputSchema": {
|
|
231
|
+
"type": "object",
|
|
232
|
+
"properties": {
|
|
233
|
+
"agent": {
|
|
234
|
+
"type": "string",
|
|
235
|
+
"description": "Specific agent or omit for all",
|
|
236
|
+
},
|
|
237
|
+
},
|
|
238
|
+
},
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
"name": "alma_health",
|
|
242
|
+
"description": "Health check for the ALMA server.",
|
|
243
|
+
"inputSchema": {
|
|
244
|
+
"type": "object",
|
|
245
|
+
"properties": {},
|
|
246
|
+
},
|
|
247
|
+
},
|
|
248
|
+
{
|
|
249
|
+
"name": "alma_consolidate",
|
|
250
|
+
"description": "Consolidate similar memories to reduce redundancy. Merges near-duplicate memories based on semantic similarity. Use dry_run=true first to preview what would be merged.",
|
|
251
|
+
"inputSchema": {
|
|
252
|
+
"type": "object",
|
|
253
|
+
"properties": {
|
|
254
|
+
"agent": {
|
|
255
|
+
"type": "string",
|
|
256
|
+
"description": "Agent whose memories to consolidate",
|
|
257
|
+
},
|
|
258
|
+
"memory_type": {
|
|
259
|
+
"type": "string",
|
|
260
|
+
"enum": [
|
|
261
|
+
"heuristics",
|
|
262
|
+
"outcomes",
|
|
263
|
+
"domain_knowledge",
|
|
264
|
+
"anti_patterns",
|
|
265
|
+
],
|
|
266
|
+
"description": "Type of memory to consolidate (default: heuristics)",
|
|
267
|
+
"default": "heuristics",
|
|
268
|
+
},
|
|
269
|
+
"similarity_threshold": {
|
|
270
|
+
"type": "number",
|
|
271
|
+
"description": "Minimum cosine similarity to group memories (0.0-1.0, default: 0.85). Higher values are more conservative.",
|
|
272
|
+
"default": 0.85,
|
|
273
|
+
},
|
|
274
|
+
"dry_run": {
|
|
275
|
+
"type": "boolean",
|
|
276
|
+
"description": "If true, preview what would be merged without modifying storage (default: true)",
|
|
277
|
+
"default": True,
|
|
278
|
+
},
|
|
279
|
+
},
|
|
280
|
+
"required": ["agent"],
|
|
281
|
+
},
|
|
282
|
+
},
|
|
283
|
+
# ==================== WORKFLOW TOOLS (v0.6.0) ====================
|
|
284
|
+
{
|
|
285
|
+
"name": "alma_checkpoint",
|
|
286
|
+
"description": "Create a checkpoint for crash recovery. Persists workflow state at key execution points.",
|
|
287
|
+
"inputSchema": {
|
|
288
|
+
"type": "object",
|
|
289
|
+
"properties": {
|
|
290
|
+
"run_id": {
|
|
291
|
+
"type": "string",
|
|
292
|
+
"description": "The workflow run identifier",
|
|
293
|
+
},
|
|
294
|
+
"node_id": {
|
|
295
|
+
"type": "string",
|
|
296
|
+
"description": "The node creating this checkpoint",
|
|
297
|
+
},
|
|
298
|
+
"state": {
|
|
299
|
+
"type": "object",
|
|
300
|
+
"description": "The state to persist",
|
|
301
|
+
},
|
|
302
|
+
"branch_id": {
|
|
303
|
+
"type": "string",
|
|
304
|
+
"description": "Optional branch identifier for parallel execution",
|
|
305
|
+
},
|
|
306
|
+
"parent_checkpoint_id": {
|
|
307
|
+
"type": "string",
|
|
308
|
+
"description": "Previous checkpoint in the chain",
|
|
309
|
+
},
|
|
310
|
+
"metadata": {
|
|
311
|
+
"type": "object",
|
|
312
|
+
"description": "Additional checkpoint metadata",
|
|
313
|
+
},
|
|
314
|
+
"skip_if_unchanged": {
|
|
315
|
+
"type": "boolean",
|
|
316
|
+
"description": "Skip if state hasn't changed (default: true)",
|
|
317
|
+
"default": True,
|
|
318
|
+
},
|
|
319
|
+
},
|
|
320
|
+
"required": ["run_id", "node_id", "state"],
|
|
321
|
+
},
|
|
322
|
+
},
|
|
323
|
+
{
|
|
324
|
+
"name": "alma_resume",
|
|
325
|
+
"description": "Get the checkpoint to resume from after a crash.",
|
|
326
|
+
"inputSchema": {
|
|
327
|
+
"type": "object",
|
|
328
|
+
"properties": {
|
|
329
|
+
"run_id": {
|
|
330
|
+
"type": "string",
|
|
331
|
+
"description": "The workflow run identifier",
|
|
332
|
+
},
|
|
333
|
+
"branch_id": {
|
|
334
|
+
"type": "string",
|
|
335
|
+
"description": "Optional branch to filter by",
|
|
336
|
+
},
|
|
337
|
+
},
|
|
338
|
+
"required": ["run_id"],
|
|
339
|
+
},
|
|
340
|
+
},
|
|
341
|
+
{
|
|
342
|
+
"name": "alma_merge_states",
|
|
343
|
+
"description": "Merge multiple branch states after parallel execution using configurable reducers.",
|
|
344
|
+
"inputSchema": {
|
|
345
|
+
"type": "object",
|
|
346
|
+
"properties": {
|
|
347
|
+
"states": {
|
|
348
|
+
"type": "array",
|
|
349
|
+
"items": {"type": "object"},
|
|
350
|
+
"description": "List of state dicts from parallel branches",
|
|
351
|
+
},
|
|
352
|
+
"reducer_config": {
|
|
353
|
+
"type": "object",
|
|
354
|
+
"description": "Mapping of key -> reducer (append, merge_dict, last_value, first_value, sum, max, min, union)",
|
|
355
|
+
},
|
|
356
|
+
},
|
|
357
|
+
"required": ["states"],
|
|
358
|
+
},
|
|
359
|
+
},
|
|
360
|
+
{
|
|
361
|
+
"name": "alma_workflow_learn",
|
|
362
|
+
"description": "Record learnings from a completed workflow execution.",
|
|
363
|
+
"inputSchema": {
|
|
364
|
+
"type": "object",
|
|
365
|
+
"properties": {
|
|
366
|
+
"agent": {
|
|
367
|
+
"type": "string",
|
|
368
|
+
"description": "The agent that executed the workflow",
|
|
369
|
+
},
|
|
370
|
+
"workflow_id": {
|
|
371
|
+
"type": "string",
|
|
372
|
+
"description": "The workflow definition identifier",
|
|
373
|
+
},
|
|
374
|
+
"run_id": {
|
|
375
|
+
"type": "string",
|
|
376
|
+
"description": "The specific run identifier",
|
|
377
|
+
},
|
|
378
|
+
"result": {
|
|
379
|
+
"type": "string",
|
|
380
|
+
"enum": [
|
|
381
|
+
"success",
|
|
382
|
+
"failure",
|
|
383
|
+
"partial",
|
|
384
|
+
"cancelled",
|
|
385
|
+
"timeout",
|
|
386
|
+
],
|
|
387
|
+
"description": "Result status",
|
|
388
|
+
},
|
|
389
|
+
"summary": {
|
|
390
|
+
"type": "string",
|
|
391
|
+
"description": "Human-readable summary of what happened",
|
|
392
|
+
},
|
|
393
|
+
"strategies_used": {
|
|
394
|
+
"type": "array",
|
|
395
|
+
"items": {"type": "string"},
|
|
396
|
+
"description": "List of strategies attempted",
|
|
397
|
+
},
|
|
398
|
+
"successful_patterns": {
|
|
399
|
+
"type": "array",
|
|
400
|
+
"items": {"type": "string"},
|
|
401
|
+
"description": "Patterns that worked well",
|
|
402
|
+
},
|
|
403
|
+
"failed_patterns": {
|
|
404
|
+
"type": "array",
|
|
405
|
+
"items": {"type": "string"},
|
|
406
|
+
"description": "Patterns that didn't work",
|
|
407
|
+
},
|
|
408
|
+
"duration_seconds": {
|
|
409
|
+
"type": "number",
|
|
410
|
+
"description": "How long the workflow took",
|
|
411
|
+
},
|
|
412
|
+
"node_count": {
|
|
413
|
+
"type": "integer",
|
|
414
|
+
"description": "Number of nodes executed",
|
|
415
|
+
},
|
|
416
|
+
"error_message": {
|
|
417
|
+
"type": "string",
|
|
418
|
+
"description": "Error details if failed",
|
|
419
|
+
},
|
|
420
|
+
"tenant_id": {
|
|
421
|
+
"type": "string",
|
|
422
|
+
"description": "Multi-tenant isolation identifier",
|
|
423
|
+
},
|
|
424
|
+
"metadata": {
|
|
425
|
+
"type": "object",
|
|
426
|
+
"description": "Additional outcome metadata",
|
|
427
|
+
},
|
|
428
|
+
},
|
|
429
|
+
"required": ["agent", "workflow_id", "run_id", "result", "summary"],
|
|
430
|
+
},
|
|
431
|
+
},
|
|
432
|
+
{
|
|
433
|
+
"name": "alma_link_artifact",
|
|
434
|
+
"description": "Link an external artifact (file, screenshot, log) to a memory.",
|
|
435
|
+
"inputSchema": {
|
|
436
|
+
"type": "object",
|
|
437
|
+
"properties": {
|
|
438
|
+
"memory_id": {
|
|
439
|
+
"type": "string",
|
|
440
|
+
"description": "The memory to link the artifact to",
|
|
441
|
+
},
|
|
442
|
+
"artifact_type": {
|
|
443
|
+
"type": "string",
|
|
444
|
+
"description": "Type (screenshot, log, report, file, document, image, etc.)",
|
|
445
|
+
},
|
|
446
|
+
"storage_url": {
|
|
447
|
+
"type": "string",
|
|
448
|
+
"description": "URL or path to the artifact in storage",
|
|
449
|
+
},
|
|
450
|
+
"filename": {
|
|
451
|
+
"type": "string",
|
|
452
|
+
"description": "Original filename",
|
|
453
|
+
},
|
|
454
|
+
"mime_type": {
|
|
455
|
+
"type": "string",
|
|
456
|
+
"description": "MIME type",
|
|
457
|
+
},
|
|
458
|
+
"size_bytes": {
|
|
459
|
+
"type": "integer",
|
|
460
|
+
"description": "Size in bytes",
|
|
461
|
+
},
|
|
462
|
+
"checksum": {
|
|
463
|
+
"type": "string",
|
|
464
|
+
"description": "SHA256 checksum for integrity",
|
|
465
|
+
},
|
|
466
|
+
"metadata": {
|
|
467
|
+
"type": "object",
|
|
468
|
+
"description": "Additional artifact metadata",
|
|
469
|
+
},
|
|
470
|
+
},
|
|
471
|
+
"required": ["memory_id", "artifact_type", "storage_url"],
|
|
472
|
+
},
|
|
473
|
+
},
|
|
474
|
+
{
|
|
475
|
+
"name": "alma_get_artifacts",
|
|
476
|
+
"description": "Get all artifacts linked to a memory.",
|
|
477
|
+
"inputSchema": {
|
|
478
|
+
"type": "object",
|
|
479
|
+
"properties": {
|
|
480
|
+
"memory_id": {
|
|
481
|
+
"type": "string",
|
|
482
|
+
"description": "The memory to get artifacts for",
|
|
483
|
+
},
|
|
484
|
+
},
|
|
485
|
+
"required": ["memory_id"],
|
|
486
|
+
},
|
|
487
|
+
},
|
|
488
|
+
{
|
|
489
|
+
"name": "alma_cleanup_checkpoints",
|
|
490
|
+
"description": "Clean up old checkpoints for a completed workflow run.",
|
|
491
|
+
"inputSchema": {
|
|
492
|
+
"type": "object",
|
|
493
|
+
"properties": {
|
|
494
|
+
"run_id": {
|
|
495
|
+
"type": "string",
|
|
496
|
+
"description": "The workflow run identifier",
|
|
497
|
+
},
|
|
498
|
+
"keep_latest": {
|
|
499
|
+
"type": "integer",
|
|
500
|
+
"description": "Number of latest checkpoints to keep (default: 1)",
|
|
501
|
+
"default": 1,
|
|
502
|
+
},
|
|
503
|
+
},
|
|
504
|
+
"required": ["run_id"],
|
|
505
|
+
},
|
|
506
|
+
},
|
|
507
|
+
{
|
|
508
|
+
"name": "alma_retrieve_scoped",
|
|
509
|
+
"description": "Retrieve memories with workflow scope filtering. Supports hierarchical scoping: node -> run -> workflow -> agent -> tenant -> global.",
|
|
510
|
+
"inputSchema": {
|
|
511
|
+
"type": "object",
|
|
512
|
+
"properties": {
|
|
513
|
+
"task": {
|
|
514
|
+
"type": "string",
|
|
515
|
+
"description": "Description of the task to perform",
|
|
516
|
+
},
|
|
517
|
+
"agent": {
|
|
518
|
+
"type": "string",
|
|
519
|
+
"description": "Name of the agent requesting memories",
|
|
520
|
+
},
|
|
521
|
+
"scope": {
|
|
522
|
+
"type": "string",
|
|
523
|
+
"enum": [
|
|
524
|
+
"node",
|
|
525
|
+
"run",
|
|
526
|
+
"workflow",
|
|
527
|
+
"agent",
|
|
528
|
+
"tenant",
|
|
529
|
+
"global",
|
|
530
|
+
],
|
|
531
|
+
"description": "Scope level for filtering (default: agent)",
|
|
532
|
+
"default": "agent",
|
|
533
|
+
},
|
|
534
|
+
"tenant_id": {
|
|
535
|
+
"type": "string",
|
|
536
|
+
"description": "Tenant identifier for multi-tenant",
|
|
537
|
+
},
|
|
538
|
+
"workflow_id": {
|
|
539
|
+
"type": "string",
|
|
540
|
+
"description": "Workflow definition identifier",
|
|
541
|
+
},
|
|
542
|
+
"run_id": {
|
|
543
|
+
"type": "string",
|
|
544
|
+
"description": "Specific run identifier",
|
|
545
|
+
},
|
|
546
|
+
"node_id": {
|
|
547
|
+
"type": "string",
|
|
548
|
+
"description": "Current node identifier",
|
|
549
|
+
},
|
|
550
|
+
"user_id": {
|
|
551
|
+
"type": "string",
|
|
552
|
+
"description": "Optional user ID for preferences",
|
|
553
|
+
},
|
|
554
|
+
"top_k": {
|
|
555
|
+
"type": "integer",
|
|
556
|
+
"description": "Maximum items per memory type (default: 5)",
|
|
557
|
+
"default": 5,
|
|
558
|
+
},
|
|
559
|
+
},
|
|
560
|
+
"required": ["task", "agent"],
|
|
561
|
+
},
|
|
562
|
+
},
|
|
563
|
+
]
|
|
564
|
+
|
|
565
|
+
async def handle_request(self, request: Dict[str, Any]) -> Dict[str, Any]:
|
|
566
|
+
"""
|
|
567
|
+
Handle an incoming MCP request.
|
|
568
|
+
|
|
569
|
+
Args:
|
|
570
|
+
request: The MCP request
|
|
571
|
+
|
|
572
|
+
Returns:
|
|
573
|
+
MCP response
|
|
574
|
+
"""
|
|
575
|
+
method = request.get("method", "")
|
|
576
|
+
params = request.get("params", {})
|
|
577
|
+
request_id = request.get("id")
|
|
578
|
+
|
|
579
|
+
try:
|
|
580
|
+
if method == "initialize":
|
|
581
|
+
return self._handle_initialize(request_id, params)
|
|
582
|
+
elif method == "tools/list":
|
|
583
|
+
return self._handle_tools_list(request_id)
|
|
584
|
+
elif method == "tools/call":
|
|
585
|
+
return await self._handle_tool_call(request_id, params)
|
|
586
|
+
elif method == "resources/list":
|
|
587
|
+
return self._handle_resources_list(request_id)
|
|
588
|
+
elif method == "resources/read":
|
|
589
|
+
return self._handle_resource_read(request_id, params)
|
|
590
|
+
elif method == "ping":
|
|
591
|
+
return self._success_response(request_id, {})
|
|
592
|
+
else:
|
|
593
|
+
return self._error_response(
|
|
594
|
+
request_id,
|
|
595
|
+
-32601,
|
|
596
|
+
f"Method not found: {method}",
|
|
597
|
+
)
|
|
598
|
+
|
|
599
|
+
except Exception as e:
|
|
600
|
+
logger.exception(f"Error handling request: {e}")
|
|
601
|
+
return self._error_response(request_id, -32603, str(e))
|
|
602
|
+
|
|
603
|
+
def _handle_initialize(
|
|
604
|
+
self,
|
|
605
|
+
request_id: Optional[int],
|
|
606
|
+
params: Dict[str, Any],
|
|
607
|
+
) -> Dict[str, Any]:
|
|
608
|
+
"""Handle initialize request."""
|
|
609
|
+
return self._success_response(
|
|
610
|
+
request_id,
|
|
611
|
+
{
|
|
612
|
+
"protocolVersion": "2024-11-05",
|
|
613
|
+
"serverInfo": {
|
|
614
|
+
"name": self.server_name,
|
|
615
|
+
"version": self.server_version,
|
|
616
|
+
},
|
|
617
|
+
"capabilities": {
|
|
618
|
+
"tools": {},
|
|
619
|
+
"resources": {},
|
|
620
|
+
},
|
|
621
|
+
},
|
|
622
|
+
)
|
|
623
|
+
|
|
624
|
+
def _handle_tools_list(self, request_id: Optional[int]) -> Dict[str, Any]:
|
|
625
|
+
"""Handle tools/list request."""
|
|
626
|
+
return self._success_response(request_id, {"tools": self.tools})
|
|
627
|
+
|
|
628
|
+
async def _handle_tool_call(
|
|
629
|
+
self,
|
|
630
|
+
request_id: Optional[int],
|
|
631
|
+
params: Dict[str, Any],
|
|
632
|
+
) -> Dict[str, Any]:
|
|
633
|
+
"""
|
|
634
|
+
Handle tools/call request.
|
|
635
|
+
|
|
636
|
+
Uses async tool variants for better concurrency in the async server.
|
|
637
|
+
"""
|
|
638
|
+
tool_name = params.get("name", "")
|
|
639
|
+
arguments = params.get("arguments", {})
|
|
640
|
+
|
|
641
|
+
# Map tool names to async functions for non-blocking execution
|
|
642
|
+
# All tools now use async variants for consistency in async server
|
|
643
|
+
async_tool_handlers = {
|
|
644
|
+
"alma_retrieve": lambda: async_alma_retrieve(
|
|
645
|
+
self.alma,
|
|
646
|
+
task=arguments.get("task", ""),
|
|
647
|
+
agent=arguments.get("agent", ""),
|
|
648
|
+
user_id=arguments.get("user_id"),
|
|
649
|
+
top_k=arguments.get("top_k", 5),
|
|
650
|
+
),
|
|
651
|
+
"alma_learn": lambda: async_alma_learn(
|
|
652
|
+
self.alma,
|
|
653
|
+
agent=arguments.get("agent", ""),
|
|
654
|
+
task=arguments.get("task", ""),
|
|
655
|
+
outcome=arguments.get("outcome", ""),
|
|
656
|
+
strategy_used=arguments.get("strategy_used", ""),
|
|
657
|
+
task_type=arguments.get("task_type"),
|
|
658
|
+
duration_ms=arguments.get("duration_ms"),
|
|
659
|
+
error_message=arguments.get("error_message"),
|
|
660
|
+
feedback=arguments.get("feedback"),
|
|
661
|
+
),
|
|
662
|
+
"alma_add_preference": lambda: async_alma_add_preference(
|
|
663
|
+
self.alma,
|
|
664
|
+
user_id=arguments.get("user_id", ""),
|
|
665
|
+
category=arguments.get("category", ""),
|
|
666
|
+
preference=arguments.get("preference", ""),
|
|
667
|
+
source=arguments.get("source", "explicit_instruction"),
|
|
668
|
+
),
|
|
669
|
+
"alma_add_knowledge": lambda: async_alma_add_knowledge(
|
|
670
|
+
self.alma,
|
|
671
|
+
agent=arguments.get("agent", ""),
|
|
672
|
+
domain=arguments.get("domain", ""),
|
|
673
|
+
fact=arguments.get("fact", ""),
|
|
674
|
+
source=arguments.get("source", "user_stated"),
|
|
675
|
+
),
|
|
676
|
+
"alma_forget": lambda: async_alma_forget(
|
|
677
|
+
self.alma,
|
|
678
|
+
agent=arguments.get("agent"),
|
|
679
|
+
older_than_days=arguments.get("older_than_days", 90),
|
|
680
|
+
below_confidence=arguments.get("below_confidence", 0.3),
|
|
681
|
+
),
|
|
682
|
+
"alma_stats": lambda: async_alma_stats(
|
|
683
|
+
self.alma,
|
|
684
|
+
agent=arguments.get("agent"),
|
|
685
|
+
),
|
|
686
|
+
"alma_health": lambda: async_alma_health(self.alma),
|
|
687
|
+
"alma_consolidate": lambda: alma_consolidate(
|
|
688
|
+
self.alma,
|
|
689
|
+
agent=arguments.get("agent", ""),
|
|
690
|
+
memory_type=arguments.get("memory_type", "heuristics"),
|
|
691
|
+
similarity_threshold=arguments.get("similarity_threshold", 0.85),
|
|
692
|
+
dry_run=arguments.get("dry_run", True),
|
|
693
|
+
),
|
|
694
|
+
# Workflow tools (v0.6.0)
|
|
695
|
+
"alma_checkpoint": lambda: async_alma_checkpoint(
|
|
696
|
+
self.alma,
|
|
697
|
+
run_id=arguments.get("run_id", ""),
|
|
698
|
+
node_id=arguments.get("node_id", ""),
|
|
699
|
+
state=arguments.get("state", {}),
|
|
700
|
+
branch_id=arguments.get("branch_id"),
|
|
701
|
+
parent_checkpoint_id=arguments.get("parent_checkpoint_id"),
|
|
702
|
+
metadata=arguments.get("metadata"),
|
|
703
|
+
skip_if_unchanged=arguments.get("skip_if_unchanged", True),
|
|
704
|
+
),
|
|
705
|
+
"alma_resume": lambda: async_alma_resume(
|
|
706
|
+
self.alma,
|
|
707
|
+
run_id=arguments.get("run_id", ""),
|
|
708
|
+
branch_id=arguments.get("branch_id"),
|
|
709
|
+
),
|
|
710
|
+
"alma_merge_states": lambda: alma_merge_states(
|
|
711
|
+
self.alma,
|
|
712
|
+
states=arguments.get("states", []),
|
|
713
|
+
reducer_config=arguments.get("reducer_config"),
|
|
714
|
+
),
|
|
715
|
+
"alma_workflow_learn": lambda: async_alma_workflow_learn(
|
|
716
|
+
self.alma,
|
|
717
|
+
agent=arguments.get("agent", ""),
|
|
718
|
+
workflow_id=arguments.get("workflow_id", ""),
|
|
719
|
+
run_id=arguments.get("run_id", ""),
|
|
720
|
+
result=arguments.get("result", ""),
|
|
721
|
+
summary=arguments.get("summary", ""),
|
|
722
|
+
strategies_used=arguments.get("strategies_used"),
|
|
723
|
+
successful_patterns=arguments.get("successful_patterns"),
|
|
724
|
+
failed_patterns=arguments.get("failed_patterns"),
|
|
725
|
+
duration_seconds=arguments.get("duration_seconds"),
|
|
726
|
+
node_count=arguments.get("node_count"),
|
|
727
|
+
error_message=arguments.get("error_message"),
|
|
728
|
+
tenant_id=arguments.get("tenant_id"),
|
|
729
|
+
metadata=arguments.get("metadata"),
|
|
730
|
+
),
|
|
731
|
+
"alma_link_artifact": lambda: async_alma_link_artifact(
|
|
732
|
+
self.alma,
|
|
733
|
+
memory_id=arguments.get("memory_id", ""),
|
|
734
|
+
artifact_type=arguments.get("artifact_type", ""),
|
|
735
|
+
storage_url=arguments.get("storage_url", ""),
|
|
736
|
+
filename=arguments.get("filename"),
|
|
737
|
+
mime_type=arguments.get("mime_type"),
|
|
738
|
+
size_bytes=arguments.get("size_bytes"),
|
|
739
|
+
checksum=arguments.get("checksum"),
|
|
740
|
+
metadata=arguments.get("metadata"),
|
|
741
|
+
),
|
|
742
|
+
"alma_get_artifacts": lambda: alma_get_artifacts(
|
|
743
|
+
self.alma,
|
|
744
|
+
memory_id=arguments.get("memory_id", ""),
|
|
745
|
+
),
|
|
746
|
+
"alma_cleanup_checkpoints": lambda: alma_cleanup_checkpoints(
|
|
747
|
+
self.alma,
|
|
748
|
+
run_id=arguments.get("run_id", ""),
|
|
749
|
+
keep_latest=arguments.get("keep_latest", 1),
|
|
750
|
+
),
|
|
751
|
+
"alma_retrieve_scoped": lambda: async_alma_retrieve_scoped(
|
|
752
|
+
self.alma,
|
|
753
|
+
task=arguments.get("task", ""),
|
|
754
|
+
agent=arguments.get("agent", ""),
|
|
755
|
+
scope=arguments.get("scope", "agent"),
|
|
756
|
+
tenant_id=arguments.get("tenant_id"),
|
|
757
|
+
workflow_id=arguments.get("workflow_id"),
|
|
758
|
+
run_id=arguments.get("run_id"),
|
|
759
|
+
node_id=arguments.get("node_id"),
|
|
760
|
+
user_id=arguments.get("user_id"),
|
|
761
|
+
top_k=arguments.get("top_k", 5),
|
|
762
|
+
),
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
if tool_name not in async_tool_handlers:
|
|
766
|
+
return self._error_response(
|
|
767
|
+
request_id,
|
|
768
|
+
-32602,
|
|
769
|
+
f"Unknown tool: {tool_name}",
|
|
770
|
+
)
|
|
771
|
+
|
|
772
|
+
result = async_tool_handlers[tool_name]()
|
|
773
|
+
|
|
774
|
+
# All handlers now return coroutines
|
|
775
|
+
if asyncio.iscoroutine(result):
|
|
776
|
+
result = await result
|
|
777
|
+
|
|
778
|
+
return self._success_response(
|
|
779
|
+
request_id,
|
|
780
|
+
{
|
|
781
|
+
"content": [
|
|
782
|
+
{
|
|
783
|
+
"type": "text",
|
|
784
|
+
"text": json.dumps(result, indent=2),
|
|
785
|
+
}
|
|
786
|
+
],
|
|
787
|
+
},
|
|
788
|
+
)
|
|
789
|
+
|
|
790
|
+
def _handle_resources_list(
|
|
791
|
+
self,
|
|
792
|
+
request_id: Optional[int],
|
|
793
|
+
) -> Dict[str, Any]:
|
|
794
|
+
"""Handle resources/list request."""
|
|
795
|
+
return self._success_response(request_id, {"resources": self.resources})
|
|
796
|
+
|
|
797
|
+
def _handle_resource_read(
|
|
798
|
+
self,
|
|
799
|
+
request_id: Optional[int],
|
|
800
|
+
params: Dict[str, Any],
|
|
801
|
+
) -> Dict[str, Any]:
|
|
802
|
+
"""Handle resources/read request."""
|
|
803
|
+
uri = params.get("uri", "")
|
|
804
|
+
|
|
805
|
+
if uri == "alma://config":
|
|
806
|
+
resource = get_config_resource(self.alma)
|
|
807
|
+
elif uri == "alma://agents":
|
|
808
|
+
resource = get_agents_resource(self.alma)
|
|
809
|
+
else:
|
|
810
|
+
return self._error_response(
|
|
811
|
+
request_id,
|
|
812
|
+
-32602,
|
|
813
|
+
f"Unknown resource: {uri}",
|
|
814
|
+
)
|
|
815
|
+
|
|
816
|
+
return self._success_response(
|
|
817
|
+
request_id,
|
|
818
|
+
{
|
|
819
|
+
"contents": [
|
|
820
|
+
{
|
|
821
|
+
"uri": resource["uri"],
|
|
822
|
+
"mimeType": resource["mimeType"],
|
|
823
|
+
"text": json.dumps(resource["content"], indent=2),
|
|
824
|
+
}
|
|
825
|
+
],
|
|
826
|
+
},
|
|
827
|
+
)
|
|
828
|
+
|
|
829
|
+
def _success_response(
|
|
830
|
+
self,
|
|
831
|
+
request_id: Optional[int],
|
|
832
|
+
result: Any,
|
|
833
|
+
) -> Dict[str, Any]:
|
|
834
|
+
"""Create a success response."""
|
|
835
|
+
return {
|
|
836
|
+
"jsonrpc": "2.0",
|
|
837
|
+
"id": request_id,
|
|
838
|
+
"result": result,
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
def _error_response(
|
|
842
|
+
self,
|
|
843
|
+
request_id: Optional[int],
|
|
844
|
+
code: int,
|
|
845
|
+
message: str,
|
|
846
|
+
) -> Dict[str, Any]:
|
|
847
|
+
"""Create an error response."""
|
|
848
|
+
return {
|
|
849
|
+
"jsonrpc": "2.0",
|
|
850
|
+
"id": request_id,
|
|
851
|
+
"error": {
|
|
852
|
+
"code": code,
|
|
853
|
+
"message": message,
|
|
854
|
+
},
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
async def run_stdio(self):
|
|
858
|
+
"""Run the server in stdio mode for Claude Code integration."""
|
|
859
|
+
logger.info("Starting ALMA MCP Server (stdio mode)")
|
|
860
|
+
|
|
861
|
+
reader = asyncio.StreamReader()
|
|
862
|
+
protocol = asyncio.StreamReaderProtocol(reader)
|
|
863
|
+
await asyncio.get_event_loop().connect_read_pipe(lambda: protocol, sys.stdin)
|
|
864
|
+
|
|
865
|
+
(
|
|
866
|
+
writer_transport,
|
|
867
|
+
writer_protocol,
|
|
868
|
+
) = await asyncio.get_event_loop().connect_write_pipe(
|
|
869
|
+
asyncio.streams.FlowControlMixin, sys.stdout
|
|
870
|
+
)
|
|
871
|
+
writer = asyncio.StreamWriter(
|
|
872
|
+
writer_transport, writer_protocol, None, asyncio.get_event_loop()
|
|
873
|
+
)
|
|
874
|
+
|
|
875
|
+
while True:
|
|
876
|
+
try:
|
|
877
|
+
# Read Content-Length header
|
|
878
|
+
header_line = await reader.readline()
|
|
879
|
+
if not header_line:
|
|
880
|
+
break
|
|
881
|
+
|
|
882
|
+
header = header_line.decode().strip()
|
|
883
|
+
if not header.startswith("Content-Length:"):
|
|
884
|
+
continue
|
|
885
|
+
|
|
886
|
+
content_length = int(header.split(":")[1].strip())
|
|
887
|
+
|
|
888
|
+
# Read empty line
|
|
889
|
+
await reader.readline()
|
|
890
|
+
|
|
891
|
+
# Read content
|
|
892
|
+
content = await reader.read(content_length)
|
|
893
|
+
request = json.loads(content.decode())
|
|
894
|
+
|
|
895
|
+
# Handle request
|
|
896
|
+
response = await self.handle_request(request)
|
|
897
|
+
|
|
898
|
+
# Send response
|
|
899
|
+
response_str = json.dumps(response)
|
|
900
|
+
response_bytes = response_str.encode()
|
|
901
|
+
header_bytes = f"Content-Length: {len(response_bytes)}\r\n\r\n".encode()
|
|
902
|
+
|
|
903
|
+
writer.write(header_bytes + response_bytes)
|
|
904
|
+
await writer.drain()
|
|
905
|
+
|
|
906
|
+
except asyncio.CancelledError:
|
|
907
|
+
break
|
|
908
|
+
except Exception as e:
|
|
909
|
+
logger.exception(f"Error in stdio loop: {e}")
|
|
910
|
+
|
|
911
|
+
async def run_http(self, host: str = "0.0.0.0", port: int = 8765):
|
|
912
|
+
"""
|
|
913
|
+
Run the server in HTTP mode for remote access.
|
|
914
|
+
|
|
915
|
+
Note: Requires aiohttp (optional dependency).
|
|
916
|
+
"""
|
|
917
|
+
try:
|
|
918
|
+
from aiohttp import web
|
|
919
|
+
except ImportError:
|
|
920
|
+
logger.error(
|
|
921
|
+
"aiohttp required for HTTP mode. Install with: pip install aiohttp"
|
|
922
|
+
)
|
|
923
|
+
return
|
|
924
|
+
|
|
925
|
+
async def handle_post(request: web.Request) -> web.Response:
|
|
926
|
+
"""Handle HTTP POST requests."""
|
|
927
|
+
try:
|
|
928
|
+
data = await request.json()
|
|
929
|
+
response = await self.handle_request(data)
|
|
930
|
+
return web.json_response(response)
|
|
931
|
+
except Exception as e:
|
|
932
|
+
return web.json_response(
|
|
933
|
+
{"error": str(e)},
|
|
934
|
+
status=500,
|
|
935
|
+
)
|
|
936
|
+
|
|
937
|
+
async def handle_health(request: web.Request) -> web.Response:
|
|
938
|
+
"""Handle health check endpoint."""
|
|
939
|
+
result = alma_health(self.alma)
|
|
940
|
+
return web.json_response(result)
|
|
941
|
+
|
|
942
|
+
app = web.Application()
|
|
943
|
+
app.router.add_post("/", handle_post)
|
|
944
|
+
app.router.add_get("/health", handle_health)
|
|
945
|
+
|
|
946
|
+
runner = web.AppRunner(app)
|
|
947
|
+
await runner.setup()
|
|
948
|
+
site = web.TCPSite(runner, host, port)
|
|
949
|
+
await site.start()
|
|
950
|
+
|
|
951
|
+
logger.info(f"ALMA MCP Server running on http://{host}:{port}")
|
|
952
|
+
|
|
953
|
+
# Keep running
|
|
954
|
+
while True:
|
|
955
|
+
await asyncio.sleep(3600)
|