|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 13 | +) |
| 14 | + |
| 15 | +// Memory entry structure |
| 16 | +type MemoryEntry struct { |
| 17 | + ID string `json:"id"` |
| 18 | + Content string `json:"content"` |
| 19 | + CreatedAt time.Time `json:"created_at"` |
| 20 | +} |
| 21 | + |
| 22 | +// Memory storage structure |
| 23 | +type MemoryStorage struct { |
| 24 | + Entries []MemoryEntry `json:"entries"` |
| 25 | +} |
| 26 | + |
| 27 | +// Input types for different operations |
| 28 | +type AddMemoryInput struct { |
| 29 | + Content string `json:"content" jsonschema:"the content to store in memory"` |
| 30 | +} |
| 31 | + |
| 32 | +type RemoveMemoryInput struct { |
| 33 | + ID string `json:"id" jsonschema:"the ID of the memory entry to remove"` |
| 34 | +} |
| 35 | + |
| 36 | +// Output types |
| 37 | +type AddMemoryOutput struct { |
| 38 | + ID string `json:"id" jsonschema:"the ID of the created memory entry"` |
| 39 | + Content string `json:"content" jsonschema:"the stored content"` |
| 40 | + CreatedAt time.Time `json:"created_at" jsonschema:"when the entry was created"` |
| 41 | +} |
| 42 | + |
| 43 | +type ListMemoryOutput struct { |
| 44 | + Entries []MemoryEntry `json:"entries" jsonschema:"list of all memory entries"` |
| 45 | +} |
| 46 | + |
| 47 | +type RemoveMemoryOutput struct { |
| 48 | + Success bool `json:"success" jsonschema:"whether the removal was successful"` |
| 49 | + Message string `json:"message" jsonschema:"status message"` |
| 50 | +} |
| 51 | + |
| 52 | +// Global variable to store the memory file path |
| 53 | +var memoryFilePath string |
| 54 | + |
| 55 | +// Load memory entries from JSON file |
| 56 | +func loadMemory() (*MemoryStorage, error) { |
| 57 | + if _, err := os.Stat(memoryFilePath); os.IsNotExist(err) { |
| 58 | + // File doesn't exist, return empty storage |
| 59 | + return &MemoryStorage{Entries: []MemoryEntry{}}, nil |
| 60 | + } |
| 61 | + |
| 62 | + data, err := os.ReadFile(memoryFilePath) |
| 63 | + if err != nil { |
| 64 | + return nil, fmt.Errorf("failed to read memory file: %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + var storage MemoryStorage |
| 68 | + if err := json.Unmarshal(data, &storage); err != nil { |
| 69 | + return nil, fmt.Errorf("failed to parse memory file: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + return &storage, nil |
| 73 | +} |
| 74 | + |
| 75 | +// Save memory entries to JSON file |
| 76 | +func saveMemory(storage *MemoryStorage) error { |
| 77 | + data, err := json.MarshalIndent(storage, "", " ") |
| 78 | + if err != nil { |
| 79 | + return fmt.Errorf("failed to marshal memory data: %w", err) |
| 80 | + } |
| 81 | + |
| 82 | + if err := os.WriteFile(memoryFilePath, data, 0644); err != nil { |
| 83 | + return fmt.Errorf("failed to write memory file: %w", err) |
| 84 | + } |
| 85 | + |
| 86 | + return nil |
| 87 | +} |
| 88 | + |
| 89 | +// Generate a unique ID for memory entries |
| 90 | +func generateID() string { |
| 91 | + return fmt.Sprintf("%d", time.Now().UnixNano()) |
| 92 | +} |
| 93 | + |
| 94 | +// Add memory entry |
| 95 | +func AddMemory(ctx context.Context, req *mcp.CallToolRequest, input AddMemoryInput) ( |
| 96 | + *mcp.CallToolResult, |
| 97 | + AddMemoryOutput, |
| 98 | + error, |
| 99 | +) { |
| 100 | + storage, err := loadMemory() |
| 101 | + if err != nil { |
| 102 | + return nil, AddMemoryOutput{}, err |
| 103 | + } |
| 104 | + |
| 105 | + entry := MemoryEntry{ |
| 106 | + ID: generateID(), |
| 107 | + Content: input.Content, |
| 108 | + CreatedAt: time.Now(), |
| 109 | + } |
| 110 | + |
| 111 | + storage.Entries = append(storage.Entries, entry) |
| 112 | + |
| 113 | + if err := saveMemory(storage); err != nil { |
| 114 | + return nil, AddMemoryOutput{}, err |
| 115 | + } |
| 116 | + |
| 117 | + output := AddMemoryOutput{ |
| 118 | + ID: entry.ID, |
| 119 | + Content: entry.Content, |
| 120 | + CreatedAt: entry.CreatedAt, |
| 121 | + } |
| 122 | + |
| 123 | + return nil, output, nil |
| 124 | +} |
| 125 | + |
| 126 | +// List all memory entries |
| 127 | +func ListMemory(ctx context.Context, req *mcp.CallToolRequest, input struct{}) ( |
| 128 | + *mcp.CallToolResult, |
| 129 | + ListMemoryOutput, |
| 130 | + error, |
| 131 | +) { |
| 132 | + storage, err := loadMemory() |
| 133 | + if err != nil { |
| 134 | + return nil, ListMemoryOutput{}, err |
| 135 | + } |
| 136 | + |
| 137 | + output := ListMemoryOutput{ |
| 138 | + Entries: storage.Entries, |
| 139 | + } |
| 140 | + |
| 141 | + return nil, output, nil |
| 142 | +} |
| 143 | + |
| 144 | +// Remove memory entry by ID |
| 145 | +func RemoveMemory(ctx context.Context, req *mcp.CallToolRequest, input RemoveMemoryInput) ( |
| 146 | + *mcp.CallToolResult, |
| 147 | + RemoveMemoryOutput, |
| 148 | + error, |
| 149 | +) { |
| 150 | + storage, err := loadMemory() |
| 151 | + if err != nil { |
| 152 | + return nil, RemoveMemoryOutput{}, err |
| 153 | + } |
| 154 | + |
| 155 | + // Find and remove the entry |
| 156 | + found := false |
| 157 | + for i, entry := range storage.Entries { |
| 158 | + if entry.ID == input.ID { |
| 159 | + storage.Entries = append(storage.Entries[:i], storage.Entries[i+1:]...) |
| 160 | + found = true |
| 161 | + break |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + if !found { |
| 166 | + output := RemoveMemoryOutput{ |
| 167 | + Success: false, |
| 168 | + Message: fmt.Sprintf("Memory entry with ID '%s' not found", input.ID), |
| 169 | + } |
| 170 | + return nil, output, nil |
| 171 | + } |
| 172 | + |
| 173 | + if err := saveMemory(storage); err != nil { |
| 174 | + return nil, RemoveMemoryOutput{}, err |
| 175 | + } |
| 176 | + |
| 177 | + output := RemoveMemoryOutput{ |
| 178 | + Success: true, |
| 179 | + Message: fmt.Sprintf("Memory entry with ID '%s' removed successfully", input.ID), |
| 180 | + } |
| 181 | + |
| 182 | + return nil, output, nil |
| 183 | +} |
| 184 | + |
| 185 | +func main() { |
| 186 | + // Get memory file path from environment variable, default to ./memory.json |
| 187 | + memoryFilePath = os.Getenv("MEMORY_FILE_PATH") |
| 188 | + if memoryFilePath == "" { |
| 189 | + memoryFilePath = "/data/memory.json" |
| 190 | + } |
| 191 | + |
| 192 | + os.MkdirAll(filepath.Dir(memoryFilePath), 0755) |
| 193 | + |
| 194 | + // Create a server with memory tools |
| 195 | + server := mcp.NewServer(&mcp.Implementation{Name: "memory", Version: "v1.0.0"}, nil) |
| 196 | + |
| 197 | + // Register memory tools |
| 198 | + mcp.AddTool(server, &mcp.Tool{ |
| 199 | + Name: "add_memory", |
| 200 | + Description: "Add a new entry to memory storage", |
| 201 | + }, AddMemory) |
| 202 | + |
| 203 | + mcp.AddTool(server, &mcp.Tool{ |
| 204 | + Name: "list_memory", |
| 205 | + Description: "List all memory entries", |
| 206 | + }, ListMemory) |
| 207 | + |
| 208 | + mcp.AddTool(server, &mcp.Tool{ |
| 209 | + Name: "remove_memory", |
| 210 | + Description: "Remove a memory entry by ID", |
| 211 | + }, RemoveMemory) |
| 212 | + |
| 213 | + if err := server.Run(context.Background(), &mcp.StdioTransport{}); err != nil { |
| 214 | + log.Fatal(err) |
| 215 | + } |
| 216 | +} |
0 commit comments