Skip to content

feat: implement HTTPS agent support in ApiClient for proxy and CA certificate #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 18, 2025
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@browserstack/mcp-server",
"version": "1.1.9",
"version": "1.2.0",
"description": "BrowserStack's Official MCP Server",
"main": "dist/index.js",
"repository": {
Expand Down
61 changes: 54 additions & 7 deletions src/lib/apiClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import axios, { AxiosRequestConfig, AxiosResponse } from "axios";
import httpsProxyAgentPkg from "https-proxy-agent";
const { HttpsProxyAgent } = httpsProxyAgentPkg;
import * as https from "https";
import * as fs from "fs";
import config from "../config.js";

type RequestOptions = {
url: string;
Expand Down Expand Up @@ -54,15 +59,52 @@ class ApiResponse<T = any> {
}
}

// Utility to create HTTPS agent if needed (proxy/CA)
function getAxiosAgent(): AxiosRequestConfig["httpsAgent"] | undefined {
const proxyHost = config.browserstackLocalOptions.proxyHost;
const proxyPort = config.browserstackLocalOptions.proxyPort;
const caCertPath = config.browserstackLocalOptions.useCaCertificate;

// If both proxy host and port are defined
if (proxyHost && proxyPort) {
const proxyUrl = `http://${proxyHost}:${proxyPort}`;
if (caCertPath && fs.existsSync(caCertPath)) {
// Proxy + CA cert
const ca = fs.readFileSync(caCertPath);
return new HttpsProxyAgent({
host: proxyHost,
port: Number(proxyPort),
ca,
rejectUnauthorized: false, // Set to true if you want strict SSL
});
} else {
// Proxy only
return new HttpsProxyAgent(proxyUrl);
}
} else if (caCertPath && fs.existsSync(caCertPath)) {
// CA only
return new https.Agent({
ca: fs.readFileSync(caCertPath),
rejectUnauthorized: false, // Set to true for strict SSL
});
}
// Default agent (no proxy, no CA)
return undefined;
}

class ApiClient {
private instance = axios.create();

private get axiosAgent() {
return getAxiosAgent();
}

private async requestWrapper<T>(
fn: () => Promise<AxiosResponse<T>>,
fn: (agent: AxiosRequestConfig["httpsAgent"]) => Promise<AxiosResponse<T>>,
raise_error: boolean = true,
): Promise<ApiResponse<T>> {
try {
const res = await fn();
const res = await fn(this.axiosAgent);
return new ApiResponse<T>(res);
} catch (error: any) {
if (error.response && !raise_error) {
Expand All @@ -79,7 +121,8 @@ class ApiClient {
raise_error = true,
}: RequestOptions): Promise<ApiResponse<T>> {
return this.requestWrapper<T>(
() => this.instance.get<T>(url, { headers, params }),
(agent) =>
this.instance.get<T>(url, { headers, params, httpsAgent: agent }),
raise_error,
);
}
Expand All @@ -91,7 +134,8 @@ class ApiClient {
raise_error = true,
}: RequestOptions): Promise<ApiResponse<T>> {
return this.requestWrapper<T>(
() => this.instance.post<T>(url, body, { headers }),
(agent) =>
this.instance.post<T>(url, body, { headers, httpsAgent: agent }),
raise_error,
);
}
Expand All @@ -103,7 +147,8 @@ class ApiClient {
raise_error = true,
}: RequestOptions): Promise<ApiResponse<T>> {
return this.requestWrapper<T>(
() => this.instance.put<T>(url, body, { headers }),
(agent) =>
this.instance.put<T>(url, body, { headers, httpsAgent: agent }),
raise_error,
);
}
Expand All @@ -115,7 +160,8 @@ class ApiClient {
raise_error = true,
}: RequestOptions): Promise<ApiResponse<T>> {
return this.requestWrapper<T>(
() => this.instance.patch<T>(url, body, { headers }),
(agent) =>
this.instance.patch<T>(url, body, { headers, httpsAgent: agent }),
raise_error,
);
}
Expand All @@ -127,7 +173,8 @@ class ApiClient {
raise_error = true,
}: RequestOptions): Promise<ApiResponse<T>> {
return this.requestWrapper<T>(
() => this.instance.delete<T>(url, { headers, params }),
(agent) =>
this.instance.delete<T>(url, { headers, params, httpsAgent: agent }),
raise_error,
);
}
Expand Down
31 changes: 22 additions & 9 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { pino } from "pino";

let logger: any;
// 1. The actual logger instance, swapped out as needed
let currentLogger: any;

if (process.env.NODE_ENV === "development") {
logger = pino({
currentLogger = pino({
level: "debug",
transport: {
targets: [
Expand All @@ -23,8 +24,8 @@ if (process.env.NODE_ENV === "development") {
},
});
} else {
// NULL logger
logger = pino({
// Null logger (logs go to /dev/null or NUL)
currentLogger = pino({
level: "info",
transport: {
target: "pino/file",
Expand All @@ -35,12 +36,24 @@ if (process.env.NODE_ENV === "development") {
});
}

/**
* Set a custom logger instance
* @param customLogger - The logger instance to use
*/
// 2. Proxy logger: always delegates to the currentLogger
const logger: any = new Proxy(
{},
{
get(_target, prop) {
// Forward function calls to currentLogger
if (typeof currentLogger[prop] === "function") {
return (...args: any[]) => currentLogger[prop](...args);
}
// Forward property gets
return currentLogger[prop];
},
},
);

// 3. Setter to update the logger instance everywhere
export function setLogger(customLogger: any): void {
logger = customLogger;
currentLogger = customLogger;
}

export default logger;