From 0f9d2407ee9bc8d89d0a980246334e3903428a4f Mon Sep 17 00:00:00 2001 From: Justin Spahr-Summers Date: Wed, 20 Nov 2024 12:22:25 +0000 Subject: [PATCH] Add an example prompt for summarizing notes --- template/src/index.ts.ejs | 65 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/template/src/index.ts.ejs b/template/src/index.ts.ejs index a3e2407..6eca196 100644 --- a/template/src/index.ts.ejs +++ b/template/src/index.ts.ejs @@ -4,6 +4,7 @@ * - Listing notes as resources * - Reading individual notes * - Creating new notes via a tool + * - Summarizing all notes via a prompt */ import { Server } from "@modelcontextprotocol/sdk/server/index.js"; @@ -13,6 +14,8 @@ import { ListResourcesRequestSchema, ListToolsRequestSchema, ReadResourceRequestSchema, + ListPromptsRequestSchema, + GetPromptRequestSchema, } from "@modelcontextprotocol/sdk/types.js"; /** @@ -30,8 +33,8 @@ const notes: { [id: string]: Note } = { }; /** - * Create an MCP server with capabilities for resources (to list/read notes) - * and tools (to create new notes). + * Create an MCP server with capabilities for resources (to list/read notes), + * tools (to create new notes), and prompts (to summarize notes). */ const server = new Server( { @@ -42,6 +45,7 @@ const server = new Server( capabilities: { resources: {}, tools: {}, + prompts: {}, }, } ); @@ -144,6 +148,63 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => { } }); +/** + * Handler that lists available prompts. + * Exposes a single "summarize_notes" prompt that summarizes all notes. + */ +server.setRequestHandler(ListPromptsRequestSchema, async () => { + return { + prompts: [ + { + name: "summarize_notes", + description: "Summarize all notes", + } + ] + }; +}); + +/** + * Handler for the summarize_notes prompt. + * Returns a prompt that requests summarization of all notes, with the notes' contents embedded as resources. + */ +server.setRequestHandler(GetPromptRequestSchema, async (request) => { + if (request.params.name !== "summarize_notes") { + throw new Error("Unknown prompt"); + } + + const embeddedNotes = Object.entries(notes).map(([id, note]) => ({ + type: "resource" as const, + resource: { + uri: `note:///${id}`, + mimeType: "text/plain", + text: note.content + } + })); + + return { + messages: [ + { + role: "user", + content: { + type: "text", + text: "Please summarize the following notes:" + } + }, + ...embeddedNotes.map(note => ({ + role: "user" as const, + content: note + })), + { + role: "user", + content: { + type: "text", + text: "Provide a concise summary of all the notes above." + } + } + ] + }; +}); + /** * Start the server using stdio transport. * This allows the server to communicate via standard input/output streams.