Last active
May 19, 2026 18:54
-
-
Save patmandenver/3404fc3b81c3976d28a94852af04d339 to your computer and use it in GitHub Desktop.
Basic Prompt MCP server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import express, { Request, Response } from 'express'; | |
| const app = express(); | |
| app.use(express.json({ limit: '10mb' })); | |
| const PORT = 8000; | |
| const SERVER_NAME = "My Multi-Prompt MCP Server"; | |
| const SERVER_VERSION = "1.3.0"; | |
| const PROTOCOL_VERSION = "2025-06-18"; | |
| // ============================================================ | |
| // SINGLE SOURCE OF TRUTH - All prompt info in one place | |
| // ============================================================ | |
| interface PromptDefinition { | |
| name: string; | |
| description: string; | |
| arguments: any[]; | |
| handler: (args: Record<string, any>) => string; | |
| } | |
| const PROMPTS: PromptDefinition[] = [ | |
| { | |
| name: "say-hi", | |
| description: "A simple prompt that just says hi (no arguments)", | |
| arguments: [], | |
| handler: () => "Say hi" | |
| } | |
| ]; | |
| // ============================================================ | |
| // MCP Endpoint | |
| // ============================================================ | |
| app.post('/mcp', (req: Request, res: Response) => { | |
| const { jsonrpc, id, method, params = {} } = req.body; | |
| // Handle notifications | |
| if (!id && method?.startsWith('notifications/')) { | |
| console.log(`[MCP] Notification: ${method}`); | |
| return res.status(202).end(); | |
| } | |
| // === INITIALIZE === | |
| if (method === 'initialize') { | |
| return res.json({ | |
| jsonrpc: "2.0", | |
| id, | |
| result: { | |
| protocolVersion: PROTOCOL_VERSION, | |
| serverInfo: { | |
| name: SERVER_NAME, | |
| title: "My Multi-Prompt MCP Server", | |
| version: SERVER_VERSION | |
| }, | |
| capabilities: { | |
| prompts: { listChanged: false } | |
| } | |
| } | |
| }); | |
| } | |
| // === PROMPTS/LIST (strip handler functions) === | |
| if (method === 'prompts/list') { | |
| return res.json({ | |
| jsonrpc: "2.0", | |
| id, | |
| result: { | |
| prompts: PROMPTS.map(p => ({ | |
| name: p.name, | |
| description: p.description, | |
| arguments: p.arguments | |
| })) | |
| } | |
| }); | |
| } | |
| // === PROMPTS/GET (Now fully DRY) === | |
| if (method === 'prompts/get') { | |
| const { name, arguments: args = {} } = params; | |
| const prompt = PROMPTS.find(p => p.name === name); | |
| if (!prompt) { | |
| return res.status(404).json({ | |
| jsonrpc: "2.0", | |
| id, | |
| error: { code: -32602, message: `Prompt not found: ${name}` } | |
| }); | |
| } | |
| const promptText = prompt.handler(args); | |
| return res.json({ | |
| jsonrpc: "2.0", | |
| id, | |
| result: { | |
| description: prompt.description, | |
| messages: [{ | |
| role: "user", | |
| content: { type: "text", text: promptText } | |
| }] | |
| } | |
| }); | |
| } | |
| // === PING === | |
| if (method === 'ping') { | |
| return res.json({ jsonrpc: "2.0", id, result: {} }); | |
| } | |
| console.warn(`[MCP] Unsupported method: ${method}`); | |
| return res.status(404).json({ | |
| jsonrpc: "2.0", | |
| id, | |
| error: { code: -32601, message: `Method not found: ${method}` } | |
| }); | |
| }); | |
| // Health check | |
| app.get('/health', (req: Request, res: Response) => { | |
| res.json({ status: "ok", server: SERVER_NAME, prompts: PROMPTS.length }); | |
| }); | |
| app.get('/mcp', (req: Request, res: Response) => { | |
| res.json({ message: "Multi-prompt MCP server is running." }); | |
| }); | |
| // Start server | |
| app.listen(PORT, '0.0.0.0', () => { | |
| console.log(` | |
| ╔════════════════════════════════════════════════════════════╗ | |
| ║ 🚀 My Multi-Prompt MCP Server (Fully DRY) ║ | |
| ╠════════════════════════════════════════════════════════════╣ | |
| ║ Endpoint: http://localhost:${PORT}/mcp ║ | |
| ║ Prompts: mcp-say-hi, mcp-single-arg, mcp-args ║ | |
| ╚════════════════════════════════════════════════════════════╝ | |
| `); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment