UNPKG

stock-nse-india

Version:

This package will help us to get equity/index details and historical data from National Stock Exchange of India.

158 lines 5.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MCPServer = void 0; const index_js_1 = require("../../index.js"); const mcp_tools_js_1 = require("../mcp-tools.js"); // Initialize NSE India client const nseClient = new index_js_1.NseIndia(); // MCP Protocol Implementation class MCPServer { constructor() { this.serverInfo = { name: 'nse-india-stdio', version: '1.2.2', }; this.setupStdinHandling(); } setupStdinHandling() { process.stdin.setEncoding('utf8'); let buffer = ''; process.stdin.on('data', (chunk) => { buffer += chunk; // Process complete JSON messages let newlineIndex; while ((newlineIndex = buffer.indexOf('\n')) !== -1) { const message = buffer.substring(0, newlineIndex).trim(); buffer = buffer.substring(newlineIndex + 1); if (message) { this.handleMessage(message); } } }); process.stdin.on('end', () => { process.exit(0); }); } async handleMessage(message) { let parsed; try { parsed = JSON.parse(message); const { id, method, params } = parsed; // Check if this is a notification (no id) or a request (with id) if (id === undefined) { // This is a notification - handle it but don't send response if (method === 'notifications/initialized') { // Just acknowledge the notification silently return; } // For other notifications without id, ignore them return; } // This is a request - validate required fields if (method === undefined) { const response = { jsonrpc: '2.0', id, error: { code: -32600, message: 'Invalid Request: missing method', }, }; this.sendResponse(response); return; } let result; let error = null; try { switch (method) { case 'initialize': result = { protocolVersion: '2024-11-05', capabilities: { tools: {}, prompts: {}, resources: {}, }, serverInfo: this.serverInfo, }; break; case 'tools/list': result = { tools: mcp_tools_js_1.mcpTools }; break; case 'tools/call': if (!params || !params.name) { error = { code: -32602, message: 'Invalid params: missing tool name', }; } else { result = await this.handleToolCall(params); } break; // Add missing MCP protocol methods case 'prompts/list': result = { prompts: [] }; break; case 'resources/list': result = { resources: [] }; break; default: error = { code: -32601, message: `Method not found: ${method}`, }; } } catch (err) { error = { code: -32603, message: err instanceof Error ? err.message : String(err), }; } const response = { jsonrpc: '2.0', id, ...(error ? { error } : { result }), }; this.sendResponse(response); } catch (err) { // Invalid JSON - only send error if we have an id if (parsed && parsed.id !== undefined) { const response = { jsonrpc: '2.0', id: parsed.id, error: { code: -32700, message: 'Parse error', }, }; this.sendResponse(response); } } } async handleToolCall(params) { const { name, arguments: args } = params; const result = await (0, mcp_tools_js_1.handleMCPToolCall)(nseClient, name, args); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2), }, ], }; } sendResponse(response) { // Ensure response has all required fields const validResponse = { jsonrpc: '2.0', id: response.id, ...(response.error ? { error: response.error } : { result: response.result }), }; process.stdout.write(JSON.stringify(validResponse) + '\n'); } } exports.MCPServer = MCPServer; //# sourceMappingURL=mcp-server.js.map