/**
* @copyright 2025 Chris Bunting <cbuntingde@gmail.com>
* @license MIT
*
* Prompt handlers for the Memory MCP Server
*/
import {
ListPromptsRequestSchema,
GetPromptRequestSchema,
McpError,
ErrorCode
} from "@modelcontextprotocol/sdk/types.js";
import { MemoryStore } from "../store/MemoryStore.js";
export function setupPromptHandlers(server: any, memoryStore: MemoryStore): void {
// List available prompts
server.setRequestHandler(ListPromptsRequestSchema, async () => {
return {
prompts: [
{
name: "memory_summary",
description: "Generate a comprehensive memory summary for a user"
},
{
name: "personalization_insights",
description: "Get personalization insights based on user memories"
}
]
};
});
// Handle prompt requests
server.setRequestHandler(GetPromptRequestSchema, async (request: any) => {
const { name, arguments: args } = request.params;
if (name === "memory_summary") {
const userId = args?.userId as string;
if (!userId) {
throw new McpError(ErrorCode.InvalidParams, "userId is required for memory summary");
}
const longTerm = memoryStore.getLongTermMemory(userId);
const episodic = memoryStore.getEpisodicMemory(userId, 10);
return {
messages: [
{
role: "user",
content: {
type: "text",
text: `Please provide a comprehensive memory summary for user ${userId}. Include:`
}
},
{
role: "user",
content: {
type: "text",
text: "1. Key demographic and preference information"
}
},
{
role: "user",
content: {
type: "text",
text: "2. Recent experiences and patterns"
}
},
{
role: "user",
content: {
type: "text",
text: "3. Insights for personalization and proactive support"
}
},
{
role: "user",
content: {
type: "resource",
resource: {
uri: `memory://long-term/${userId}`,
mimeType: "application/json",
text: JSON.stringify(longTerm, null, 2)
}
}
},
{
role: "user",
content: {
type: "resource",
resource: {
uri: `memory://episodic/${userId}`,
mimeType: "application/json",
text: JSON.stringify(episodic, null, 2)
}
}
}
]
};
} else if (name === "personalization_insights") {
const userId = args?.userId as string;
if (!userId) {
throw new McpError(ErrorCode.InvalidParams, "userId is required for personalization insights");
}
const longTerm = memoryStore.getLongTermMemory(userId);
const episodic = memoryStore.getEpisodicMemory(userId, 20);
return {
messages: [
{
role: "user",
content: {
type: "text",
text: `Based on the user's memory data, provide personalization insights for:`
}
},
{
role: "user",
content: {
type: "text",
text: "1. Preferred communication style and approach"
}
},
{
role: "user",
content: {
type: "text",
text: "2. Anticipated needs based on past experiences"
}
},
{
role: "user",
content: {
type: "text",
text: "3. Opportunities for proactive personalization"
}
},
{
role: "user",
content: {
type: "resource",
resource: {
uri: `memory://long-term/${userId}`,
mimeType: "application/json",
text: JSON.stringify(longTerm, null, 2)
}
}
},
{
role: "user",
content: {
type: "resource",
resource: {
uri: `memory://episodic/${userId}`,
mimeType: "application/json",
text: JSON.stringify(episodic, null, 2)
}
}
}
]
};
}
throw new McpError(ErrorCode.MethodNotFound, `Unknown prompt: ${name}`);
});
}