Skip to content

Instantly share code, notes, and snippets.

@mayeenulislam
Last active June 13, 2026 16:58
Show Gist options
  • Select an option

  • Save mayeenulislam/8955741d20b312a73ba7767076f3f3c1 to your computer and use it in GitHub Desktop.

Select an option

Save mayeenulislam/8955741d20b312a73ba7767076f3f3c1 to your computer and use it in GitHub Desktop.
A sample MCP server for storing session memory

Session Memory MCP

Store an important note for this session

File Structure

session-memory-mcp/
├─ server.ts
├─ package.json
├─ node_modules/

HTTP and SSE Transport Code Example
The http-server.ts and sse-server.ts are just the files containing different Transport protocols.

Use

Run npx tsx server.ts or node --loader tsx ./server.ts to start the server

Claude

{
	"mcpServers": {
		"session-memory-mcp": {
			"command": "npx",
			"args": [
					"tsx",
					"server.ts"
				],
			"cwd": "D:\\path\\to\\mcp-session-memory"
		}
	}
}
// Using HTTP
import { HttpServerTransport } from "@modelcontextprotocol/sdk/server/http.js";
// ...existing code
const transport = new HttpServerTransport({
port: 3333,
path: "/mcp",
});
await server.connect(transport);
{
"name": "session-memory-mcp",
"version": "1.0.0",
"type": "module",
"scripts": {
"start": "tsx index.ts"
},
"dependencies": {
"@modelcontextprotocol/sdk": "^1.0.0",
"zod": "^3.22.0"
},
"devDependencies": {
"tsx": "^4.0.0",
"typescript": "^5.0.0"
}
}
#!/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...");
// Using SSE
import { StreamableHttpServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
// ...existing code
server.registerTool(
// ... existing code
async ({ note }, { sendProgress }) => {
sendProgress?.("Saving note...");
memory.push(note);
let history = "";
let count = 1;
for (const item of memory) {
history += `${count}. ${item}\n`;
count++;
}
sendProgress?.("Done.");
return {
content: [
{
type: "text",
text: `I have saved this. Here is everything I remember so far:\n\n${history}`,
},
],
};
},
);
const transport = new StreamableHttpServerTransport({
port: 3333,
path: "/mcp",
});
await server.connect(transport);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment