Skip to content

Commit 02d58c7

Browse files
committed
chore: [broken] Add a test to verify that the http-proxy is called.
Right now the code is broken because there is some issue with the promise returned by the agent. I'm still investigating it.
1 parent e85fe91 commit 02d58c7

File tree

4 files changed

+125
-2
lines changed

4 files changed

+125
-2
lines changed

package-lock.json

Lines changed: 63 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
},
1717
"type": "module",
1818
"scripts": {
19-
"start": "node dist/index.js --transport http --loggers stderr mcp",
19+
"start": "node dist/index.js --transport stdio --loggers stderr mcp",
2020
"prepare": "npm run build",
2121
"build:clean": "rm -rf dist",
2222
"build:compile": "tsc --project tsconfig.build.json",
@@ -43,6 +43,7 @@
4343
"@modelcontextprotocol/inspector": "^0.16.0",
4444
"@redocly/cli": "^1.34.4",
4545
"@types/express": "^5.0.1",
46+
"@types/http-proxy": "^1.17.16",
4647
"@types/node": "^24.0.12",
4748
"@types/proper-lockfile": "^4.1.4",
4849
"@types/simple-oauth2": "^5.0.7",
@@ -53,6 +54,7 @@
5354
"eslint-config-prettier": "^10.1.5",
5455
"eslint-plugin-prettier": "^5.5.1",
5556
"globals": "^16.3.0",
57+
"http-proxy": "^1.18.1",
5658
"mongodb-runner": "^5.9.2",
5759
"ollama-ai-provider": "^1.2.0",
5860
"openapi-types": "^12.1.3",
@@ -63,14 +65,15 @@
6365
"tsx": "^4.20.3",
6466
"typescript": "^5.8.3",
6567
"typescript-eslint": "^8.36.0",
66-
"vitest": "^3.2.4",
6768
"uuid": "^11.1.0",
69+
"vitest": "^3.2.4",
6870
"yaml": "^2.8.0"
6971
},
7072
"dependencies": {
7173
"@modelcontextprotocol/sdk": "^1.15.0",
7274
"@mongodb-js/device-id": "^0.3.1",
7375
"@mongodb-js/devtools-connect": "^3.9.2",
76+
"@mongodb-js/devtools-proxy-support": "^0.5.1",
7477
"@mongosh/service-provider-node-driver": "^3.10.2",
7578
"@vitest/eslint-plugin": "^1.3.4",
7679
"bson": "^6.10.4",

src/common/atlas/apiClient.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { paths, operations } from "./openapi.js";
66
import { CommonProperties, TelemetryEvent } from "../../telemetry/types.js";
77
import { packageInfo } from "../packageInfo.js";
88
import logger, { LogId } from "../logger.js";
9+
import { createFetch, useOrCreateAgent } from "@mongodb-js/devtools-proxy-support";
10+
import HTTPS from "https";
911

1012
const ATLAS_API_VERSION = "2025-03-12";
1113

@@ -29,11 +31,25 @@ export class ApiClient {
2931
clientSecret: string;
3032
};
3133
};
34+
35+
private static customFetch = createFetch({
36+
useEnvironmentVariableProxies: true,
37+
}) as unknown as typeof fetch;
38+
39+
private static customAgent = useOrCreateAgent({
40+
useEnvironmentVariableProxies: true,
41+
});
42+
3243
private client: Client<paths>;
3344
private oauth2Client?: ClientCredentials;
3445
private accessToken?: AccessToken;
3546

47+
private ensureAgentIsInitialized = async () => {
48+
await ApiClient.customAgent?.initialize?.();
49+
};
50+
3651
private getAccessToken = async () => {
52+
// await this.ensureAgentIsInitialized();
3753
if (this.oauth2Client && (!this.accessToken || this.accessToken.expired())) {
3854
this.accessToken = await this.oauth2Client.getToken({});
3955
}
@@ -72,6 +88,7 @@ export class ApiClient {
7288
"User-Agent": this.options.userAgent,
7389
Accept: `application/vnd.atlas.${ATLAS_API_VERSION}+json`,
7490
},
91+
fetch: ApiClient.customFetch,
7592
});
7693
if (this.options.credentials?.clientId && this.options.credentials?.clientSecret) {
7794
this.oauth2Client = new ClientCredentials({
@@ -88,6 +105,7 @@ export class ApiClient {
88105
headers: {
89106
"User-Agent": this.options.userAgent,
90107
},
108+
agent: ApiClient.customAgent,
91109
},
92110
});
93111
this.client.use(this.authMiddleware);

tests/unit/common/apiClient.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
22
import { ApiClient } from "../../../src/common/atlas/apiClient.js";
33
import { CommonProperties, TelemetryEvent, TelemetryResult } from "../../../src/telemetry/types.js";
4+
import httpProxy from "http-proxy";
45

56
describe("ApiClient", () => {
67
let apiClient: ApiClient;
@@ -94,6 +95,44 @@ describe("ApiClient", () => {
9495
});
9596
});
9697

98+
describe("oauth authentication proxy", () => {
99+
let apiClient: ApiClient;
100+
let proxyServer: httpProxy;
101+
let requests: unknown[];
102+
103+
beforeEach(() => {
104+
process.env.HTTP_PROXY = "localhost:8888";
105+
apiClient = new ApiClient({
106+
baseUrl: "https://httpbin.org",
107+
credentials: {
108+
clientId: "test-client-id",
109+
clientSecret: "test-client-secret",
110+
},
111+
userAgent: "test-user-agent",
112+
});
113+
114+
proxyServer = httpProxy.createProxyServer({ target: "https://api.test.com" });
115+
proxyServer.on("proxyReq", (proxyReq, req, res, options) => {
116+
requests.push(req);
117+
});
118+
119+
requests = [];
120+
});
121+
122+
afterEach(async () => {
123+
delete process.env.HTTP_PROXY;
124+
125+
await proxyServer.close();
126+
//await apiClient.close();
127+
});
128+
129+
it("should send the oauth request through a proxy if configured", async () => {
130+
await apiClient.validateAccessToken();
131+
console.log(requests);
132+
expect(true).toBeFalsy();
133+
});
134+
});
135+
97136
describe("sendEvents", () => {
98137
it("should send events to authenticated endpoint when token is available and valid", async () => {
99138
const mockFetch = vi.spyOn(global, "fetch");

0 commit comments

Comments
 (0)