get-work-items
Retrieve specific work items from Azure DevOps using WIQL queries or IDs, and specify fields to include in the response, simplifying integration across multiple organizations and projects.
Instructions
Get work items from Azure DevOps
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| fields | No | Fields to include in the response | |
| ids | No | Specific work item IDs to retrieve | |
| wiql | No | Work Item Query Language (WIQL) query |
Implementation Reference
- src/handlers/tool-handlers.ts:136-192 (handler)The primary handler function implementing the get-work-items tool. Retrieves work items via WIQL query, specific IDs, or default recent items from the project. Uses Azure DevOps REST API (WIT endpoints).private async getWorkItems(args: any): Promise<any> { try { let result; if (args.wiql) { // Query using WIQL const wiqlResult = await this.makeApiRequest('/wit/wiql?api-version=7.1', 'POST', { query: args.wiql }); if (wiqlResult.workItems && wiqlResult.workItems.length > 0) { const ids = wiqlResult.workItems.map((wi: any) => wi.id); const fields = args.fields ? args.fields.join(',') : undefined; const fieldsParam = fields ? `&fields=${encodeURIComponent(fields)}` : ''; result = await this.makeApiRequest( `/wit/workitems?ids=${ids.join(',')}${fieldsParam}&api-version=7.1` ); } else { result = { value: [] }; } } else if (args.ids && args.ids.length > 0) { // Get specific work items by ID const fields = args.fields ? args.fields.join(',') : undefined; const fieldsParam = fields ? `&fields=${encodeURIComponent(fields)}` : ''; result = await this.makeApiRequest( `/wit/workitems?ids=${args.ids.join(',')}${fieldsParam}&api-version=7.1` ); } else { // Default query for recent work items const defaultWiql = `SELECT [System.Id], [System.Title], [System.State], [System.AssignedTo] FROM WorkItems WHERE [System.TeamProject] = '${this.currentConfig!.project}' ORDER BY [System.ChangedDate] DESC`; const wiqlResult = await this.makeApiRequest('/wit/wiql?api-version=7.1', 'POST', { query: defaultWiql }); if (wiqlResult.workItems && wiqlResult.workItems.length > 0) { const ids = wiqlResult.workItems.slice(0, 20).map((wi: any) => wi.id); // Limit to 20 items result = await this.makeApiRequest( `/wit/workitems?ids=${ids.join(',')}&api-version=7.1` ); } else { result = { value: [] }; } } return { content: [{ type: 'text', text: JSON.stringify(result, null, 2), }], }; } catch (error) { throw new Error(`Failed to get work items: ${error instanceof Error ? error.message : 'Unknown error'}`); } }
- src/handlers/tool-handlers.ts:33-34 (registration)Switch case in handleToolCall method that registers and dispatches 'get-work-items' tool calls to the getWorkItems handler.case 'get-work-items': return await this.getWorkItems(args || {});
- src/index.ts:101-121 (schema)Tool registration in MCP listTools response, including name, description, and input schema validation for parameters (wiql, ids, fields).name: 'get-work-items', description: 'Get work items from Azure DevOps', inputSchema: { type: 'object', properties: { wiql: { type: 'string', description: 'Work Item Query Language (WIQL) query', }, ids: { type: 'array', items: { type: 'number' }, description: 'Specific work item IDs to retrieve', }, fields: { type: 'array', items: { type: 'string' }, description: 'Fields to include in the response', }, }, },
- src/index.ts:100-122 (registration)The tool object added to the tools array returned by listToolsRequestHandler, registering the tool with MCP server.{ name: 'get-work-items', description: 'Get work items from Azure DevOps', inputSchema: { type: 'object', properties: { wiql: { type: 'string', description: 'Work Item Query Language (WIQL) query', }, ids: { type: 'array', items: { type: 'number' }, description: 'Specific work item IDs to retrieve', }, fields: { type: 'array', items: { type: 'string' }, description: 'Fields to include in the response', }, }, }, },
- src/handlers/tool-handlers.ts:71-131 (helper)Shared helper method used by getWorkItems (and other tools) to make authenticated API requests to Azure DevOps using PAT.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(); }); }