()
| 17 | |
| 18 | // Create an MCP server instance |
| 19 | const getServer = () => { |
| 20 | const server = new McpServer( |
| 21 | { |
| 22 | name: 'simple-sse-server', |
| 23 | version: '1.0.0' |
| 24 | }, |
| 25 | { capabilities: { logging: {} } } |
| 26 | ); |
| 27 | |
| 28 | server.registerTool( |
| 29 | 'start-notification-stream', |
| 30 | { |
| 31 | description: 'Starts sending periodic notifications', |
| 32 | inputSchema: { |
| 33 | interval: z.number().describe('Interval in milliseconds between notifications').default(1000), |
| 34 | count: z.number().describe('Number of notifications to send').default(10) |
| 35 | } |
| 36 | }, |
| 37 | async ({ interval, count }, extra): Promise<CallToolResult> => { |
| 38 | const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); |
| 39 | let counter = 0; |
| 40 | |
| 41 | // Send the initial notification |
| 42 | await server.sendLoggingMessage( |
| 43 | { |
| 44 | level: 'info', |
| 45 | data: `Starting notification stream with ${count} messages every ${interval}ms` |
| 46 | }, |
| 47 | extra.sessionId |
| 48 | ); |
| 49 | |
| 50 | // Send periodic notifications |
| 51 | while (counter < count) { |
| 52 | counter++; |
| 53 | await sleep(interval); |
| 54 | |
| 55 | try { |
| 56 | await server.sendLoggingMessage( |
| 57 | { |
| 58 | level: 'info', |
| 59 | data: `Notification #${counter} at ${new Date().toISOString()}` |
| 60 | }, |
| 61 | extra.sessionId |
| 62 | ); |
| 63 | } catch (error) { |
| 64 | console.error('Error sending notification:', error); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return { |
| 69 | content: [ |
| 70 | { |
| 71 | type: 'text', |
| 72 | text: `Completed sending ${count} notifications every ${interval}ms` |
| 73 | } |
| 74 | ] |
| 75 | }; |
| 76 | } |
no test coverage detected
searching dependent graphs…