UNPKG

onerios-mcp-server

Version:

OneriosMCP server providing memory, backlog management, file operations, and utility functions for enhanced AI assistant capabilities

369 lines (368 loc) 13.8 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.webUtils = exports.WebUtils = void 0; const zod_1 = require("zod"); const https = __importStar(require("https")); const http = __importStar(require("http")); const url_1 = require("url"); class WebUtils { constructor() { this.tools = [ { name: 'validate_url', description: 'Validate and parse URL structure', inputSchema: zod_1.z.object({ url: zod_1.z.string().describe('URL to validate'), check_reachable: zod_1.z.boolean().optional().default(false).describe('Whether to check if URL is reachable'), }), }, { name: 'http_get', description: 'Make HTTP GET request', inputSchema: zod_1.z.object({ url: zod_1.z.string().describe('URL to make GET request to'), headers: zod_1.z.record(zod_1.z.string()).optional().describe('Optional HTTP headers'), timeout: zod_1.z.number().optional().default(5000).describe('Request timeout in milliseconds'), }), }, { name: 'http_post', description: 'Make HTTP POST request', inputSchema: zod_1.z.object({ url: zod_1.z.string().describe('URL to make POST request to'), data: zod_1.z.string().describe('Data to send in POST body'), headers: zod_1.z.record(zod_1.z.string()).optional().describe('Optional HTTP headers'), timeout: zod_1.z.number().optional().default(5000).describe('Request timeout in milliseconds'), }), }, { name: 'parse_query_string', description: 'Parse URL query string parameters', inputSchema: zod_1.z.object({ query_string: zod_1.z.string().describe('Query string to parse (with or without ?)'), }), }, { name: 'build_query_string', description: 'Build query string from parameters', inputSchema: zod_1.z.object({ parameters: zod_1.z.record(zod_1.z.union([zod_1.z.string(), zod_1.z.number(), zod_1.z.boolean()])) .describe('Parameters to convert to query string'), }), }, { name: 'extract_domain', description: 'Extract domain and subdomain information from URL', inputSchema: zod_1.z.object({ url: zod_1.z.string().describe('URL to extract domain from'), }), }, ]; } getTools() { return this.tools; } hasHandler(name) { return this.tools.some(tool => tool.name === name); } async handleTool(name, args) { switch (name) { case 'validate_url': return this.validateUrl(args); case 'http_get': return this.httpGet(args); case 'http_post': return this.httpPost(args); case 'parse_query_string': return this.parseQueryString(args); case 'build_query_string': return this.buildQueryString(args); case 'extract_domain': return this.extractDomain(args); default: throw new Error(`Unknown tool: ${name}`); } } async validateUrl(args) { try { const url = new url_1.URL(args.url); let result = `URL Validation Results: - Valid: Yes - Protocol: ${url.protocol} - Hostname: ${url.hostname} - Port: ${url.port || (url.protocol === 'https:' ? '443' : '80')} - Pathname: ${url.pathname} - Search: ${url.search} - Hash: ${url.hash}`; if (args.check_reachable) { try { const reachable = await this.checkUrlReachable(args.url); result += `\n- Reachable: ${reachable ? 'Yes' : 'No'}`; } catch (error) { result += `\n- Reachable: Error - ${error}`; } } return { content: [{ type: 'text', text: result, }], }; } catch (error) { return { content: [{ type: 'text', text: `URL Validation Results: - Valid: No - Error: ${error}`, }], }; } } async httpGet(args) { return new Promise((resolve, reject) => { try { const url = new url_1.URL(args.url); const isHttps = url.protocol === 'https:'; const client = isHttps ? https : http; const options = { hostname: url.hostname, port: url.port, path: url.pathname + url.search, method: 'GET', headers: args.headers || {}, timeout: args.timeout || 5000, }; const req = client.request(options, (res) => { let data = ''; res.on('data', (chunk) => { data += chunk; }); res.on('end', () => { const result = `HTTP GET Response: - Status: ${res.statusCode} ${res.statusMessage} - Headers: ${JSON.stringify(res.headers, null, 2)} - Body Length: ${data.length} characters - Body Preview: ${data.substring(0, 500)}${data.length > 500 ? '...' : ''}`; resolve({ content: [{ type: 'text', text: result, }], }); }); }); req.on('error', (error) => { reject(new Error(`HTTP GET failed: ${error.message}`)); }); req.on('timeout', () => { req.destroy(); reject(new Error('HTTP GET request timed out')); }); req.end(); } catch (error) { reject(new Error(`HTTP GET failed: ${error}`)); } }); } async httpPost(args) { return new Promise((resolve, reject) => { try { const url = new url_1.URL(args.url); const isHttps = url.protocol === 'https:'; const client = isHttps ? https : http; const postData = args.data; const headers = { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(postData), ...args.headers, }; const options = { hostname: url.hostname, port: url.port, path: url.pathname + url.search, method: 'POST', headers, timeout: args.timeout || 5000, }; const req = client.request(options, (res) => { let data = ''; res.on('data', (chunk) => { data += chunk; }); res.on('end', () => { const result = `HTTP POST Response: - Status: ${res.statusCode} ${res.statusMessage} - Headers: ${JSON.stringify(res.headers, null, 2)} - Body Length: ${data.length} characters - Body Preview: ${data.substring(0, 500)}${data.length > 500 ? '...' : ''}`; resolve({ content: [{ type: 'text', text: result, }], }); }); }); req.on('error', (error) => { reject(new Error(`HTTP POST failed: ${error.message}`)); }); req.on('timeout', () => { req.destroy(); reject(new Error('HTTP POST request timed out')); }); req.write(postData); req.end(); } catch (error) { reject(new Error(`HTTP POST failed: ${error}`)); } }); } async parseQueryString(args) { try { const queryString = args.query_string.startsWith('?') ? args.query_string.substring(1) : args.query_string; const params = new URLSearchParams(queryString); const result = {}; params.forEach((value, key) => { if (result[key]) { result[key].push(value); } else { result[key] = [value]; } }); const formatted = Object.entries(result) .map(([key, values]) => `${key}: ${values.join(', ')}`) .join('\n'); return { content: [{ type: 'text', text: `Query String Parameters: ${formatted || 'No parameters found'}`, }], }; } catch (error) { throw new Error(`Failed to parse query string: ${error}`); } } async buildQueryString(args) { try { const params = new URLSearchParams(); Object.entries(args.parameters).forEach(([key, value]) => { params.append(key, String(value)); }); const queryString = params.toString(); return { content: [{ type: 'text', text: queryString ? `?${queryString}` : 'No parameters provided', }], }; } catch (error) { throw new Error(`Failed to build query string: ${error}`); } } async extractDomain(args) { try { const url = new url_1.URL(args.url); const hostname = url.hostname; const parts = hostname.split('.'); let domain = ''; let subdomain = ''; if (parts.length >= 2) { domain = parts.slice(-2).join('.'); if (parts.length > 2) { subdomain = parts.slice(0, -2).join('.'); } } else { domain = hostname; } const result = `Domain Information: - Full Hostname: ${hostname} - Domain: ${domain} - Subdomain: ${subdomain || 'None'} - Protocol: ${url.protocol} - Port: ${url.port || 'Default'}`; return { content: [{ type: 'text', text: result, }], }; } catch (error) { throw new Error(`Failed to extract domain: ${error}`); } } async checkUrlReachable(url) { return new Promise((resolve) => { try { const urlObj = new url_1.URL(url); const isHttps = urlObj.protocol === 'https:'; const client = isHttps ? https : http; const req = client.request({ hostname: urlObj.hostname, port: urlObj.port, path: '/', method: 'HEAD', timeout: 5000, }, (res) => { resolve(res.statusCode !== undefined && res.statusCode < 400); }); req.on('error', () => resolve(false)); req.on('timeout', () => { req.destroy(); resolve(false); }); req.end(); } catch { resolve(false); } }); } } exports.WebUtils = WebUtils; exports.webUtils = new WebUtils();