|
#!/usr/bin/env tsx |
|
|
|
// 🧠 Noob note: |
|
// ↑ First line is a shebang/hashbang, to make this file directly executable. |
|
|
|
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; |
|
import { z } from "zod"; |
|
|
|
const memory: string[] = []; |
|
|
|
// ------------------------------------ |
|
// 01. Create MCP Server |
|
// ------------------------------------ |
|
const server = new McpServer({ |
|
name: "session-memory-mcp", |
|
version: "1.0.0", |
|
}); |
|
|
|
// ------------------------------------ |
|
// 02. Register a Tool |
|
// ------------------------------------ |
|
// a. name, |
|
// b. intro (+schema), |
|
// c. capability |
|
// ------------------------------------ |
|
server.registerTool( |
|
"remember", |
|
{ |
|
title: "Remember", |
|
description: "Store an important note for this session", |
|
inputSchema: z.object({ |
|
note: z.string().describe("The note to remember"), |
|
}), |
|
}, |
|
async ({ note }) => { |
|
memory.push(note); |
|
|
|
const history = memory |
|
.map((item, index) => `${index + 1}. ${item}`) |
|
.join("\n"); |
|
|
|
return { |
|
content: [ |
|
{ |
|
type: "text", |
|
text: `I have saved this.\n\nEverything I remember:\n\n${history}`, |
|
}, |
|
], |
|
}; |
|
} |
|
); |
|
|
|
// ------------------------------------ |
|
// 03. Register Prompt: review_memories |
|
// ------------------------------------ |
|
server.registerPrompt( |
|
"review_memories", |
|
{ |
|
title: "Review Memories", |
|
description: "Analyze and summarize all stored memories", |
|
}, |
|
async () => { |
|
const history = |
|
memory.length > 0 |
|
? memory |
|
.map((item, index) => `${index + 1}. ${item}`) |
|
.join("\n") |
|
: "No memories have been stored yet."; |
|
|
|
return { |
|
description: "Review all saved memories", |
|
messages: [ |
|
{ |
|
role: "user", |
|
content: { |
|
type: "text", |
|
text: `Here are my saved memories: |
|
|
|
${history} |
|
|
|
Please: |
|
|
|
1. Summarize the important information. |
|
2. Identify recurring themes or patterns. |
|
3. Highlight anything that may require action. |
|
4. Provide a concise overview.`, |
|
}, |
|
}, |
|
], |
|
}; |
|
} |
|
); |
|
|
|
// ------------------------------------ |
|
// 04. Register Prompt with Arguments |
|
// ------------------------------------ |
|
server.registerPrompt( |
|
"find_memories", |
|
{ |
|
title: "Find Memories", |
|
description: "Review memories related to a specific topic", |
|
argsSchema: z.object({ |
|
topic: z |
|
.string() |
|
.describe("Topic or keyword to search for"), |
|
}), |
|
}, |
|
async ({ topic }) => { |
|
const matches = memory.filter((note) => |
|
note.toLowerCase().includes(topic.toLowerCase()) |
|
); |
|
|
|
const result = |
|
matches.length > 0 |
|
? matches.map((item, index) => `${index + 1}. ${item}`).join("\n") |
|
: `No memories found related to "${topic}".`; |
|
|
|
return { |
|
messages: [ |
|
{ |
|
role: "user", |
|
content: { |
|
type: "text", |
|
text: `These memories are related to "${topic}": |
|
|
|
${result} |
|
|
|
Please summarize the findings and explain their significance.`, |
|
}, |
|
}, |
|
], |
|
}; |
|
} |
|
); |
|
|
|
// ------------------------------------ |
|
// 05. Start the server over stdio |
|
// ------------------------------------ |
|
const transport = new StdioServerTransport(); |
|
|
|
await server.connect(transport); |
|
|
|
console.error("Session Memory MCP running..."); |