@azure-devops/mcp 2.2.1-nightly.20251105 → 2.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/auth.js CHANGED
@@ -53,6 +53,15 @@ class OAuthAuthenticator {
53
53
  }
54
54
  function createAuthenticator(type, tenantId) {
55
55
  switch (type) {
56
+ case "envvar":
57
+ // Read token from fixed environment variable
58
+ return async () => {
59
+ const token = process.env["ADO_MCP_AUTH_TOKEN"];
60
+ if (!token) {
61
+ throw new Error("Environment variable 'ADO_MCP_AUTH_TOKEN' is not set or empty. Please set it with a valid Azure DevOps Personal Access Token.");
62
+ }
63
+ return token;
64
+ };
56
65
  case "azcli":
57
66
  case "env":
58
67
  if (type !== "env") {
@@ -0,0 +1 @@
1
+ export {};
package/dist/http.js ADDED
@@ -0,0 +1,52 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+ import express from "express";
4
+ import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
5
+ import { serverBuildAndConnect } from "./server.js";
6
+ import { packageVersion } from "./version.js";
7
+ const app = express();
8
+ app.use(express.json());
9
+ app.post('/mcp/:orgName', async (req, res) => {
10
+ // In stateless mode, create a new instance of transport and server for each request
11
+ // to ensure complete isolation. A single instance would cause request ID collisions
12
+ // when multiple clients connect concurrently.
13
+ try {
14
+ const transport = new StreamableHTTPServerTransport({
15
+ sessionIdGenerator: undefined,
16
+ });
17
+ const server = await serverBuildAndConnect(req.params.orgName, transport);
18
+ res.on('close', () => {
19
+ transport.close();
20
+ server.close();
21
+ });
22
+ await transport.handleRequest(req, res, req.body);
23
+ }
24
+ catch (error) {
25
+ console.error('Error handling MCP request:', error);
26
+ if (!res.headersSent) {
27
+ res.status(500).json({
28
+ jsonrpc: '2.0',
29
+ error: {
30
+ code: -32603,
31
+ message: 'Internal server error',
32
+ },
33
+ id: null,
34
+ });
35
+ }
36
+ }
37
+ });
38
+ app.get('/mcp/:orgName', async (req, res) => {
39
+ console.log('Received GET MCP request');
40
+ res.writeHead(405).end(JSON.stringify({
41
+ jsonrpc: "2.0",
42
+ error: {
43
+ code: -32000,
44
+ message: "Method not allowed."
45
+ },
46
+ id: null
47
+ }));
48
+ });
49
+ const PORT = 3000;
50
+ app.listen(PORT, () => {
51
+ console.log(`Azure DevOps MCP Server with http transport listening on port ${PORT}. Version: ${packageVersion}`);
52
+ });
package/dist/index.js CHANGED
@@ -38,9 +38,9 @@ const argv = yargs(hideBin(process.argv))
38
38
  })
39
39
  .option("authentication", {
40
40
  alias: "a",
41
- describe: "Type of authentication to use. Supported values are 'interactive', 'azcli' and 'env' (default: 'interactive')",
41
+ describe: "Type of authentication to use",
42
42
  type: "string",
43
- choices: ["interactive", "azcli", "env"],
43
+ choices: ["interactive", "azcli", "env", "envvar"],
44
44
  default: defaultAuthenticationType,
45
45
  })
46
46
  .option("tenant", {
File without changes
@@ -0,0 +1,73 @@
1
+ import * as fs from 'fs/promises';
2
+ import * as os from 'os';
3
+ import * as path from 'path';
4
+ const CACHE_FILE = path.join(os.homedir(), '.ado_orgs.cache');
5
+ const CACHE_TTL_MS = 7 * 24 * 60 * 60 * 1000; // 1 week in milliseconds
6
+ async function loadCache() {
7
+ try {
8
+ const cacheData = await fs.readFile(CACHE_FILE, 'utf-8');
9
+ return JSON.parse(cacheData);
10
+ }
11
+ catch (error) {
12
+ // Cache file doesn't exist or is invalid, return empty cache
13
+ return {};
14
+ }
15
+ }
16
+ async function trySavingCache(cache) {
17
+ try {
18
+ await fs.writeFile(CACHE_FILE, JSON.stringify(cache, null, 2), 'utf-8');
19
+ }
20
+ catch (error) {
21
+ console.error('Failed to save org tenants cache:', error);
22
+ }
23
+ }
24
+ async function fetchTenantFromApi(orgName) {
25
+ const url = `https://vssps.dev.azure.com/${orgName}`;
26
+ try {
27
+ const response = await fetch(url, { method: 'HEAD' });
28
+ if (response.status !== 404) {
29
+ throw new Error(`Expected status 404, got ${response.status}`);
30
+ }
31
+ const tenantId = response.headers.get('x-vss-resourcetenant');
32
+ if (!tenantId) {
33
+ throw new Error('x-vss-resourcetenant header not found in response');
34
+ }
35
+ return tenantId;
36
+ }
37
+ catch (error) {
38
+ throw new Error(`Failed to fetch tenant for organization ${orgName}: ${error}`);
39
+ }
40
+ }
41
+ function isCacheEntryExpired(entry) {
42
+ return Date.now() - entry.refreshedOn > CACHE_TTL_MS;
43
+ }
44
+ export async function getOrgTenant(orgName) {
45
+ // Load cache
46
+ const cache = await loadCache();
47
+ // Check if tenant is cached and not expired
48
+ const cachedEntry = cache[orgName];
49
+ if (cachedEntry && !isCacheEntryExpired(cachedEntry)) {
50
+ return cachedEntry.tenantId;
51
+ }
52
+ // Try to fetch fresh tenant from API
53
+ try {
54
+ const tenantId = await fetchTenantFromApi(orgName);
55
+ // Cache the result
56
+ cache[orgName] = {
57
+ tenantId,
58
+ refreshedOn: Date.now()
59
+ };
60
+ await trySavingCache(cache);
61
+ return tenantId;
62
+ }
63
+ catch (error) {
64
+ // If we have an expired cache entry, return it as fallback
65
+ if (cachedEntry) {
66
+ console.error(`Failed to fetch fresh tenant for ADO org ${orgName}, using expired cache entry:`, error);
67
+ return cachedEntry.tenantId;
68
+ }
69
+ // No cache entry available, log and return empty result
70
+ console.error(`Failed to fetch tenant for ADO org ${orgName}:`, error);
71
+ return undefined;
72
+ }
73
+ }
package/dist/prompts.js CHANGED
File without changes
package/dist/server.js ADDED
@@ -0,0 +1,36 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
4
+ import * as azdev from "azure-devops-node-api";
5
+ import { DefaultAzureCredential } from "@azure/identity";
6
+ import { configurePrompts } from "./prompts.js";
7
+ import { configureAllTools } from "./tools.js";
8
+ import { userAgent } from "./utils.js";
9
+ import { packageVersion } from "./version.js";
10
+ async function getAzureDevOpsToken() {
11
+ process.env.AZURE_TOKEN_CREDENTIALS = "dev";
12
+ const credential = new DefaultAzureCredential(); // CodeQL [SM05138] resolved by explicitly setting AZURE_TOKEN_CREDENTIALS
13
+ const token = await credential.getToken("499b84ac-1321-427f-aa17-267ca6975798/.default");
14
+ return token;
15
+ }
16
+ async function getAzureDevOpsClient(orgUrl) {
17
+ const token = await getAzureDevOpsToken();
18
+ const authHandler = azdev.getBearerHandler(token.token);
19
+ const connection = new azdev.WebApi(orgUrl, authHandler, undefined, {
20
+ productName: "AzureDevOps.MCP",
21
+ productVersion: packageVersion,
22
+ userAgent: userAgent
23
+ });
24
+ return connection;
25
+ }
26
+ export async function serverBuildAndConnect(orgName, transport) {
27
+ const server = new McpServer({
28
+ name: "Azure DevOps MCP Server",
29
+ version: packageVersion,
30
+ });
31
+ const orgUrl = "https://dev.azure.com/" + orgName;
32
+ configurePrompts(server);
33
+ configureAllTools(server, () => orgName, getAzureDevOpsToken, () => getAzureDevOpsClient(orgUrl));
34
+ await server.connect(transport);
35
+ return server;
36
+ }
package/dist/tenant.js ADDED
@@ -0,0 +1,73 @@
1
+ import * as fs from 'fs/promises';
2
+ import * as os from 'os';
3
+ import * as path from 'path';
4
+ const CACHE_FILE = path.join(os.homedir(), '.ado_orgs.cache');
5
+ const CACHE_TTL_MS = 7 * 24 * 60 * 60 * 1000; // 1 week in milliseconds
6
+ async function loadCache() {
7
+ try {
8
+ const cacheData = await fs.readFile(CACHE_FILE, 'utf-8');
9
+ return JSON.parse(cacheData);
10
+ }
11
+ catch (error) {
12
+ // Cache file doesn't exist or is invalid, return empty cache
13
+ return {};
14
+ }
15
+ }
16
+ async function saveCache(cache) {
17
+ try {
18
+ await fs.writeFile(CACHE_FILE, JSON.stringify(cache, null, 2), 'utf-8');
19
+ }
20
+ catch (error) {
21
+ console.warn('Failed to save cache:', error);
22
+ }
23
+ }
24
+ async function fetchTenantFromApi(orgName) {
25
+ const url = `https://vssps.dev.azure.com/${orgName}`;
26
+ try {
27
+ const response = await fetch(url, { method: 'HEAD' });
28
+ if (response.status !== 404) {
29
+ throw new Error(`Expected status 404, got ${response.status}`);
30
+ }
31
+ const tenantId = response.headers.get('x-vss-resourcetenant');
32
+ if (!tenantId) {
33
+ throw new Error('x-vss-resourcetenant header not found in response');
34
+ }
35
+ return tenantId;
36
+ }
37
+ catch (error) {
38
+ throw new Error(`Failed to fetch tenant for organization ${orgName}: ${error}`);
39
+ }
40
+ }
41
+ function isCacheEntryExpired(entry) {
42
+ return Date.now() - entry.refreshedOn > CACHE_TTL_MS;
43
+ }
44
+ export async function getOrgTenant(orgName) {
45
+ // Load cache
46
+ const cache = await loadCache();
47
+ // Check if tenant is cached and not expired
48
+ const cachedEntry = cache[orgName];
49
+ if (cachedEntry && !isCacheEntryExpired(cachedEntry)) {
50
+ return cachedEntry.tenantId;
51
+ }
52
+ // Try to fetch fresh tenant from API
53
+ try {
54
+ const tenantId = await fetchTenantFromApi(orgName);
55
+ // Cache the fresh result
56
+ cache[orgName] = {
57
+ tenantId,
58
+ refreshedOn: Date.now()
59
+ };
60
+ await saveCache(cache);
61
+ return tenantId;
62
+ }
63
+ catch (error) {
64
+ // If we have an expired cache entry, return it as fallback
65
+ if (cachedEntry) {
66
+ console.error(`Failed to fetch fresh tenant for ADO org ${orgName}, using expired cache entry:`, error);
67
+ return cachedEntry.tenantId;
68
+ }
69
+ // No cache entry available, re-throw the error
70
+ console.error(`Failed to fetch tenant for ADO org ${orgName}:`, error);
71
+ return undefined;
72
+ }
73
+ }
@@ -0,0 +1,108 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+ import { AlertType, AlertValidityStatus, Confidence, Severity, State } from "azure-devops-node-api/interfaces/AlertInterfaces.js";
4
+ import { z } from "zod";
5
+ import { getEnumKeys, mapStringArrayToEnum, mapStringToEnum } from "../utils.js";
6
+ const ADVSEC_TOOLS = {
7
+ get_alerts: "advsec_get_alerts",
8
+ get_alert_details: "advsec_get_alert_details",
9
+ };
10
+ function configureAdvSecTools(server, tokenProvider, connectionProvider) {
11
+ server.tool(ADVSEC_TOOLS.get_alerts, "Retrieve Advanced Security alerts for a repository.", {
12
+ project: z.string().describe("The name or ID of the Azure DevOps project."),
13
+ repository: z.string().describe("The name or ID of the repository to get alerts for."),
14
+ alertType: z
15
+ .enum(getEnumKeys(AlertType))
16
+ .optional()
17
+ .describe("Filter alerts by type. If not specified, returns all alert types."),
18
+ states: z
19
+ .array(z.enum(getEnumKeys(State)))
20
+ .optional()
21
+ .describe("Filter alerts by state. If not specified, returns alerts in any state."),
22
+ severities: z
23
+ .array(z.enum(getEnumKeys(Severity)))
24
+ .optional()
25
+ .describe("Filter alerts by severity level. If not specified, returns alerts at any severity."),
26
+ ruleId: z.string().optional().describe("Filter alerts by rule ID."),
27
+ ruleName: z.string().optional().describe("Filter alerts by rule name."),
28
+ toolName: z.string().optional().describe("Filter alerts by tool name."),
29
+ ref: z.string().optional().describe("Filter alerts by git reference (branch). If not provided and onlyDefaultBranch is true, only includes alerts from default branch."),
30
+ onlyDefaultBranch: z.boolean().optional().default(true).describe("If true, only return alerts found on the default branch. Defaults to true."),
31
+ confidenceLevels: z
32
+ .array(z.enum(getEnumKeys(Confidence)))
33
+ .optional()
34
+ .default(["high", "other"])
35
+ .describe("Filter alerts by confidence levels. Only applicable for secret alerts. Defaults to both 'high' and 'other'."),
36
+ validity: z
37
+ .array(z.enum(getEnumKeys(AlertValidityStatus)))
38
+ .optional()
39
+ .describe("Filter alerts by validity status. Only applicable for secret alerts."),
40
+ top: z.number().optional().default(100).describe("Maximum number of alerts to return. Defaults to 100."),
41
+ orderBy: z.enum(["id", "firstSeen", "lastSeen", "fixedOn", "severity"]).optional().default("severity").describe("Order results by specified field. Defaults to 'severity'."),
42
+ continuationToken: z.string().optional().describe("Continuation token for pagination."),
43
+ }, async ({ project, repository, alertType, states, severities, ruleId, ruleName, toolName, ref, onlyDefaultBranch, confidenceLevels, validity, top, orderBy, continuationToken }) => {
44
+ try {
45
+ const connection = await connectionProvider();
46
+ const alertApi = await connection.getAlertApi();
47
+ const isSecretAlert = !alertType || alertType.toLowerCase() === "secret";
48
+ const criteria = {
49
+ ...(alertType && { alertType: mapStringToEnum(alertType, AlertType) }),
50
+ ...(states && { states: mapStringArrayToEnum(states, State) }),
51
+ ...(severities && { severities: mapStringArrayToEnum(severities, Severity) }),
52
+ ...(ruleId && { ruleId }),
53
+ ...(ruleName && { ruleName }),
54
+ ...(toolName && { toolName }),
55
+ ...(ref && { ref }),
56
+ ...(onlyDefaultBranch !== undefined && { onlyDefaultBranch }),
57
+ ...(isSecretAlert && confidenceLevels && { confidenceLevels: mapStringArrayToEnum(confidenceLevels, Confidence) }),
58
+ ...(isSecretAlert && validity && { validity: mapStringArrayToEnum(validity, AlertValidityStatus) }),
59
+ };
60
+ const result = await alertApi.getAlerts(project, repository, top, orderBy, criteria, undefined, // expand parameter
61
+ continuationToken);
62
+ return {
63
+ content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
64
+ };
65
+ }
66
+ catch (error) {
67
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
68
+ return {
69
+ content: [
70
+ {
71
+ type: "text",
72
+ text: `Error fetching Advanced Security alerts: ${errorMessage}`,
73
+ },
74
+ ],
75
+ isError: true,
76
+ };
77
+ }
78
+ });
79
+ server.tool(ADVSEC_TOOLS.get_alert_details, "Get detailed information about a specific Advanced Security alert.", {
80
+ project: z.string().describe("The name or ID of the Azure DevOps project."),
81
+ repository: z.string().describe("The name or ID of the repository containing the alert."),
82
+ alertId: z.number().describe("The ID of the alert to retrieve details for."),
83
+ ref: z.string().optional().describe("Git reference (branch) to filter the alert."),
84
+ }, async ({ project, repository, alertId, ref }) => {
85
+ try {
86
+ const connection = await connectionProvider();
87
+ const alertApi = await connection.getAlertApi();
88
+ const result = await alertApi.getAlert(project, alertId, repository, ref, undefined // expand parameter
89
+ );
90
+ return {
91
+ content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
92
+ };
93
+ }
94
+ catch (error) {
95
+ const errorMessage = error instanceof Error ? error.message : "Unknown error occurred";
96
+ return {
97
+ content: [
98
+ {
99
+ type: "text",
100
+ text: `Error fetching alert details: ${errorMessage}`,
101
+ },
102
+ ],
103
+ isError: true,
104
+ };
105
+ }
106
+ });
107
+ }
108
+ export { ADVSEC_TOOLS, configureAdvSecTools };
@@ -0,0 +1,271 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+ import { apiVersion, getEnumKeys, safeEnumConvert } from "../utils.js";
4
+ import { BuildQueryOrder, DefinitionQueryOrder } from "azure-devops-node-api/interfaces/BuildInterfaces.js";
5
+ import { z } from "zod";
6
+ import { StageUpdateType } from "azure-devops-node-api/interfaces/BuildInterfaces.js";
7
+ const BUILD_TOOLS = {
8
+ get_builds: "build_get_builds",
9
+ get_changes: "build_get_changes",
10
+ get_definitions: "build_get_definitions",
11
+ get_definition_revisions: "build_get_definition_revisions",
12
+ get_log: "build_get_log",
13
+ get_log_by_id: "build_get_log_by_id",
14
+ get_status: "build_get_status",
15
+ pipelines_get_run: "pipelines_get_run",
16
+ pipelines_list_runs: "pipelines_list_runs",
17
+ pipelines_run_pipeline: "pipelines_run_pipeline",
18
+ update_build_stage: "build_update_build_stage",
19
+ };
20
+ function configureBuildTools(server, tokenProvider, connectionProvider, userAgentProvider) {
21
+ server.tool(BUILD_TOOLS.get_definitions, "Retrieves a list of build definitions for a given project.", {
22
+ project: z.string().describe("Project ID or name to get build definitions for"),
23
+ repositoryId: z.string().optional().describe("Repository ID to filter build definitions"),
24
+ repositoryType: z.enum(["TfsGit", "GitHub", "BitbucketCloud"]).optional().describe("Type of repository to filter build definitions"),
25
+ name: z.string().optional().describe("Name of the build definition to filter"),
26
+ path: z.string().optional().describe("Path of the build definition to filter"),
27
+ queryOrder: z
28
+ .enum(getEnumKeys(DefinitionQueryOrder))
29
+ .optional()
30
+ .describe("Order in which build definitions are returned"),
31
+ top: z.number().optional().describe("Maximum number of build definitions to return"),
32
+ continuationToken: z.string().optional().describe("Token for continuing paged results"),
33
+ minMetricsTime: z.coerce.date().optional().describe("Minimum metrics time to filter build definitions"),
34
+ definitionIds: z.array(z.number()).optional().describe("Array of build definition IDs to filter"),
35
+ builtAfter: z.coerce.date().optional().describe("Return definitions that have builds after this date"),
36
+ notBuiltAfter: z.coerce.date().optional().describe("Return definitions that do not have builds after this date"),
37
+ includeAllProperties: z.boolean().optional().describe("Whether to include all properties in the results"),
38
+ includeLatestBuilds: z.boolean().optional().describe("Whether to include the latest builds for each definition"),
39
+ taskIdFilter: z.string().optional().describe("Task ID to filter build definitions"),
40
+ processType: z.number().optional().describe("Process type to filter build definitions"),
41
+ yamlFilename: z.string().optional().describe("YAML filename to filter build definitions"),
42
+ }, async ({ project, repositoryId, repositoryType, name, path, queryOrder, top, continuationToken, minMetricsTime, definitionIds, builtAfter, notBuiltAfter, includeAllProperties, includeLatestBuilds, taskIdFilter, processType, yamlFilename, }) => {
43
+ const connection = await connectionProvider();
44
+ const buildApi = await connection.getBuildApi();
45
+ const buildDefinitions = await buildApi.getDefinitions(project, name, repositoryId, repositoryType, safeEnumConvert(DefinitionQueryOrder, queryOrder), top, continuationToken, minMetricsTime, definitionIds, path, builtAfter, notBuiltAfter, includeAllProperties, includeLatestBuilds, taskIdFilter, processType, yamlFilename);
46
+ return {
47
+ content: [{ type: "text", text: JSON.stringify(buildDefinitions, null, 2) }],
48
+ };
49
+ });
50
+ server.tool(BUILD_TOOLS.get_definition_revisions, "Retrieves a list of revisions for a specific build definition.", {
51
+ project: z.string().describe("Project ID or name to get the build definition revisions for"),
52
+ definitionId: z.number().describe("ID of the build definition to get revisions for"),
53
+ }, async ({ project, definitionId }) => {
54
+ const connection = await connectionProvider();
55
+ const buildApi = await connection.getBuildApi();
56
+ const revisions = await buildApi.getDefinitionRevisions(project, definitionId);
57
+ return {
58
+ content: [{ type: "text", text: JSON.stringify(revisions, null, 2) }],
59
+ };
60
+ });
61
+ server.tool(BUILD_TOOLS.get_builds, "Retrieves a list of builds for a given project.", {
62
+ project: z.string().describe("Project ID or name to get builds for"),
63
+ definitions: z.array(z.number()).optional().describe("Array of build definition IDs to filter builds"),
64
+ queues: z.array(z.number()).optional().describe("Array of queue IDs to filter builds"),
65
+ buildNumber: z.string().optional().describe("Build number to filter builds"),
66
+ minTime: z.coerce.date().optional().describe("Minimum finish time to filter builds"),
67
+ maxTime: z.coerce.date().optional().describe("Maximum finish time to filter builds"),
68
+ requestedFor: z.string().optional().describe("User ID or name who requested the build"),
69
+ reasonFilter: z.number().optional().describe("Reason filter for the build (see BuildReason enum)"),
70
+ statusFilter: z.number().optional().describe("Status filter for the build (see BuildStatus enum)"),
71
+ resultFilter: z.number().optional().describe("Result filter for the build (see BuildResult enum)"),
72
+ tagFilters: z.array(z.string()).optional().describe("Array of tags to filter builds"),
73
+ properties: z.array(z.string()).optional().describe("Array of property names to include in the results"),
74
+ top: z.number().optional().describe("Maximum number of builds to return"),
75
+ continuationToken: z.string().optional().describe("Token for continuing paged results"),
76
+ maxBuildsPerDefinition: z.number().optional().describe("Maximum number of builds per definition"),
77
+ deletedFilter: z.number().optional().describe("Filter for deleted builds (see QueryDeletedOption enum)"),
78
+ queryOrder: z
79
+ .enum(getEnumKeys(BuildQueryOrder))
80
+ .default("QueueTimeDescending")
81
+ .optional()
82
+ .describe("Order in which builds are returned"),
83
+ branchName: z.string().optional().describe("Branch name to filter builds"),
84
+ buildIds: z.array(z.number()).optional().describe("Array of build IDs to retrieve"),
85
+ repositoryId: z.string().optional().describe("Repository ID to filter builds"),
86
+ repositoryType: z.enum(["TfsGit", "GitHub", "BitbucketCloud"]).optional().describe("Type of repository to filter builds"),
87
+ }, async ({ project, definitions, queues, buildNumber, minTime, maxTime, requestedFor, reasonFilter, statusFilter, resultFilter, tagFilters, properties, top, continuationToken, maxBuildsPerDefinition, deletedFilter, queryOrder, branchName, buildIds, repositoryId, repositoryType, }) => {
88
+ const connection = await connectionProvider();
89
+ const buildApi = await connection.getBuildApi();
90
+ const builds = await buildApi.getBuilds(project, definitions, queues, buildNumber, minTime, maxTime, requestedFor, reasonFilter, statusFilter, resultFilter, tagFilters, properties, top, continuationToken, maxBuildsPerDefinition, deletedFilter, safeEnumConvert(BuildQueryOrder, queryOrder), branchName, buildIds, repositoryId, repositoryType);
91
+ return {
92
+ content: [{ type: "text", text: JSON.stringify(builds, null, 2) }],
93
+ };
94
+ });
95
+ server.tool(BUILD_TOOLS.get_log, "Retrieves the logs for a specific build.", {
96
+ project: z.string().describe("Project ID or name to get the build log for"),
97
+ buildId: z.number().describe("ID of the build to get the log for"),
98
+ }, async ({ project, buildId }) => {
99
+ const connection = await connectionProvider();
100
+ const buildApi = await connection.getBuildApi();
101
+ const logs = await buildApi.getBuildLogs(project, buildId);
102
+ return {
103
+ content: [{ type: "text", text: JSON.stringify(logs, null, 2) }],
104
+ };
105
+ });
106
+ server.tool(BUILD_TOOLS.get_log_by_id, "Get a specific build log by log ID.", {
107
+ project: z.string().describe("Project ID or name to get the build log for"),
108
+ buildId: z.number().describe("ID of the build to get the log for"),
109
+ logId: z.number().describe("ID of the log to retrieve"),
110
+ startLine: z.number().optional().describe("Starting line number for the log content, defaults to 0"),
111
+ endLine: z.number().optional().describe("Ending line number for the log content, defaults to the end of the log"),
112
+ }, async ({ project, buildId, logId, startLine, endLine }) => {
113
+ const connection = await connectionProvider();
114
+ const buildApi = await connection.getBuildApi();
115
+ const logLines = await buildApi.getBuildLogLines(project, buildId, logId, startLine, endLine);
116
+ return {
117
+ content: [{ type: "text", text: JSON.stringify(logLines, null, 2) }],
118
+ };
119
+ });
120
+ server.tool(BUILD_TOOLS.get_changes, "Get the changes associated with a specific build.", {
121
+ project: z.string().describe("Project ID or name to get the build changes for"),
122
+ buildId: z.number().describe("ID of the build to get changes for"),
123
+ continuationToken: z.string().optional().describe("Continuation token for pagination"),
124
+ top: z.number().default(100).describe("Number of changes to retrieve, defaults to 100"),
125
+ includeSourceChange: z.boolean().optional().describe("Whether to include source changes in the results, defaults to false"),
126
+ }, async ({ project, buildId, continuationToken, top, includeSourceChange }) => {
127
+ const connection = await connectionProvider();
128
+ const buildApi = await connection.getBuildApi();
129
+ const changes = await buildApi.getBuildChanges(project, buildId, continuationToken, top, includeSourceChange);
130
+ return {
131
+ content: [{ type: "text", text: JSON.stringify(changes, null, 2) }],
132
+ };
133
+ });
134
+ server.tool(BUILD_TOOLS.pipelines_get_run, "Gets a run for a particular pipeline.", {
135
+ project: z.string().describe("Project ID or name to run the build in"),
136
+ pipelineId: z.number().describe("ID of the pipeline to run"),
137
+ runId: z.number().describe("ID of the run to get"),
138
+ }, async ({ project, pipelineId, runId }) => {
139
+ const connection = await connectionProvider();
140
+ const pipelinesApi = await connection.getPipelinesApi();
141
+ const pipelineRun = await pipelinesApi.getRun(project, pipelineId, runId);
142
+ return {
143
+ content: [{ type: "text", text: JSON.stringify(pipelineRun, null, 2) }],
144
+ };
145
+ });
146
+ server.tool(BUILD_TOOLS.pipelines_list_runs, "Gets top 10000 runs for a particular pipeline.", {
147
+ project: z.string().describe("Project ID or name to run the build in"),
148
+ pipelineId: z.number().describe("ID of the pipeline to run"),
149
+ }, async ({ project, pipelineId }) => {
150
+ const connection = await connectionProvider();
151
+ const pipelinesApi = await connection.getPipelinesApi();
152
+ const pipelineRuns = await pipelinesApi.listRuns(project, pipelineId);
153
+ return {
154
+ content: [{ type: "text", text: JSON.stringify(pipelineRuns, null, 2) }],
155
+ };
156
+ });
157
+ const variableSchema = z.object({
158
+ value: z.string().optional(),
159
+ isSecret: z.boolean().optional(),
160
+ });
161
+ const resourcesSchema = z.object({
162
+ builds: z
163
+ .record(z.string().describe("Name of the build resource."), z.object({
164
+ version: z.string().optional().describe("Version of the build resource."),
165
+ }))
166
+ .optional(),
167
+ containers: z
168
+ .record(z.string().describe("Name of the container resource."), z.object({
169
+ version: z.string().optional().describe("Version of the container resource."),
170
+ }))
171
+ .optional(),
172
+ packages: z
173
+ .record(z.string().describe("Name of the package resource."), z.object({
174
+ version: z.string().optional().describe("Version of the package resource."),
175
+ }))
176
+ .optional(),
177
+ pipelines: z.record(z.string().describe("Name of the pipeline resource."), z.object({
178
+ runId: z.number().describe("Id of the source pipeline run that triggered or is referenced by this pipeline run."),
179
+ version: z.string().optional().describe("Version of the source pipeline run."),
180
+ })),
181
+ repositories: z
182
+ .record(z.string().describe("Name of the repository resource."), z.object({
183
+ refName: z.string().describe("Reference name, e.g., refs/heads/main."),
184
+ token: z.string().optional(),
185
+ tokenType: z.string().optional(),
186
+ version: z.string().optional().describe("Version of the repository resource, git commit sha."),
187
+ }))
188
+ .optional(),
189
+ });
190
+ server.tool(BUILD_TOOLS.pipelines_run_pipeline, "Starts a new run of a pipeline.", {
191
+ project: z.string().describe("Project ID or name to run the build in"),
192
+ pipelineId: z.number().describe("ID of the pipeline to run"),
193
+ pipelineVersion: z.number().optional().describe("Version of the pipeline to run. If not provided, the latest version will be used."),
194
+ previewRun: z.boolean().optional().describe("If true, returns the final YAML document after parsing templates without creating a new run."),
195
+ resources: resourcesSchema.optional().describe("A dictionary of resources to pass to the pipeline."),
196
+ stagesToSkip: z.array(z.string()).optional().describe("A list of stages to skip."),
197
+ templateParameters: z.record(z.string(), z.string()).optional().describe("Custom build parameters as key-value pairs"),
198
+ variables: z.record(z.string(), variableSchema).optional().describe("A dictionary of variables to pass to the pipeline."),
199
+ yamlOverride: z.string().optional().describe("YAML override for the pipeline run."),
200
+ }, async ({ project, pipelineId, pipelineVersion, previewRun, resources, stagesToSkip, templateParameters, variables, yamlOverride }) => {
201
+ if (!previewRun && yamlOverride) {
202
+ throw new Error("Parameter 'yamlOverride' can only be specified together with parameter 'previewRun'.");
203
+ }
204
+ const connection = await connectionProvider();
205
+ const pipelinesApi = await connection.getPipelinesApi();
206
+ const runRequest = {
207
+ previewRun: previewRun,
208
+ resources: {
209
+ ...resources,
210
+ },
211
+ stagesToSkip: stagesToSkip,
212
+ templateParameters: templateParameters,
213
+ variables: variables,
214
+ yamlOverride: yamlOverride,
215
+ };
216
+ const pipelineRun = await pipelinesApi.runPipeline(runRequest, project, pipelineId, pipelineVersion);
217
+ const queuedBuild = { id: pipelineRun.id };
218
+ const buildId = queuedBuild.id;
219
+ if (buildId === undefined) {
220
+ throw new Error("Failed to get build ID from pipeline run");
221
+ }
222
+ return {
223
+ content: [{ type: "text", text: JSON.stringify(pipelineRun, null, 2) }],
224
+ };
225
+ });
226
+ server.tool(BUILD_TOOLS.get_status, "Fetches the status of a specific build.", {
227
+ project: z.string().describe("Project ID or name to get the build status for"),
228
+ buildId: z.number().describe("ID of the build to get the status for"),
229
+ }, async ({ project, buildId }) => {
230
+ const connection = await connectionProvider();
231
+ const buildApi = await connection.getBuildApi();
232
+ const build = await buildApi.getBuildReport(project, buildId);
233
+ return {
234
+ content: [{ type: "text", text: JSON.stringify(build, null, 2) }],
235
+ };
236
+ });
237
+ server.tool(BUILD_TOOLS.update_build_stage, "Updates the stage of a specific build.", {
238
+ project: z.string().describe("Project ID or name to update the build stage for"),
239
+ buildId: z.number().describe("ID of the build to update"),
240
+ stageName: z.string().describe("Name of the stage to update"),
241
+ status: z.enum(getEnumKeys(StageUpdateType)).describe("New status for the stage"),
242
+ forceRetryAllJobs: z.boolean().default(false).describe("Whether to force retry all jobs in the stage."),
243
+ }, async ({ project, buildId, stageName, status, forceRetryAllJobs }) => {
244
+ const connection = await connectionProvider();
245
+ const orgUrl = connection.serverUrl;
246
+ const endpoint = `${orgUrl}/${project}/_apis/build/builds/${buildId}/stages/${stageName}?api-version=${apiVersion}`;
247
+ const token = await tokenProvider();
248
+ const body = {
249
+ forceRetryAllJobs: forceRetryAllJobs,
250
+ state: safeEnumConvert(StageUpdateType, status),
251
+ };
252
+ const response = await fetch(endpoint, {
253
+ method: "PATCH",
254
+ headers: {
255
+ "Content-Type": "application/json",
256
+ "Authorization": `Bearer ${token}`,
257
+ "User-Agent": userAgentProvider(),
258
+ },
259
+ body: JSON.stringify(body),
260
+ });
261
+ if (!response.ok) {
262
+ const errorText = await response.text();
263
+ throw new Error(`Failed to update build stage: ${response.status} ${errorText}`);
264
+ }
265
+ const updatedBuild = await response.text();
266
+ return {
267
+ content: [{ type: "text", text: JSON.stringify(updatedBuild, null, 2) }],
268
+ };
269
+ });
270
+ }
271
+ export { BUILD_TOOLS, configureBuildTools };