@wonderwhy-er/desktop-commander 0.1.34 → 0.1.35
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +105 -36
- package/dist/command-manager.d.ts +1 -7
- package/dist/command-manager.js +31 -50
- package/dist/config-manager.d.ts +27 -16
- package/dist/config-manager.js +109 -191
- package/dist/config.js +8 -4
- package/dist/error-handlers.js +4 -0
- package/dist/handlers/edit-search-handlers.js +9 -13
- package/dist/handlers/filesystem-handlers.d.ts +0 -4
- package/dist/handlers/filesystem-handlers.js +10 -18
- package/dist/handlers/index.d.ts +0 -1
- package/dist/handlers/index.js +0 -1
- package/dist/index.js +18 -3
- package/dist/sandbox/index.d.ts +9 -0
- package/dist/sandbox/index.js +50 -0
- package/dist/sandbox/mac-sandbox.d.ts +19 -0
- package/dist/sandbox/mac-sandbox.js +174 -0
- package/dist/server.js +152 -175
- package/dist/setup-claude-server.js +98 -49
- package/dist/terminal-manager.d.ts +1 -1
- package/dist/terminal-manager.js +20 -3
- package/dist/tools/config.d.ts +0 -58
- package/dist/tools/config.js +44 -107
- package/dist/tools/debug-path.d.ts +1 -0
- package/dist/tools/debug-path.js +44 -0
- package/dist/tools/edit.js +8 -5
- package/dist/tools/execute.js +4 -4
- package/dist/tools/filesystem-fixed.d.ts +22 -0
- package/dist/tools/filesystem-fixed.js +176 -0
- package/dist/tools/filesystem.d.ts +4 -6
- package/dist/tools/filesystem.js +101 -80
- package/dist/tools/schemas.d.ts +15 -14
- package/dist/tools/schemas.js +10 -6
- package/dist/tools/search.js +3 -3
- package/dist/utils.d.ts +5 -0
- package/dist/utils.js +92 -32
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -2
package/dist/config-manager.js
CHANGED
|
@@ -1,242 +1,160 @@
|
|
|
1
1
|
import fs from 'fs/promises';
|
|
2
2
|
import path from 'path';
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
3
|
+
import { existsSync } from 'fs';
|
|
4
|
+
import { mkdir } from 'fs/promises';
|
|
5
|
+
import os from 'os';
|
|
5
6
|
/**
|
|
6
|
-
*
|
|
7
|
+
* Singleton config manager for the server
|
|
7
8
|
*/
|
|
8
|
-
|
|
9
|
+
class ConfigManager {
|
|
9
10
|
constructor() {
|
|
10
11
|
this.config = {};
|
|
11
12
|
this.initialized = false;
|
|
13
|
+
// Get user's home directory
|
|
14
|
+
const homeDir = os.homedir();
|
|
15
|
+
// Define config directory and file paths
|
|
16
|
+
const configDir = path.join(homeDir, '.claude-server-commander');
|
|
17
|
+
this.configPath = path.join(configDir, 'config.json');
|
|
12
18
|
}
|
|
13
19
|
/**
|
|
14
|
-
*
|
|
20
|
+
* Initialize configuration - load from disk or create default
|
|
15
21
|
*/
|
|
16
|
-
async
|
|
22
|
+
async init() {
|
|
23
|
+
if (this.initialized)
|
|
24
|
+
return;
|
|
17
25
|
try {
|
|
18
|
-
console.error(`Loading config from ${CONFIG_FILE}...`);
|
|
19
|
-
console.error(`Current working directory: ${process.cwd()}`);
|
|
20
|
-
console.error(`Absolute config path: ${path.resolve(CONFIG_FILE)}`);
|
|
21
26
|
// Ensure config directory exists
|
|
22
|
-
const configDir = path.dirname(
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
await fs.mkdir(configDir, { recursive: true });
|
|
26
|
-
console.error(`Config directory ready: ${configDir}`);
|
|
27
|
-
}
|
|
28
|
-
catch (mkdirError) {
|
|
29
|
-
console.error(`Error creating config directory: ${mkdirError.message}`);
|
|
30
|
-
// Continue if directory already exists
|
|
31
|
-
if (mkdirError.code !== 'EEXIST') {
|
|
32
|
-
throw mkdirError;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
// Check if the directory exists and is writable
|
|
36
|
-
try {
|
|
37
|
-
const dirStats = await fs.stat(configDir);
|
|
38
|
-
console.error(`Config directory exists: ${dirStats.isDirectory()}`);
|
|
39
|
-
await fs.access(configDir, fs.constants.W_OK);
|
|
40
|
-
console.error(`Directory ${configDir} is writable`);
|
|
41
|
-
}
|
|
42
|
-
catch (dirError) {
|
|
43
|
-
console.error(`Config directory check error: ${dirError.message}`);
|
|
44
|
-
}
|
|
45
|
-
// Check file permissions
|
|
46
|
-
try {
|
|
47
|
-
const fileStats = await fs.stat(CONFIG_FILE).catch(() => null);
|
|
48
|
-
if (fileStats) {
|
|
49
|
-
console.error(`Config file exists, permissions: ${fileStats.mode.toString(8)}`);
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
console.error('Config file does not exist, will create');
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
catch (statError) {
|
|
56
|
-
console.error(`Error checking file stats: ${statError.message}`);
|
|
27
|
+
const configDir = path.dirname(this.configPath);
|
|
28
|
+
if (!existsSync(configDir)) {
|
|
29
|
+
await mkdir(configDir, { recursive: true });
|
|
57
30
|
}
|
|
58
|
-
|
|
31
|
+
// Check if config file exists
|
|
59
32
|
try {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
else {
|
|
69
|
-
throw readError;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
if (configData) {
|
|
73
|
-
try {
|
|
74
|
-
this.config = JSON.parse(configData);
|
|
75
|
-
console.error(`Config parsed successfully: ${JSON.stringify(this.config, null, 2)}`);
|
|
76
|
-
}
|
|
77
|
-
catch (parseError) {
|
|
78
|
-
console.error(`Failed to parse config JSON: ${parseError.message}`);
|
|
79
|
-
// If file exists but has invalid JSON, use default empty config
|
|
80
|
-
this.config = {};
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
else {
|
|
84
|
-
// If file doesn't exist, use default empty config
|
|
85
|
-
this.config = {};
|
|
86
|
-
}
|
|
87
|
-
this.initialized = true;
|
|
88
|
-
// Create default config file if it doesn't exist
|
|
89
|
-
if (!configData) {
|
|
90
|
-
console.error('Creating default config file');
|
|
33
|
+
await fs.access(this.configPath);
|
|
34
|
+
// Load existing config
|
|
35
|
+
const configData = await fs.readFile(this.configPath, 'utf8');
|
|
36
|
+
this.config = JSON.parse(configData);
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
// Config file doesn't exist, create default
|
|
40
|
+
this.config = this.getDefaultConfig();
|
|
91
41
|
await this.saveConfig();
|
|
92
42
|
}
|
|
43
|
+
this.initialized = true;
|
|
93
44
|
}
|
|
94
45
|
catch (error) {
|
|
95
|
-
console.error(
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
this.
|
|
99
|
-
this.initialized = true; // Mark as initialized even with empty config
|
|
46
|
+
console.error('Failed to initialize config:', error);
|
|
47
|
+
// Fall back to default config in memory
|
|
48
|
+
this.config = this.getDefaultConfig();
|
|
49
|
+
this.initialized = true;
|
|
100
50
|
}
|
|
101
|
-
return this.config;
|
|
102
51
|
}
|
|
103
52
|
/**
|
|
104
|
-
*
|
|
53
|
+
* Alias for init() to maintain backward compatibility
|
|
54
|
+
*/
|
|
55
|
+
async loadConfig() {
|
|
56
|
+
return this.init();
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Create default configuration
|
|
60
|
+
*/
|
|
61
|
+
getDefaultConfig() {
|
|
62
|
+
return {
|
|
63
|
+
blockedCommands: [
|
|
64
|
+
// Disk and partition management
|
|
65
|
+
"mkfs", // Create a filesystem on a device
|
|
66
|
+
"format", // Format a storage device (cross-platform)
|
|
67
|
+
"mount", // Mount a filesystem
|
|
68
|
+
"umount", // Unmount a filesystem
|
|
69
|
+
"fdisk", // Manipulate disk partition tables
|
|
70
|
+
"dd", // Convert and copy files, can write directly to disks
|
|
71
|
+
"parted", // Disk partition manipulator
|
|
72
|
+
"diskpart", // Windows disk partitioning utility
|
|
73
|
+
// System administration and user management
|
|
74
|
+
"sudo", // Execute command as superuser
|
|
75
|
+
"su", // Substitute user identity
|
|
76
|
+
"passwd", // Change user password
|
|
77
|
+
"adduser", // Add a user to the system
|
|
78
|
+
"useradd", // Create a new user
|
|
79
|
+
"usermod", // Modify user account
|
|
80
|
+
"groupadd", // Create a new group
|
|
81
|
+
"chsh", // Change login shell
|
|
82
|
+
"visudo", // Edit the sudoers file
|
|
83
|
+
// System control
|
|
84
|
+
"shutdown", // Shutdown the system
|
|
85
|
+
"reboot", // Restart the system
|
|
86
|
+
"halt", // Stop the system
|
|
87
|
+
"poweroff", // Power off the system
|
|
88
|
+
"init", // Change system runlevel
|
|
89
|
+
// Network and security
|
|
90
|
+
"iptables", // Linux firewall administration
|
|
91
|
+
"firewall", // Generic firewall command
|
|
92
|
+
"netsh", // Windows network configuration
|
|
93
|
+
// Windows system commands
|
|
94
|
+
"sfc", // System File Checker
|
|
95
|
+
"bcdedit", // Boot Configuration Data editor
|
|
96
|
+
"reg", // Windows registry editor
|
|
97
|
+
"net", // Network/user/service management
|
|
98
|
+
"sc", // Service Control manager
|
|
99
|
+
"runas", // Execute command as another user
|
|
100
|
+
"cipher", // Encrypt/decrypt files or wipe data
|
|
101
|
+
"takeown" // Take ownership of files
|
|
102
|
+
],
|
|
103
|
+
defaultShell: os.platform() === 'win32' ? 'powershell.exe' : 'bash',
|
|
104
|
+
allowedDirectories: []
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Save config to disk
|
|
105
109
|
*/
|
|
106
110
|
async saveConfig() {
|
|
107
111
|
try {
|
|
108
|
-
|
|
109
|
-
console.error(`Current working directory: ${process.cwd()}`);
|
|
110
|
-
console.error(`Absolute config path: ${path.resolve(CONFIG_FILE)}`);
|
|
111
|
-
// Always try to create the config directory first
|
|
112
|
-
const configDir = path.dirname(CONFIG_FILE);
|
|
113
|
-
try {
|
|
114
|
-
console.error(`Ensuring config directory exists: ${configDir}`);
|
|
115
|
-
await fs.mkdir(configDir, { recursive: true });
|
|
116
|
-
console.error(`Config directory ready: ${configDir}`);
|
|
117
|
-
}
|
|
118
|
-
catch (mkdirError) {
|
|
119
|
-
console.error(`Failed to create directory: ${mkdirError.message}`);
|
|
120
|
-
if (mkdirError.code !== 'EEXIST') {
|
|
121
|
-
throw mkdirError;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
// Check directory permissions
|
|
125
|
-
try {
|
|
126
|
-
await fs.access(configDir, fs.constants.W_OK);
|
|
127
|
-
console.error(`Directory ${configDir} is writable`);
|
|
128
|
-
}
|
|
129
|
-
catch (accessError) {
|
|
130
|
-
console.error(`Directory access error: ${accessError.message}`);
|
|
131
|
-
throw new Error(`Config directory is not writable: ${accessError.message}`);
|
|
132
|
-
}
|
|
133
|
-
const configJson = JSON.stringify(this.config, null, 2);
|
|
134
|
-
console.error(`Config to save: ${configJson}`);
|
|
135
|
-
try {
|
|
136
|
-
// Try to write the file with explicit encoding and permissions
|
|
137
|
-
await fs.writeFile(CONFIG_FILE, configJson, {
|
|
138
|
-
encoding: 'utf-8',
|
|
139
|
-
mode: 0o644 // Readable/writable by owner, readable by others
|
|
140
|
-
});
|
|
141
|
-
console.error('Config saved successfully');
|
|
142
|
-
}
|
|
143
|
-
catch (writeError) {
|
|
144
|
-
console.error(`Write file error: ${writeError.message}, code: ${writeError.code}, stack: ${writeError.stack}`);
|
|
145
|
-
throw writeError;
|
|
146
|
-
}
|
|
112
|
+
await fs.writeFile(this.configPath, JSON.stringify(this.config, null, 2), 'utf8');
|
|
147
113
|
}
|
|
148
114
|
catch (error) {
|
|
149
|
-
console.error(
|
|
150
|
-
|
|
151
|
-
throw new Error(`Failed to save configuration: ${error instanceof Error ? error.message : String(error)}`);
|
|
115
|
+
console.error('Failed to save config:', error);
|
|
116
|
+
throw error;
|
|
152
117
|
}
|
|
153
118
|
}
|
|
119
|
+
/**
|
|
120
|
+
* Get the entire config
|
|
121
|
+
*/
|
|
122
|
+
async getConfig() {
|
|
123
|
+
await this.init();
|
|
124
|
+
return { ...this.config };
|
|
125
|
+
}
|
|
154
126
|
/**
|
|
155
127
|
* Get a specific configuration value
|
|
156
128
|
*/
|
|
157
129
|
async getValue(key) {
|
|
158
|
-
|
|
159
|
-
console.error(`getValue for key "${key}" - loading config first`);
|
|
160
|
-
await this.loadConfig();
|
|
161
|
-
}
|
|
162
|
-
console.error(`Getting value for key "${key}": ${JSON.stringify(this.config[key])}`);
|
|
130
|
+
await this.init();
|
|
163
131
|
return this.config[key];
|
|
164
132
|
}
|
|
165
133
|
/**
|
|
166
134
|
* Set a specific configuration value
|
|
167
135
|
*/
|
|
168
136
|
async setValue(key, value) {
|
|
169
|
-
|
|
170
|
-
if (!this.initialized) {
|
|
171
|
-
console.error('setValue - loading config first');
|
|
172
|
-
await this.loadConfig();
|
|
173
|
-
}
|
|
137
|
+
await this.init();
|
|
174
138
|
this.config[key] = value;
|
|
175
139
|
await this.saveConfig();
|
|
176
140
|
}
|
|
177
|
-
/**
|
|
178
|
-
* Get the entire configuration object
|
|
179
|
-
*/
|
|
180
|
-
async getConfig() {
|
|
181
|
-
if (!this.initialized) {
|
|
182
|
-
console.error('getConfig - loading config first');
|
|
183
|
-
await this.loadConfig();
|
|
184
|
-
}
|
|
185
|
-
console.error(`Getting full config: ${JSON.stringify(this.config, null, 2)}`);
|
|
186
|
-
return { ...this.config }; // Return a copy to prevent untracked mutations
|
|
187
|
-
}
|
|
188
141
|
/**
|
|
189
142
|
* Update multiple configuration values at once
|
|
190
143
|
*/
|
|
191
|
-
async updateConfig(
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
console.error('updateConfig - loading config first');
|
|
195
|
-
await this.loadConfig();
|
|
196
|
-
}
|
|
197
|
-
this.config = {
|
|
198
|
-
...this.config,
|
|
199
|
-
...partialConfig
|
|
200
|
-
};
|
|
144
|
+
async updateConfig(updates) {
|
|
145
|
+
await this.init();
|
|
146
|
+
this.config = { ...this.config, ...updates };
|
|
201
147
|
await this.saveConfig();
|
|
202
148
|
return { ...this.config };
|
|
203
149
|
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
this.config =
|
|
209
|
-
this.
|
|
210
|
-
}
|
|
211
|
-
async loadConfig() {
|
|
212
|
-
console.error('Using memory-only configuration (no filesystem operations)');
|
|
213
|
-
return this.config;
|
|
214
|
-
}
|
|
215
|
-
async saveConfig() {
|
|
216
|
-
console.error('Memory-only configuration - changes will not persist after restart');
|
|
217
|
-
// No-op - we don't save to filesystem
|
|
218
|
-
return;
|
|
219
|
-
}
|
|
220
|
-
async getValue(key) {
|
|
221
|
-
console.error(`Getting memory value for key "${key}": ${JSON.stringify(this.config[key])}`);
|
|
222
|
-
return this.config[key];
|
|
223
|
-
}
|
|
224
|
-
async setValue(key, value) {
|
|
225
|
-
console.error(`Setting memory value for key "${key}": ${JSON.stringify(value)}`);
|
|
226
|
-
this.config[key] = value;
|
|
227
|
-
}
|
|
228
|
-
async getConfig() {
|
|
229
|
-
console.error(`Getting full memory config: ${JSON.stringify(this.config, null, 2)}`);
|
|
230
|
-
return { ...this.config };
|
|
231
|
-
}
|
|
232
|
-
async updateConfig(partialConfig) {
|
|
233
|
-
console.error(`Updating memory config with: ${JSON.stringify(partialConfig, null, 2)}`);
|
|
234
|
-
this.config = {
|
|
235
|
-
...this.config,
|
|
236
|
-
...partialConfig
|
|
237
|
-
};
|
|
150
|
+
/**
|
|
151
|
+
* Reset configuration to defaults
|
|
152
|
+
*/
|
|
153
|
+
async resetConfig() {
|
|
154
|
+
this.config = this.getDefaultConfig();
|
|
155
|
+
await this.saveConfig();
|
|
238
156
|
return { ...this.config };
|
|
239
157
|
}
|
|
240
158
|
}
|
|
241
|
-
// Export
|
|
159
|
+
// Export singleton instance
|
|
242
160
|
export const configManager = new ConfigManager();
|
package/dist/config.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import path from 'path';
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
import os from 'os';
|
|
3
|
+
// Use user's home directory for configuration files
|
|
4
|
+
const USER_HOME = os.homedir();
|
|
5
|
+
const CONFIG_DIR = path.join(USER_HOME, '.claude-server-commander');
|
|
6
|
+
// Paths relative to the config directory
|
|
7
|
+
export const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json');
|
|
8
|
+
export const LOG_FILE = path.join(CONFIG_DIR, 'server.log');
|
|
9
|
+
export const ERROR_LOG_FILE = path.join(CONFIG_DIR, 'error.log');
|
|
6
10
|
export const DEFAULT_COMMAND_TIMEOUT = 1000; // milliseconds
|
package/dist/error-handlers.js
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
+
import { capture } from "./utils.js";
|
|
1
2
|
/**
|
|
2
3
|
* Creates a standard error response for tools
|
|
3
4
|
* @param message The error message
|
|
4
5
|
* @returns A ServerResult with the error message
|
|
5
6
|
*/
|
|
6
7
|
export function createErrorResponse(message) {
|
|
8
|
+
capture('server_request_error', {
|
|
9
|
+
error: message
|
|
10
|
+
});
|
|
7
11
|
return {
|
|
8
12
|
content: [{ type: "text", text: `Error: ${message}` }],
|
|
9
13
|
isError: true,
|
|
@@ -1,24 +1,18 @@
|
|
|
1
1
|
import { parseEditBlock, performSearchReplace } from '../tools/edit.js';
|
|
2
2
|
import { searchTextInFiles } from '../tools/search.js';
|
|
3
3
|
import { EditBlockArgsSchema, SearchCodeArgsSchema } from '../tools/schemas.js';
|
|
4
|
-
import { withTimeout } from '../utils.js';
|
|
4
|
+
import { capture, withTimeout } from '../utils.js';
|
|
5
5
|
import { createErrorResponse } from '../error-handlers.js';
|
|
6
6
|
/**
|
|
7
7
|
* Handle edit_block command
|
|
8
8
|
*/
|
|
9
9
|
export async function handleEditBlock(args) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
return createErrorResponse(error);
|
|
15
|
-
}
|
|
16
|
-
return performSearchReplace(filePath, searchReplace);
|
|
17
|
-
}
|
|
18
|
-
catch (error) {
|
|
19
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
20
|
-
return createErrorResponse(errorMessage);
|
|
10
|
+
const parsed = EditBlockArgsSchema.parse(args);
|
|
11
|
+
const { filePath, searchReplace, error } = await parseEditBlock(parsed.blockContent);
|
|
12
|
+
if (error) {
|
|
13
|
+
return createErrorResponse(error);
|
|
21
14
|
}
|
|
15
|
+
return performSearchReplace(filePath, searchReplace);
|
|
22
16
|
}
|
|
23
17
|
/**
|
|
24
18
|
* Handle search_code command
|
|
@@ -50,7 +44,9 @@ export async function handleSearchCode(args) {
|
|
|
50
44
|
delete globalThis.currentSearchProcess;
|
|
51
45
|
}
|
|
52
46
|
catch (error) {
|
|
53
|
-
|
|
47
|
+
capture('server_request_error', {
|
|
48
|
+
error: 'Error terminating search process'
|
|
49
|
+
});
|
|
54
50
|
}
|
|
55
51
|
}
|
|
56
52
|
if (results.length === 0) {
|
|
@@ -31,7 +31,3 @@ export declare function handleSearchFiles(args: unknown): Promise<ServerResult>;
|
|
|
31
31
|
* Handle get_file_info command
|
|
32
32
|
*/
|
|
33
33
|
export declare function handleGetFileInfo(args: unknown): Promise<ServerResult>;
|
|
34
|
-
/**
|
|
35
|
-
* Handle list_allowed_directories command
|
|
36
|
-
*/
|
|
37
|
-
export declare function handleListAllowedDirectories(): ServerResult;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { readFile, readMultipleFiles, writeFile, createDirectory, listDirectory, moveFile, searchFiles, getFileInfo
|
|
1
|
+
import { readFile, readMultipleFiles, writeFile, createDirectory, listDirectory, moveFile, searchFiles, getFileInfo } from '../tools/filesystem.js';
|
|
2
2
|
import { withTimeout } from '../utils.js';
|
|
3
3
|
import { createErrorResponse } from '../error-handlers.js';
|
|
4
4
|
import { ReadFileArgsSchema, ReadMultipleFilesArgsSchema, WriteFileArgsSchema, CreateDirectoryArgsSchema, ListDirectoryArgsSchema, MoveFileArgsSchema, SearchFilesArgsSchema, GetFileInfoArgsSchema } from '../tools/schemas.js';
|
|
@@ -21,8 +21,7 @@ export async function handleReadFile(args) {
|
|
|
21
21
|
const HANDLER_TIMEOUT = 60000; // 60 seconds total operation timeout
|
|
22
22
|
const readFileOperation = async () => {
|
|
23
23
|
const parsed = ReadFileArgsSchema.parse(args);
|
|
24
|
-
|
|
25
|
-
const fileResult = await readFile(parsed.path, true, parsed.isUrl);
|
|
24
|
+
const fileResult = await readFile(parsed.path, parsed.isUrl);
|
|
26
25
|
if (fileResult.isImage) {
|
|
27
26
|
// For image files, return as an image content type
|
|
28
27
|
return {
|
|
@@ -47,9 +46,12 @@ export async function handleReadFile(args) {
|
|
|
47
46
|
}
|
|
48
47
|
};
|
|
49
48
|
// Execute with timeout at the handler level
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
49
|
+
const result = await withTimeout(readFileOperation(), HANDLER_TIMEOUT, 'Read file handler operation', null);
|
|
50
|
+
if (result == null) {
|
|
51
|
+
// Handles the impossible case where withTimeout resolves to null instead of throwing
|
|
52
|
+
throw new Error('Failed to read the file');
|
|
53
|
+
}
|
|
54
|
+
return result;
|
|
53
55
|
}
|
|
54
56
|
/**
|
|
55
57
|
* Handle read_multiple_files command
|
|
@@ -214,15 +216,5 @@ export async function handleGetFileInfo(args) {
|
|
|
214
216
|
return createErrorResponse(errorMessage);
|
|
215
217
|
}
|
|
216
218
|
}
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
*/
|
|
220
|
-
export function handleListAllowedDirectories() {
|
|
221
|
-
const directories = listAllowedDirectories();
|
|
222
|
-
return {
|
|
223
|
-
content: [{
|
|
224
|
-
type: "text",
|
|
225
|
-
text: `Allowed directories:\n${directories.join('\n')}`
|
|
226
|
-
}],
|
|
227
|
-
};
|
|
228
|
-
}
|
|
219
|
+
// The listAllowedDirectories function has been removed
|
|
220
|
+
// Use get_config to retrieve the allowedDirectories configuration
|
package/dist/handlers/index.d.ts
CHANGED
package/dist/handlers/index.js
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { FilteredStdioServerTransport } from './custom-stdio.js';
|
|
3
3
|
import { server } from './server.js';
|
|
4
|
-
import {
|
|
4
|
+
import { configManager } from './config-manager.js';
|
|
5
5
|
import { join, dirname } from 'path';
|
|
6
6
|
import { fileURLToPath, pathToFileURL } from 'url';
|
|
7
7
|
import { platform } from 'os';
|
|
@@ -81,12 +81,25 @@ async function runServer() {
|
|
|
81
81
|
process.exit(1);
|
|
82
82
|
});
|
|
83
83
|
capture('run_server_start');
|
|
84
|
-
|
|
85
|
-
|
|
84
|
+
try {
|
|
85
|
+
console.error("Loading configuration...");
|
|
86
|
+
await configManager.loadConfig();
|
|
87
|
+
console.error("Configuration loaded successfully");
|
|
88
|
+
}
|
|
89
|
+
catch (configError) {
|
|
90
|
+
console.error(`Failed to load configuration: ${configError instanceof Error ? configError.message : String(configError)}`);
|
|
91
|
+
console.error(configError instanceof Error && configError.stack ? configError.stack : 'No stack trace available');
|
|
92
|
+
console.error("Continuing with in-memory configuration only");
|
|
93
|
+
// Continue anyway - we'll use an in-memory config
|
|
94
|
+
}
|
|
95
|
+
console.error("Connecting server...");
|
|
86
96
|
await server.connect(transport);
|
|
97
|
+
console.error("Server connected successfully");
|
|
87
98
|
}
|
|
88
99
|
catch (error) {
|
|
89
100
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
101
|
+
console.error(`FATAL ERROR: ${errorMessage}`);
|
|
102
|
+
console.error(error instanceof Error && error.stack ? error.stack : 'No stack trace available');
|
|
90
103
|
process.stderr.write(JSON.stringify({
|
|
91
104
|
type: 'error',
|
|
92
105
|
timestamp: new Date().toISOString(),
|
|
@@ -100,6 +113,8 @@ async function runServer() {
|
|
|
100
113
|
}
|
|
101
114
|
runServer().catch(async (error) => {
|
|
102
115
|
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
116
|
+
console.error(`RUNTIME ERROR: ${errorMessage}`);
|
|
117
|
+
console.error(error instanceof Error && error.stack ? error.stack : 'No stack trace available');
|
|
103
118
|
process.stderr.write(JSON.stringify({
|
|
104
119
|
type: 'error',
|
|
105
120
|
timestamp: new Date().toISOString(),
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { CommandExecutionResult } from '../types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Platform detection and sandbox execution router
|
|
4
|
+
*/
|
|
5
|
+
export declare function executeSandboxedCommand(command: string, timeoutMs?: number, shell?: string): Promise<CommandExecutionResult>;
|
|
6
|
+
/**
|
|
7
|
+
* Check if sandboxed execution is available for the current platform
|
|
8
|
+
*/
|
|
9
|
+
export declare function isSandboxAvailable(): boolean;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import os from 'os';
|
|
2
|
+
import { executeSandboxedCommand as macExecuteSandboxedCommand } from './mac-sandbox.js';
|
|
3
|
+
import { configManager } from '../config-manager.js';
|
|
4
|
+
/**
|
|
5
|
+
* Platform detection and sandbox execution router
|
|
6
|
+
*/
|
|
7
|
+
export async function executeSandboxedCommand(command, timeoutMs = 30000, shell) {
|
|
8
|
+
// Get the allowed directories from config
|
|
9
|
+
const config = await configManager.getConfig();
|
|
10
|
+
const allowedDirectories = config.allowedDirectories || [os.homedir()];
|
|
11
|
+
const platform = os.platform();
|
|
12
|
+
// Platform-specific sandbox execution
|
|
13
|
+
switch (platform) {
|
|
14
|
+
case 'darwin': // macOS
|
|
15
|
+
try {
|
|
16
|
+
const result = await macExecuteSandboxedCommand(command, allowedDirectories, timeoutMs);
|
|
17
|
+
return {
|
|
18
|
+
pid: result.pid,
|
|
19
|
+
output: result.output,
|
|
20
|
+
isBlocked: result.isBlocked
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
console.error('Mac sandbox execution error:', error);
|
|
25
|
+
return {
|
|
26
|
+
pid: -1,
|
|
27
|
+
output: `Sandbox execution error: ${error instanceof Error ? error.message : String(error)}`,
|
|
28
|
+
isBlocked: false
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
// Add cases for other platforms when implemented
|
|
32
|
+
// case 'linux':
|
|
33
|
+
// case 'win32':
|
|
34
|
+
default:
|
|
35
|
+
// For unsupported platforms, return an error
|
|
36
|
+
return {
|
|
37
|
+
pid: -1,
|
|
38
|
+
output: `Sandbox execution not supported on ${platform}. Command was not executed: ${command}`,
|
|
39
|
+
isBlocked: false
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Check if sandboxed execution is available for the current platform
|
|
45
|
+
*/
|
|
46
|
+
export function isSandboxAvailable() {
|
|
47
|
+
const platform = os.platform();
|
|
48
|
+
// Currently only implemented for macOS
|
|
49
|
+
return platform === 'darwin';
|
|
50
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generate a temporary sandbox profile for macOS that restricts access to allowed directories
|
|
3
|
+
* @param allowedDirectories Array of directories that should be accessible
|
|
4
|
+
* @returns Path to the generated sandbox profile file
|
|
5
|
+
*/
|
|
6
|
+
export declare function generateSandboxProfile(allowedDirectories: string[]): Promise<string>;
|
|
7
|
+
/**
|
|
8
|
+
* Execute a command in a macOS sandbox with access restricted to allowed directories
|
|
9
|
+
* @param command Command to execute
|
|
10
|
+
* @param allowedDirectories Array of allowed directory paths
|
|
11
|
+
* @param options Additional execution options
|
|
12
|
+
* @returns Promise resolving to the execution result
|
|
13
|
+
*/
|
|
14
|
+
export declare function executeSandboxedCommand(command: string, allowedDirectories: string[], timeoutMs?: number): Promise<{
|
|
15
|
+
output: string;
|
|
16
|
+
exitCode: number | null;
|
|
17
|
+
isBlocked: boolean;
|
|
18
|
+
pid: number;
|
|
19
|
+
}>;
|