cc-libs/apis/libmcpcomputer.lua
2026-06-15 19:57:45 +02:00

131 lines
3.4 KiB
Lua

-- Computer-side MCP execution primitives.
--
-- Protocol-agnostic building blocks for the MCP `exec-lua` / `write-file` features.
-- The transport lives elsewhere: servers/mcp-computer-server.lua reacts to sandbox
-- gateway_request events and replies through the trap-sandbox gateway (see
-- apis/libsandbox.lua). This module only knows how to run code and write files.
local function defaultOs()
return os;
end
local function formatLabel(label)
if type(label) ~= 'string' or label == '' then
return 'null';
end
return label;
end
local function appendOutput(output, value)
output[#output + 1] = tostring(value);
end
local function serializeReturn(value)
local valueType = type(value);
if valueType == 'nil' then
return { type = 'nil' };
end
if valueType == 'string' or valueType == 'number' or valueType == 'boolean' then
return { type = valueType, value = value };
end
local ok, serialized = pcall(textutils.serialize, value);
if ok then
return { type = valueType, repr = serialized };
end
return { type = valueType, repr = tostring(value) };
end
local function createExecEnv(output)
local env = setmetatable({}, { __index = _G });
env.write = function(value)
appendOutput(output, value);
end;
env.print = function(...)
local values = table.pack(...);
for i = 1, values.n do
if i > 1 then
appendOutput(output, '\t');
end
appendOutput(output, values[i]);
end
appendOutput(output, '\n');
end;
return env;
end
local function createMcpComputer()
local api = {};
api.formatLabel = formatLabel;
function api.formatPong(osLike)
osLike = osLike or defaultOs();
return 'pong from ' .. tostring(osLike.getComputerID())
.. ' (Label: ' .. formatLabel(osLike.getComputerLabel()) .. ')';
end
function api.executeLua(code)
local output = {};
if type(code) ~= 'string' or code == '' then
return { ok = false, error = 'code must be a non-empty string', output = '' };
end
local fn, loadErr = load(code, 'mcp-exec', 't', createExecEnv(output));
if not fn then
return { ok = false, error = tostring(loadErr), output = table.concat(output) };
end
local values = table.pack(pcall(fn));
if not values[1] then
return { ok = false, error = tostring(values[2]), output = table.concat(output) };
end
local returns = {};
for i = 2, values.n do
returns[#returns + 1] = serializeReturn(values[i]);
end
return { ok = true, returns = returns, output = table.concat(output) };
end
function api.writeFile(path, content, fsLike)
fsLike = fsLike or fs;
if type(path) ~= 'string' or path == '' then
return { ok = false, error = 'path must be a non-empty string' };
end
if type(content) ~= 'string' then
return { ok = false, error = 'content must be a string' };
end
local handle, openErr = fsLike.open(path, 'w');
if not handle then
return { ok = false, error = tostring(openErr or 'failed to open file') };
end
local ok, writeErr = pcall(function()
handle.write(content);
end);
local closeOk, closeErr = pcall(function()
handle.close();
end);
if not ok then
return { ok = false, error = tostring(writeErr) };
end
if not closeOk then
return { ok = false, error = tostring(closeErr) };
end
return { ok = true, path = path, bytes = string.len(content) };
end
return api;
end
return createMcpComputer;