Implement clear-subtasks MCP command for clearing subtasks from parent tasks

This commit is contained in:
Eyal Toledano
2025-03-31 13:07:42 -04:00
parent 0b7b395aa4
commit 995e95263c
7 changed files with 164 additions and 4 deletions

View File

@@ -14,4 +14,5 @@
- Implement add-subtask MCP command for adding subtasks to existing tasks
- Implement remove-subtask MCP command for removing subtasks from parent tasks
- Implement analyze-complexity MCP command for analyzing task complexity and generating recommendations
- Implement clear-subtasks MCP command for clearing subtasks from parent tasks
- Document MCP server naming conventions in architecture.mdc and mcp.mdc files (file names use kebab-case, direct functions use camelCase with Direct suffix, tool registration functions use camelCase with Tool suffix, and MCP tool names use snake_case)

View File

@@ -0,0 +1,102 @@
/**
* Direct function wrapper for clearSubtasks
*/
import { clearSubtasks } from '../../../../scripts/modules/task-manager.js';
import { findTasksJsonPath } from '../utils/path-utils.js';
import fs from 'fs';
/**
* Clear subtasks from specified tasks
* @param {Object} args - Function arguments
* @param {string} [args.id] - Task IDs (comma-separated) to clear subtasks from
* @param {boolean} [args.all] - Clear subtasks from all tasks
* @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 clearSubtasksDirect(args, log) {
try {
log.info(`Clearing subtasks with args: ${JSON.stringify(args)}`);
// Either id or all must be provided
if (!args.id && !args.all) {
return {
success: false,
error: {
code: 'INPUT_VALIDATION_ERROR',
message: 'Either task IDs with id parameter or all parameter must be provided'
}
};
}
// Find the tasks.json path
const tasksPath = findTasksJsonPath(args.file, args.projectRoot);
// Check if tasks.json exists
if (!fs.existsSync(tasksPath)) {
return {
success: false,
error: {
code: 'FILE_NOT_FOUND_ERROR',
message: `Tasks file not found at ${tasksPath}`
}
};
}
let taskIds;
// If all is specified, get all task IDs
if (args.all) {
log.info('Clearing subtasks from all tasks');
const data = JSON.parse(fs.readFileSync(tasksPath, 'utf8'));
if (!data || !data.tasks || data.tasks.length === 0) {
return {
success: false,
error: {
code: 'INPUT_VALIDATION_ERROR',
message: 'No valid tasks found in the tasks file'
}
};
}
taskIds = data.tasks.map(t => t.id).join(',');
} else {
// Use the provided task IDs
taskIds = args.id;
}
log.info(`Clearing subtasks from tasks: ${taskIds}`);
// Call the core function
clearSubtasks(tasksPath, taskIds);
// Read the updated data to provide a summary
const updatedData = JSON.parse(fs.readFileSync(tasksPath, 'utf8'));
const taskIdArray = taskIds.split(',').map(id => parseInt(id.trim(), 10));
// Build a summary of what was done
const clearedTasksCount = taskIdArray.length;
const taskSummary = taskIdArray.map(id => {
const task = updatedData.tasks.find(t => t.id === id);
return task ? { id, title: task.title } : { id, title: 'Task not found' };
});
return {
success: true,
data: {
message: `Successfully cleared subtasks from ${clearedTasksCount} task(s)`,
tasksCleared: taskSummary
}
};
} catch (error) {
log.error(`Error in clearSubtasksDirect: ${error.message}`);
return {
success: false,
error: {
code: 'CORE_FUNCTION_ERROR',
message: error.message
}
};
}
}

View File

@@ -20,6 +20,7 @@ import { addTaskDirect } from './direct-functions/add-task.js';
import { addSubtaskDirect } from './direct-functions/add-subtask.js';
import { removeSubtaskDirect } from './direct-functions/remove-subtask.js';
import { analyzeTaskComplexityDirect } from './direct-functions/analyze-task-complexity.js';
import { clearSubtasksDirect } from './direct-functions/clear-subtasks.js';
// Re-export utility functions
export { findTasksJsonPath } from './utils/path-utils.js';
@@ -40,7 +41,8 @@ export const directFunctions = new Map([
['addTaskDirect', addTaskDirect],
['addSubtaskDirect', addSubtaskDirect],
['removeSubtaskDirect', removeSubtaskDirect],
['analyzeTaskComplexityDirect', analyzeTaskComplexityDirect]
['analyzeTaskComplexityDirect', analyzeTaskComplexityDirect],
['clearSubtasksDirect', clearSubtasksDirect]
]);
// Re-export all direct function implementations
@@ -59,5 +61,6 @@ export {
addTaskDirect,
addSubtaskDirect,
removeSubtaskDirect,
analyzeTaskComplexityDirect
analyzeTaskComplexityDirect,
clearSubtasksDirect
};

View File

@@ -0,0 +1,52 @@
/**
* tools/clear-subtasks.js
* Tool for clearing subtasks from parent tasks
*/
import { z } from "zod";
import {
handleApiResult,
createErrorResponse
} from "./utils.js";
import { clearSubtasksDirect } from "../core/task-master-core.js";
/**
* Register the clearSubtasks tool with the MCP server
* @param {Object} server - FastMCP server instance
*/
export function registerClearSubtasksTool(server) {
server.addTool({
name: "clear_subtasks",
description: "Clear subtasks from specified tasks",
parameters: z.object({
id: z.string().optional().describe("Task IDs (comma-separated) to clear subtasks from"),
all: z.boolean().optional().describe("Clear subtasks from all tasks"),
file: z.string().optional().describe("Path to the tasks file (default: tasks/tasks.json)"),
projectRoot: z.string().optional().describe("Root directory of the project (default: current working directory)")
}).refine(data => data.id || data.all, {
message: "Either 'id' or 'all' parameter must be provided",
path: ["id", "all"]
}),
execute: async (args, { log }) => {
try {
log.info(`Clearing subtasks with args: ${JSON.stringify(args)}`);
// Call the direct function wrapper
const result = await clearSubtasksDirect(args, log);
// Log result
if (result.success) {
log.info(`Subtasks cleared successfully: ${result.data.message}`);
} else {
log.error(`Failed to clear subtasks: ${result.error.message}`);
}
// Use handleApiResult to format the response
return handleApiResult(result, log, 'Error clearing subtasks');
} catch (error) {
log.error(`Error in clearSubtasks tool: ${error.message}`);
return createErrorResponse(error.message);
}
},
});
}

View File

@@ -18,6 +18,7 @@ import { registerAddTaskTool } from "./add-task.js";
import { registerAddSubtaskTool } from "./add-subtask.js";
import { registerRemoveSubtaskTool } from "./remove-subtask.js";
import { registerAnalyzeTool } from "./analyze.js";
import { registerClearSubtasksTool } from "./clear-subtasks.js";
/**
* Register all Task Master tools with the MCP server
@@ -38,6 +39,7 @@ export function registerTaskMasterTools(server) {
registerAddSubtaskTool(server);
registerRemoveSubtaskTool(server);
registerAnalyzeTool(server);
registerClearSubtasksTool(server);
logger.info("Registered all Task Master tools with MCP server");
}

View File

@@ -736,7 +736,7 @@ Following MCP implementation standards:
- Unit test for analyzeTaskComplexityDirect.js
- Integration test for MCP tool
## 29. Implement clear-subtasks MCP command [pending]
## 29. Implement clear-subtasks MCP command [done]
### Dependencies: None
### Description: Create direct function wrapper and MCP tool for clearing subtasks from a parent task.
### Details:

View File

@@ -1609,7 +1609,7 @@
"title": "Implement clear-subtasks MCP command",
"description": "Create direct function wrapper and MCP tool for clearing subtasks from a parent task.",
"details": "Following MCP implementation standards:\n\n1. Create clearSubtasksDirect.js in mcp-server/src/core/direct-functions/:\n - Import clearSubtasks from task-manager.js\n - Handle file paths using findTasksJsonPath utility\n - Process arguments: taskId\n - Validate inputs and handle errors with try/catch\n - Return standardized { success, data/error } object\n\n2. Export from task-master-core.js:\n - Import the function from its file\n - Add to directFunctions map\n\n3. Create clear-subtasks.js MCP tool in mcp-server/src/tools/:\n - Import z from zod for parameter schema\n - Import executeMCPToolAction from ./utils.js\n - Import clearSubtasksDirect from task-master-core.js\n - Define parameters matching CLI options using zod schema\n - Implement registerClearSubtasksTool(server) with server.addTool\n - Use executeMCPToolAction in execute method\n\n4. Register in tools/index.js with tool name 'clear_subtasks'\n\n5. Add to .cursor/mcp.json with appropriate schema\n\n6. Write tests following testing guidelines:\n - Unit test for clearSubtasksDirect.js\n - Integration test for MCP tool",
"status": "pending",
"status": "done",
"dependencies": [],
"parentTaskId": 23
},