-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathmove-task.js
More file actions
232 lines (218 loc) · 6.48 KB
/
Copy pathmove-task.js
File metadata and controls
232 lines (218 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
* tools/move-task.js
* Tool for moving tasks or subtasks to a new position
*/
import {
createErrorResponse,
handleApiResult,
withNormalizedProjectRoot
} from '@tm/mcp';
import { z } from 'zod';
import { resolveTag } from '../../../scripts/modules/utils.js';
import {
moveTaskCrossTagDirect,
moveTaskDirect
} from '../core/task-master-core.js';
import { findTasksPath } from '../core/utils/path-utils.js';
/**
* Register the moveTask tool with the MCP server
* @param {Object} server - FastMCP server instance
*/
export function registerMoveTaskTool(server) {
server.addTool({
name: 'move_task',
description: 'Move a task or subtask to a new position',
parameters: z.object({
from: z
.string()
.describe(
'ID of the task/subtask to move (e.g., "5" or "5.2"). Can be comma-separated to move multiple tasks (e.g., "5,6,7")'
),
to: z
.string()
.optional()
.describe(
'ID of the destination (e.g., "7" or "7.3"). Required for within-tag moves. For cross-tag moves, if omitted, task will be moved to the target tag maintaining its ID'
),
file: z.string().optional().describe('Custom path to tasks.json file'),
projectRoot: z
.string()
.describe(
'Root directory of the project (typically derived from session)'
),
tag: z.string().optional().describe('Tag context to operate on'),
fromTag: z.string().optional().describe('Source tag for cross-tag moves'),
toTag: z.string().optional().describe('Target tag for cross-tag moves'),
withDependencies: z
.boolean()
.optional()
.describe('Move dependent tasks along with main task'),
ignoreDependencies: z
.boolean()
.optional()
.describe('Break cross-tag dependencies during move')
}),
annotations: {
title: 'Move Task',
destructiveHint: true
},
execute: withNormalizedProjectRoot(async (args, { log, session }) => {
try {
// Check if this is a cross-tag move
const isCrossTagMove =
args.fromTag && args.toTag && args.fromTag !== args.toTag;
if (isCrossTagMove) {
// Cross-tag move logic
if (!args.from) {
return createErrorResponse(
'Source IDs are required for cross-tag moves',
'MISSING_SOURCE_IDS'
);
}
// Warn if 'to' parameter is provided for cross-tag moves
if (args.to) {
log.warn(
'The "to" parameter is not used for cross-tag moves and will be ignored. Tasks retain their original IDs in the target tag.'
);
}
// Find tasks.json path if not provided
let tasksJsonPath = args.file;
if (!tasksJsonPath) {
tasksJsonPath = findTasksPath(args, log);
}
// Use cross-tag move function
return handleApiResult({
result: await moveTaskCrossTagDirect(
{
sourceIds: args.from,
sourceTag: args.fromTag,
targetTag: args.toTag,
withDependencies: args.withDependencies || false,
ignoreDependencies: args.ignoreDependencies || false,
tasksJsonPath,
projectRoot: args.projectRoot
},
log,
{ session }
),
log,
errorPrefix: 'Error moving tasks between tags',
projectRoot: args.projectRoot,
tag: args.toTag
});
} else {
// Within-tag move logic (existing functionality)
if (!args.to) {
return createErrorResponse(
'Destination ID is required for within-tag moves',
'MISSING_DESTINATION_ID'
);
}
const resolvedTag = resolveTag({
projectRoot: args.projectRoot,
tag: args.tag
});
// Find tasks.json path if not provided
let tasksJsonPath = args.file;
if (!tasksJsonPath) {
tasksJsonPath = findTasksPath(args, log);
}
// Parse comma-separated IDs
const fromIds = args.from.split(',').map((id) => id.trim());
const toIds = args.to.split(',').map((id) => id.trim());
// Validate matching IDs count
if (fromIds.length !== toIds.length) {
if (fromIds.length > 1) {
const results = [];
const skipped = [];
// Move tasks one by one, only generate files on the last move
for (let i = 0; i < fromIds.length; i++) {
const fromId = fromIds[i];
const toId = toIds[i];
// Skip if source and destination are the same
if (fromId === toId) {
log.info(`Skipping ${fromId} -> ${toId} (same ID)`);
skipped.push({ fromId, toId, reason: 'same ID' });
continue;
}
const shouldGenerateFiles = i === fromIds.length - 1;
const result = await moveTaskDirect(
{
sourceId: fromId,
destinationId: toId,
tasksJsonPath,
projectRoot: args.projectRoot,
tag: resolvedTag,
generateFiles: shouldGenerateFiles
},
log,
{ session }
);
if (!result.success) {
log.error(
`Failed to move ${fromId} to ${toId}: ${result.error.message}`
);
} else {
results.push(result.data);
}
}
return handleApiResult({
result: {
success: true,
data: {
moves: results,
skipped: skipped.length > 0 ? skipped : undefined,
message: `Successfully moved ${results.length} tasks${skipped.length > 0 ? `, skipped ${skipped.length}` : ''}`
}
},
log,
errorPrefix: 'Error moving multiple tasks',
projectRoot: args.projectRoot,
tag: resolvedTag
});
}
return handleApiResult({
result: {
success: true,
data: {
moves: results,
skippedMoves: skippedMoves,
message: `Successfully moved ${results.length} tasks${skippedMoves.length > 0 ? `, skipped ${skippedMoves.length} moves` : ''}`
}
},
log,
errorPrefix: 'Error moving multiple tasks',
projectRoot: args.projectRoot,
tag: resolvedTag
});
} else {
// Moving a single task
return handleApiResult({
result: await moveTaskDirect(
{
sourceId: args.from,
destinationId: args.to,
tasksJsonPath,
projectRoot: args.projectRoot,
tag: resolvedTag,
generateFiles: true
},
log,
{ session }
),
log,
errorPrefix: 'Error moving task',
projectRoot: args.projectRoot,
tag: resolvedTag
});
}
}
} catch (error) {
return createErrorResponse(
`Failed to move task: ${error.message}`,
'MOVE_TASK_ERROR'
);
}
})
});
}