Skip to content

Commit b50b249

Browse files
authored
Merge pull request #3 from cforge42/dev
update index.ts
2 parents 0fd6480 + 84b8f1e commit b50b249

File tree

1 file changed

+73
-1
lines changed

1 file changed

+73
-1
lines changed

src/index.ts

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from "@modelcontextprotocol/sdk/types.js";
99
import { randomBytes } from 'crypto';
1010
import { join } from 'path';
11-
import { mkdir, writeFile, appendFile, readFile, access, unlink } from 'fs/promises';
11+
import { mkdir, writeFile, appendFile, readFile, access, unlink, readdir } from 'fs/promises';
1212
import { exec, ExecOptions } from 'child_process';
1313
import { promisify } from 'util';
1414
import { platform } from 'os';
@@ -323,6 +323,37 @@ async function readCodeFile(filePath: string) {
323323
}
324324
}
325325

326+
/**
327+
* List files in the code storage directory (non-recursive).
328+
*/
329+
async function listCodeFiles() {
330+
try {
331+
const files = await readdir(CODE_STORAGE_DIR, { withFileTypes: false });
332+
return makeResponse({ status: 'success', files }, false);
333+
} catch (error) {
334+
return makeResponse({ status: 'error', error: error instanceof Error ? error.message : String(error) }, true);
335+
}
336+
}
337+
338+
/**
339+
* Delete a code file (best-effort, checks path)
340+
*/
341+
async function deleteCodeFile(filePath: string) {
342+
try {
343+
// Only allow deletion of files inside CODE_STORAGE_DIR for safety
344+
if (!filePath.startsWith(CODE_STORAGE_DIR)) {
345+
return makeResponse({ status: 'error', error: 'file_path must be inside CODE_STORAGE_DIR' }, true);
346+
}
347+
348+
await access(filePath);
349+
await unlink(filePath);
350+
351+
return makeResponse({ status: 'success', message: 'File deleted', file_path: filePath }, false);
352+
} catch (error) {
353+
return makeResponse({ status: 'error', error: error instanceof Error ? error.message : String(error), file_path: filePath }, true);
354+
}
355+
}
356+
326357
/**
327358
* Install dependencies using the appropriate package manager
328359
*/
@@ -631,6 +662,28 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
631662
required: ["file_path"]
632663
}
633664
},
665+
{
666+
name: "list_code_files",
667+
description: "List files in the code storage directory",
668+
inputSchema: {
669+
type: "object",
670+
properties: {}
671+
}
672+
},
673+
{
674+
name: "delete_code_file",
675+
description: "Delete a code file from the storage directory (must be inside CODE_STORAGE_DIR)",
676+
inputSchema: {
677+
type: "object",
678+
properties: {
679+
file_path: {
680+
type: "string",
681+
description: "Full path to the file to delete"
682+
}
683+
},
684+
required: ["file_path"]
685+
}
686+
},
634687

635688
{
636689
name: "install_dependencies",
@@ -874,6 +927,25 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
874927
}]
875928
};
876929
}
930+
931+
case "list_code_files": {
932+
const result = await listCodeFiles();
933+
return {
934+
content: [{ type: 'text', text: result.text, isError: result.isError }]
935+
};
936+
}
937+
938+
case "delete_code_file": {
939+
const args = request.params.arguments as { file_path?: string };
940+
if (!args?.file_path) {
941+
throw new Error("File path is required");
942+
}
943+
944+
const result = await deleteCodeFile(args.file_path);
945+
return {
946+
content: [{ type: 'text', text: result.text, isError: result.isError }]
947+
};
948+
}
877949

878950
case "install_dependencies": {
879951
const args = request.params.arguments as InstallDependenciesArgs;

0 commit comments

Comments
 (0)