feat: implement next-task MCP command

- Create direct function wrapper in next-task.js with error handling and caching

- Add MCP tool integration for finding the next task to work on

- Update task-master-core.js to expose nextTaskDirect function

- Update changeset to document the new command
This commit is contained in:
Eyal Toledano
2025-03-31 12:00:23 -04:00
parent 219b40b516
commit 729ae4d2d5
7 changed files with 176 additions and 2 deletions

View File

@@ -8,4 +8,5 @@
- Implement generate MCP command for creating individual task files from tasks.json
- Implement set-status MCP command for updating task status
- Implement show-task MCP command for displaying detailed task information
- Implement next-task MCP command for finding the next task to work on
- 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,112 @@
/**
* next-task.js
* Direct function implementation for finding the next task to work on
*/
import { findNextTask } from '../../../../scripts/modules/task-manager.js';
import { readJSON } from '../../../../scripts/modules/utils.js';
import { getCachedOrExecute } from '../../tools/utils.js';
import { findTasksJsonPath } from '../utils/path-utils.js';
/**
* Direct function wrapper for finding the next task to work on with error handling and caching.
*
* @param {Object} args - Command arguments
* @param {Object} log - Logger object
* @returns {Promise<Object>} - Next task result { success: boolean, data?: any, error?: { code: string, message: string }, fromCache: boolean }
*/
export async function nextTaskDirect(args, log) {
let tasksPath;
try {
// Find the tasks path first - needed for cache key and execution
tasksPath = findTasksJsonPath(args, log);
} catch (error) {
log.error(`Tasks file not found: ${error.message}`);
return {
success: false,
error: {
code: 'FILE_NOT_FOUND_ERROR',
message: error.message
},
fromCache: false
};
}
// Generate cache key using task path
const cacheKey = `nextTask:${tasksPath}`;
// Define the action function to be executed on cache miss
const coreNextTaskAction = async () => {
try {
log.info(`Finding next task from ${tasksPath}`);
// Read tasks data
const data = readJSON(tasksPath);
if (!data || !data.tasks) {
return {
success: false,
error: {
code: 'INVALID_TASKS_FILE',
message: `No valid tasks found in ${tasksPath}`
}
};
}
// Find the next task
const nextTask = findNextTask(data.tasks);
if (!nextTask) {
log.info('No eligible next task found. All tasks are either completed or have unsatisfied dependencies');
return {
success: true,
data: {
message: 'No eligible next task found. All tasks are either completed or have unsatisfied dependencies',
nextTask: null,
allTasks: data.tasks
}
};
}
// Return the next task data with the full tasks array for reference
log.info(`Successfully found next task ${nextTask.id}: ${nextTask.title}`);
return {
success: true,
data: {
nextTask,
allTasks: data.tasks
}
};
} catch (error) {
log.error(`Error finding next task: ${error.message}`);
return {
success: false,
error: {
code: 'CORE_FUNCTION_ERROR',
message: error.message || 'Failed to find next task'
}
};
}
};
// Use the caching utility
try {
const result = await getCachedOrExecute({
cacheKey,
actionFn: coreNextTaskAction,
log
});
log.info(`nextTaskDirect completed. From cache: ${result.fromCache}`);
return result; // Returns { success, data/error, fromCache }
} catch (error) {
// Catch unexpected errors from getCachedOrExecute itself
log.error(`Unexpected error during getCachedOrExecute for nextTask: ${error.message}`);
return {
success: false,
error: {
code: 'UNEXPECTED_ERROR',
message: error.message
},
fromCache: false
};
}
}

View File

@@ -14,6 +14,7 @@ import { updateSubtaskByIdDirect } from './direct-functions/update-subtask-by-id
import { generateTaskFilesDirect } from './direct-functions/generate-task-files.js';
import { setTaskStatusDirect } from './direct-functions/set-task-status.js';
import { showTaskDirect } from './direct-functions/show-task.js';
import { nextTaskDirect } from './direct-functions/next-task.js';
// Re-export utility functions
export { findTasksJsonPath } from './utils/path-utils.js';
@@ -29,6 +30,7 @@ export {
generateTaskFilesDirect,
setTaskStatusDirect,
showTaskDirect,
nextTaskDirect,
};
/**
@@ -45,5 +47,6 @@ export const directFunctions = {
generate: generateTaskFilesDirect,
setStatus: setTaskStatusDirect,
showTask: showTaskDirect,
nextTask: nextTaskDirect,
// Add more functions as we implement them
};

View File

@@ -12,6 +12,7 @@ import { registerUpdateTaskTool } from "./update-task.js";
import { registerUpdateSubtaskTool } from "./update-subtask.js";
import { registerGenerateTool } from "./generate.js";
import { registerShowTaskTool } from "./show-task.js";
import { registerNextTaskTool } from "./next-task.js";
/**
* Register all Task Master tools with the MCP server
@@ -26,6 +27,7 @@ export function registerTaskMasterTools(server) {
registerUpdateSubtaskTool(server);
registerGenerateTool(server);
registerShowTaskTool(server);
registerNextTaskTool(server);
}
export default {

View File

@@ -0,0 +1,56 @@
/**
* tools/next-task.js
* Tool to find the next task to work on
*/
import { z } from "zod";
import {
handleApiResult,
createErrorResponse
} from "./utils.js";
import { nextTaskDirect } from "../core/task-master-core.js";
/**
* Register the next-task tool with the MCP server
* @param {Object} server - FastMCP server instance
*/
export function registerNextTaskTool(server) {
server.addTool({
name: "next_task",
description: "Find the next task to work on based on dependencies and status",
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)"
),
}),
execute: async (args, { log }) => {
try {
log.info(`Finding next task with args: ${JSON.stringify(args)}`);
// Call the direct function wrapper
const result = await nextTaskDirect(args, log);
// Log result
if (result.success) {
if (result.data.nextTask) {
log.info(`Successfully found next task ID: ${result.data.nextTask.id}${result.fromCache ? ' (from cache)' : ''}`);
} else {
log.info(`No eligible next task found${result.fromCache ? ' (from cache)' : ''}`);
}
} else {
log.error(`Failed to find next task: ${result.error.message}`);
}
// Use handleApiResult to format the response
return handleApiResult(result, log, 'Error finding next task');
} catch (error) {
log.error(`Error in next-task tool: ${error.message}`);
return createErrorResponse(`Failed to find next task: ${error.message}`);
}
},
});
}

View File

@@ -538,7 +538,7 @@ Following MCP implementation standards:
- Unit test for showTaskDirect.js
- Integration test for MCP tool
## 23. Implement next-task MCP command [pending]
## 23. Implement next-task MCP command [in-progress]
### Dependencies: None
### Description: Create direct function wrapper and MCP tool for finding the next task to work on.
### Details:

View File

@@ -1555,7 +1555,7 @@
"title": "Implement next-task MCP command",
"description": "Create direct function wrapper and MCP tool for finding the next task to work on.",
"details": "Following MCP implementation standards:\n\n1. Create nextTaskDirect.js in mcp-server/src/core/direct-functions/:\n - Import nextTask from task-manager.js\n - Handle file paths using findTasksJsonPath utility\n - Process arguments (no specific args needed except projectRoot/file)\n - 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 next-task.js MCP tool in mcp-server/src/tools/:\n - Import z from zod for parameter schema\n - Import executeMCPToolAction from ./utils.js\n - Import nextTaskDirect from task-master-core.js\n - Define parameters matching CLI options using zod schema\n - Implement registerNextTaskTool(server) with server.addTool\n - Use executeMCPToolAction in execute method\n\n4. Register in tools/index.js with tool name 'next_task'\n\n5. Add to .cursor/mcp.json with appropriate schema\n\n6. Write tests following testing guidelines:\n - Unit test for nextTaskDirect.js\n - Integration test for MCP tool",
"status": "pending",
"status": "in-progress",
"dependencies": [],
"parentTaskId": 23
},