|
| 1 | +import { z } from 'zod' |
| 2 | +import express, { Request, Response } from 'express' |
| 3 | +import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js' |
| 4 | +import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js' |
| 5 | + |
| 6 | +interface ToolResult { |
| 7 | + [key: string]: unknown |
| 8 | + content: Array<{ |
| 9 | + type: 'text' |
| 10 | + text: string |
| 11 | + }> |
| 12 | + isError?: boolean |
| 13 | +} |
| 14 | + |
| 15 | +interface PromptResult { |
| 16 | + [key: string]: unknown |
| 17 | + messages: Array<{ |
| 18 | + role: 'user' | 'assistant' |
| 19 | + content: { |
| 20 | + type: 'text' |
| 21 | + text: string |
| 22 | + } |
| 23 | + }> |
| 24 | +} |
| 25 | + |
| 26 | +const server = new McpServer({ |
| 27 | + name: 'mcp-sse-server', |
| 28 | + version: '1.0.0', |
| 29 | +}) |
| 30 | + |
| 31 | +const textResource = { |
| 32 | + name: 'text-example', |
| 33 | + uri: 'resource://text-example', |
| 34 | + async callback() { |
| 35 | + return { |
| 36 | + contents: [ |
| 37 | + { |
| 38 | + uri: 'resource://text-example', |
| 39 | + text: 'Hello, MCP!', |
| 40 | + }, |
| 41 | + ], |
| 42 | + } |
| 43 | + }, |
| 44 | +} |
| 45 | + |
| 46 | +server.resource(textResource.name, textResource.uri, textResource.callback) |
| 47 | + |
| 48 | +const calculateTool = { |
| 49 | + name: 'calculate', |
| 50 | + description: 'Calculate the result of the expression', |
| 51 | + parameters: { |
| 52 | + expression: z.string().describe('The mathematical expression to calculate'), |
| 53 | + }, |
| 54 | + async handler(extra: any): Promise<ToolResult> { |
| 55 | + try { |
| 56 | + const result = Function(`'use strict'; return (${extra.expression})`)() |
| 57 | + return { |
| 58 | + content: [ |
| 59 | + { |
| 60 | + type: 'text', |
| 61 | + text: String(result), |
| 62 | + }, |
| 63 | + ], |
| 64 | + } |
| 65 | + } catch (error: any) { |
| 66 | + return { |
| 67 | + content: [ |
| 68 | + { |
| 69 | + type: 'text', |
| 70 | + text: `Calculation error: ${error.message}`, |
| 71 | + }, |
| 72 | + ], |
| 73 | + isError: true, |
| 74 | + } |
| 75 | + } |
| 76 | + }, |
| 77 | +} |
| 78 | + |
| 79 | +server.tool( |
| 80 | + calculateTool.name, |
| 81 | + calculateTool.description, |
| 82 | + calculateTool.parameters, |
| 83 | + calculateTool.handler |
| 84 | +) |
| 85 | + |
| 86 | +const codeAnalysisPrompt = { |
| 87 | + name: 'analyze-code', |
| 88 | + description: 'Analyze code and provide improvement suggestions', |
| 89 | + arguments: { |
| 90 | + language: z.string().describe('Programming language'), |
| 91 | + code: z.string().describe('Code to analyze'), |
| 92 | + }, |
| 93 | + async getMessages(extra: any): Promise<PromptResult> { |
| 94 | + return { |
| 95 | + messages: [ |
| 96 | + { |
| 97 | + role: 'user', |
| 98 | + content: { |
| 99 | + type: 'text', |
| 100 | + text: `Please analyze the following ${ |
| 101 | + extra.language || 'unspecified' |
| 102 | + } code and provide improvement suggestions:\n\n\`\`\`${ |
| 103 | + extra.language |
| 104 | + }\n${extra.code}\n\`\`\``, |
| 105 | + }, |
| 106 | + }, |
| 107 | + ], |
| 108 | + } |
| 109 | + }, |
| 110 | +} |
| 111 | + |
| 112 | +server.prompt( |
| 113 | + codeAnalysisPrompt.name, |
| 114 | + codeAnalysisPrompt.description, |
| 115 | + codeAnalysisPrompt.arguments, |
| 116 | + codeAnalysisPrompt.getMessages |
| 117 | +) |
| 118 | + |
| 119 | +const app = express() |
| 120 | + |
| 121 | +const transports: { [sessionId: string]: SSEServerTransport } = {} |
| 122 | + |
| 123 | +app.get('/sse', async (_: Request, res: Response) => { |
| 124 | + const transport = new SSEServerTransport('/messages', res) |
| 125 | + transports[transport.sessionId] = transport |
| 126 | + |
| 127 | + res.on('close', () => { |
| 128 | + delete transports[transport.sessionId] |
| 129 | + }) |
| 130 | + |
| 131 | + await server.connect(transport) |
| 132 | +}) |
| 133 | + |
| 134 | +app.post('/messages', async (req: Request, res: Response) => { |
| 135 | + const sessionId = req.query.sessionId as string |
| 136 | + const transport = transports[sessionId] |
| 137 | + |
| 138 | + if (transport) { |
| 139 | + await transport.handlePostMessage(req, res) |
| 140 | + } else { |
| 141 | + res.status(400).send('not found') |
| 142 | + } |
| 143 | +}) |
| 144 | + |
| 145 | +const PORT = process.env.PORT || 3001 |
| 146 | +app.listen(PORT, () => { |
| 147 | + console.log(`MCP SSE Running at http://localhost:${PORT}`) |
| 148 | +}) |
0 commit comments