Skip to content

Commit 2736bca

Browse files
committed
Fix tests + move assistant request up to abstract base class
1 parent d3b4d6b commit 2736bca

File tree

4 files changed

+29
-23
lines changed

4 files changed

+29
-23
lines changed

src/tools/assistant/assistantTool.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,22 @@ import {
77
} from "../tool.js";
88
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
99
import { Server } from "../../server.js";
10-
import { Session } from "../../common/session.js";
11-
import { UserConfig } from "../../common/config.js";
12-
import { Telemetry } from "../../telemetry/telemetry.js";
1310
import { packageInfo } from "../../common/packageInfo.js";
1411

1512
export abstract class AssistantToolBase extends ToolBase {
1613
protected server?: Server;
1714
public category: ToolCategory = "assistant";
1815
protected baseUrl: URL;
19-
protected requiredHeaders: Record<string, string>;
16+
protected requiredHeaders: Headers;
2017

2118
constructor({ session, config, telemetry, elicitation }: ToolConstructorParams) {
2219
super({ session, config, telemetry, elicitation });
2320
this.baseUrl = new URL(config.assistantBaseUrl);
2421
const serverVersion = packageInfo.version;
25-
this.requiredHeaders = {
22+
this.requiredHeaders = new Headers({
2623
"x-request-origin": "mongodb-mcp-server",
2724
"user-agent": serverVersion ? `mongodb-mcp-server/v${serverVersion}` : "mongodb-mcp-server",
28-
};
25+
});
2926
}
3027

3128
public register(server: Server): boolean {
@@ -39,10 +36,16 @@ export abstract class AssistantToolBase extends ToolBase {
3936
return {};
4037
}
4138

42-
protected handleError(
43-
error: unknown,
44-
args: ToolArgs<typeof this.argsShape>
45-
): Promise<CallToolResult> | CallToolResult {
46-
return super.handleError(error, args);
39+
protected async callAssistantApi(args: { method: "GET" | "POST"; endpoint: string; body?: unknown }) {
40+
const endpoint = new URL(args.endpoint, this.baseUrl);
41+
const headers = new Headers(this.requiredHeaders);
42+
if (args.method === "POST") {
43+
headers.set("Content-Type", "application/json");
44+
}
45+
return await fetch(endpoint, {
46+
method: args.method,
47+
headers,
48+
body: JSON.stringify(args.body),
49+
});
4750
}
4851
}

src/tools/assistant/listKnowledgeSources.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ export class ListKnowledgeSourcesTool extends AssistantToolBase {
2828
public operationType: OperationType = "read";
2929

3030
protected async execute(): Promise<CallToolResult> {
31-
const searchEndpoint = new URL("content/sources", this.baseUrl);
32-
const response = await fetch(searchEndpoint, {
31+
const response = await this.callAssistantApi({
3332
method: "GET",
34-
headers: this.requiredHeaders,
33+
endpoint: "content/sources",
3534
});
3635
if (!response.ok) {
3736
const message = `Failed to list knowledge sources: ${response.statusText}`;

src/tools/assistant/searchKnowledge.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,10 @@ export class SearchKnowledgeTool extends AssistantToolBase {
4646
public operationType: OperationType = "read";
4747

4848
protected async execute(args: ToolArgs<typeof this.argsShape>): Promise<CallToolResult> {
49-
const searchEndpoint = new URL("content/search", this.baseUrl);
50-
const response = await fetch(searchEndpoint, {
49+
const response = await this.callAssistantApi({
5150
method: "POST",
52-
headers: new Headers({ ...this.requiredHeaders, "Content-Type": "application/json" }),
53-
body: JSON.stringify(args),
51+
endpoint: "content/search",
52+
body: args,
5453
});
5554
if (!response.ok) {
5655
const message = `Failed to search knowledge base: ${response.statusText}`;

tests/integration/tools/assistant/assistantHelpers.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
import { setupIntegrationTest, IntegrationTest, defaultTestConfig } from "../../helpers.js";
1+
import { setupIntegrationTest, IntegrationTest, defaultTestConfig, defaultDriverOptions } from "../../helpers.js";
22
import { describe, SuiteCollector } from "vitest";
33
import { vi, beforeAll, afterAll } from "vitest";
44

55
export type IntegrationTestFunction = (integration: IntegrationTest) => void;
66

77
export function describeWithAssistant(name: string, fn: IntegrationTestFunction): SuiteCollector<object> {
88
const testDefinition = (): void => {
9-
const integration = setupIntegrationTest(() => ({
10-
...defaultTestConfig,
11-
assistantBaseUrl: "https://knowledge.test.mongodb.com/api/", // Use test URL
12-
}));
9+
const integration = setupIntegrationTest(
10+
() => ({
11+
...defaultTestConfig,
12+
assistantBaseUrl: "https://knowledge.test.mongodb.com/api/", // Use test URL
13+
}),
14+
() => ({
15+
...defaultDriverOptions,
16+
})
17+
);
1318

1419
describe(name, () => {
1520
fn(integration);

0 commit comments

Comments
 (0)