Implement fix-dependencies MCP command for automatically fixing invalid dependencies

This commit is contained in:
Eyal Toledano
2025-03-31 14:01:49 -04:00
parent 535fb5be71
commit d06e45bf12
5 changed files with 97 additions and 2 deletions

View File

@@ -0,0 +1,55 @@
/**
* Direct function wrapper for fixDependenciesCommand
*/
import { fixDependenciesCommand } from '../../../../scripts/modules/dependency-manager.js';
import { findTasksJsonPath } from '../utils/path-utils.js';
import fs from 'fs';
/**
* Fix invalid dependencies in tasks.json automatically
* @param {Object} args - Function arguments
* @param {string} [args.file] - Path to the tasks file
* @param {string} [args.projectRoot] - Project root directory
* @param {Object} log - Logger object
* @returns {Promise<{success: boolean, data?: Object, error?: {code: string, message: string}}>}
*/
export async function fixDependenciesDirect(args, log) {
try {
log.info(`Fixing invalid dependencies in tasks...`);
// Determine the tasks file path
const tasksPath = args.file || await findTasksJsonPath(args.projectRoot);
// Verify the file exists
if (!fs.existsSync(tasksPath)) {
return {
success: false,
error: {
code: 'FILE_NOT_FOUND',
message: `Tasks file not found at ${tasksPath}`
}
};
}
// Call the original command function
await fixDependenciesCommand(tasksPath);
return {
success: true,
data: {
message: 'Dependencies fixed successfully',
tasksPath
}
};
} catch (error) {
log.error(`Error fixing dependencies: ${error.message}`);
return {
success: false,
error: {
code: 'FIX_DEPENDENCIES_ERROR',
message: error.message
}
};
}
}

View File

@@ -24,6 +24,7 @@ import { clearSubtasksDirect } from './direct-functions/clear-subtasks.js';
import { expandAllTasksDirect } from './direct-functions/expand-all-tasks.js';
import { removeDependencyDirect } from './direct-functions/remove-dependency.js';
import { validateDependenciesDirect } from './direct-functions/validate-dependencies.js';
import { fixDependenciesDirect } from './direct-functions/fix-dependencies.js';
// Re-export utility functions
export { findTasksJsonPath } from './utils/path-utils.js';
@@ -48,7 +49,8 @@ export const directFunctions = new Map([
['clearSubtasksDirect', clearSubtasksDirect],
['expandAllTasksDirect', expandAllTasksDirect],
['removeDependencyDirect', removeDependencyDirect],
['validateDependenciesDirect', validateDependenciesDirect]
['validateDependenciesDirect', validateDependenciesDirect],
['fixDependenciesDirect', fixDependenciesDirect]
]);
// Re-export all direct function implementations
@@ -71,5 +73,6 @@ export {
clearSubtasksDirect,
expandAllTasksDirect,
removeDependencyDirect,
validateDependenciesDirect
validateDependenciesDirect,
fixDependenciesDirect
};

View File

@@ -0,0 +1,34 @@
/**
* tools/fix-dependencies.js
* Tool for automatically fixing invalid task dependencies
*/
import { z } from "zod";
import {
handleApiResult,
createErrorResponse
} from "./utils.js";
import { fixDependenciesDirect } from "../core/task-master-core.js";
/**
* Register the fixDependencies tool with the MCP server
* @param {Object} server - FastMCP server instance
*/
export function registerFixDependenciesTool(server) {
server.addTool({
name: "fix_dependencies",
description: "Fix invalid dependencies in tasks automatically",
parameters: z.object({
file: z.string().optional().describe("Path to the tasks file"),
projectRoot: z.string().optional().describe("Root directory of the project (default: current working directory)")
}),
handler: async ({ file, projectRoot }, { logger }) => {
try {
const result = await fixDependenciesDirect({ file, projectRoot }, logger);
return handleApiResult(result);
} catch (error) {
return createErrorResponse(error);
}
}
});
}

View File

@@ -22,6 +22,7 @@ import { registerClearSubtasksTool } from "./clear-subtasks.js";
import { registerExpandAllTool } from "./expand-all.js";
import { registerRemoveDependencyTool } from "./remove-dependency.js";
import { registerValidateDependenciesTool } from "./validate-dependencies.js";
import { registerFixDependenciesTool } from "./fix-dependencies.js";
/**
* Register all Task Master tools with the MCP server
@@ -50,6 +51,7 @@ export function registerTaskMasterTools(server) {
registerExpandAllTool(server);
registerRemoveDependencyTool(server);
registerValidateDependenciesTool(server);
registerFixDependenciesTool(server);
logger.info("Successfully registered all Task Master tools");
} catch (error) {