add-work-item-comment
Enable team collaboration by adding comments to Azure DevOps work items. Specify work item ID and comment text to enhance task communication and tracking within projects.
Instructions
Add a comment to an existing work item in Azure DevOps
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| comment | Yes | Comment text to add | |
| id | Yes | Work item ID to add comment to |
Implementation Reference
- src/handlers/tool-handlers.ts:862-910 (handler)Implements the core logic for adding a comment to an Azure DevOps work item using the comments REST API endpoint (api-version=6.0-preview.4). Validates inputs, makes authenticated POST request, and formats success/error responses.private async addWorkItemComment(args: any): Promise<any> { if (!args.id) { throw new Error('Work item ID is required'); } if (!args.comment) { throw new Error('Comment text is required'); } try { const commentData = { text: args.comment }; // Use API version 6.0-preview.4 for comments - required for work item comments endpoint const endpoint = `/wit/workitems/${args.id}/comments?api-version=6.0-preview.4`; console.log(`[DEBUG] Adding comment to work item ${args.id} with endpoint: ${endpoint}`); const result = await this.makeApiRequest( endpoint, 'POST', commentData ); return { content: [{ type: 'text', text: JSON.stringify({ success: true, comment: { id: result.id, workItemId: args.id, text: result.text, createdBy: result.createdBy?.displayName || result.createdBy, createdDate: result.createdDate, url: result.url }, message: `Successfully added comment to work item ${args.id}` }, null, 2), }], }; } catch (error) { // Provide specific guidance for API version issues if (error instanceof Error && error.message.includes('preview')) { throw new Error(`Failed to add work item comment - API version issue: ${error.message}. Try using a different API version.`); } throw new Error(`Failed to add work item comment: ${error instanceof Error ? error.message : 'Unknown error'}`); } }
- src/handlers/tool-handlers.ts:39-40 (registration)Switch case that routes 'add-work-item-comment' tool calls to the corresponding handler method.case 'add-work-item-comment': return await this.addWorkItemComment(args || {});
- src/index.ts:211-228 (schema)Defines the tool name, description, and input schema (requiring 'id' number and 'comment' string) advertised in the ListTools response.{ name: 'add-work-item-comment', description: 'Add a comment to an existing work item in Azure DevOps', inputSchema: { type: 'object', properties: { id: { type: 'number', description: 'Work item ID to add comment to', }, comment: { type: 'string', description: 'Comment text to add', }, }, required: ['id', 'comment'], }, },
- src/handlers/tool-handlers.ts:71-131 (helper)Generic helper method used by all tool handlers (including addWorkItemComment) to make authenticated HTTPS requests to Azure DevOps REST APIs.private async makeApiRequest(endpoint: string, method: string = 'GET', body?: any): Promise<any> { if (!this.currentConfig) { throw new Error('No configuration available'); } const { organizationUrl, pat, project } = this.currentConfig; const baseUrl = `${organizationUrl}/${project}/_apis`; const requestUrl = `${baseUrl}${endpoint}`; return new Promise((resolve, reject) => { const urlParts = new url.URL(requestUrl); const postData = body ? JSON.stringify(body) : undefined; const options = { hostname: urlParts.hostname, port: urlParts.port || 443, path: urlParts.pathname + urlParts.search, method, headers: { 'Authorization': `Basic ${Buffer.from(`:${pat}`).toString('base64')}`, 'Content-Type': method === 'PATCH' && endpoint.includes('/wit/workitems/') ? 'application/json-patch+json' : 'application/json', 'Accept': 'application/json', // For preview APIs, we need to properly handle the API version in the URL, not headers ...(postData && { 'Content-Length': Buffer.byteLength(postData) }), }, }; const req = https.request(options, (res) => { let data = ''; res.on('data', (chunk) => { data += chunk; }); res.on('end', () => { try { if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) { const result = data ? JSON.parse(data) : {}; resolve(result); } else { reject(new Error(`HTTP ${res.statusCode}: ${data}`)); } } catch (error) { reject(new Error(`Failed to parse response: ${error}`)); } }); }); req.on('error', (error) => { reject(new Error(`Request failed: ${error.message}`)); }); if (postData) { req.write(postData); } req.end(); }); }