Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import sharp from "sharp";
import type { ApiResponse } from "./apiClient.js";
import { BrowserStackConfig } from "./types.js";
import { getBrowserStackAuth } from "./get-auth.js";

export function sanitizeUrlParam(param: string): string {
// Remove any characters that could be used for command injection
Expand Down Expand Up @@ -38,3 +40,25 @@ export async function assertOkResponse(
);
}
}

export async function fetchFromBrowserStackAPI(
url: string,
config: BrowserStackConfig,
): Promise<any> {
const authString = getBrowserStackAuth(config);
const auth = Buffer.from(authString).toString("base64");

const res = await fetch(url, {
headers: {
Authorization: `Basic ${auth}`,
},
});

if (!res.ok) {
throw new Error(
`Failed to fetch from ${url}: ${res.status} ${res.statusText}`,
);
}

return res.json();
}
2 changes: 2 additions & 0 deletions src/server-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import addFailureLogsTools from "./tools/get-failure-logs.js";
import addAutomateTools from "./tools/automate.js";
import addSelfHealTools from "./tools/selfheal.js";
import addAppLiveTools from "./tools/applive.js";
import addBuildInsightsTools from "./tools/build-insights.js";
import { setupOnInitialized } from "./oninitialized.js";
import { BrowserStackConfig } from "./lib/types.js";
import addRCATools from "./tools/rca-agent.js";
Expand Down Expand Up @@ -58,6 +59,7 @@ export class BrowserStackMcpServer {
addFailureLogsTools,
addAutomateTools,
addSelfHealTools,
addBuildInsightsTools,
addRCATools,
];

Expand Down
98 changes: 98 additions & 0 deletions src/tools/build-insights.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
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 } from "../lib/utils.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}`;

const [buildData, qualityData] = await Promise.all([
fetchFromBrowserStackAPI(buildUrl, config),
fetchFromBrowserStackAPI(qualityGateUrl, config),
]);

// 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,
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 {
return await fetchBuildInsightsTool(args, config);
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : "Unknown error";
return {
content: [
{
type: "text",
text: `Error during fetching build insights: ${errorMessage}`,
},
],
};
}
},
);

return tools;
}