wit_link_work_item_to_pull_request
Link a specific work item to an existing pull request in Azure DevOps using project, repository, and work item IDs for streamlined tracking and collaboration.
Instructions
Link a single work item to an existing pull request.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| projectId | Yes | The project ID of the Azure DevOps project (note: project name is not valid). | |
| pullRequestId | Yes | The ID of the pull request to link to. | |
| pullRequestProjectId | No | The project ID containing the pull request. If not provided, defaults to the work item's project ID (for same-project linking). | |
| repositoryId | Yes | The ID of the repository containing the pull request. Do not use the repository name here, use the ID instead. | |
| workItemId | Yes | The ID of the work item to link to the pull request. |
Implementation Reference
- src/tools/workitems.ts:24-24 (registration)Maps the friendly name 'link_work_item_to_pull_request' to the MCP tool name 'wit_link_work_item_to_pull_request' in the WORKITEM_TOOLS constant used for registration.link_work_item_to_pull_request: "wit_link_work_item_to_pull_request",
- src/tools/workitems.ts:399-405 (schema)Zod schema defining the input parameters for the tool.{ projectId: z.string().describe("The project ID of the Azure DevOps project (note: project name is not valid)."), repositoryId: z.string().describe("The ID of the repository containing the pull request. Do not use the repository name here, use the ID instead."), pullRequestId: z.number().describe("The ID of the pull request to link to."), workItemId: z.number().describe("The ID of the work item to link to the pull request."), 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)."), },
- src/tools/workitems.ts:406-464 (handler)The handler function that executes the tool: constructs a VSTFS URI for the pull request (vstfs:///Git/PullRequestId/...), creates a PATCH document to add an 'ArtifactLink' relation to the work item, and updates the work item via the Azure DevOps WorkItemTrackingApi.async ({ projectId, repositoryId, pullRequestId, workItemId, pullRequestProjectId }) => { try { const connection = await connectionProvider(); const workItemTrackingApi = await connection.getWorkItemTrackingApi(); // Create artifact link relation using vstfs format // Format: vstfs:///Git/PullRequestId/{project}/{repositoryId}/{pullRequestId} const artifactProjectId = pullRequestProjectId && pullRequestProjectId.trim() !== "" ? pullRequestProjectId : projectId; const artifactPathValue = `${artifactProjectId}/${repositoryId}/${pullRequestId}`; const vstfsUrl = `vstfs:///Git/PullRequestId/${encodeURIComponent(artifactPathValue)}`; // Use the PATCH document format for adding a relation const patchDocument = [ { op: "add", path: "/relations/-", value: { rel: "ArtifactLink", url: vstfsUrl, attributes: { name: "Pull Request", }, }, }, ]; // Use the WorkItem API to update the work item with the new relation const workItem = await workItemTrackingApi.updateWorkItem({}, patchDocument, workItemId, projectId); if (!workItem) { return { content: [{ type: "text", text: "Work item update failed" }], isError: true }; } return { content: [ { type: "text", text: JSON.stringify( { workItemId, pullRequestId, success: true, }, null, 2 ), }, ], }; } catch (error) { const errorMessage = error instanceof Error ? error.message : "Unknown error occurred"; return { content: [{ type: "text", text: `Error linking work item to pull request: ${errorMessage}` }], isError: true, }; } } );