1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
257
258
259
260
261
262
263
264
265
266
267
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
|
package memory
import (
"encoding/json"
"fmt"
"os"
"strings"
"sync"
"github.com/xlgmokha/mcp/pkg/mcp"
)
// KnowledgeGraph represents the in-memory knowledge graph
type KnowledgeGraph struct {
Entities map[string]*Entity `json:"entities"`
Relations map[string]Relation `json:"relations"`
}
// Entity represents an entity in the knowledge graph
type Entity struct {
Name string `json:"name"`
EntityType string `json:"entityType"`
Observations []string `json:"observations"`
}
// Relation represents a relationship between entities
type Relation struct {
From string `json:"from"`
To string `json:"to"`
RelationType string `json:"relationType"`
}
// MemoryOperations provides memory graph operations
type MemoryOperations struct {
memoryFile string
graph *KnowledgeGraph
mu sync.RWMutex
loaded bool
}
// NewMemoryOperations creates a new MemoryOperations helper
func NewMemoryOperations(memoryFile string) *MemoryOperations {
return &MemoryOperations{
memoryFile: memoryFile,
graph: &KnowledgeGraph{
Entities: make(map[string]*Entity),
Relations: make(map[string]Relation),
},
}
}
// New creates a new Memory MCP server
func New(memoryFile string) *mcp.Server {
memory := NewMemoryOperations(memoryFile)
builder := mcp.NewServerBuilder("mcp-memory", "1.0.0")
// Add create_entities tool
builder.AddTool(mcp.NewTool("create_entities", "Create multiple new entities in the knowledge graph", map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"entities": map[string]interface{}{
"type": "array",
"items": map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"name": map[string]interface{}{
"type": "string",
"description": "The name of the entity",
},
"entityType": map[string]interface{}{
"type": "string",
"description": "The type of the entity",
},
"observations": map[string]interface{}{
"type": "array",
"items": map[string]interface{}{
"type": "string",
},
"description": "An array of observation contents associated with the entity",
},
},
"required": []string{"name", "entityType", "observations"},
},
},
},
"required": []string{"entities"},
}, func(req mcp.CallToolRequest) (mcp.CallToolResult, error) {
if err := memory.ensureGraphLoaded(); err != nil {
return mcp.NewToolError(fmt.Sprintf("Failed to load graph: %v", err)), nil
}
memory.mu.Lock()
defer memory.mu.Unlock()
entitiesArg, ok := req.Arguments["entities"]
if !ok {
return mcp.NewToolError("entities parameter is required"), nil
}
entitiesSlice, ok := entitiesArg.([]interface{})
if !ok {
return mcp.NewToolError("entities must be an array"), nil
}
var createdEntities []string
for _, entityArg := range entitiesSlice {
entityMap, ok := entityArg.(map[string]interface{})
if !ok {
return mcp.NewToolError("each entity must be an object"), nil
}
name, ok := entityMap["name"].(string)
if !ok {
return mcp.NewToolError("entity name must be a string"), nil
}
entityType, ok := entityMap["entityType"].(string)
if !ok {
return mcp.NewToolError("entity type must be a string"), nil
}
observationsArg, ok := entityMap["observations"]
if !ok {
return mcp.NewToolError("entity observations are required"), nil
}
observationsSlice, ok := observationsArg.([]interface{})
if !ok {
return mcp.NewToolError("entity observations must be an array"), nil
}
var observations []string
for _, obs := range observationsSlice {
obsStr, ok := obs.(string)
if !ok {
return mcp.NewToolError("each observation must be a string"), nil
}
observations = append(observations, obsStr)
}
memory.graph.Entities[name] = &Entity{
Name: name,
EntityType: entityType,
Observations: observations,
}
createdEntities = append(createdEntities, name)
}
if err := memory.saveGraph(); err != nil {
return mcp.NewToolError(fmt.Sprintf("Failed to save graph: %v", err)), nil
}
return mcp.NewToolResult(mcp.NewTextContent(fmt.Sprintf("Created %d entities: %s", len(createdEntities), strings.Join(createdEntities, ", ")))), nil
}))
// Add create_relations tool
builder.AddTool(mcp.NewTool("create_relations", "Create multiple new relations between entities in the knowledge graph. Relations should be in active voice", map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"relations": map[string]interface{}{
"type": "array",
"items": map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"from": map[string]interface{}{
"type": "string",
"description": "The name of the entity where the relation starts",
},
"to": map[string]interface{}{
"type": "string",
"description": "The name of the entity where the relation ends",
},
"relationType": map[string]interface{}{
"type": "string",
"description": "The type of the relation",
},
},
"required": []string{"from", "to", "relationType"},
},
},
},
"required": []string{"relations"},
}, func(req mcp.CallToolRequest) (mcp.CallToolResult, error) {
if err := memory.ensureGraphLoaded(); err != nil {
return mcp.NewToolError(fmt.Sprintf("Failed to load graph: %v", err)), nil
}
memory.mu.Lock()
defer memory.mu.Unlock()
relationsArg, ok := req.Arguments["relations"]
if !ok {
return mcp.NewToolError("relations parameter is required"), nil
}
relationsSlice, ok := relationsArg.([]interface{})
if !ok {
return mcp.NewToolError("relations must be an array"), nil
}
var createdRelations []string
for _, relationArg := range relationsSlice {
relationMap, ok := relationArg.(map[string]interface{})
if !ok {
return mcp.NewToolError("each relation must be an object"), nil
}
from, ok := relationMap["from"].(string)
if !ok {
return mcp.NewToolError("relation 'from' must be a string"), nil
}
to, ok := relationMap["to"].(string)
if !ok {
return mcp.NewToolError("relation 'to' must be a string"), nil
}
relationType, ok := relationMap["relationType"].(string)
if !ok {
return mcp.NewToolError("relation type must be a string"), nil
}
if _, exists := memory.graph.Entities[from]; !exists {
return mcp.NewToolError(fmt.Sprintf("entity '%s' does not exist", from)), nil
}
if _, exists := memory.graph.Entities[to]; !exists {
return mcp.NewToolError(fmt.Sprintf("entity '%s' does not exist", to)), nil
}
relationKey := fmt.Sprintf("%s-%s-%s", from, relationType, to)
memory.graph.Relations[relationKey] = Relation{
From: from,
To: to,
RelationType: relationType,
}
createdRelations = append(createdRelations, fmt.Sprintf("%s %s %s", from, relationType, to))
}
if err := memory.saveGraph(); err != nil {
return mcp.NewToolError(fmt.Sprintf("Failed to save graph: %v", err)), nil
}
return mcp.NewToolResult(mcp.NewTextContent(fmt.Sprintf("Created %d relations: %s", len(createdRelations), strings.Join(createdRelations, ", ")))), nil
}))
// Add add_observations tool
builder.AddTool(mcp.NewTool("add_observations", "Add new observations to existing entities in the knowledge graph", map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"observations": map[string]interface{}{
"type": "array",
"items": map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"entityName": map[string]interface{}{
"type": "string",
"description": "The name of the entity to add the observations to",
},
"contents": map[string]interface{}{
"type": "array",
"items": map[string]interface{}{
"type": "string",
},
"description": "An array of observation contents to add",
},
},
"required": []string{"entityName", "contents"},
},
},
},
"required": []string{"observations"},
}, func(req mcp.CallToolRequest) (mcp.CallToolResult, error) {
if err := memory.ensureGraphLoaded(); err != nil {
return mcp.NewToolError(fmt.Sprintf("Failed to load graph: %v", err)), nil
}
memory.mu.Lock()
defer memory.mu.Unlock()
observationsArg, ok := req.Arguments["observations"]
if !ok {
return mcp.NewToolError("observations parameter is required"), nil
}
observationsSlice, ok := observationsArg.([]interface{})
if !ok {
return mcp.NewToolError("observations must be an array"), nil
}
var addedCount int
for _, obsArg := range observationsSlice {
obsMap, ok := obsArg.(map[string]interface{})
if !ok {
return mcp.NewToolError("each observation must be an object"), nil
}
entityName, ok := obsMap["entityName"].(string)
if !ok {
return mcp.NewToolError("entity name must be a string"), nil
}
contentsArg, ok := obsMap["contents"]
if !ok {
return mcp.NewToolError("observation contents are required"), nil
}
contentsSlice, ok := contentsArg.([]interface{})
if !ok {
return mcp.NewToolError("observation contents must be an array"), nil
}
entity, exists := memory.graph.Entities[entityName]
if !exists {
return mcp.NewToolError(fmt.Sprintf("entity '%s' does not exist", entityName)), nil
}
for _, content := range contentsSlice {
contentStr, ok := content.(string)
if !ok {
return mcp.NewToolError("each observation content must be a string"), nil
}
entity.Observations = append(entity.Observations, contentStr)
addedCount++
}
}
if err := memory.saveGraph(); err != nil {
return mcp.NewToolError(fmt.Sprintf("Failed to save graph: %v", err)), nil
}
return mcp.NewToolResult(mcp.NewTextContent(fmt.Sprintf("Added %d observations", addedCount))), nil
}))
// Add delete_entities tool
builder.AddTool(mcp.NewTool("delete_entities", "Delete multiple entities and their associated relations from the knowledge graph", map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"entityNames": map[string]interface{}{
"type": "array",
"items": map[string]interface{}{
"type": "string",
},
"description": "An array of entity names to delete",
},
},
"required": []string{"entityNames"},
}, func(req mcp.CallToolRequest) (mcp.CallToolResult, error) {
if err := memory.ensureGraphLoaded(); err != nil {
return mcp.NewToolError(fmt.Sprintf("Failed to load graph: %v", err)), nil
}
memory.mu.Lock()
defer memory.mu.Unlock()
entityNamesArg, ok := req.Arguments["entityNames"]
if !ok {
return mcp.NewToolError("entityNames parameter is required"), nil
}
entityNamesSlice, ok := entityNamesArg.([]interface{})
if !ok {
return mcp.NewToolError("entityNames must be an array"), nil
}
var deletedEntities []string
var deletedRelations int
for _, nameArg := range entityNamesSlice {
name, ok := nameArg.(string)
if !ok {
return mcp.NewToolError("each entity name must be a string"), nil
}
if _, exists := memory.graph.Entities[name]; !exists {
continue
}
delete(memory.graph.Entities, name)
deletedEntities = append(deletedEntities, name)
for key, relation := range memory.graph.Relations {
if relation.From == name || relation.To == name {
delete(memory.graph.Relations, key)
deletedRelations++
}
}
}
if err := memory.saveGraph(); err != nil {
return mcp.NewToolError(fmt.Sprintf("Failed to save graph: %v", err)), nil
}
return mcp.NewToolResult(mcp.NewTextContent(fmt.Sprintf("Deleted %d entities and %d relations", len(deletedEntities), deletedRelations))), nil
}))
// Add delete_observations tool
builder.AddTool(mcp.NewTool("delete_observations", "Delete specific observations from entities in the knowledge graph", map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"deletions": map[string]interface{}{
"type": "array",
"items": map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"entityName": map[string]interface{}{
"type": "string",
"description": "The name of the entity containing the observations",
},
"observations": map[string]interface{}{
"type": "array",
"items": map[string]interface{}{
"type": "string",
},
"description": "An array of observations to delete",
},
},
"required": []string{"entityName", "observations"},
},
},
},
"required": []string{"deletions"},
}, func(req mcp.CallToolRequest) (mcp.CallToolResult, error) {
if err := memory.ensureGraphLoaded(); err != nil {
return mcp.NewToolError(fmt.Sprintf("Failed to load graph: %v", err)), nil
}
memory.mu.Lock()
defer memory.mu.Unlock()
deletionsArg, ok := req.Arguments["deletions"]
if !ok {
return mcp.NewToolError("deletions parameter is required"), nil
}
deletionsSlice, ok := deletionsArg.([]interface{})
if !ok {
return mcp.NewToolError("deletions must be an array"), nil
}
var deletedCount int
for _, delArg := range deletionsSlice {
delMap, ok := delArg.(map[string]interface{})
if !ok {
return mcp.NewToolError("each deletion must be an object"), nil
}
entityName, ok := delMap["entityName"].(string)
if !ok {
return mcp.NewToolError("entity name must be a string"), nil
}
observationsArg, ok := delMap["observations"]
if !ok {
return mcp.NewToolError("observations are required"), nil
}
observationsSlice, ok := observationsArg.([]interface{})
if !ok {
return mcp.NewToolError("observations must be an array"), nil
}
entity, exists := memory.graph.Entities[entityName]
if !exists {
continue
}
for _, obsArg := range observationsSlice {
obsStr, ok := obsArg.(string)
if !ok {
continue
}
for i, existingObs := range entity.Observations {
if existingObs == obsStr {
entity.Observations = append(entity.Observations[:i], entity.Observations[i+1:]...)
deletedCount++
break
}
}
}
}
if err := memory.saveGraph(); err != nil {
return mcp.NewToolError(fmt.Sprintf("Failed to save graph: %v", err)), nil
}
return mcp.NewToolResult(mcp.NewTextContent(fmt.Sprintf("Deleted %d observations", deletedCount))), nil
}))
// Add delete_relations tool
builder.AddTool(mcp.NewTool("delete_relations", "Delete multiple relations from the knowledge graph", map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"relations": map[string]interface{}{
"type": "array",
"items": map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"from": map[string]interface{}{
"type": "string",
"description": "The name of the entity where the relation starts",
},
"to": map[string]interface{}{
"type": "string",
"description": "The name of the entity where the relation ends",
},
"relationType": map[string]interface{}{
"type": "string",
"description": "The type of the relation",
},
},
"required": []string{"from", "to", "relationType"},
},
},
},
"required": []string{"relations"},
}, func(req mcp.CallToolRequest) (mcp.CallToolResult, error) {
if err := memory.ensureGraphLoaded(); err != nil {
return mcp.NewToolError(fmt.Sprintf("Failed to load graph: %v", err)), nil
}
memory.mu.Lock()
defer memory.mu.Unlock()
relationsArg, ok := req.Arguments["relations"]
if !ok {
return mcp.NewToolError("relations parameter is required"), nil
}
relationsSlice, ok := relationsArg.([]interface{})
if !ok {
return mcp.NewToolError("relations must be an array"), nil
}
var deletedCount int
for _, relationArg := range relationsSlice {
relationMap, ok := relationArg.(map[string]interface{})
if !ok {
continue
}
from, ok := relationMap["from"].(string)
if !ok {
continue
}
to, ok := relationMap["to"].(string)
if !ok {
continue
}
relationType, ok := relationMap["relationType"].(string)
if !ok {
continue
}
relationKey := fmt.Sprintf("%s-%s-%s", from, relationType, to)
if _, exists := memory.graph.Relations[relationKey]; exists {
delete(memory.graph.Relations, relationKey)
deletedCount++
}
}
if err := memory.saveGraph(); err != nil {
return mcp.NewToolError(fmt.Sprintf("Failed to save graph: %v", err)), nil
}
return mcp.NewToolResult(mcp.NewTextContent(fmt.Sprintf("Deleted %d relations", deletedCount))), nil
}))
// Add read_graph tool
builder.AddTool(mcp.NewTool("read_graph", "Read the entire knowledge graph", map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{},
}, func(req mcp.CallToolRequest) (mcp.CallToolResult, error) {
if err := memory.ensureGraphLoaded(); err != nil {
return mcp.NewToolError(fmt.Sprintf("Failed to load graph: %v", err)), nil
}
memory.mu.RLock()
defer memory.mu.RUnlock()
graphData, err := json.MarshalIndent(memory.graph, "", " ")
if err != nil {
return mcp.NewToolError(fmt.Sprintf("Failed to serialize graph: %v", err)), nil
}
return mcp.NewToolResult(mcp.NewTextContent(string(graphData))), nil
}))
// Add search_nodes tool
builder.AddTool(mcp.NewTool("search_nodes", "Search for nodes in the knowledge graph based on a query", map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"query": map[string]interface{}{
"type": "string",
"description": "The search query to match against entity names, types, and observation content",
},
},
"required": []string{"query"},
}, func(req mcp.CallToolRequest) (mcp.CallToolResult, error) {
if err := memory.ensureGraphLoaded(); err != nil {
return mcp.NewToolError(fmt.Sprintf("Failed to load graph: %v", err)), nil
}
memory.mu.RLock()
defer memory.mu.RUnlock()
query, ok := req.Arguments["query"].(string)
if !ok {
return mcp.NewToolError("query parameter is required"), nil
}
query = strings.ToLower(query)
var matchedEntities []*Entity
for _, entity := range memory.graph.Entities {
if strings.Contains(strings.ToLower(entity.Name), query) ||
strings.Contains(strings.ToLower(entity.EntityType), query) {
matchedEntities = append(matchedEntities, entity)
continue
}
for _, observation := range entity.Observations {
if strings.Contains(strings.ToLower(observation), query) {
matchedEntities = append(matchedEntities, entity)
break
}
}
}
resultData, err := json.MarshalIndent(matchedEntities, "", " ")
if err != nil {
return mcp.NewToolError(fmt.Sprintf("Failed to serialize results: %v", err)), nil
}
return mcp.NewToolResult(mcp.NewTextContent(fmt.Sprintf("Found %d matching entities:\n%s", len(matchedEntities), string(resultData)))), nil
}))
// Add open_nodes tool
builder.AddTool(mcp.NewTool("open_nodes", "Open specific nodes in the knowledge graph by their names", map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"names": map[string]interface{}{
"type": "array",
"items": map[string]interface{}{
"type": "string",
},
"description": "An array of entity names to retrieve",
},
},
"required": []string{"names"},
}, func(req mcp.CallToolRequest) (mcp.CallToolResult, error) {
if err := memory.ensureGraphLoaded(); err != nil {
return mcp.NewToolError(fmt.Sprintf("Failed to load graph: %v", err)), nil
}
memory.mu.RLock()
defer memory.mu.RUnlock()
namesArg, ok := req.Arguments["names"]
if !ok {
return mcp.NewToolError("names parameter is required"), nil
}
namesSlice, ok := namesArg.([]interface{})
if !ok {
return mcp.NewToolError("names must be an array"), nil
}
var foundEntities []*Entity
for _, nameArg := range namesSlice {
name, ok := nameArg.(string)
if !ok {
continue
}
if entity, exists := memory.graph.Entities[name]; exists {
foundEntities = append(foundEntities, entity)
}
}
resultData, err := json.MarshalIndent(foundEntities, "", " ")
if err != nil {
return mcp.NewToolError(fmt.Sprintf("Failed to serialize results: %v", err)), nil
}
return mcp.NewToolResult(mcp.NewTextContent(fmt.Sprintf("Found %d entities:\n%s", len(foundEntities), string(resultData)))), nil
}))
// Add knowledge-query prompt
builder.AddPrompt(mcp.NewPrompt("knowledge-query", "Prompt for querying and exploring the knowledge graph", []mcp.PromptArgument{
{
Name: "query",
Description: "What you want to search for or ask about in the knowledge graph",
Required: true,
},
{
Name: "context",
Description: "Additional context about your question (optional)",
Required: false,
},
}, func(req mcp.GetPromptRequest) (mcp.GetPromptResult, error) {
query, hasQuery := req.Arguments["query"].(string)
context, hasContext := req.Arguments["context"].(string)
if !hasQuery || query == "" {
return mcp.GetPromptResult{}, fmt.Errorf("query argument is required")
}
var messages []mcp.PromptMessage
userContent := fmt.Sprintf(`I want to search the knowledge graph for: %s`, query)
if hasContext && context != "" {
userContent += fmt.Sprintf("\n\nAdditional context: %s", context)
}
messages = append(messages, mcp.PromptMessage{
Role: "user",
Content: mcp.NewTextContent(userContent),
})
assistantContent := fmt.Sprintf(`I'll help you search the knowledge graph for "%s". Here are some strategies you can use:
**Search Commands:**
- Use "search_nodes" tool with query: "%s"
- Use "read_graph" tool to see the entire knowledge structure
- Use "open_nodes" tool if you know specific entity names
**What to look for:**
- Entities with names containing "%s"
- Entity types that match your query
- Observations that mention "%s"
- Related entities through relationships
**Next steps:**
1. Start with a broad search using search_nodes
2. Examine the results to find relevant entities
3. Use open_nodes to get detailed information about specific entities
4. Look at relationships to find connected information
Would you like me to search for this information in the knowledge graph?`, query, query, query, query)
messages = append(messages, mcp.PromptMessage{
Role: "assistant",
Content: mcp.NewTextContent(assistantContent),
})
description := fmt.Sprintf("Knowledge graph search guidance for: %s", query)
return mcp.GetPromptResult{
Description: description,
Messages: messages,
}, nil
}))
// Add memory:// pattern resource
builder.AddResource(mcp.NewResource(
"memory://graph",
"Knowledge Graph",
"application/json",
func(req mcp.ReadResourceRequest) (mcp.ReadResourceResult, error) {
if err := memory.ensureGraphLoaded(); err != nil {
return mcp.ReadResourceResult{}, fmt.Errorf("failed to load graph: %v", err)
}
memory.mu.RLock()
defer memory.mu.RUnlock()
graphData, err := json.MarshalIndent(memory.graph, "", " ")
if err != nil {
return mcp.ReadResourceResult{}, fmt.Errorf("failed to serialize graph: %v", err)
}
return mcp.ReadResourceResult{
Contents: []mcp.Content{
mcp.NewTextContent(string(graphData)),
},
}, nil
},
))
// Add knowledge graph root
rootName := fmt.Sprintf("Knowledge Graph (%d entities, %d relations)", len(memory.graph.Entities), len(memory.graph.Relations))
builder.AddRoot(mcp.NewRoot("memory://graph", rootName))
return builder.Build()
}
// Helper methods for MemoryOperations
func (memory *MemoryOperations) ensureGraphLoaded() error {
memory.mu.Lock()
defer memory.mu.Unlock()
if !memory.loaded {
memory.loaded = true
return memory.loadGraphInternal()
}
return nil
}
func (memory *MemoryOperations) loadGraphInternal() error {
if memory.memoryFile == "" {
return nil
}
data, err := os.ReadFile(memory.memoryFile)
if os.IsNotExist(err) {
return nil
}
if err != nil {
return fmt.Errorf("failed to read memory file: %v", err)
}
if len(data) == 0 {
return nil
}
var loadedGraph KnowledgeGraph
if err := json.Unmarshal(data, &loadedGraph); err != nil {
return fmt.Errorf("failed to parse memory file: %v", err)
}
if loadedGraph.Entities != nil {
memory.graph.Entities = loadedGraph.Entities
}
if loadedGraph.Relations != nil {
memory.graph.Relations = loadedGraph.Relations
}
return nil
}
func (memory *MemoryOperations) saveGraph() error {
if memory.memoryFile == "" {
return nil
}
data, err := json.MarshalIndent(memory.graph, "", " ")
if err != nil {
return fmt.Errorf("failed to serialize graph: %v", err)
}
if err := os.WriteFile(memory.memoryFile, data, 0644); err != nil {
return fmt.Errorf("failed to write memory file: %v", err)
}
return nil
}
|