Skip to content

Commit 5151aff

Browse files
authored
Merge pull request #102 from QuantGeekDev/fix/tool-loading
fix: update tool loader pathing
2 parents 676fb44 + f048ecd commit 5151aff

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

.cursor/rules/mcp-framework.mdc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,6 @@ server.start();
112112

113113
```
114114
</example>
115+
116+
It's important to keep in mind that mcp-framework is used exclusively as a dependency in other repositories - just like express js would be.
117+
This means that we are running it from node_modules within the repo - this impacts how relative directories work

src/core/MCPServer.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,6 @@ export class MCPServer {
9595
this.toolLoader = new ToolLoader(this.basePath);
9696
this.promptLoader = new PromptLoader(this.basePath);
9797
this.resourceLoader = new ResourceLoader(this.basePath);
98-
99-
this.server = new Server(
100-
{ name: this.serverName, version: this.serverVersion },
101-
{ capabilities: this.capabilities }
102-
);
103-
logger.debug(`SDK Server instance created.`);
10498
}
10599

106100
private resolveBasePath(configPath?: string): string {
@@ -363,9 +357,7 @@ export class MCPServer {
363357
logger.debug('Resources capability enabled');
364358
}
365359

366-
(this.server as any).updateCapabilities?.(this.capabilities);
367-
logger.debug(`Capabilities updated: ${JSON.stringify(this.capabilities)}`);
368-
360+
logger.debug(`Capabilities detected: ${JSON.stringify(this.capabilities)}`);
369361
return this.capabilities;
370362
}
371363

@@ -406,6 +398,7 @@ export class MCPServer {
406398
const sdkVersion = this.getSdkVersion();
407399
logger.info(`Starting MCP server: (Framework: ${frameworkVersion}, SDK: ${sdkVersion})...`);
408400

401+
// Load all tools, prompts, and resources first
409402
const tools = await this.toolLoader.loadTools();
410403
this.toolsMap = new Map(tools.map((tool: ToolProtocol) => [tool.name, tool]));
411404

@@ -432,6 +425,14 @@ export class MCPServer {
432425
await this.detectCapabilities();
433426
logger.info(`Capabilities detected: ${JSON.stringify(this.capabilities)}`);
434427

428+
this.server = new Server(
429+
{ name: this.serverName, version: this.serverVersion },
430+
{ capabilities: this.capabilities }
431+
);
432+
logger.debug(
433+
`SDK Server instance created with capabilities: ${JSON.stringify(this.capabilities)}`
434+
);
435+
435436
this.setupHandlers();
436437

437438
this.transport = this.createTransport();

src/loaders/toolLoader.ts

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ToolProtocol } from '../tools/BaseTool.js';
22
import { join, dirname } from 'path';
3-
import { promises as fs } from 'fs';
3+
import { promises as fs, existsSync } from 'fs';
44
import { logger } from '../core/Logger.js';
55
import { discoverFilesRecursively, hasValidFiles } from '../utils/fileDiscovery.js';
66

@@ -9,23 +9,43 @@ export class ToolLoader {
99
private readonly EXCLUDED_FILES = ['BaseTool.js', '*.test.js', '*.spec.js'];
1010

1111
constructor(basePath?: string) {
12-
if (basePath) {
13-
// If basePath is provided, it should be the directory containing the tools folder
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) {
1419
this.TOOLS_DIR = join(basePath, 'tools');
20+
logger.debug(`Using provided base path for tools: ${this.TOOLS_DIR}`);
1521
} else {
16-
// For backwards compatibility, use the old behavior with process.argv[1]
22+
// For backwards compatibility
1723
const mainModulePath = process.argv[1];
18-
this.TOOLS_DIR = join(dirname(mainModulePath), 'tools');
24+
const moduleDir = dirname(mainModulePath);
25+
26+
if (moduleDir.endsWith('dist')) {
27+
this.TOOLS_DIR = join(moduleDir, 'tools');
28+
} else {
29+
this.TOOLS_DIR = join(moduleDir, 'dist', 'tools');
30+
}
31+
logger.debug(`Using module path for tools: ${this.TOOLS_DIR}`);
1932
}
20-
logger.debug(`Initialized ToolLoader with directory: ${this.TOOLS_DIR}`);
2133
}
2234

2335
async hasTools(): Promise<boolean> {
2436
try {
25-
return await hasValidFiles(this.TOOLS_DIR, {
37+
const hasDistTools = await hasValidFiles(this.TOOLS_DIR, {
2638
extensions: ['.js'],
2739
excludePatterns: this.EXCLUDED_FILES,
2840
});
41+
42+
if (hasDistTools) {
43+
logger.debug(`Found tools in ${this.TOOLS_DIR}`);
44+
return true;
45+
}
46+
47+
logger.debug(`No tools found in ${this.TOOLS_DIR}`);
48+
return false;
2949
} catch (error) {
3050
logger.debug(`No tools directory found: ${(error as Error).message}`);
3151
return false;

0 commit comments

Comments
 (0)