ado_pr_changes
Fetch detailed changes and full diff content from an Azure DevOps Pull Request using the Azure DevOps Node API for efficient code review and integration.
Instructions
Fetches changes from an Azure DevOps Pull Request with full diff content using the Azure DevOps Node API.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| organizationId | Yes | Optional organization identifier to load specific configuration settings. | |
| pullRequestId | Yes | The numeric ID of the Pull Request (as a string). |
Implementation Reference
- The main execution handler for the 'ado_pr_changes' tool. It fetches ADO configuration, PR details, latest iteration changes, formats the output, and returns the result or error.export const adoPrChangesHandler = async (input: AdoPrChangesInput): Promise<CallToolResult> => { try { const config = await getAdoConfig(input.organizationId); if (!config.organization || !config.project) { return { content: [{ type: "text", text: "ERROR: Missing required configuration parameters (organization, project). Please ensure Azure CLI is authenticated and configuration is set." }], isError: true }; } const { gitApi } = await getAdoConnectionAndApi(config.organization); const pullRequestIdNum = parseInt(input.pullRequestId, 10); const pr = await getPrDetails(gitApi, pullRequestIdNum, config.project); if (!pr || !pr.repository?.id || !pr.sourceRefName || !pr.targetRefName) { return { content: [{ type: "text", text: "ERROR: Could not retrieve valid PR details." }], isError: true }; } const repositoryId = pr.repository.id; const sourceBranch = pr.sourceRefName.replace("refs/heads/", ""); const targetBranch = pr.targetRefName.replace("refs/heads/", ""); const { iterationChanges, latestIteration } = await getLatestPrIterationChanges(gitApi, repositoryId, pullRequestIdNum, config.project); // Pass necessary arguments to the formatter const output = await formatPrChangesOutput( gitApi, repositoryId, config.project, input.pullRequestId, sourceBranch, targetBranch, iterationChanges, latestIteration ); return { content: [{ type: "text", text: output }] }; } catch (error: any) { console.error("Error processing ADO PR changes:", error); return { content: [{ type: "text", text: `ERROR: Failed to get PR changes. ${error.message}` }], isError: true }; } };
- Zod schema defining the input parameters for the tool: required pullRequestId (string of digits) and optional organizationId.export const adoPrChangesSchema = z.object({ pullRequestId: z.string().min(1).regex(/^\d+$/).describe("The numeric ID of the Pull Request (as a string)."), organizationId: z.string().min(1).describe("Optional organization identifier to load specific configuration settings.") });
- src/toolRegistrar.ts:19-24 (registration)Tool registration in the MCP server, associating the name 'ado_pr_changes', description, schema, and handler function.server.tool( "ado_pr_changes", "Fetches changes from an Azure DevOps Pull Request with full diff content using the Azure DevOps Node API.", adoPrChangesSchema.shape, // Use .shape here adoPrChangesHandler );
- Key helper function that retrieves the iterations for a PR and fetches changes from the latest iteration, providing the core data for diffs.export async function getLatestPrIterationChanges( gitApi: GitApi, repositoryId: string, pullRequestId: number, project: string ): Promise<{ iterationChanges: GitInterfaces.GitPullRequestIterationChanges, latestIteration: GitInterfaces.GitPullRequestIteration }> { const iterations = await gitApi.getPullRequestIterations(repositoryId, pullRequestId, project); if (!iterations || iterations.length === 0) { throw new Error("Could not retrieve iterations for the PR."); } const latestIteration = iterations[iterations.length - 1]; if (!latestIteration?.id) { throw new Error("Could not get latest iteration ID."); } const iterationChanges = await gitApi.getPullRequestIterationChanges(repositoryId, pullRequestId, latestIteration.id, project); if (!iterationChanges) { throw new Error("Could not retrieve changes for the latest PR iteration."); } return { iterationChanges, latestIteration }; }
- Helper to establish Azure DevOps connection and Git API using Azure CLI authentication.export async function getAdoConnectionAndApi(organization: string): Promise<{ connection: azdev.WebApi, gitApi: GitApi }> { const orgUrl = `https://dev.azure.com/${organization}`; // Get access token from Azure CLI const accessToken = await getAzureCliAccessToken(organization); const authHandler = azdev.getBearerHandler(accessToken); const connection = new azdev.WebApi(orgUrl, authHandler); const gitApi: GitApi = await connection.getGitApi(); return { connection, gitApi }; }