fix: Improve MCP server robustness and debugging

- Refactor  for more reliable project root detection, particularly when running within integrated environments like Cursor IDE. Includes deriving root from script path and avoiding fallback to '/'.
- Enhance error handling in :
    - Add detailed debug information (paths searched, CWD, etc.) to the error message when  is not found in the provided project root.
    - Improve clarity of error messages and potential solutions.
- Add verbose logging in  to trace session object content and the finally resolved project root path, aiding in debugging path-related issues.
- Add default values for  and  to the example  environment configuration.
This commit is contained in:
Eyal Toledano
2025-04-02 22:04:00 -04:00
parent 1a74b50658
commit a49a77d19f
28 changed files with 658 additions and 257 deletions

View File

@@ -6,7 +6,8 @@
import { z } from "zod";
import {
handleApiResult,
createErrorResponse
createErrorResponse,
getProjectRootFromSession
} from "./utils.js";
import { updateTasksDirect } from "../core/task-master-core.js";
@@ -17,10 +18,10 @@ import { updateTasksDirect } from "../core/task-master-core.js";
export function registerUpdateTool(server) {
server.addTool({
name: "update",
description: "Update tasks with ID >= specified ID based on the provided prompt",
description: "Update multiple upcoming tasks (with ID >= 'from' ID) based on new context or changes provided in the prompt.",
parameters: z.object({
from: z.union([z.number(), z.string()]).describe("Task ID from which to start updating"),
prompt: z.string().describe("Explanation of changes or new context"),
from: z.union([z.number(), z.string()]).describe("Task ID from which to start updating (inclusive)"),
prompt: z.string().describe("Explanation of changes or new context to apply"),
research: z.boolean().optional().describe("Use Perplexity AI for research-backed updates"),
file: z.string().optional().describe("Path to the tasks file"),
projectRoot: z
@@ -30,17 +31,31 @@ export function registerUpdateTool(server) {
"Root directory of the project (default: current working directory)"
),
}),
execute: async (args, { log }) => {
execute: async (args, { log, session, reportProgress }) => {
try {
log.info(`Updating tasks with args: ${JSON.stringify(args)}`);
await reportProgress({ progress: 0 });
// Call the direct function wrapper
const result = await updateTasksDirect(args, log);
let rootFolder = getProjectRootFromSession(session, log);
// Log result
log.info(`${result.success ? `Successfully updated tasks from ID ${args.from}` : 'Failed to update tasks'}`);
if (!rootFolder && args.projectRoot) {
rootFolder = args.projectRoot;
log.info(`Using project root from args as fallback: ${rootFolder}`);
}
const result = await updateTasksDirect({
projectRoot: rootFolder,
...args
}, log, { reportProgress, mcpLog: log, session});
await reportProgress({ progress: 100 });
if (result.success) {
log.info(`Successfully updated tasks from ID ${args.from}: ${result.data.message}`);
} else {
log.error(`Failed to update tasks: ${result.error?.message || 'Unknown error'}`);
}
// Use handleApiResult to format the response
return handleApiResult(result, log, 'Error updating tasks');
} catch (error) {
log.error(`Error in update tool: ${error.message}`);