Skip to content

Commit 123ab82

Browse files
fix bug with fetching deployment id caused by refactor
1 parent c3efcd1 commit 123ab82

File tree

6 files changed

+45
-24
lines changed

6 files changed

+45
-24
lines changed

src/tools/atlas/atlasTool.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ For more information on Atlas API access roles, visit: https://www.mongodb.com/d
8282
* @returns The tool metadata
8383
*/
8484
protected resolveTelemetryMetadata(
85+
result: CallToolResult,
8586
...args: Parameters<ToolCallback<typeof this.argsShape>>
8687
): TelemetryToolMetadata {
8788
const toolMetadata: TelemetryToolMetadata = {};

src/tools/atlasLocal/atlasLocalTool.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import type { Client } from "@mongodb-js/atlas-local";
66
import { LogId } from "../../common/logger.js";
77
import { z } from "zod";
88

9+
export const AtlasLocalToolMetadataDeploymentIdKey = "deploymentId";
10+
911
export abstract class AtlasLocalToolBase extends ToolBase {
1012
public category: ToolCategory = "atlas-local";
1113

@@ -39,10 +41,20 @@ please log a ticket here: https://github.com/mongodb-js/mongodb-mcp-server/issue
3941
return this.executeWithAtlasLocalClient(client, ...args);
4042
}
4143

42-
protected async lookupDeploymentId(client: Client, containerId: string): Promise<string> {
43-
// Lookup and return the deployment id for telemetry metadata.
44-
const deploymentId = await client.getDeploymentId(containerId);
45-
return deploymentId;
44+
protected async lookupDeploymentId(client: Client, containerId: string): Promise<string | undefined> {
45+
try {
46+
// Lookup and return the deployment id for telemetry metadata.
47+
const deploymentId = await client.getDeploymentId(containerId);
48+
return deploymentId;
49+
} catch (error) {
50+
this.session.logger.debug({
51+
id: LogId.telemetryMetadataError,
52+
context: "tool",
53+
message: `Error looking up deployment ID: ${String(error)}`,
54+
});
55+
56+
return undefined;
57+
}
4658
}
4759

4860
protected abstract executeWithAtlasLocalClient(
@@ -94,6 +106,7 @@ please log a ticket here: https://github.com/mongodb-js/mongodb-mcp-server/issue
94106
}
95107

96108
protected async resolveTelemetryMetadata(
109+
result: CallToolResult,
97110
...args: Parameters<ToolCallback<typeof this.argsShape>>
98111
): Promise<TelemetryToolMetadata> {
99112
const toolMetadata: TelemetryToolMetadata = {};
@@ -116,10 +129,20 @@ please log a ticket here: https://github.com/mongodb-js/mongodb-mcp-server/issue
116129
return toolMetadata;
117130
}
118131

132+
const resultDeploymentId = result._meta?.[AtlasLocalToolMetadataDeploymentIdKey];
133+
if (resultDeploymentId !== undefined && typeof resultDeploymentId === "string") {
134+
toolMetadata.atlasLocaldeploymentId = resultDeploymentId;
135+
}
136+
119137
const data = parsedResult.data;
120138

121139
// Extract deploymentName using type guard and lookup deployment ID
122-
if ("deploymentName" in data && typeof data.deploymentName === "string" && data.deploymentName.trim() !== "") {
140+
if (
141+
resultDeploymentId === undefined &&
142+
"deploymentName" in data &&
143+
typeof data.deploymentName === "string" &&
144+
data.deploymentName.trim() !== ""
145+
) {
123146
const deploymentId = await this.lookupDeploymentId(client, data.deploymentName);
124147
toolMetadata.atlasLocaldeploymentId = deploymentId;
125148
}
Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
2-
import { AtlasLocalToolBase } from "../atlasLocalTool.js";
3-
import type { OperationType, ToolArgs, TelemetryToolMetadata } from "../../tool.js";
2+
import { AtlasLocalToolBase, AtlasLocalToolMetadataDeploymentIdKey } from "../atlasLocalTool.js";
3+
import type { OperationType, ToolArgs } from "../../tool.js";
44
import type { Client, CreateDeploymentOptions } from "@mongodb-js/atlas-local";
55
import { CommonArgs } from "../../args.js";
6-
import type { ToolCallback } from "@modelcontextprotocol/sdk/server/mcp.js";
76

87
export class CreateDeploymentTool extends AtlasLocalToolBase {
98
public name = "atlas-local-create-deployment";
@@ -13,8 +12,6 @@ export class CreateDeploymentTool extends AtlasLocalToolBase {
1312
deploymentName: CommonArgs.string().describe("Name of the deployment to create").optional(),
1413
};
1514

16-
private createdDeploymentId?: string;
17-
1815
protected async executeWithAtlasLocalClient(
1916
client: Client,
2017
{ deploymentName }: ToolArgs<typeof this.argsShape>
@@ -31,7 +28,7 @@ export class CreateDeploymentTool extends AtlasLocalToolBase {
3128
const deployment = await client.createDeployment(deploymentOptions);
3229

3330
// Capture deployment ID for telemetry
34-
this.createdDeploymentId = await this.lookupDeploymentId(client, deployment.containerId);
31+
const createdDeploymentId = await this.lookupDeploymentId(client, deployment.containerId);
3532

3633
return {
3734
content: [
@@ -40,17 +37,9 @@ export class CreateDeploymentTool extends AtlasLocalToolBase {
4037
text: `Deployment with container ID "${deployment.containerId}" and name "${deployment.name}" created.`,
4138
},
4239
],
40+
_meta: {
41+
[AtlasLocalToolMetadataDeploymentIdKey]: createdDeploymentId,
42+
},
4343
};
4444
}
45-
46-
// Create tool needs to override resolveTelemetryMetadata because it doesn't
47-
// have the deployment name in the arguments, but rather in the response.
48-
protected resolveTelemetryMetadata(
49-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
50-
...args: Parameters<ToolCallback<typeof this.argsShape>>
51-
): Promise<TelemetryToolMetadata> {
52-
return Promise.resolve({
53-
atlasLocaldeploymentId: this.createdDeploymentId,
54-
});
55-
}
5645
}

src/tools/atlasLocal/delete/deleteDeployment.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
2-
import { AtlasLocalToolBase } from "../atlasLocalTool.js";
2+
import { AtlasLocalToolBase, AtlasLocalToolMetadataDeploymentIdKey } from "../atlasLocalTool.js";
33
import type { OperationType, ToolArgs } from "../../tool.js";
44
import type { Client } from "@mongodb-js/atlas-local";
55
import { CommonArgs } from "../../args.js";
@@ -16,11 +16,16 @@ export class DeleteDeploymentTool extends AtlasLocalToolBase {
1616
client: Client,
1717
{ deploymentName }: ToolArgs<typeof this.argsShape>
1818
): Promise<CallToolResult> {
19+
const deploymentId = await this.lookupDeploymentId(client, deploymentName);
20+
1921
// Delete the deployment
2022
await client.deleteDeployment(deploymentName);
2123

2224
return {
2325
content: [{ type: "text", text: `Deployment "${deploymentName}" deleted successfully.` }],
26+
_meta: {
27+
[AtlasLocalToolMetadataDeploymentIdKey]: deploymentId,
28+
},
2429
};
2530
}
2631
}

src/tools/mongodb/mongodbTool.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ export abstract class MongoDBToolBase extends ToolBase {
9393
}
9494

9595
protected resolveTelemetryMetadata(
96+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
97+
result: CallToolResult,
9698
// eslint-disable-next-line @typescript-eslint/no-unused-vars
9799
args: ToolArgs<typeof this.argsShape>
98100
): TelemetryToolMetadata {

src/tools/tool.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ export abstract class ToolBase {
274274
}
275275

276276
protected abstract resolveTelemetryMetadata(
277+
result: CallToolResult,
277278
...args: Parameters<ToolCallback<typeof this.argsShape>>
278279
): TelemetryToolMetadata | Promise<TelemetryToolMetadata>;
279280

@@ -292,7 +293,7 @@ export abstract class ToolBase {
292293
return;
293294
}
294295
const duration = Date.now() - startTime;
295-
const metadata = await this.resolveTelemetryMetadata(...args);
296+
const metadata = await this.resolveTelemetryMetadata(result, ...args);
296297
const event: ToolEvent = {
297298
timestamp: new Date().toISOString(),
298299
source: "mdbmcp",

0 commit comments

Comments
 (0)