Skip to content

Commit 5fb42e6

Browse files
committed
feat: add nested tool loading
1 parent effbb13 commit 5fb42e6

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

src/core/MCPServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class MCPServer {
102102
return configPath;
103103
}
104104
if (process.argv[1]) {
105-
return process.argv[1];
105+
return dirname(process.argv[1]);
106106
}
107107
return process.cwd();
108108
}

src/loaders/toolLoader.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,28 @@ export class ToolLoader {
99
private readonly EXCLUDED_FILES = ['BaseTool.js', '*.test.js', '*.spec.js'];
1010

1111
constructor(basePath?: string) {
12-
const projectRoot = process.cwd();
13-
const distToolsPath = join(projectRoot, 'dist', 'tools');
14-
15-
if (existsSync(distToolsPath)) {
16-
this.TOOLS_DIR = distToolsPath;
17-
logger.debug(`Using project's dist/tools directory: ${this.TOOLS_DIR}`);
18-
} else if (basePath) {
12+
if (basePath) {
1913
this.TOOLS_DIR = join(basePath, 'tools');
2014
logger.debug(`Using provided base path for tools: ${this.TOOLS_DIR}`);
2115
} else {
22-
// For backwards compatibility
23-
const mainModulePath = process.argv[1];
24-
const moduleDir = dirname(mainModulePath);
16+
const projectRoot = process.cwd();
17+
const distToolsPath = join(projectRoot, 'dist', 'tools');
2518

26-
if (moduleDir.endsWith('dist')) {
27-
this.TOOLS_DIR = join(moduleDir, 'tools');
19+
if (existsSync(distToolsPath)) {
20+
this.TOOLS_DIR = distToolsPath;
21+
logger.debug(`Using project's dist/tools directory: ${this.TOOLS_DIR}`);
2822
} else {
29-
this.TOOLS_DIR = join(moduleDir, 'dist', 'tools');
23+
// For backwards compatibility
24+
const mainModulePath = process.argv[1];
25+
const moduleDir = dirname(mainModulePath);
26+
27+
if (moduleDir.endsWith('dist')) {
28+
this.TOOLS_DIR = join(moduleDir, 'tools');
29+
} else {
30+
this.TOOLS_DIR = join(moduleDir, 'dist', 'tools');
31+
}
32+
logger.debug(`Using module path for tools: ${this.TOOLS_DIR}`);
3033
}
31-
logger.debug(`Using module path for tools: ${this.TOOLS_DIR}`);
3234
}
3335
}
3436

@@ -95,17 +97,13 @@ export class ToolLoader {
9597
const importPath = `file://${fullPath}`;
9698
const module = await import(importPath);
9799

98-
// Handle both CommonJS (module.default) and ES6 (module.default) exports
99100
let ToolClass = module.default;
100101

101-
// If no default export, try the module itself (CommonJS style)
102102
if (!ToolClass && typeof module === 'function') {
103103
ToolClass = module;
104104
}
105105

106-
// If still no class, try common export patterns
107106
if (!ToolClass) {
108-
// Try named exports or direct module.exports
109107
const keys = Object.keys(module);
110108
if (keys.length === 1) {
111109
ToolClass = module[keys[0]];

0 commit comments

Comments
 (0)