Created
April 5, 2025 03:45
-
-
Save nsdevaraj/43a489f837b6fd3b884647e706c5864f to your computer and use it in GitHub Desktop.
mcp with socket
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
{ | |
"type": "module", | |
"dependencies": { | |
"@coinpaprika/api-nodejs-client": "^2.0.0", | |
"@modelcontextprotocol/sdk": "^1.7.0", | |
"ws": "^8.18.1", | |
"zod": "^3.24.2" | |
} | |
} |
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 { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js" | |
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js" | |
import { z } from "zod" | |
import { WebSocketServer } from 'ws' | |
const server = new McpServer({ | |
name: "MCP Server Boilerplate", | |
version: "1.0.0", | |
}) | |
// Function to send WebSocket message asynchronously | |
async function sendWebSocketMessage(a, b) { | |
return new Promise((resolve, reject) => { | |
try { | |
// Broadcast to all connected clients | |
wss.clients.forEach(client => { | |
if (client.readyState === WebSocket.OPEN) { | |
const response = { | |
status: 'success', | |
timestamp: new Date().toISOString(), | |
result: a + b, | |
message: 'Calculation completed successfully' | |
}; | |
client.send(JSON.stringify(response)); | |
} | |
}); | |
resolve(`Sent WebSocket message with result: ${a + b}`); | |
} catch (error) { | |
reject(`Error sending WebSocket message: ${error.message}`); | |
} | |
}); | |
} | |
server.tool("add", "Add two numbers", { a: z.number(), b: z.number() }, async ({ a, b }) => ({ | |
content: [{ type: "text", text: await sendWebSocketMessage(a, b) }], | |
})) | |
// Create WebSocket server | |
const wss = new WebSocketServer({ port: 8080 }) | |
wss.on('connection', (ws) => { | |
ws.on('message', (message) => { | |
try { | |
const data = JSON.parse(message.toString()) | |
// Create a JSON response | |
const response = { | |
status: 'success', | |
timestamp: new Date().toISOString(), | |
received: String(data.a + data.b), | |
message: 'Message received successfully' | |
} | |
// Send JSON response | |
ws.send(JSON.stringify(response)) | |
} catch (error) { | |
// Handle JSON parsing errors | |
const errorResponse = { | |
status: 'error', | |
timestamp: new Date().toISOString(), | |
error: 'Invalid JSON format', | |
message: error.message | |
} | |
ws.send(JSON.stringify(errorResponse)) | |
} | |
}) | |
ws.on('close', () => { | |
// console.log('Client disconnected') | |
}) | |
}) | |
const transport = new StdioServerTransport() | |
await server.connect(transport) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment