#!/usr/bin/env node
const http = require('http');
const readline = require('readline');
const API_BASE = 'http://localhost:9100';
const tools = [
{name: 'rpa_click', description: 'Click at coordinates', inputSchema: {type: 'object', properties: {x: {type: 'number'}, y: {type: 'number'}}, required: ['x', 'y']}},
{name: 'rpa_type', description: 'Type text', inputSchema: {type: 'object', properties: {text: {type: 'string'}}, required: ['text']}},
{name: 'rpa_screenshot', description: 'Take screenshot', inputSchema: {type: 'object', properties: {}}},
{name: 'rpa_execute', description: 'Execute workflow', inputSchema: {type: 'object', properties: {steps: {type: 'array'}}, required: ['steps']}},
{name: 'rpa_advanced', description: 'Execute advanced workflow', inputSchema: {type: 'object', properties: {steps: {type: 'array'}}, required: ['steps']}},
{name: 'rpa_natural', description: 'Natural language automation', inputSchema: {type: 'object', properties: {instruction: {type: 'string'}}, required: ['instruction']}}
];
async function callApi(method, path, body) {
return new Promise((resolve, reject) => {
const opts = {method, headers: {}};
if (body) {
opts.headers['Content-Type'] = typeof body === 'string' ? 'text/plain' : 'application/json';
}
const req = http.request(API_BASE + path, opts, res => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => resolve(data));
});
req.on('error', reject);
if (body) req.write(typeof body === 'string' ? body : JSON.stringify(body));
req.end();
});
}
async function handleTool(name, args) {
switch (name) {
case 'rpa_click': return await callApi('POST', `/auto/click?x=${args.x}&y=${args.y}`);
case 'rpa_type': return await callApi('POST', '/auto/type', args.text);
case 'rpa_screenshot': return await callApi('GET', '/screenshot');
case 'rpa_execute': return await callApi('POST', '/workflow/execute', args.steps);
case 'rpa_advanced': return await callApi('POST', '/advanced/execute', {steps: args.steps});
case 'rpa_natural': return await callApi('POST', '/natural/execute', args.instruction);
default: throw new Error('Unknown tool: ' + name);
}
}
const rl = readline.createInterface({input: process.stdin, output: process.stdout});
rl.on('line', async line => {
try {
const msg = JSON.parse(line);
let response;
if (msg.method === 'initialize') {
response = {protocolVersion: '2025-11-25', serverInfo: {name: 'rpa-mcp-server', version: '2.0.0'}, capabilities: {tools: {}}};
} else if (msg.method === 'tools/list') {
response = {tools};
} else if (msg.method === 'tools/call') {
const result = await handleTool(msg.params.name, msg.params.arguments || {});
response = {content: [{type: 'text', text: result}]};
} else {
throw new Error('Unknown method: ' + msg.method);
}
console.log(JSON.stringify({jsonrpc: '2.0', id: msg.id, result: response}));
} catch (e) {
console.log(JSON.stringify({jsonrpc: '2.0', id: 1, error: {code: -32603, message: e.message}}));
}
});