@radholm/azure-devops-mcp 1.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1316 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+ import * as fs from "fs";
4
+ import * as path from "path";
5
+ import { WorkItemExpand } from "azure-devops-node-api/interfaces/WorkItemTrackingInterfaces.js";
6
+ import { QueryExpand } from "azure-devops-node-api/interfaces/WorkItemTrackingInterfaces.js";
7
+ import { z } from "zod";
8
+ import { batchApiVersion, markdownCommentsApiVersion, getEnumKeys, safeEnumConvert, encodeFormattedValue } from "../utils.js";
9
+ import { elicitProject, elicitTeam } from "../shared/elicitations.js";
10
+ import { createExternalContentResponse } from "../shared/content-safety.js";
11
+ const WORKITEM_TOOLS = {
12
+ my_work_items: "wit_my_work_items",
13
+ list_backlogs: "wit_list_backlogs",
14
+ list_backlog_work_items: "wit_list_backlog_work_items",
15
+ get_work_item: "wit_get_work_item",
16
+ get_work_items_batch_by_ids: "wit_get_work_items_batch_by_ids",
17
+ update_work_item: "wit_update_work_item",
18
+ create_work_item: "wit_create_work_item",
19
+ list_work_item_comments: "wit_list_work_item_comments",
20
+ list_work_item_revisions: "wit_list_work_item_revisions",
21
+ get_work_items_for_iteration: "wit_get_work_items_for_iteration",
22
+ add_work_item_comment: "wit_add_work_item_comment",
23
+ update_work_item_comment: "wit_update_work_item_comment",
24
+ add_child_work_items: "wit_add_child_work_items",
25
+ link_work_item_to_pull_request: "wit_link_work_item_to_pull_request",
26
+ get_work_item_type: "wit_get_work_item_type",
27
+ get_query: "wit_get_query",
28
+ get_query_results_by_id: "wit_get_query_results_by_id",
29
+ update_work_items_batch: "wit_update_work_items_batch",
30
+ work_items_link: "wit_work_items_link",
31
+ work_item_unlink: "wit_work_item_unlink",
32
+ add_artifact_link: "wit_add_artifact_link",
33
+ get_work_item_attachment: "wit_get_work_item_attachment",
34
+ query_by_wiql: "wit_query_by_wiql",
35
+ };
36
+ function getLinkTypeFromName(name) {
37
+ switch (name.toLowerCase()) {
38
+ case "parent":
39
+ return "System.LinkTypes.Hierarchy-Reverse";
40
+ case "child":
41
+ return "System.LinkTypes.Hierarchy-Forward";
42
+ case "duplicate":
43
+ return "System.LinkTypes.Duplicate-Forward";
44
+ case "duplicate of":
45
+ return "System.LinkTypes.Duplicate-Reverse";
46
+ case "related":
47
+ return "System.LinkTypes.Related";
48
+ case "successor":
49
+ return "System.LinkTypes.Dependency-Forward";
50
+ case "predecessor":
51
+ return "System.LinkTypes.Dependency-Reverse";
52
+ case "tested by":
53
+ return "Microsoft.VSTS.Common.TestedBy-Forward";
54
+ case "tests":
55
+ return "Microsoft.VSTS.Common.TestedBy-Reverse";
56
+ case "affects":
57
+ return "Microsoft.VSTS.Common.Affects-Forward";
58
+ case "affected by":
59
+ return "Microsoft.VSTS.Common.Affects-Reverse";
60
+ case "artifact":
61
+ return "ArtifactLink";
62
+ default:
63
+ throw new Error(`Unknown link type: ${name}`);
64
+ }
65
+ }
66
+ function configureWorkItemTools(server, tokenProvider, connectionProvider, userAgentProvider) {
67
+ server.tool(WORKITEM_TOOLS.list_backlogs, "Receive a list of backlogs for a given project and team. If a project or team is not specified, you will be prompted to select one.", {
68
+ project: z.string().optional().describe("The name or ID of the Azure DevOps project. Reuse from prior context if already known. If not provided, a project selection prompt will be shown."),
69
+ team: z.string().optional().describe("The name or ID of the Azure DevOps team. Reuse from prior context if already known. If not provided, a team selection prompt will be shown."),
70
+ }, async ({ project, team }) => {
71
+ try {
72
+ const connection = await connectionProvider();
73
+ let resolvedProject = project;
74
+ if (!resolvedProject) {
75
+ const result = await elicitProject(server, connection, "Select the Azure DevOps project to list backlogs for.");
76
+ if ("response" in result)
77
+ return result.response;
78
+ resolvedProject = result.resolved;
79
+ }
80
+ let resolvedTeam = team;
81
+ if (!resolvedTeam) {
82
+ const result = await elicitTeam(server, connection, resolvedProject, "Select the Azure DevOps team to list backlogs for.");
83
+ if ("response" in result)
84
+ return result.response;
85
+ resolvedTeam = result.resolved;
86
+ }
87
+ const workApi = await connection.getWorkApi();
88
+ const teamContext = { project: resolvedProject, team: resolvedTeam };
89
+ const backlogs = await workApi.getBacklogs(teamContext);
90
+ return {
91
+ content: [{ type: "text", text: JSON.stringify(backlogs, null, 2) }],
92
+ };
93
+ }
94
+ catch (error) {
95
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
96
+ return {
97
+ content: [{ type: "text", text: `Error listing backlogs: ${errorMessage}` }],
98
+ isError: true,
99
+ };
100
+ }
101
+ });
102
+ server.tool(WORKITEM_TOOLS.list_backlog_work_items, "Retrieve a list of backlogs of for a given project, team, and backlog category. If a project or team is not specified, you will be prompted to select one.", {
103
+ project: z.string().optional().describe("The name or ID of the Azure DevOps project. Reuse from prior context if already known. If not provided, a project selection prompt will be shown."),
104
+ team: z.string().optional().describe("The name or ID of the Azure DevOps team. Reuse from prior context if already known. If not provided, a team selection prompt will be shown."),
105
+ backlogId: z.string().describe("The ID of the backlog category to retrieve work items from."),
106
+ }, async ({ project, team, backlogId }) => {
107
+ try {
108
+ const connection = await connectionProvider();
109
+ let resolvedProject = project;
110
+ if (!resolvedProject) {
111
+ const result = await elicitProject(server, connection, "Select the Azure DevOps project to list backlog work items for.");
112
+ if ("response" in result)
113
+ return result.response;
114
+ resolvedProject = result.resolved;
115
+ }
116
+ let resolvedTeam = team;
117
+ if (!resolvedTeam) {
118
+ const result = await elicitTeam(server, connection, resolvedProject, "Select the Azure DevOps team to list backlog work items for.");
119
+ if ("response" in result)
120
+ return result.response;
121
+ resolvedTeam = result.resolved;
122
+ }
123
+ const workApi = await connection.getWorkApi();
124
+ const teamContext = { project: resolvedProject, team: resolvedTeam };
125
+ const workItems = await workApi.getBacklogLevelWorkItems(teamContext, backlogId);
126
+ return {
127
+ content: [{ type: "text", text: JSON.stringify(workItems, null, 2) }],
128
+ };
129
+ }
130
+ catch (error) {
131
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
132
+ return {
133
+ content: [{ type: "text", text: `Error listing backlog work items: ${errorMessage}` }],
134
+ isError: true,
135
+ };
136
+ }
137
+ });
138
+ server.tool(WORKITEM_TOOLS.my_work_items, "Retrieve a list of work items relevent to the authenticated user. If a project is not specified, you will be prompted to select one.", {
139
+ project: z.string().optional().describe("The name or ID of the Azure DevOps project. Reuse from prior context if already known. If not provided, a project selection prompt will be shown."),
140
+ type: z.enum(["assignedtome", "myactivity"]).default("assignedtome").describe("The type of work items to retrieve. Defaults to 'assignedtome'."),
141
+ top: z.coerce.number().default(50).describe("The maximum number of work items to return. Defaults to 50."),
142
+ includeCompleted: z.boolean().default(false).describe("Whether to include completed work items. Defaults to false."),
143
+ }, async ({ project, type, top, includeCompleted }) => {
144
+ try {
145
+ const connection = await connectionProvider();
146
+ let resolvedProject = project;
147
+ if (!resolvedProject) {
148
+ const result = await elicitProject(server, connection, "Select the Azure DevOps project to retrieve work items for.");
149
+ if ("response" in result)
150
+ return result.response;
151
+ resolvedProject = result.resolved;
152
+ }
153
+ const workApi = await connection.getWorkApi();
154
+ const workItems = await workApi.getPredefinedQueryResults(resolvedProject, type, top, includeCompleted);
155
+ return {
156
+ content: [{ type: "text", text: JSON.stringify(workItems, null, 2) }],
157
+ };
158
+ }
159
+ catch (error) {
160
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
161
+ return {
162
+ content: [{ type: "text", text: `Error retrieving work items: ${errorMessage}` }],
163
+ isError: true,
164
+ };
165
+ }
166
+ });
167
+ server.tool(WORKITEM_TOOLS.get_work_items_batch_by_ids, "Retrieve list of work items by IDs in batch. If a project is not specified, you will be prompted to select one.", {
168
+ project: z.string().optional().describe("The name or ID of the Azure DevOps project. Reuse from prior context if already known. If not provided, a project selection prompt will be shown."),
169
+ ids: z.array(z.coerce.number().min(1)).describe("The IDs of the work items to retrieve."),
170
+ fields: z.array(z.string()).optional().describe("Optional list of fields to include in the response. If not provided, a hardcoded default set of fields will be used."),
171
+ }, async ({ project, ids, fields }) => {
172
+ try {
173
+ const connection = await connectionProvider();
174
+ let resolvedProject = project;
175
+ if (!resolvedProject) {
176
+ const result = await elicitProject(server, connection, "Select the Azure DevOps project to retrieve work items for.");
177
+ if ("response" in result)
178
+ return result.response;
179
+ resolvedProject = result.resolved;
180
+ }
181
+ const workItemApi = await connection.getWorkItemTrackingApi();
182
+ const defaultFields = ["System.Id", "System.WorkItemType", "System.Title", "System.State", "System.Parent", "System.Tags", "Microsoft.VSTS.Common.StackRank", "System.AssignedTo"];
183
+ // If no fields are provided, use the default set of fields
184
+ const fieldsToUse = !fields || fields.length === 0 ? defaultFields : fields;
185
+ const workitems = await workItemApi.getWorkItemsBatch({ ids, fields: fieldsToUse }, resolvedProject);
186
+ // List of identity fields that need to be transformed from objects to formatted strings
187
+ const identityFields = [
188
+ "System.AssignedTo",
189
+ "System.CreatedBy",
190
+ "System.ChangedBy",
191
+ "System.AuthorizedAs",
192
+ "Microsoft.VSTS.Common.ActivatedBy",
193
+ "Microsoft.VSTS.Common.ResolvedBy",
194
+ "Microsoft.VSTS.Common.ClosedBy",
195
+ ];
196
+ // Format identity fields to include displayName and uniqueName
197
+ // Removing the identity object as the response. It's too much and not needed
198
+ if (workitems && Array.isArray(workitems)) {
199
+ workitems.forEach((item) => {
200
+ if (item.fields) {
201
+ identityFields.forEach((fieldName) => {
202
+ if (item.fields && item.fields[fieldName] && typeof item.fields[fieldName] === "object") {
203
+ const identityField = item.fields[fieldName];
204
+ const name = identityField.displayName || "";
205
+ const email = identityField.uniqueName || "";
206
+ item.fields[fieldName] = `${name} <${email}>`.trim();
207
+ }
208
+ });
209
+ }
210
+ });
211
+ }
212
+ return {
213
+ content: [{ type: "text", text: JSON.stringify(workitems, null, 2) }],
214
+ };
215
+ }
216
+ catch (error) {
217
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
218
+ return {
219
+ content: [{ type: "text", text: `Error retrieving work items batch: ${errorMessage}` }],
220
+ isError: true,
221
+ };
222
+ }
223
+ });
224
+ server.tool(WORKITEM_TOOLS.get_work_item, "Get a single work item by ID. If a project is not specified, you will be prompted to select one.", {
225
+ id: z.coerce.number().min(1).describe("The ID of the work item to retrieve."),
226
+ project: z.string().optional().describe("The name or ID of the Azure DevOps project. Reuse from prior context if already known. If not provided, a project selection prompt will be shown."),
227
+ fields: z
228
+ .array(z.string())
229
+ .optional()
230
+ .describe("Optional list of fields to include in the response. If not provided, all fields will be returned. Cannot be used together with the expand parameter."),
231
+ asOf: z.coerce.date().optional().describe("Optional date string to retrieve the work item as of a specific time. If not provided, the current state will be returned."),
232
+ expand: z
233
+ .enum(["all", "fields", "links", "none", "relations"])
234
+ .describe("Optional expand parameter to include additional details in the response. Cannot be used together with the fields parameter.")
235
+ .optional()
236
+ .describe("Expand options include 'All', 'Fields', 'Links', 'None', and 'Relations'. Relations can be used to get child workitems. Defaults to 'None'. Cannot be used together with the fields parameter."),
237
+ }, async ({ id, project, fields, asOf, expand }) => {
238
+ try {
239
+ const connection = await connectionProvider();
240
+ let resolvedProject = project;
241
+ if (!resolvedProject) {
242
+ const result = await elicitProject(server, connection, "Select the Azure DevOps project to retrieve the work item from.");
243
+ if ("response" in result)
244
+ return result.response;
245
+ resolvedProject = result.resolved;
246
+ }
247
+ // The Azure DevOps API does not support using expand and fields together.
248
+ // When both are provided, prefer fields as it is the more specific selection.
249
+ if (fields && fields.length > 0 && expand != null) {
250
+ expand = "none";
251
+ }
252
+ const workItemApi = await connection.getWorkItemTrackingApi();
253
+ const workItem = await workItemApi.getWorkItem(id, fields, asOf, expand, resolvedProject);
254
+ return {
255
+ content: [{ type: "text", text: JSON.stringify(workItem, null, 2) }],
256
+ };
257
+ }
258
+ catch (error) {
259
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
260
+ return {
261
+ content: [{ type: "text", text: `Error retrieving work item: ${errorMessage}` }],
262
+ isError: true,
263
+ };
264
+ }
265
+ });
266
+ server.tool(WORKITEM_TOOLS.list_work_item_comments, "Retrieve list of comments for a work item by ID. If a project is not specified, you will be prompted to select one.", {
267
+ project: z.string().optional().describe("The name or ID of the Azure DevOps project. Reuse from prior context if already known. If not provided, a project selection prompt will be shown."),
268
+ workItemId: z.coerce.number().min(1).describe("The ID of the work item to retrieve comments for."),
269
+ top: z.coerce.number().default(50).describe("Optional number of comments to retrieve. Defaults to all comments."),
270
+ }, async ({ project, workItemId, top }) => {
271
+ try {
272
+ const connection = await connectionProvider();
273
+ let resolvedProject = project;
274
+ if (!resolvedProject) {
275
+ const result = await elicitProject(server, connection, "Select the Azure DevOps project to list work item comments for.");
276
+ if ("response" in result)
277
+ return result.response;
278
+ resolvedProject = result.resolved;
279
+ }
280
+ const workItemApi = await connection.getWorkItemTrackingApi();
281
+ const comments = await workItemApi.getComments(resolvedProject, workItemId, top);
282
+ return {
283
+ content: [{ type: "text", text: JSON.stringify(comments, null, 2) }],
284
+ };
285
+ }
286
+ catch (error) {
287
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
288
+ return {
289
+ content: [{ type: "text", text: `Error listing work item comments: ${errorMessage}` }],
290
+ isError: true,
291
+ };
292
+ }
293
+ });
294
+ server.tool(WORKITEM_TOOLS.add_work_item_comment, "Add comment to a work item by ID. If a project is not specified, you will be prompted to select one.", {
295
+ project: z.string().optional().describe("The name or ID of the Azure DevOps project. Reuse from prior context if already known. If not provided, a project selection prompt will be shown."),
296
+ workItemId: z.coerce.number().min(1).describe("The ID of the work item to add a comment to."),
297
+ comment: z.string().describe("The text of the comment to add to the work item."),
298
+ format: z.enum(["Markdown", "Html"]).optional().default("Markdown").describe("The format of the comment text, e.g., 'Markdown', 'Html'. Optional, defaults to 'Markdown'."),
299
+ }, async ({ project, workItemId, comment, format }) => {
300
+ try {
301
+ const connection = await connectionProvider();
302
+ let resolvedProject = project;
303
+ if (!resolvedProject) {
304
+ const result = await elicitProject(server, connection, "Select the Azure DevOps project to add a work item comment in.");
305
+ if ("response" in result)
306
+ return result.response;
307
+ resolvedProject = result.resolved;
308
+ }
309
+ const orgUrl = connection.serverUrl;
310
+ const accessToken = await tokenProvider();
311
+ const body = {
312
+ text: comment,
313
+ };
314
+ const formatParameter = (format ?? "Markdown") === "Markdown" ? 0 : 1;
315
+ const response = await fetch(`${orgUrl}/${encodeURIComponent(resolvedProject)}/_apis/wit/workItems/${workItemId}/comments?format=${formatParameter}&api-version=${markdownCommentsApiVersion}`, {
316
+ method: "POST",
317
+ headers: {
318
+ "Authorization": `Bearer ${accessToken}`,
319
+ "Content-Type": "application/json",
320
+ "User-Agent": userAgentProvider(),
321
+ },
322
+ body: JSON.stringify(body),
323
+ });
324
+ if (!response.ok) {
325
+ throw new Error(`Failed to add a work item comment: ${response.statusText}}`);
326
+ }
327
+ const comments = await response.text();
328
+ return {
329
+ content: [{ type: "text", text: comments }],
330
+ };
331
+ }
332
+ catch (error) {
333
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
334
+ return {
335
+ content: [{ type: "text", text: `Error adding work item comment: ${errorMessage}` }],
336
+ isError: true,
337
+ };
338
+ }
339
+ });
340
+ server.tool(WORKITEM_TOOLS.update_work_item_comment, "Update an existing comment on a work item by ID. If a project is not specified, you will be prompted to select one.", {
341
+ project: z.string().optional().describe("The name or ID of the Azure DevOps project. Reuse from prior context if already known. If not provided, a project selection prompt will be shown."),
342
+ workItemId: z.coerce.number().min(1).describe("The ID of the work item."),
343
+ commentId: z.coerce.number().min(1).describe("The ID of the comment to update."),
344
+ text: z.string().describe("The updated comment text."),
345
+ format: z.enum(["Markdown", "Html"]).optional().default("Markdown").describe("The format of the comment text, e.g., 'Markdown', 'Html'. Optional, defaults to 'Markdown'."),
346
+ }, async ({ project, workItemId, commentId, text, format }) => {
347
+ try {
348
+ const connection = await connectionProvider();
349
+ let resolvedProject = project;
350
+ if (!resolvedProject) {
351
+ const result = await elicitProject(server, connection, "Select the Azure DevOps project to update the work item comment in.");
352
+ if ("response" in result)
353
+ return result.response;
354
+ resolvedProject = result.resolved;
355
+ }
356
+ const orgUrl = connection.serverUrl;
357
+ const accessToken = await tokenProvider();
358
+ const body = { text };
359
+ const formatParameter = (format ?? "Markdown") === "Markdown" ? 0 : 1;
360
+ const response = await fetch(`${orgUrl}/${encodeURIComponent(resolvedProject)}/_apis/wit/workItems/${workItemId}/comments/${commentId}?format=${formatParameter}&api-version=${markdownCommentsApiVersion}`, {
361
+ method: "PATCH",
362
+ headers: {
363
+ "Authorization": `Bearer ${accessToken}`,
364
+ "Content-Type": "application/json",
365
+ "User-Agent": userAgentProvider(),
366
+ },
367
+ body: JSON.stringify(body),
368
+ });
369
+ if (!response.ok) {
370
+ throw new Error(`Failed to update work item comment: ${response.statusText}`);
371
+ }
372
+ const updatedComment = await response.text();
373
+ return {
374
+ content: [{ type: "text", text: updatedComment }],
375
+ };
376
+ }
377
+ catch (error) {
378
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
379
+ return {
380
+ content: [{ type: "text", text: `Error updating work item comment: ${errorMessage}` }],
381
+ isError: true,
382
+ };
383
+ }
384
+ });
385
+ server.tool(WORKITEM_TOOLS.list_work_item_revisions, "Retrieve list of revisions for a work item by ID. If a project is not specified, you will be prompted to select one.", {
386
+ project: z.string().optional().describe("The name or ID of the Azure DevOps project. Reuse from prior context if already known. If not provided, a project selection prompt will be shown."),
387
+ workItemId: z.coerce.number().min(1).describe("The ID of the work item to retrieve revisions for."),
388
+ top: z.coerce.number().default(50).describe("Optional number of revisions to retrieve. If not provided, all revisions will be returned."),
389
+ skip: z.coerce.number().optional().describe("Optional number of revisions to skip for pagination. Defaults to 0."),
390
+ expand: z
391
+ .enum(getEnumKeys(WorkItemExpand))
392
+ .default("None")
393
+ .optional()
394
+ .describe("Optional expand parameter to include additional details. Defaults to 'None'."),
395
+ }, async ({ project, workItemId, top, skip, expand }) => {
396
+ try {
397
+ const connection = await connectionProvider();
398
+ let resolvedProject = project;
399
+ if (!resolvedProject) {
400
+ const result = await elicitProject(server, connection, "Select the Azure DevOps project to list work item revisions for.");
401
+ if ("response" in result)
402
+ return result.response;
403
+ resolvedProject = result.resolved;
404
+ }
405
+ const workItemApi = await connection.getWorkItemTrackingApi();
406
+ const revisions = await workItemApi.getRevisions(workItemId, top, skip, safeEnumConvert(WorkItemExpand, expand), resolvedProject);
407
+ // Dynamically clean up identity objects in revision fields
408
+ // Identity objects typically have properties like displayName, url, _links, id, uniqueName, imageUrl, descriptor
409
+ if (revisions && Array.isArray(revisions)) {
410
+ revisions.forEach((revision) => {
411
+ if (revision.fields) {
412
+ const fields = revision.fields;
413
+ Object.keys(fields).forEach((fieldName) => {
414
+ const fieldValue = fields[fieldName];
415
+ // Check if this is an identity object by looking for common identity properties
416
+ if (fieldValue &&
417
+ typeof fieldValue === "object" &&
418
+ !Array.isArray(fieldValue) &&
419
+ "displayName" in fieldValue &&
420
+ ("url" in fieldValue || "_links" in fieldValue || "uniqueName" in fieldValue)) {
421
+ // Remove unwanted properties from identity objects
422
+ delete fieldValue.url;
423
+ delete fieldValue._links;
424
+ delete fieldValue.id;
425
+ delete fieldValue.uniqueName;
426
+ delete fieldValue.imageUrl;
427
+ delete fieldValue.descriptor;
428
+ }
429
+ });
430
+ }
431
+ });
432
+ }
433
+ return {
434
+ content: [{ type: "text", text: JSON.stringify(revisions, null, 2) }],
435
+ };
436
+ }
437
+ catch (error) {
438
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
439
+ return {
440
+ content: [{ type: "text", text: `Error listing work item revisions: ${errorMessage}` }],
441
+ isError: true,
442
+ };
443
+ }
444
+ });
445
+ server.tool(WORKITEM_TOOLS.add_child_work_items, "Create one or many child work items from a parent by work item type and parent id. If a project is not specified, you will be prompted to select one.", {
446
+ parentId: z.coerce.number().min(1).describe("The ID of the parent work item to create a child work item under."),
447
+ project: z.string().optional().describe("The name or ID of the Azure DevOps project. Reuse from prior context if already known. If not provided, a project selection prompt will be shown."),
448
+ workItemType: z.string().describe("The type of the child work item to create."),
449
+ items: z.array(z.object({
450
+ title: z.string().describe("The title of the child work item."),
451
+ description: z.string().describe("The description of the child work item."),
452
+ format: z.enum(["Markdown", "Html"]).default("Markdown").describe("Format for the description on the child work item, e.g., 'Markdown', 'Html'. Defaults to 'Markdown'."),
453
+ areaPath: z.string().optional().describe("Optional area path for the child work item."),
454
+ iterationPath: z.string().optional().describe("Optional iteration path for the child work item."),
455
+ })),
456
+ }, async ({ parentId, project, workItemType, items }) => {
457
+ try {
458
+ const connection = await connectionProvider();
459
+ let resolvedProject = project;
460
+ if (!resolvedProject) {
461
+ const result = await elicitProject(server, connection, "Select the Azure DevOps project to create child work items in.");
462
+ if ("response" in result)
463
+ return result.response;
464
+ resolvedProject = result.resolved;
465
+ }
466
+ const orgUrl = connection.serverUrl;
467
+ const accessToken = await tokenProvider();
468
+ if (items.length > 50) {
469
+ return {
470
+ content: [{ type: "text", text: `A maximum of 50 child work items can be created in a single call.` }],
471
+ isError: true,
472
+ };
473
+ }
474
+ const body = items.map((item, x) => {
475
+ const encodedDescription = encodeFormattedValue(item.description, item.format);
476
+ const ops = [
477
+ {
478
+ op: "add",
479
+ path: "/id",
480
+ value: `-${x + 1}`,
481
+ },
482
+ {
483
+ op: "add",
484
+ path: "/fields/System.Title",
485
+ value: item.title,
486
+ },
487
+ {
488
+ op: "add",
489
+ path: "/fields/System.Description",
490
+ value: encodedDescription,
491
+ },
492
+ {
493
+ op: "add",
494
+ path: "/fields/Microsoft.VSTS.TCM.ReproSteps",
495
+ value: encodedDescription,
496
+ },
497
+ {
498
+ op: "add",
499
+ path: "/relations/-",
500
+ value: {
501
+ rel: "System.LinkTypes.Hierarchy-Reverse",
502
+ url: `${connection.serverUrl}/${resolvedProject}/_apis/wit/workItems/${parentId}`,
503
+ },
504
+ },
505
+ ];
506
+ if (item.areaPath && item.areaPath.trim().length > 0) {
507
+ ops.push({
508
+ op: "add",
509
+ path: "/fields/System.AreaPath",
510
+ value: item.areaPath,
511
+ });
512
+ }
513
+ if (item.iterationPath && item.iterationPath.trim().length > 0) {
514
+ ops.push({
515
+ op: "add",
516
+ path: "/fields/System.IterationPath",
517
+ value: item.iterationPath,
518
+ });
519
+ }
520
+ if (item.format && item.format === "Markdown") {
521
+ ops.push({
522
+ op: "add",
523
+ path: "/multilineFieldsFormat/System.Description",
524
+ value: item.format,
525
+ });
526
+ ops.push({
527
+ op: "add",
528
+ path: "/multilineFieldsFormat/Microsoft.VSTS.TCM.ReproSteps",
529
+ value: item.format,
530
+ });
531
+ }
532
+ return {
533
+ method: "PATCH",
534
+ uri: `/${encodeURIComponent(resolvedProject)}/_apis/wit/workitems/$${encodeURIComponent(workItemType)}?api-version=${batchApiVersion}`,
535
+ headers: {
536
+ "Content-Type": "application/json-patch+json",
537
+ },
538
+ body: ops,
539
+ };
540
+ });
541
+ const response = await fetch(`${orgUrl}/_apis/wit/$batch?api-version=${batchApiVersion}`, {
542
+ method: "PATCH",
543
+ headers: {
544
+ "Authorization": `Bearer ${accessToken}`,
545
+ "Content-Type": "application/json",
546
+ "User-Agent": userAgentProvider(),
547
+ },
548
+ body: JSON.stringify(body),
549
+ });
550
+ if (!response.ok) {
551
+ throw new Error(`Failed to update work items in batch: ${response.statusText}`);
552
+ }
553
+ const result = await response.json();
554
+ return {
555
+ content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
556
+ };
557
+ }
558
+ catch (error) {
559
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
560
+ return {
561
+ content: [{ type: "text", text: `Error creating child work items: ${errorMessage}` }],
562
+ isError: true,
563
+ };
564
+ }
565
+ });
566
+ server.tool(WORKITEM_TOOLS.link_work_item_to_pull_request, "Link a single work item to an existing pull request.", {
567
+ projectId: z.string().describe("The project ID of the Azure DevOps project (note: project name is not valid)."),
568
+ repositoryId: z.string().describe("The ID of the repository containing the pull request. Do not use the repository name here, use the ID instead."),
569
+ pullRequestId: z.coerce.number().min(1).describe("The ID of the pull request to link to."),
570
+ workItemId: z.coerce.number().min(1).describe("The ID of the work item to link to the pull request."),
571
+ pullRequestProjectId: z.string().optional().describe("The project ID containing the pull request. If not provided, defaults to the work item's project ID (for same-project linking)."),
572
+ }, async ({ projectId, repositoryId, pullRequestId, workItemId, pullRequestProjectId }) => {
573
+ try {
574
+ const connection = await connectionProvider();
575
+ const workItemTrackingApi = await connection.getWorkItemTrackingApi();
576
+ // Create artifact link relation using vstfs format
577
+ // Format: vstfs:///Git/PullRequestId/{project}/{repositoryId}/{pullRequestId}
578
+ const artifactProjectId = pullRequestProjectId && pullRequestProjectId.trim() !== "" ? pullRequestProjectId : projectId;
579
+ const artifactPathValue = `${artifactProjectId}/${repositoryId}/${pullRequestId}`;
580
+ const vstfsUrl = `vstfs:///Git/PullRequestId/${encodeURIComponent(artifactPathValue)}`;
581
+ // Use the PATCH document format for adding a relation
582
+ const patchDocument = [
583
+ {
584
+ op: "add",
585
+ path: "/relations/-",
586
+ value: {
587
+ rel: "ArtifactLink",
588
+ url: vstfsUrl,
589
+ attributes: {
590
+ name: "Pull Request",
591
+ },
592
+ },
593
+ },
594
+ ];
595
+ // Use the WorkItem API to update the work item with the new relation
596
+ const workItem = await workItemTrackingApi.updateWorkItem({}, patchDocument, workItemId, projectId);
597
+ if (!workItem) {
598
+ return { content: [{ type: "text", text: "Work item update failed" }], isError: true };
599
+ }
600
+ return {
601
+ content: [
602
+ {
603
+ type: "text",
604
+ text: JSON.stringify({
605
+ workItemId,
606
+ pullRequestId,
607
+ success: true,
608
+ }, null, 2),
609
+ },
610
+ ],
611
+ };
612
+ }
613
+ catch (error) {
614
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
615
+ return {
616
+ content: [{ type: "text", text: `Error linking work item to pull request: ${errorMessage}` }],
617
+ isError: true,
618
+ };
619
+ }
620
+ });
621
+ server.tool(WORKITEM_TOOLS.get_work_items_for_iteration, "Retrieve a list of work items for a specified iteration. If a project is not specified, you will be prompted to select one.", {
622
+ project: z.string().optional().describe("The name or ID of the Azure DevOps project. Reuse from prior context if already known. If not provided, a project selection prompt will be shown."),
623
+ team: z.string().optional().describe("The name or ID of the Azure DevOps team. If not provided, the default team will be used."),
624
+ iterationId: z.string().describe("The ID of the iteration to retrieve work items for."),
625
+ }, async ({ project, team, iterationId }) => {
626
+ try {
627
+ const connection = await connectionProvider();
628
+ let resolvedProject = project;
629
+ if (!resolvedProject) {
630
+ const result = await elicitProject(server, connection, "Select the Azure DevOps project to retrieve work items for iteration.");
631
+ if ("response" in result)
632
+ return result.response;
633
+ resolvedProject = result.resolved;
634
+ }
635
+ const workApi = await connection.getWorkApi();
636
+ //get the work items for the current iteration
637
+ const workItems = await workApi.getIterationWorkItems({ project: resolvedProject, team }, iterationId);
638
+ return {
639
+ content: [{ type: "text", text: JSON.stringify(workItems, null, 2) }],
640
+ };
641
+ }
642
+ catch (error) {
643
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
644
+ return {
645
+ content: [{ type: "text", text: `Error retrieving work items for iteration: ${errorMessage}` }],
646
+ isError: true,
647
+ };
648
+ }
649
+ });
650
+ server.tool(WORKITEM_TOOLS.update_work_item, "Update a work item by ID with specified fields.", {
651
+ id: z.coerce.number().min(1).describe("The ID of the work item to update."),
652
+ updates: z
653
+ .array(z.object({
654
+ op: z
655
+ .string()
656
+ .transform((val) => val.toLowerCase())
657
+ .pipe(z.enum(["add", "replace", "remove"]))
658
+ .default("add")
659
+ .describe("The operation to perform on the field."),
660
+ path: z.string().describe("The path of the field to update, e.g., '/fields/System.Title'."),
661
+ value: z.string().describe("The new value for the field. This is required for 'Add' and 'Replace' operations, and should be omitted for 'Remove' operations."),
662
+ }))
663
+ .describe("An array of field updates to apply to the work item."),
664
+ }, async ({ id, updates }) => {
665
+ try {
666
+ const connection = await connectionProvider();
667
+ const workItemApi = await connection.getWorkItemTrackingApi();
668
+ // Convert operation names to lowercase for API
669
+ const apiUpdates = updates.map((update) => ({
670
+ ...update,
671
+ op: update.op,
672
+ }));
673
+ const updatedWorkItem = await workItemApi.updateWorkItem(null, apiUpdates, id);
674
+ return {
675
+ content: [{ type: "text", text: JSON.stringify(updatedWorkItem, null, 2) }],
676
+ };
677
+ }
678
+ catch (error) {
679
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
680
+ return {
681
+ content: [{ type: "text", text: `Error updating work item: ${errorMessage}` }],
682
+ isError: true,
683
+ };
684
+ }
685
+ });
686
+ server.tool(WORKITEM_TOOLS.get_work_item_type, "Get a specific work item type. If a project is not specified, you will be prompted to select one.", {
687
+ project: z.string().optional().describe("The name or ID of the Azure DevOps project. Reuse from prior context if already known. If not provided, a project selection prompt will be shown."),
688
+ workItemType: z.string().describe("The name of the work item type to retrieve."),
689
+ }, async ({ project, workItemType }) => {
690
+ try {
691
+ const connection = await connectionProvider();
692
+ let resolvedProject = project;
693
+ if (!resolvedProject) {
694
+ const result = await elicitProject(server, connection, "Select the Azure DevOps project to retrieve the work item type from.");
695
+ if ("response" in result)
696
+ return result.response;
697
+ resolvedProject = result.resolved;
698
+ }
699
+ const workItemApi = await connection.getWorkItemTrackingApi();
700
+ const workItemTypeInfo = await workItemApi.getWorkItemType(resolvedProject, workItemType);
701
+ return {
702
+ content: [{ type: "text", text: JSON.stringify(workItemTypeInfo, null, 2) }],
703
+ };
704
+ }
705
+ catch (error) {
706
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
707
+ return {
708
+ content: [{ type: "text", text: `Error retrieving work item type: ${errorMessage}` }],
709
+ isError: true,
710
+ };
711
+ }
712
+ });
713
+ server.tool(WORKITEM_TOOLS.create_work_item, "Create a new work item in a specified project and work item type. If a project is not specified, you will be prompted to select one.", {
714
+ project: z.string().optional().describe("The name or ID of the Azure DevOps project. Reuse from prior context if already known. If not provided, a project selection prompt will be shown."),
715
+ workItemType: z.string().describe("The type of work item to create, e.g., 'Task', 'Bug', etc."),
716
+ fields: z
717
+ .array(z.object({
718
+ name: z.string().describe("The name of the field, e.g., 'System.Title'."),
719
+ value: z.string().describe("The value of the field."),
720
+ format: z.enum(["Html", "Markdown"]).optional().describe("the format of the field value, e.g., 'Html', 'Markdown'. Optional, defaults to 'Markdown'."),
721
+ }))
722
+ .describe("A record of field names and values to set on the new work item. Each fild is the field name and each value is the corresponding value to set for that field."),
723
+ }, async ({ project, workItemType, fields }) => {
724
+ try {
725
+ const connection = await connectionProvider();
726
+ let resolvedProject = project;
727
+ if (!resolvedProject) {
728
+ const result = await elicitProject(server, connection, "Select the Azure DevOps project to create the work item in.");
729
+ if ("response" in result)
730
+ return result.response;
731
+ resolvedProject = result.resolved;
732
+ }
733
+ const workItemApi = await connection.getWorkItemTrackingApi();
734
+ const document = fields.map(({ name, value, format }) => ({
735
+ op: "add",
736
+ path: `/fields/${name}`,
737
+ value: encodeFormattedValue(value, format),
738
+ }));
739
+ // Check if any field has format === "Markdown" and add the multilineFieldsFormat operation
740
+ // this should only happen for large text fields, but since we dont't know by field name, lets assume if the users
741
+ // passes a value longer than 100 characters, then we can set the format to Markdown
742
+ fields.forEach(({ name, value, format }) => {
743
+ if (value.length > 100 && format === "Markdown") {
744
+ document.push({
745
+ op: "add",
746
+ path: `/multilineFieldsFormat/${name}`,
747
+ value: "Markdown",
748
+ });
749
+ }
750
+ });
751
+ const newWorkItem = await workItemApi.createWorkItem(null, document, resolvedProject, workItemType);
752
+ if (!newWorkItem) {
753
+ return { content: [{ type: "text", text: "Work item was not created" }], isError: true };
754
+ }
755
+ return {
756
+ content: [{ type: "text", text: JSON.stringify(newWorkItem, null, 2) }],
757
+ };
758
+ }
759
+ catch (error) {
760
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
761
+ return {
762
+ content: [{ type: "text", text: `Error creating work item: ${errorMessage}` }],
763
+ isError: true,
764
+ };
765
+ }
766
+ });
767
+ server.tool(WORKITEM_TOOLS.get_query, "Get a query by its ID or path. If a project is not specified, you will be prompted to select one.", {
768
+ project: z.string().optional().describe("The name or ID of the Azure DevOps project. Reuse from prior context if already known. If not provided, a project selection prompt will be shown."),
769
+ query: z.string().describe("The ID or path of the query to retrieve."),
770
+ expand: z
771
+ .enum(getEnumKeys(QueryExpand))
772
+ .optional()
773
+ .describe("Optional expand parameter to include additional details in the response. Defaults to 'None'."),
774
+ depth: z.coerce.number().default(0).describe("Optional depth parameter to specify how deep to expand the query. Defaults to 0."),
775
+ includeDeleted: z.boolean().default(false).describe("Whether to include deleted items in the query results. Defaults to false."),
776
+ useIsoDateFormat: z.boolean().default(false).describe("Whether to use ISO date format in the response. Defaults to false."),
777
+ }, async ({ project, query, expand, depth, includeDeleted, useIsoDateFormat }) => {
778
+ try {
779
+ const connection = await connectionProvider();
780
+ let resolvedProject = project;
781
+ if (!resolvedProject) {
782
+ const result = await elicitProject(server, connection, "Select the Azure DevOps project to retrieve the query from.");
783
+ if ("response" in result)
784
+ return result.response;
785
+ resolvedProject = result.resolved;
786
+ }
787
+ const workItemApi = await connection.getWorkItemTrackingApi();
788
+ const queryDetails = await workItemApi.getQuery(resolvedProject, query, safeEnumConvert(QueryExpand, expand), depth, includeDeleted, useIsoDateFormat);
789
+ return {
790
+ content: [{ type: "text", text: JSON.stringify(queryDetails, null, 2) }],
791
+ };
792
+ }
793
+ catch (error) {
794
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
795
+ return {
796
+ content: [{ type: "text", text: `Error retrieving query: ${errorMessage}` }],
797
+ isError: true,
798
+ };
799
+ }
800
+ });
801
+ server.tool(WORKITEM_TOOLS.get_query_results_by_id, "Retrieve the results of a work item query given the query ID. Supports full or IDs-only response types.", {
802
+ id: z.string().describe("The ID of the query to retrieve results for."),
803
+ project: z.string().optional().describe("The name or ID of the Azure DevOps project. If not provided, the default project will be used."),
804
+ team: z.string().optional().describe("The name or ID of the Azure DevOps team. If not provided, the default team will be used."),
805
+ timePrecision: z.boolean().optional().describe("Whether to include time precision in the results. Defaults to false."),
806
+ top: z.coerce.number().default(50).describe("The maximum number of results to return. Defaults to 50."),
807
+ responseType: z.enum(["full", "ids"]).default("full").describe("Response type: 'full' returns complete query results (default), 'ids' returns only work item IDs for reduced payload size."),
808
+ }, async ({ id, project, team, timePrecision, top, responseType }) => {
809
+ try {
810
+ const connection = await connectionProvider();
811
+ const workItemApi = await connection.getWorkItemTrackingApi();
812
+ const teamContext = { project, team };
813
+ const queryResult = await workItemApi.queryById(id, teamContext, timePrecision, top);
814
+ // If ids mode, extract and return only the IDs
815
+ if (responseType === "ids") {
816
+ const ids = queryResult.workItems?.map((workItem) => workItem.id).filter((id) => id !== undefined) || [];
817
+ return {
818
+ content: [{ type: "text", text: JSON.stringify({ ids, count: ids.length }, null, 2) }],
819
+ };
820
+ }
821
+ // Default: return full query results
822
+ return {
823
+ content: [{ type: "text", text: JSON.stringify(queryResult, null, 2) }],
824
+ };
825
+ }
826
+ catch (error) {
827
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
828
+ return {
829
+ content: [{ type: "text", text: `Error retrieving query results: ${errorMessage}` }],
830
+ isError: true,
831
+ };
832
+ }
833
+ });
834
+ server.tool(WORKITEM_TOOLS.update_work_items_batch, "Update work items in batch", {
835
+ updates: z
836
+ .array(z.object({
837
+ op: z.enum(["Add", "Replace", "Remove"]).default("Add").describe("The operation to perform on the field."),
838
+ id: z.coerce.number().min(1).describe("The ID of the work item to update."),
839
+ path: z.string().describe("The path of the field to update, e.g., '/fields/System.Title'."),
840
+ value: z.string().describe("The new value for the field. This is required for 'add' and 'replace' operations, and should be omitted for 'remove' operations."),
841
+ format: z
842
+ .enum(["Html", "Markdown"])
843
+ .optional()
844
+ .describe("The format of the field value. Only to be used for large text fields. e.g., 'Html', 'Markdown'. Optional, defaults to 'Markdown'."),
845
+ }))
846
+ .describe("An array of updates to apply to work items. Each update should include the operation (op), work item ID (id), field path (path), and new value (value)."),
847
+ }, async ({ updates }) => {
848
+ try {
849
+ const connection = await connectionProvider();
850
+ const orgUrl = connection.serverUrl;
851
+ const accessToken = await tokenProvider();
852
+ // Extract unique IDs from the updates array
853
+ const uniqueIds = Array.from(new Set(updates.map((update) => update.id)));
854
+ const body = uniqueIds.map((id) => {
855
+ const workItemUpdates = updates.filter((update) => update.id === id);
856
+ const operations = workItemUpdates.map(({ op, path, value, format }) => ({
857
+ op: op,
858
+ path: path,
859
+ value: encodeFormattedValue(value, format),
860
+ }));
861
+ // Add format operations for Markdown fields
862
+ workItemUpdates.forEach(({ path, value, format }) => {
863
+ if (format === "Markdown" && value && value.length > 100) {
864
+ operations.push({
865
+ op: "Add",
866
+ path: `/multilineFieldsFormat${path.replace("/fields", "")}`,
867
+ value: "Markdown",
868
+ });
869
+ }
870
+ });
871
+ return {
872
+ method: "PATCH",
873
+ uri: `/_apis/wit/workitems/${id}?api-version=${batchApiVersion}`,
874
+ headers: {
875
+ "Content-Type": "application/json-patch+json",
876
+ },
877
+ body: operations,
878
+ };
879
+ });
880
+ const response = await fetch(`${orgUrl}/_apis/wit/$batch?api-version=${batchApiVersion}`, {
881
+ method: "PATCH",
882
+ headers: {
883
+ "Authorization": `Bearer ${accessToken}`,
884
+ "Content-Type": "application/json",
885
+ "User-Agent": userAgentProvider(),
886
+ },
887
+ body: JSON.stringify(body),
888
+ });
889
+ if (!response.ok) {
890
+ throw new Error(`Failed to update work items in batch: ${response.statusText}`);
891
+ }
892
+ const result = await response.json();
893
+ return {
894
+ content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
895
+ };
896
+ }
897
+ catch (error) {
898
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
899
+ return {
900
+ content: [{ type: "text", text: `Error updating work items in batch: ${errorMessage}` }],
901
+ isError: true,
902
+ };
903
+ }
904
+ });
905
+ server.tool(WORKITEM_TOOLS.work_items_link, "Link work items together in batch. If a project is not specified, you will be prompted to select one.", {
906
+ project: z.string().optional().describe("The name or ID of the Azure DevOps project. Reuse from prior context if already known. If not provided, a project selection prompt will be shown."),
907
+ updates: z
908
+ .array(z.object({
909
+ id: z.coerce.number().min(1).describe("The ID of the work item to update."),
910
+ linkToId: z.coerce.number().min(1).describe("The ID of the work item to link to."),
911
+ type: z
912
+ .enum(["parent", "child", "duplicate", "duplicate of", "related", "successor", "predecessor", "tested by", "tests", "affects", "affected by"])
913
+ .default("related")
914
+ .describe("Type of link to create between the work items. Options include 'parent', 'child', 'duplicate', 'duplicate of', 'related', 'successor', 'predecessor', 'tested by', 'tests', 'affects', and 'affected by'. Defaults to 'related'."),
915
+ comment: z.string().optional().describe("Optional comment to include with the link. This can be used to provide additional context for the link being created."),
916
+ }))
917
+ .describe(""),
918
+ }, async ({ project, updates }) => {
919
+ try {
920
+ const connection = await connectionProvider();
921
+ let resolvedProject = project;
922
+ if (!resolvedProject) {
923
+ const result = await elicitProject(server, connection, "Select the Azure DevOps project to link work items in.");
924
+ if ("response" in result)
925
+ return result.response;
926
+ resolvedProject = result.resolved;
927
+ }
928
+ const orgUrl = connection.serverUrl;
929
+ const accessToken = await tokenProvider();
930
+ // Extract unique IDs from the updates array
931
+ const uniqueIds = Array.from(new Set(updates.map((update) => update.id)));
932
+ const body = uniqueIds.map((id) => ({
933
+ method: "PATCH",
934
+ uri: `/_apis/wit/workitems/${id}?api-version=${batchApiVersion}`,
935
+ headers: {
936
+ "Content-Type": "application/json-patch+json",
937
+ },
938
+ body: updates
939
+ .filter((update) => update.id === id)
940
+ .map(({ linkToId, type, comment }) => ({
941
+ op: "add",
942
+ path: "/relations/-",
943
+ value: {
944
+ rel: `${getLinkTypeFromName(type)}`,
945
+ url: `${orgUrl}/${resolvedProject}/_apis/wit/workItems/${linkToId}`,
946
+ attributes: {
947
+ comment: comment || "",
948
+ },
949
+ },
950
+ })),
951
+ }));
952
+ const response = await fetch(`${orgUrl}/_apis/wit/$batch?api-version=${batchApiVersion}`, {
953
+ method: "PATCH",
954
+ headers: {
955
+ "Authorization": `Bearer ${accessToken}`,
956
+ "Content-Type": "application/json",
957
+ "User-Agent": userAgentProvider(),
958
+ },
959
+ body: JSON.stringify(body),
960
+ });
961
+ if (!response.ok) {
962
+ throw new Error(`Failed to update work items in batch: ${response.statusText}`);
963
+ }
964
+ const result = await response.json();
965
+ return {
966
+ content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
967
+ };
968
+ }
969
+ catch (error) {
970
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
971
+ return {
972
+ content: [{ type: "text", text: `Error linking work items: ${errorMessage}` }],
973
+ isError: true,
974
+ };
975
+ }
976
+ });
977
+ server.tool(WORKITEM_TOOLS.work_item_unlink, "Remove one or many links from a single work item. If a project is not specified, you will be prompted to select one.", {
978
+ project: z.string().optional().describe("The name or ID of the Azure DevOps project. Reuse from prior context if already known. If not provided, a project selection prompt will be shown."),
979
+ id: z.coerce.number().min(1).describe("The ID of the work item to remove the links from."),
980
+ type: z
981
+ .enum(["parent", "child", "duplicate", "duplicate of", "related", "successor", "predecessor", "tested by", "tests", "affects", "affected by", "artifact"])
982
+ .default("related")
983
+ .describe("Type of link to remove. Options include 'parent', 'child', 'duplicate', 'duplicate of', 'related', 'successor', 'predecessor', 'tested by', 'tests', 'affects', 'affected by', and 'artifact'. Defaults to 'related'."),
984
+ url: z.string().optional().describe("Optional URL to match for the link to remove. If not provided, all links of the specified type will be removed."),
985
+ }, async ({ project, id, type, url }) => {
986
+ try {
987
+ const connection = await connectionProvider();
988
+ let resolvedProject = project;
989
+ if (!resolvedProject) {
990
+ const result = await elicitProject(server, connection, "Select the Azure DevOps project to unlink work items in.");
991
+ if ("response" in result)
992
+ return result.response;
993
+ resolvedProject = result.resolved;
994
+ }
995
+ const workItemApi = await connection.getWorkItemTrackingApi();
996
+ const workItem = await workItemApi.getWorkItem(id, undefined, undefined, WorkItemExpand.Relations, resolvedProject);
997
+ const relations = workItem.relations ?? [];
998
+ const linkType = getLinkTypeFromName(type);
999
+ let relationIndexes = [];
1000
+ if (url && url.trim().length > 0) {
1001
+ // If url is provided, find relations matching both rel type and url
1002
+ relationIndexes = relations.map((relation, idx) => (relation.rel === linkType && relation.url === url ? idx : -1)).filter((idx) => idx !== -1);
1003
+ }
1004
+ else {
1005
+ // If url is not provided, find all relations matching rel type
1006
+ relationIndexes = relations.map((relation, idx) => (relation.rel === linkType ? idx : -1)).filter((idx) => idx !== -1);
1007
+ }
1008
+ if (relationIndexes.length === 0) {
1009
+ return {
1010
+ content: [{ type: "text", text: `No matching relations found for link type '${type}'${url ? ` and URL '${url}'` : ""}.\n${JSON.stringify(relations, null, 2)}` }],
1011
+ isError: true,
1012
+ };
1013
+ }
1014
+ // Get the relations that will be removed for logging
1015
+ const removedRelations = relationIndexes.map((idx) => relations[idx]);
1016
+ // Sort indexes in descending order to avoid index shifting when removing
1017
+ relationIndexes.sort((a, b) => b - a);
1018
+ const apiUpdates = relationIndexes.map((idx) => ({
1019
+ op: "remove",
1020
+ path: `/relations/${idx}`,
1021
+ }));
1022
+ const updatedWorkItem = await workItemApi.updateWorkItem(null, apiUpdates, id, resolvedProject);
1023
+ return {
1024
+ content: [
1025
+ {
1026
+ type: "text",
1027
+ text: `Removed ${removedRelations.length} link(s) of type '${type}':\n` +
1028
+ JSON.stringify(removedRelations, null, 2) +
1029
+ `\n\nUpdated work item result:\n` +
1030
+ JSON.stringify(updatedWorkItem, null, 2),
1031
+ },
1032
+ ],
1033
+ isError: false,
1034
+ };
1035
+ }
1036
+ catch (error) {
1037
+ return {
1038
+ content: [
1039
+ {
1040
+ type: "text",
1041
+ text: `Error unlinking work item: ${error instanceof Error ? error.message : "Unknown error occurred"}`,
1042
+ },
1043
+ ],
1044
+ isError: true,
1045
+ };
1046
+ }
1047
+ });
1048
+ server.tool(WORKITEM_TOOLS.add_artifact_link, "Add artifact links (repository, branch, commit, builds) to work items. You can either provide the full vstfs URI or the individual components to build it automatically. If a project is not specified, you will be prompted to select one.", {
1049
+ workItemId: z.coerce.number().min(1).describe("The ID of the work item to add the artifact link to."),
1050
+ project: z.string().optional().describe("The name or ID of the Azure DevOps project. Reuse from prior context if already known. If not provided, a project selection prompt will be shown."),
1051
+ // Option 1: Provide full URI directly
1052
+ artifactUri: z.string().optional().describe("The complete VSTFS URI of the artifact to link. If provided, individual component parameters are ignored."),
1053
+ // Option 2: Provide individual components to build URI automatically based on linkType
1054
+ projectId: z.string().optional().describe("The project ID (GUID) containing the artifact. Required for Git artifacts when artifactUri is not provided."),
1055
+ repositoryId: z.string().optional().describe("The repository ID (GUID) containing the artifact. Required for Git artifacts when artifactUri is not provided."),
1056
+ branchName: z.string().optional().describe("The branch name (e.g., 'main'). Required when linkType is 'Branch'."),
1057
+ commitId: z.string().optional().describe("The commit SHA hash. Required when linkType is 'Fixed in Commit'."),
1058
+ pullRequestId: z.coerce.number().min(1).optional().describe("The pull request ID. Required when linkType is 'Pull Request'."),
1059
+ buildId: z.coerce.number().min(1).optional().describe("The build ID. Required when linkType is 'Build', 'Found in build', or 'Integrated in build'."),
1060
+ linkType: z
1061
+ .enum([
1062
+ "Branch",
1063
+ "Build",
1064
+ "Fixed in Changeset",
1065
+ "Fixed in Commit",
1066
+ "Found in build",
1067
+ "Integrated in build",
1068
+ "Model Link",
1069
+ "Pull Request",
1070
+ "Related Workitem",
1071
+ "Result Attachment",
1072
+ "Source Code File",
1073
+ "Tag",
1074
+ "Test Result",
1075
+ "Wiki",
1076
+ ])
1077
+ .default("Branch")
1078
+ .describe("Type of artifact link, defaults to 'Branch'. This determines both the link type and how to build the VSTFS URI from individual components."),
1079
+ comment: z.string().optional().describe("Comment to include with the artifact link."),
1080
+ }, async ({ workItemId, project, artifactUri, projectId, repositoryId, branchName, commitId, pullRequestId, buildId, linkType, comment }) => {
1081
+ try {
1082
+ const connection = await connectionProvider();
1083
+ let resolvedProject = project;
1084
+ if (!resolvedProject) {
1085
+ const result = await elicitProject(server, connection, "Select the Azure DevOps project to add the artifact link in.");
1086
+ if ("response" in result)
1087
+ return result.response;
1088
+ resolvedProject = result.resolved;
1089
+ }
1090
+ const workItemTrackingApi = await connection.getWorkItemTrackingApi();
1091
+ let finalArtifactUri;
1092
+ if (artifactUri) {
1093
+ // Use the provided full URI
1094
+ finalArtifactUri = artifactUri;
1095
+ }
1096
+ else {
1097
+ // Build the URI from individual components based on linkType
1098
+ switch (linkType) {
1099
+ case "Branch":
1100
+ if (!projectId || !repositoryId || !branchName) {
1101
+ return {
1102
+ content: [{ type: "text", text: "For 'Branch' links, 'projectId', 'repositoryId', and 'branchName' are required." }],
1103
+ isError: true,
1104
+ };
1105
+ }
1106
+ finalArtifactUri = `vstfs:///Git/Ref/${encodeURIComponent(projectId)}%2F${encodeURIComponent(repositoryId)}%2FGB${encodeURIComponent(branchName)}`;
1107
+ break;
1108
+ case "Fixed in Commit":
1109
+ if (!projectId || !repositoryId || !commitId) {
1110
+ return {
1111
+ content: [{ type: "text", text: "For 'Fixed in Commit' links, 'projectId', 'repositoryId', and 'commitId' are required." }],
1112
+ isError: true,
1113
+ };
1114
+ }
1115
+ finalArtifactUri = `vstfs:///Git/Commit/${encodeURIComponent(projectId)}%2F${encodeURIComponent(repositoryId)}%2F${encodeURIComponent(commitId)}`;
1116
+ break;
1117
+ case "Pull Request":
1118
+ if (!projectId || !repositoryId || pullRequestId === undefined) {
1119
+ return {
1120
+ content: [{ type: "text", text: "For 'Pull Request' links, 'projectId', 'repositoryId', and 'pullRequestId' are required." }],
1121
+ isError: true,
1122
+ };
1123
+ }
1124
+ finalArtifactUri = `vstfs:///Git/PullRequestId/${encodeURIComponent(projectId)}%2F${encodeURIComponent(repositoryId)}%2F${encodeURIComponent(pullRequestId.toString())}`;
1125
+ break;
1126
+ case "Build":
1127
+ case "Found in build":
1128
+ case "Integrated in build":
1129
+ if (buildId === undefined) {
1130
+ return {
1131
+ content: [{ type: "text", text: `For '${linkType}' links, 'buildId' is required.` }],
1132
+ isError: true,
1133
+ };
1134
+ }
1135
+ finalArtifactUri = `vstfs:///Build/Build/${encodeURIComponent(buildId.toString())}`;
1136
+ break;
1137
+ default:
1138
+ return {
1139
+ content: [{ type: "text", text: `URI building from components is not supported for link type '${linkType}'. Please provide the full 'artifactUri' instead.` }],
1140
+ isError: true,
1141
+ };
1142
+ }
1143
+ }
1144
+ // Create the patch document for adding an artifact link relation
1145
+ const patchDocument = [
1146
+ {
1147
+ op: "add",
1148
+ path: "/relations/-",
1149
+ value: {
1150
+ rel: "ArtifactLink",
1151
+ url: finalArtifactUri,
1152
+ attributes: {
1153
+ name: linkType,
1154
+ ...(comment && { comment }),
1155
+ },
1156
+ },
1157
+ },
1158
+ ];
1159
+ // Use the WorkItem API to update the work item with the new relation
1160
+ const workItem = await workItemTrackingApi.updateWorkItem({}, patchDocument, workItemId, resolvedProject);
1161
+ if (!workItem) {
1162
+ return { content: [{ type: "text", text: "Work item update failed" }], isError: true };
1163
+ }
1164
+ return {
1165
+ content: [
1166
+ {
1167
+ type: "text",
1168
+ text: JSON.stringify({
1169
+ workItemId,
1170
+ artifactUri: finalArtifactUri,
1171
+ linkType,
1172
+ comment: comment || null,
1173
+ success: true,
1174
+ }, null, 2),
1175
+ },
1176
+ ],
1177
+ };
1178
+ }
1179
+ catch (error) {
1180
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
1181
+ return {
1182
+ content: [{ type: "text", text: `Error adding artifact link to work item: ${errorMessage}` }],
1183
+ isError: true,
1184
+ };
1185
+ }
1186
+ });
1187
+ server.tool(WORKITEM_TOOLS.query_by_wiql, "Execute a WIQL (Work Item Query Language) query and return the matching work items. If a project is not specified, you will be prompted to select one.", {
1188
+ wiql: z.string().max(32768).describe('The WIQL query string to execute, e.g., "SELECT [System.Id], [System.Title] FROM WorkItems WHERE [System.TeamProject] = @project"'),
1189
+ project: z.string().optional().describe("The name or ID of the Azure DevOps project. Reuse from prior context if already known. If not provided, a project selection prompt will be shown."),
1190
+ team: z.string().optional().describe("The name or ID of the Azure DevOps team. If not provided, the default team context will be used."),
1191
+ timePrecision: z.boolean().optional().describe("Whether to include time precision in date fields. Defaults to false."),
1192
+ top: z.coerce.number().default(50).describe("The maximum number of results to return. Defaults to 50."),
1193
+ }, async ({ wiql, project, team, timePrecision, top }) => {
1194
+ try {
1195
+ const connection = await connectionProvider();
1196
+ let resolvedProject = project;
1197
+ if (!resolvedProject) {
1198
+ const result = await elicitProject(server, connection, "Select the Azure DevOps project to run the WIQL query against.");
1199
+ if ("response" in result)
1200
+ return result.response;
1201
+ resolvedProject = result.resolved;
1202
+ }
1203
+ const workItemApi = await connection.getWorkItemTrackingApi();
1204
+ const teamContext = { project: resolvedProject, team };
1205
+ const queryResult = await workItemApi.queryByWiql({ query: wiql }, teamContext, timePrecision, top);
1206
+ return createExternalContentResponse(queryResult, "wiql query results");
1207
+ }
1208
+ catch (error) {
1209
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
1210
+ return {
1211
+ content: [{ type: "text", text: `Error executing WIQL query: ${errorMessage}` }],
1212
+ isError: true,
1213
+ };
1214
+ }
1215
+ });
1216
+ server.tool(WORKITEM_TOOLS.get_work_item_attachment, "Download a work item attachment by its ID. By default returns the content as a base64-encoded resource. If savePath is provided, saves the file locally to that directory and returns the file path instead. Useful for viewing images (e.g. screenshots) or other files attached to work items such as bugs. If a project is not specified, you will be prompted to select one.", {
1217
+ project: z.string().optional().describe("The name or ID of the Azure DevOps project. Reuse from prior context if already known. If not provided, a project selection prompt will be shown."),
1218
+ attachmentId: z.string().describe("The GUID of the attachment. Found in the attachment URL: https://dev.azure.com/{org}/{project}/_apis/wit/attachments/{attachmentId}"),
1219
+ fileName: z.string().optional().describe("The file name of the attachment, e.g. 'screenshot.png'. Used to determine the MIME type or the saved file's name."),
1220
+ savePath: z
1221
+ .string()
1222
+ .optional()
1223
+ .describe("Optional local directory path where the file should be saved. Must be a relative path (e.g. 'temp' or 'downloads/attachments'); absolute paths and path traversals are not allowed. If provided, saves the attachment to this directory and returns the file path. If omitted, returns the content as a base64-encoded resource."),
1224
+ }, async ({ project, attachmentId, fileName, savePath }) => {
1225
+ const isAbsolutePath = (value) => path.posix.isAbsolute(value) || path.win32.isAbsolute(value);
1226
+ const hasDriveLetter = (value) => /^[a-zA-Z]:/.test(value);
1227
+ if (savePath !== undefined && (savePath.includes("..") || isAbsolutePath(savePath) || hasDriveLetter(savePath))) {
1228
+ throw new Error("Invalid savePath: absolute paths and path traversals are not allowed.");
1229
+ }
1230
+ if (fileName !== undefined && fileName.includes("..")) {
1231
+ throw new Error("Invalid fileName: path traversal is not allowed.");
1232
+ }
1233
+ try {
1234
+ const connection = await connectionProvider();
1235
+ let resolvedProject = project;
1236
+ if (!resolvedProject) {
1237
+ const result = await elicitProject(server, connection, "Select the Azure DevOps project to retrieve the work item attachment from.");
1238
+ if ("response" in result)
1239
+ return result.response;
1240
+ resolvedProject = result.resolved;
1241
+ }
1242
+ const workItemApi = await connection.getWorkItemTrackingApi();
1243
+ const stream = await workItemApi.getAttachmentContent(attachmentId, fileName, resolvedProject);
1244
+ const chunks = [];
1245
+ await new Promise((resolve, reject) => {
1246
+ stream.on("data", (chunk) => chunks.push(Buffer.from(chunk)));
1247
+ stream.on("end", resolve);
1248
+ stream.on("error", reject);
1249
+ });
1250
+ const buffer = Buffer.concat(chunks);
1251
+ if (savePath) {
1252
+ const resolvedFileName = fileName ?? attachmentId;
1253
+ const localFilePath = path.join(savePath, resolvedFileName);
1254
+ if (fs.existsSync(localFilePath)) {
1255
+ throw new Error(`File already exists: ${localFilePath}`);
1256
+ }
1257
+ fs.writeFileSync(localFilePath, buffer);
1258
+ return {
1259
+ content: [{ type: "text", text: `Attachment saved to: ${localFilePath}` }],
1260
+ };
1261
+ }
1262
+ const mimeType = getMimeType(fileName);
1263
+ if (mimeType.startsWith("text/")) {
1264
+ return {
1265
+ content: [{ type: "text", text: buffer.toString("utf-8") }],
1266
+ };
1267
+ }
1268
+ const base64Data = buffer.toString("base64");
1269
+ return {
1270
+ content: [
1271
+ {
1272
+ type: "resource",
1273
+ resource: {
1274
+ uri: `data:${mimeType};base64,${base64Data}`,
1275
+ mimeType,
1276
+ blob: base64Data,
1277
+ },
1278
+ },
1279
+ ],
1280
+ };
1281
+ }
1282
+ catch (error) {
1283
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
1284
+ return {
1285
+ content: [{ type: "text", text: `Error retrieving work item attachment: ${errorMessage}` }],
1286
+ isError: true,
1287
+ };
1288
+ }
1289
+ });
1290
+ }
1291
+ function getMimeType(fileName) {
1292
+ const ext = fileName?.split(".").pop()?.toLowerCase();
1293
+ const mimeTypes = {
1294
+ png: "image/png",
1295
+ jpg: "image/jpeg",
1296
+ jpeg: "image/jpeg",
1297
+ gif: "image/gif",
1298
+ bmp: "image/bmp",
1299
+ svg: "image/svg+xml",
1300
+ webp: "image/webp",
1301
+ pdf: "application/pdf",
1302
+ txt: "text/plain",
1303
+ md: "text/markdown",
1304
+ markdown: "text/markdown",
1305
+ csv: "text/csv",
1306
+ html: "text/html",
1307
+ htm: "text/html",
1308
+ xml: "text/xml",
1309
+ json: "application/json",
1310
+ yaml: "text/yaml",
1311
+ yml: "text/yaml",
1312
+ zip: "application/zip",
1313
+ };
1314
+ return (ext && mimeTypes[ext]) ?? "application/octet-stream";
1315
+ }
1316
+ export { WORKITEM_TOOLS, configureWorkItemTools };