Allow filesystem to accept DOCKER_ROOT_WORKSPACE

This commit is contained in:
colinmcneil
2024-12-17 15:20:48 -05:00
committed by Jim Clark
parent 4e8a8d270a
commit bb0adaafd8
2 changed files with 17 additions and 8 deletions

View File

@@ -122,6 +122,7 @@ Note: you can provide sandboxed directories to the server by mounting them to `/
"--mount", "type=bind,src=/Users/username/Desktop,dst=/projects/Desktop",
"--mount", "type=bind,src=/path/to/other/allowed/dir,dst=/projects/other/allowed/dir,ro",
"--mount", "type=bind,src=/path/to/file.txt,dst=/projects/path/to/file.txt",
"--env", "DOCKER_ROOT_WORKSPACE=/projects",
"ai/mcp-filesystem"
]
}

View File

@@ -17,9 +17,20 @@ import { minimatch } from 'minimatch';
// Command line argument parsing
const args = process.argv.slice(2);
if (args.length === 0) {
console.error("Usage: mcp-server-filesystem <allowed-directory> [additional-directories...]");
process.exit(1);
let allowedDirectories: string[] = [];
if (process.env.DOCKER_ROOT_WORKSPACE) {
allowedDirectories = await fs.readdir(process.env.DOCKER_ROOT_WORKSPACE);
}
else {
if (args.length === 0) {
console.error("Usage: mcp-server-filesystem <allowed-directory> [additional-directories...]");
process.exit(1);
}
allowedDirectories = args.map(dir =>
normalizePath(path.resolve(expandHome(dir)))
);
}
// Normalize all paths consistently
@@ -35,12 +46,9 @@ function expandHome(filepath: string): string {
}
// Store allowed directories in normalized form
const allowedDirectories = args.map(dir =>
normalizePath(path.resolve(expandHome(dir)))
);
// Validate that all directories exist and are accessible
await Promise.all(args.map(async (dir) => {
await Promise.all(allowedDirectories.map(async (dir) => {
try {
const stats = await fs.stat(dir);
if (!stats.isDirectory()) {
@@ -644,4 +652,4 @@ async function runServer() {
runServer().catch((error) => {
console.error("Fatal error running server:", error);
process.exit(1);
});
});