UNPKG

onerios-mcp-server

Version:

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

201 lines (200 loc) 7.26 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.fileOperations = exports.FileOperations = void 0; const zod_1 = require("zod"); const fs = __importStar(require("fs/promises")); const path = __importStar(require("path")); const glob_1 = require("glob"); class FileOperations { constructor() { this.tools = [ { name: 'read_file', description: 'Read the contents of a file', inputSchema: zod_1.z.object({ path: zod_1.z.string().describe('Path to the file to read'), encoding: zod_1.z.string().optional().default('utf8').describe('File encoding'), }), }, { name: 'write_file', description: 'Write content to a file', inputSchema: zod_1.z.object({ path: zod_1.z.string().describe('Path to the file to write'), content: zod_1.z.string().describe('Content to write to the file'), encoding: zod_1.z.string().optional().default('utf8').describe('File encoding'), }), }, { name: 'list_directory', description: 'List contents of a directory', inputSchema: zod_1.z.object({ path: zod_1.z.string().describe('Path to the directory to list'), recursive: zod_1.z.boolean().optional().default(false).describe('List recursively'), }), }, { name: 'search_files', description: 'Search for files matching a pattern', inputSchema: zod_1.z.object({ pattern: zod_1.z.string().describe('Glob pattern to search for'), directory: zod_1.z.string().optional().default('.').describe('Directory to search in'), }), }, { name: 'file_exists', description: 'Check if a file or directory exists', inputSchema: zod_1.z.object({ path: zod_1.z.string().describe('Path to check'), }), }, ]; } getTools() { return this.tools; } hasHandler(name) { return this.tools.some(tool => tool.name === name); } async handleTool(name, args) { switch (name) { case 'read_file': return this.readFile(args); case 'write_file': return this.writeFile(args); case 'list_directory': return this.listDirectory(args); case 'search_files': return this.searchFiles(args); case 'file_exists': return this.fileExists(args); default: throw new Error(`Unknown tool: ${name}`); } } async readFile(args) { try { const content = await fs.readFile(args.path, args.encoding); return { content: [{ type: 'text', text: content, }], }; } catch (error) { throw new Error(`Failed to read file ${args.path}: ${error}`); } } async writeFile(args) { try { // Ensure directory exists const dir = path.dirname(args.path); await fs.mkdir(dir, { recursive: true }); await fs.writeFile(args.path, args.content, args.encoding); return { content: [{ type: 'text', text: `Successfully wrote to ${args.path}`, }], }; } catch (error) { throw new Error(`Failed to write file ${args.path}: ${error}`); } } async listDirectory(args) { try { if (args.recursive) { const files = await (0, glob_1.glob)('**/*', { cwd: args.path, dot: true }); return { content: [{ type: 'text', text: files.join('\n'), }], }; } else { const items = await fs.readdir(args.path, { withFileTypes: true }); const formatted = items.map(item => `${item.isDirectory() ? 'd' : 'f'} ${item.name}`).join('\n'); return { content: [{ type: 'text', text: formatted, }], }; } } catch (error) { throw new Error(`Failed to list directory ${args.path}: ${error}`); } } async searchFiles(args) { try { const files = await (0, glob_1.glob)(args.pattern, { cwd: args.directory }); return { content: [{ type: 'text', text: files.join('\n'), }], }; } catch (error) { throw new Error(`Failed to search files with pattern ${args.pattern}: ${error}`); } } async fileExists(args) { try { await fs.access(args.path); return { content: [{ type: 'text', text: `File exists: ${args.path}`, }], }; } catch { return { content: [{ type: 'text', text: `File does not exist: ${args.path}`, }], }; } } } exports.FileOperations = FileOperations; exports.fileOperations = new FileOperations();