-
Notifications
You must be signed in to change notification settings - Fork 273
mcp: memory server example #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e652a7e
mcp: memory server example
MegaGrindStone 9a36533
Merge branch 'modelcontextprotocol:main' into main
MegaGrindStone 1cae449
fix: use new API schema in memory example
MegaGrindStone 0c74f30
refactor: address PR feedback in memory example
MegaGrindStone 3d0fa94
refactor: remove readGraph proxy function in memory example
MegaGrindStone 4f69af6
refactor: fix lint in memory example
MegaGrindStone c0a5c7b
refactor: address PR feedback in memory example
MegaGrindStone bed7d19
refactor: simplify test variable name in memory example
MegaGrindStone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,13 +11,15 @@ import ( | |
| "os" | ||
| "path/filepath" | ||
| "reflect" | ||
| "slices" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/modelcontextprotocol/go-sdk/mcp" | ||
| ) | ||
|
|
||
| // getStoreFactories provides test factories for both storage implementations. | ||
| func getStoreFactories() map[string]func(t *testing.T) store { | ||
| // stores provides test factories for both storage implementations. | ||
| func stores() map[string]func(t *testing.T) store { | ||
| return map[string]func(t *testing.T) store{ | ||
| "file": func(t *testing.T) store { | ||
| tempDir, err := os.MkdirTemp("", "kb-test-file-*") | ||
|
|
@@ -35,11 +37,9 @@ func getStoreFactories() map[string]func(t *testing.T) store { | |
|
|
||
| // TestKnowledgeBaseOperations verifies CRUD operations work correctly. | ||
| func TestKnowledgeBaseOperations(t *testing.T) { | ||
| factories := getStoreFactories() | ||
|
|
||
| for name, factory := range factories { | ||
| for name, newStore := range stores() { | ||
| t.Run(name, func(t *testing.T) { | ||
| s := factory(t) | ||
| s := newStore(t) | ||
| kb := knowledgeBase{s: s} | ||
|
|
||
| // Verify empty graph loads correctly | ||
|
|
@@ -147,19 +147,16 @@ func TestKnowledgeBaseOperations(t *testing.T) { | |
|
|
||
| // Confirm observation removal | ||
| graph, _ = kb.loadGraph() | ||
| aliceFound := false | ||
| for _, e := range graph.Entities { | ||
| if e.Name == "Alice" { | ||
| aliceFound = true | ||
| for _, obs := range e.Observations { | ||
| if obs == "Works as developer" { | ||
| t.Errorf("observation 'Works as developer' should have been deleted") | ||
| } | ||
| } | ||
| } | ||
| } | ||
| if !aliceFound { | ||
| aliceIndex := slices.IndexFunc(graph.Entities, func(e Entity) bool { | ||
| return e.Name == "Alice" | ||
| }) | ||
| if aliceIndex == -1 { | ||
| t.Errorf("entity 'Alice' not found after deleting observation") | ||
| } else { | ||
| alice := graph.Entities[aliceIndex] | ||
| if slices.Contains(alice.Observations, "Works as developer") { | ||
| t.Errorf("observation 'Works as developer' should have been deleted") | ||
| } | ||
| } | ||
|
|
||
| // Remove relations | ||
|
|
@@ -191,11 +188,9 @@ func TestKnowledgeBaseOperations(t *testing.T) { | |
|
|
||
| // TestSaveAndLoadGraph ensures data persists correctly across save/load cycles. | ||
| func TestSaveAndLoadGraph(t *testing.T) { | ||
| factories := getStoreFactories() | ||
|
|
||
| for name, factory := range factories { | ||
| for name, newStore := range stores() { | ||
| t.Run(name, func(t *testing.T) { | ||
| s := factory(t) | ||
| s := newStore(t) | ||
| kb := knowledgeBase{s: s} | ||
|
|
||
| // Setup test data | ||
|
|
@@ -251,11 +246,9 @@ func TestSaveAndLoadGraph(t *testing.T) { | |
|
|
||
| // TestDuplicateEntitiesAndRelations verifies duplicate prevention logic. | ||
| func TestDuplicateEntitiesAndRelations(t *testing.T) { | ||
| factories := getStoreFactories() | ||
|
|
||
| for name, factory := range factories { | ||
| for name, newStore := range stores() { | ||
| t.Run(name, func(t *testing.T) { | ||
| s := factory(t) | ||
| s := newStore(t) | ||
| kb := knowledgeBase{s: s} | ||
|
|
||
| // Setup initial state | ||
|
|
@@ -355,10 +348,9 @@ func TestErrorHandling(t *testing.T) { | |
| } | ||
| }) | ||
|
|
||
| factories := getStoreFactories() | ||
| for name, factory := range factories { | ||
| for name, newStore := range stores() { | ||
| t.Run(fmt.Sprintf("AddObservationToNonExistentEntity_%s", name), func(t *testing.T) { | ||
| s := factory(t) | ||
| s := newStore(t) | ||
| kb := knowledgeBase{s: s} | ||
|
|
||
| // Setup valid entity for comparison | ||
|
|
@@ -385,11 +377,9 @@ func TestErrorHandling(t *testing.T) { | |
|
|
||
| // TestFileFormatting verifies the JSON storage format structure. | ||
| func TestFileFormatting(t *testing.T) { | ||
| factories := getStoreFactories() | ||
|
|
||
| for name, factory := range factories { | ||
| for name, newStore := range stores() { | ||
| t.Run(name, func(t *testing.T) { | ||
| s := factory(t) | ||
| s := newStore(t) | ||
| kb := knowledgeBase{s: s} | ||
|
|
||
| // Setup test entity | ||
|
|
@@ -438,11 +428,9 @@ func TestFileFormatting(t *testing.T) { | |
|
|
||
| // TestMCPServerIntegration tests the knowledge base through MCP server layer. | ||
| func TestMCPServerIntegration(t *testing.T) { | ||
| factories := getStoreFactories() | ||
|
|
||
| for name, factory := range factories { | ||
| for name, newStore := range stores() { | ||
| t.Run(name, func(t *testing.T) { | ||
| s := factory(t) | ||
| s := newStore(t) | ||
| kb := knowledgeBase{s: s} | ||
|
|
||
| // Create mock server session | ||
|
|
@@ -639,11 +627,9 @@ func TestMCPServerIntegration(t *testing.T) { | |
|
|
||
| // TestMCPErrorHandling tests error scenarios through MCP layer. | ||
| func TestMCPErrorHandling(t *testing.T) { | ||
| factories := getStoreFactories() | ||
|
|
||
| for name, factory := range factories { | ||
| for name, newStore := range stores() { | ||
| t.Run(name, func(t *testing.T) { | ||
| s := factory(t) | ||
| s := newStore(t) | ||
| kb := knowledgeBase{s: s} | ||
|
|
||
| ctx := context.Background() | ||
|
|
@@ -661,15 +647,15 @@ func TestMCPErrorHandling(t *testing.T) { | |
| }, | ||
| } | ||
|
|
||
| obsResult, err := kb.AddObservations(ctx, serverSession, addObsParams) | ||
| if err != nil { | ||
| t.Fatalf("MCP AddObservations call failed: %v", err) | ||
| } | ||
| if !obsResult.IsError { | ||
| _, err := kb.AddObservations(ctx, serverSession, addObsParams) | ||
| if err == nil { | ||
| t.Errorf("expected MCP AddObservations to return error for non-existent entity") | ||
| } | ||
| if len(obsResult.Content) == 0 { | ||
| t.Errorf("expected error content in MCP response") | ||
| } else { | ||
| // Verify the error message contains expected text | ||
| expectedErrorMsg := "entity with name NonExistentEntity not found" | ||
|
||
| if !strings.Contains(err.Error(), expectedErrorMsg) { | ||
| t.Errorf("expected error message to contain '%s', got: %v", expectedErrorMsg, err) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.