116 lines
2.8 KiB
Lua
116 lines
2.8 KiB
Lua
local createMcpComputer = require('/apis/libmcpcomputer');
|
|
local createVersion = require('/apis/libversion');
|
|
|
|
local args = table.pack(...);
|
|
local command = args[1];
|
|
|
|
local function printUsage()
|
|
print('mcp-computer usage:');
|
|
print();
|
|
print(' mcp-computer <ws-url>');
|
|
print(' mcp-computer -url <ws-url>');
|
|
print(' mcp-computer --version');
|
|
print(' mcp-computer --help');
|
|
print();
|
|
print('examples:');
|
|
print(' mcp-computer ws://192.168.1.20:3001');
|
|
print(' mcp-computer -url ws://mcp-bridge.local:3001');
|
|
print();
|
|
print('with no URL, falls back to the mcp-computer.ws-url setting.');
|
|
end
|
|
|
|
local function fail(message)
|
|
print(message);
|
|
error('mcp-computer failed', 0);
|
|
end
|
|
|
|
local function decodeJson(text)
|
|
return textutils.unserializeJSON(text);
|
|
end
|
|
|
|
local function sendJson(ws, value)
|
|
ws.send(textutils.serializeJSON(value));
|
|
end
|
|
|
|
local function waitForHelloOk(ws)
|
|
local message = ws.receive(5);
|
|
if not message then
|
|
return false, 'timed out waiting for hello-ok';
|
|
end
|
|
|
|
local frame = decodeJson(message);
|
|
if type(frame) ~= 'table' or frame.type ~= 'hello-ok' then
|
|
return false, 'unexpected hello response';
|
|
end
|
|
|
|
return true;
|
|
end
|
|
|
|
if command == '-help' or command == '--help' or command == 'help' then
|
|
printUsage();
|
|
return;
|
|
end
|
|
|
|
if command == '-version' or command == '--version' or command == 'version' then
|
|
print('v' .. createVersion().forSelf());
|
|
return;
|
|
end
|
|
|
|
local mcpComputer = createMcpComputer();
|
|
local config, err = mcpComputer.resolveUrl(args, settings);
|
|
if not config then
|
|
print(err);
|
|
print('use: mcp-computer -help');
|
|
return;
|
|
end
|
|
|
|
if not http then
|
|
fail('CC:Tweaked HTTP/WebSocket is unavailable: enable the http API.');
|
|
end
|
|
|
|
if not http.websocket then
|
|
fail('CC:Tweaked WebSocket is unavailable: enable HTTP WebSocket support.');
|
|
end
|
|
|
|
local version = createVersion().forSelf();
|
|
print('mcp-computer v' .. version .. ' connecting to ' .. config.url);
|
|
|
|
local ws, connectErr = http.websocket(config.url);
|
|
if not ws then
|
|
fail('websocket failed: ' .. tostring(connectErr));
|
|
end
|
|
|
|
local ok, runtimeErr = pcall(function()
|
|
sendJson(ws, mcpComputer.hello(os));
|
|
|
|
local helloOk, helloErr = waitForHelloOk(ws);
|
|
if not helloOk then
|
|
error(helloErr, 0);
|
|
end
|
|
|
|
print('linked as ' .. tostring(os.getComputerID())
|
|
.. ' (Label: ' .. mcpComputer.formatLabel(os.getComputerLabel()) .. ')');
|
|
print('waiting for requests... Press Ctrl+T to stop.');
|
|
|
|
while true do
|
|
local message = ws.receive();
|
|
if not message then return; end
|
|
|
|
local frame = decodeJson(message);
|
|
local response = mcpComputer.handleRequest(frame, os);
|
|
if response then
|
|
sendJson(ws, response);
|
|
end
|
|
end
|
|
end);
|
|
|
|
pcall(function() ws.close(); end);
|
|
|
|
if not ok then
|
|
if tostring(runtimeErr) == 'Terminated' then
|
|
print('stopped.');
|
|
return;
|
|
end
|
|
fail(tostring(runtimeErr));
|
|
end
|