-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathbuild-insights.ts
More file actions
102 lines (93 loc) · 3.39 KB
/
Copy pathbuild-insights.ts
File metadata and controls
102 lines (93 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
import logger from "../logger.js";
import { BrowserStackConfig } from "../lib/types.js";
import { fetchFromBrowserStackAPI, handleMCPError } from "../lib/utils.js";
import { trackMCP } from "../lib/instrumentation.js";
// Tool function that fetches build insights from two APIs
export async function fetchBuildInsightsTool(
args: { buildId: string },
config: BrowserStackConfig,
): Promise<CallToolResult> {
try {
const buildUrl = `https://api-automation.browserstack.com/ext/v1/builds/${args.buildId}`;
const qualityGateUrl = `https://api-automation.browserstack.com/ext/v1/quality-gates/${args.buildId}`;
// Quality gate data is optional — a failure there should not block build insights
const [buildData, qualityData] = await Promise.all([
fetchFromBrowserStackAPI(buildUrl, config),
fetchFromBrowserStackAPI(qualityGateUrl, config).catch((error) => {
logger.warn("Failed to fetch quality gate data", error);
return null;
}),
]);
// Select useful fields for users
const insights = {
name: buildData.name,
status: buildData.status,
duration: buildData.duration,
user: buildData.user,
tags: buildData.tags,
alerts: buildData.alerts,
status_stats: buildData.status_stats,
failure_categories: buildData.failure_categories,
smart_tags: buildData.smart_tags,
unique_errors: buildData.unique_errors?.overview,
observability_url: buildData?.observability_url,
ci_build_url: buildData.ci_info?.build_url,
branch: buildData.vcs_info?.branch,
commit_sha: buildData.vcs_info?.sha,
vcs_name: buildData.vcs_info?.name,
quality_gate_result: qualityData?.quality_gate_result,
};
const qualityProfiles = qualityData?.quality_profiles?.map(
(profile: any) => ({
name: profile.name,
result: profile.result,
}),
);
const qualityProfilesText =
qualityProfiles && qualityProfiles.length > 0
? `Quality Gate Profiles (respond only if explicitly requested): ${JSON.stringify(qualityProfiles, null, 2)}`
: "No Quality Gate Profiles available.";
return {
content: [
{
type: "text",
text: "Build insights:\n" + JSON.stringify(insights, null, 2),
},
{ type: "text", text: qualityProfilesText },
],
};
} catch (error) {
logger.error("Error fetching build insights", error);
throw error;
}
}
// Registers the fetchBuildInsights tool with the MCP server
export default function addBuildInsightsTools(
server: McpServer,
config: BrowserStackConfig,
) {
const tools: Record<string, any> = {};
tools.fetchBuildInsights = server.tool(
"fetchBuildInsights",
"Fetches insights about a BrowserStack build by combining build details and quality gate results.",
{
buildId: z.string().describe("The build UUID of the BrowserStack build"),
},
async (args) => {
try {
trackMCP(
"fetchBuildInsights",
server.server.getClientVersion()!,
config,
);
return await fetchBuildInsightsTool(args, config);
} catch (error) {
return handleMCPError("fetchBuildInsights", server, config, error);
}
},
);
return tools;
}