get_work_item
Retrieve detailed information for a specific work item in Azure DevOps by providing its ID, enabling efficient tracking and management of project tasks.
Instructions
Get details of a specific work item
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| expand | No | The level of detail to include in the response. Defaults to "all" if not specified. | |
| workItemId | Yes | The ID of the work item |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"expand": {
"description": "The level of detail to include in the response. Defaults to \"all\" if not specified.",
"enum": [
0,
1,
2,
3,
4
],
"type": "number"
},
"workItemId": {
"description": "The ID of the work item",
"type": "number"
}
},
"required": [
"workItemId"
],
"type": "object"
}
Implementation Reference
- Core handler function that retrieves a work item by ID from Azure DevOps, expands it with all possible fields using cached work item type fields, and handles errors.export async function getWorkItem( connection: WebApi, workItemId: number, expand: string = 'all', ): Promise<WorkItem> { try { const witApi = await connection.getWorkItemTrackingApi(); const workItem = await witApi.getWorkItem( workItemId, undefined, undefined, expandMap[expand.toLowerCase()], ); if (!workItem) { throw new AzureDevOpsResourceNotFoundError( `Work item '${workItemId}' not found`, ); } // Extract project and work item type to get all possible fields const projectName = workItem.fields?.['System.TeamProject']; const workItemType = workItem.fields?.['System.WorkItemType']; if (!projectName || !workItemType) { // If we can't determine the project or type, return the original work item return workItem; } // Get all possible fields for this work item type const allFields = workItemTypeFieldsCache[projectName.toString()]?.[ workItemType.toString() ] ?? (await witApi.getWorkItemTypeFieldsWithReferences( projectName.toString(), workItemType.toString(), WorkItemTypeFieldsExpandLevel.All, )); workItemTypeFieldsCache[projectName.toString()] = { ...workItemTypeFieldsCache[projectName.toString()], [workItemType.toString()]: allFields, }; // Create a new work item object with all fields const enhancedWorkItem = { ...workItem }; // Initialize fields object if it doesn't exist if (!enhancedWorkItem.fields) { enhancedWorkItem.fields = {}; } // Set null for all potential fields that don't have values for (const field of allFields) { if ( field.referenceName && !(field.referenceName in enhancedWorkItem.fields) ) { enhancedWorkItem.fields[field.referenceName] = field.defaultValue; } } return enhancedWorkItem; } catch (error) { if (error instanceof AzureDevOpsError) { throw error; } throw new Error( `Failed to get work item: ${error instanceof Error ? error.message : String(error)}`, ); }
- Zod schema defining the input parameters for the get_work_item tool: workItemId (required number) and expand (optional enum).export const GetWorkItemSchema = z.object({ workItemId: z.number().describe('The ID of the work item'), expand: z .enum(['none', 'relations', 'fields', 'links', 'all']) .optional() .describe( 'The level of detail to include in the response. Defaults to "all" if not specified.', ), });
- src/features/work-items/tool-definitions.ts:20-24 (registration)Registers the 'get_work_item' tool in the workItemsTools array, specifying its name, description, and input schema converted to JSON schema.{ name: 'get_work_item', description: 'Get details of a specific work item', inputSchema: zodToJsonSchema(GetWorkItemSchema), },
- src/features/work-items/index.ts:65-75 (registration)Request handler switch case that matches the tool name 'get_work_item', parses arguments using the schema, calls the getWorkItem handler, and formats the response.case 'get_work_item': { const args = GetWorkItemSchema.parse(request.params.arguments); const result = await getWorkItem( connection, args.workItemId, args.expand, ); return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }], }; }
- Helper mapping for string expand options to Azure DevOps WorkItemExpand enum values, used in the handler.const expandMap: Record<string, WorkItemExpand> = { none: WorkItemExpand.None, relations: WorkItemExpand.Relations, fields: WorkItemExpand.Fields, links: WorkItemExpand.Links, all: WorkItemExpand.All, };