UNPKG

@xcud/remote-commander

Version:

MCP server for remote file operations via REST API

177 lines (176 loc) 5.04 kB
import { httpClient } from '../http-client.js'; /** * Placeholder for path validation - now handled by HTTP client */ export async function validatePath(inputPath) { // Path validation is now handled by the HTTP client return inputPath; } /** * Read file from URL (keep original functionality for URLs) */ export async function readFileFromUrl(url, timeout = 30000) { // For URLs, we still use the original implementation // TODO: Could proxy through API if needed throw new Error('URL reading not implemented in remote commander. Please use local paths only.'); } /** * Read file from remote server via API */ export async function readFileFromDisk(filePath, offset = 0, length = 1000) { await httpClient.init(); const response = await httpClient.readFile({ path: filePath, offset, length }); if (!response.success) { throw new Error(response.error || 'Failed to read file'); } return { content: response.data || '', mimeType: 'text/plain', isImage: false }; } /** * Main read file function - routes to remote API */ export async function readFile(path, isUrl = false, offset = 0, length = 1000) { if (isUrl) { return readFileFromUrl(path); } return readFileFromDisk(path, offset, length); } /** * Internal read file function (alias for readFile) */ export async function readFileInternal(filePath, offset = 0, length = 1000) { const result = await readFile(filePath, false, offset, length); return result.content; } /** * Write file via remote API */ export async function writeFile(path, content, mode = 'rewrite') { await httpClient.init(); const response = await httpClient.writeFile({ path, content, mode }); if (!response.success) { throw new Error(response.error || 'Failed to write file'); } } /** * Read multiple files via remote API */ export async function readMultipleFiles(paths) { const results = []; // Read files sequentially for now // TODO: Could optimize with parallel requests for (const filePath of paths) { try { const fileResult = await readFile(filePath); results.push({ path: filePath, content: fileResult.content, mimeType: fileResult.mimeType, isImage: fileResult.isImage }); } catch (error) { results.push({ path: filePath, error: error instanceof Error ? error.message : 'Unknown error' }); } } return results; } /** * Create directory via remote API */ export async function createDirectory(path) { await httpClient.init(); const response = await httpClient.createDirectory({ path }); if (!response.success) { throw new Error(response.error || 'Failed to create directory'); } } /** * List directory via remote API */ export async function listDirectory(path) { await httpClient.init(); const response = await httpClient.listDirectory({ path }); if (!response.success) { throw new Error(response.error || 'Failed to list directory'); } return response.data || []; } /** * Move/rename file via remote API */ export async function moveFile(source, destination) { // TODO: Implement move file endpoint in API throw new Error('Move file not yet implemented in remote commander'); } /** * Search files via remote API */ export async function searchFiles(path, pattern, timeoutMs = 30000) { await httpClient.init(); const response = await httpClient.searchFiles({ path, pattern, timeoutMs }); if (!response.success) { throw new Error(response.error || 'Failed to search files'); } return response.data || []; } /** * Get file info via remote API */ export async function getFileInfo(path) { await httpClient.init(); const response = await httpClient.getFileInfo({ path }); if (!response.success) { throw new Error(response.error || 'Failed to get file info'); } return response.data; } /** * Search code via remote API */ export async function searchCode(path, pattern, options = {}) { await httpClient.init(); const response = await httpClient.searchCode({ path, pattern, ...options }); if (!response.success) { throw new Error(response.error || 'Failed to search code'); } return response.data; } /** * Edit block via remote API */ export async function editBlock(filePath, oldString, newString, expectedReplacements = 1) { await httpClient.init(); const response = await httpClient.editBlock({ file_path: filePath, old_string: oldString, new_string: newString, expected_replacements: expectedReplacements }); if (!response.success) { throw new Error(response.error || 'Failed to edit block'); } return response.data; }