refactor(mcp-server): Prioritize session roots for project path discovery

This commit refactors how the MCP server determines the project root directory, prioritizing the path provided by the client session (e.g., Cursor) for increased reliability and simplification.

Previously, project root discovery relied on a complex chain of fallbacks (environment variables, CWD searching, package path checks) within `findTasksJsonPath`. This could be brittle and less accurate when running within an integrated environment like Cursor.

Key changes:

- **Prioritize Session Roots:** MCP tools (`add-task`, `add-dependency`, etc.) now first attempt to extract the project root URI directly from `session.roots[0].uri`.

- **New Utility `getProjectRootFromSession`:** Added a utility function in `mcp-server/src/tools/utils.js` to encapsulate the logic for extracting and decoding the root URI from the session object.

- **Refactor MCP Tools:** Updated tools (`add-task.js`, `add-dependency.js`) to use `getProjectRootFromSession`.

- **Simplify `findTasksJsonPath`:** Prioritized `args.projectRoot`, removed checks for `TASK_MASTER_PROJECT_ROOT` env var and package directory fallback. Retained CWD search and cache check for CLI compatibility.

- **Fix `reportProgress` Usage:** Corrected parameters in `add-dependency.js`.

This change makes project root determination more robust for the MCP server while preserving discovery mechanisms for the standalone CLI.
This commit is contained in:
Eyal Toledano
2025-04-02 12:33:46 -04:00
parent 3af469b35f
commit e04c16cec6
7 changed files with 1621 additions and 61 deletions

71
mcp-test.js Normal file
View File

@@ -0,0 +1,71 @@
#!/usr/bin/env node
import { Config } from 'fastmcp';
import path from 'path';
import fs from 'fs';
// Log the current directory
console.error(`Current working directory: ${process.cwd()}`);
try {
console.error('Attempting to load FastMCP Config...');
// Check if .cursor/mcp.json exists
const mcpPath = path.join(process.cwd(), '.cursor', 'mcp.json');
console.error(`Checking if mcp.json exists at: ${mcpPath}`);
if (fs.existsSync(mcpPath)) {
console.error('mcp.json file found');
console.error(`File content: ${JSON.stringify(JSON.parse(fs.readFileSync(mcpPath, 'utf8')), null, 2)}`);
} else {
console.error('mcp.json file not found');
}
// Try to create Config
const config = new Config();
console.error('Config created successfully');
// Check if env property exists
if (config.env) {
console.error(`Config.env exists with keys: ${Object.keys(config.env).join(', ')}`);
// Print each env var value (careful with sensitive values)
for (const [key, value] of Object.entries(config.env)) {
if (key.includes('KEY')) {
console.error(`${key}: [value hidden]`);
} else {
console.error(`${key}: ${value}`);
}
}
} else {
console.error('Config.env does not exist');
}
} catch (error) {
console.error(`Error loading Config: ${error.message}`);
console.error(`Stack trace: ${error.stack}`);
}
// Log process.env to see if values from mcp.json were loaded automatically
console.error('\nChecking if process.env already has values from mcp.json:');
const envVars = [
'ANTHROPIC_API_KEY',
'PERPLEXITY_API_KEY',
'MODEL',
'PERPLEXITY_MODEL',
'MAX_TOKENS',
'TEMPERATURE',
'DEFAULT_SUBTASKS',
'DEFAULT_PRIORITY'
];
for (const varName of envVars) {
if (process.env[varName]) {
if (varName.includes('KEY')) {
console.error(`${varName}: [value hidden]`);
} else {
console.error(`${varName}: ${process.env[varName]}`);
}
} else {
console.error(`${varName}: not set`);
}
}