welsonjs/lib/stdio-server.js
Namhyeon, Go c22ca660ec Add MCP JSON-RPC handlers and stdio fixes
Implement MCP JSON-RPC handling and related I/O fixes: mcploader now handles initialize, tools/list and tools/call (implements add_both_numbers) and returns JSON-RPC responses; stdio-server.send serializes object messages before writing; console logging on WScript now writes muted messages to StdErr; bump jsonrpc2 version to 0.1.7 and reformat extract(). These changes enable proper MCP capability negotiation, tool discovery and invocation over stdio.
2026-04-12 20:39:50 +09:00

66 lines
1.6 KiB
JavaScript

// stdio-server.js
// Copyright 2019-2026, Namhyeon Go <gnh1201@catswords.re.kr> and the WelsonJS contributors.
// SPDX-License-Identifier: GPL-3.0-or-later
// https://github.com/gnh1201/welsonjs
//
var STD = require("lib/std");
function StdioServer() {
// Set event-attachable object
STD.EventTarget.apply(this, arguments);
this.messages = [];
this.receive = function () {
return this.messages.shift();
};
this.send = function (message) {
if (typeof message === "object") {
try {
var _serialized = JSON.stringify(message);
message = _serialized;
} catch (e) { /* ignore */ };
}
WScript.StdOut.WriteLine(message);
};
this.listen = function () {
while (!WScript.StdIn.AtEndOfStream) {
this.messages.push(WScript.StdIn.ReadLine());
this.dispatchEvent(new STD.Event("message"));
}
};
}
StdioServer.prototype = new STD.EventTarget();
StdioServer.prototype.constructor = StdioServer;
/*
// For example
var server = new StdioServer();
server.addEventListener("message", function(e) {
// receive message
var message = e.target.receive();
console.log(message);
// send message
e.target.send("Hello world");
});
server.listen();
*/
exports.create = function () {
return new StdioServer();
};
exports.VERSIONINFO = "Stdio Server (stdio-server.js) version 0.1";
exports.AUTHOR = "gnh1201@catswords.re.kr";
exports.global = global;
exports.require = global.require;