Skip to content

Commit 1319767

Browse files
committed
wip
1 parent fcbfcff commit 1319767

File tree

6 files changed

+63
-24
lines changed

6 files changed

+63
-24
lines changed

src/telemetry/types.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,7 @@ export type CommonProperties = {
141141
* For MongoDB tools, this is typically empty, while for Atlas tools, this should include
142142
* the project and organization IDs if available.
143143
*/
144-
export type TelemetryToolMetadata = AtlasLocalToolMetadata | AtlasMetadata | PerfAdvisorToolMetadata;
145-
146-
export type AtlasLocalToolMetadata = {
147-
atlas_local_deployment_id?: string;
148-
};
144+
export type TelemetryToolMetadata = AtlasMetadata | PerfAdvisorToolMetadata | ConnectionMetadata;
149145

150146
export type AtlasMetadata = {
151147
project_id?: string;
@@ -155,3 +151,12 @@ export type AtlasMetadata = {
155151
export type PerfAdvisorToolMetadata = AtlasMetadata & {
156152
operations: string[];
157153
};
154+
155+
export type ConnectionMetadata = AtlasMetadata &
156+
AtlasLocalToolMetadata & {
157+
connection_auth_type?: string;
158+
};
159+
160+
type AtlasLocalToolMetadata = {
161+
atlas_local_deployment_id?: string;
162+
};

src/tools/atlas/connect/connectCluster.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { ensureCurrentIpInAccessList } from "../../../common/atlas/accessListUti
88
import type { AtlasClusterConnectionInfo } from "../../../common/connectionManager.js";
99
import { getDefaultRoleFromConfig } from "../../../common/atlas/roles.js";
1010
import { AtlasArgs } from "../../args.js";
11+
import { AtlasLocalToolMetadata } from "../../../telemetry/types.js";
1112

1213
const addedIpAccessListMessage =
1314
"Note: Your current IP address has been added to the Atlas project's IP access list to enable secure connection.";
@@ -303,4 +304,19 @@ export class ConnectClusterTool extends AtlasToolBase {
303304

304305
return { content };
305306
}
307+
308+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
309+
protected override resolveTelemetryMetadata(_args: ToolArgs<typeof this.argsShape>): AtlasLocalToolMetadata {
310+
const metadata: AtlasLocalToolMetadata = {};
311+
const connectionStringAuthType = this.session.connectionManager.currentConnectionState.connectionStringAuthType;
312+
if (connectionStringAuthType) {
313+
metadata.connection_auth_type = connectionStringAuthType;
314+
}
315+
316+
if (this.session.connectedAtlasCluster?.projectId) {
317+
metadata.project_id = this.session.connectedAtlasCluster.projectId;
318+
}
319+
320+
return metadata;
321+
}
306322
}

src/tools/atlasLocal/atlasLocalTool.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { ToolBase } from "../tool.js";
44
import type { ToolCallback } from "@modelcontextprotocol/sdk/server/mcp.js";
55
import type { Client } from "@mongodb-js/atlas-local";
66
import { LogId } from "../../common/logger.js";
7-
import type { AtlasLocalToolMetadata } from "../../telemetry/types.js";
7+
import type { ConnectionMetadata } from "../../telemetry/types.js";
88

9-
export const AtlasLocalToolMetadataDeploymentIdKey = "deploymentId";
9+
export const ConnectionMetadataDeploymentIdKey = "deploymentId";
1010

1111
export abstract class AtlasLocalToolBase extends ToolBase {
1212
public category: ToolCategory = "atlas-local";
@@ -67,7 +67,7 @@ please log a ticket here: https://github.com/mongodb-js/mongodb-mcp-server/issue
6767
}
6868

6969
return {
70-
[AtlasLocalToolMetadataDeploymentIdKey]: deploymentId,
70+
[ConnectionMetadataDeploymentIdKey]: deploymentId,
7171
};
7272
}
7373

@@ -119,12 +119,12 @@ please log a ticket here: https://github.com/mongodb-js/mongodb-mcp-server/issue
119119
return super.handleError(error, args);
120120
}
121121

122-
protected resolveTelemetryMetadata(result: CallToolResult): AtlasLocalToolMetadata {
123-
const toolMetadata: AtlasLocalToolMetadata = {};
122+
protected resolveTelemetryMetadata(result: CallToolResult): ConnectionMetadata {
123+
const toolMetadata: ConnectionMetadata = {};
124124

125125
// Atlas Local tools set the deployment ID in the result metadata for telemetry
126126
// If the deployment ID is set, we use it for telemetry
127-
const resultDeploymentId = result._meta?.[AtlasLocalToolMetadataDeploymentIdKey];
127+
const resultDeploymentId = result._meta?.[ConnectionMetadataDeploymentIdKey];
128128
if (resultDeploymentId !== undefined && typeof resultDeploymentId === "string") {
129129
toolMetadata.atlas_local_deployment_id = resultDeploymentId;
130130
}

src/tools/mongodb/connect/connect.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { ToolArgs, OperationType, ToolConstructorParams } from "../../tool.
55
import assert from "assert";
66
import type { Server } from "../../../server.js";
77
import { LogId } from "../../../common/logger.js";
8+
import { AtlasLocalToolMetadata } from "../../../telemetry/types.js";
89

910
const disconnectedSchema = z
1011
.object({

src/tools/mongodb/mongodbTool.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
66
import { ErrorCodes, MongoDBError } from "../../common/errors.js";
77
import { LogId } from "../../common/logger.js";
88
import type { Server } from "../../server.js";
9-
import type { AtlasMetadata } from "../../telemetry/types.js";
9+
import type { ConnectionMetadata } from "../../telemetry/types.js";
10+
import type { ToolCallback } from "@modelcontextprotocol/sdk/server/mcp.js";
1011

1112
export const DbOperationArgs = {
1213
database: z.string().describe("Database name"),
@@ -111,19 +112,21 @@ export abstract class MongoDBToolBase extends ToolBase {
111112
return this.session.connectToMongoDB({ connectionString });
112113
}
113114

115+
/**
116+
* Resolves the tool metadata from the arguments passed to the mongoDB tools.
117+
*
118+
* Since MongoDB tools are executed against a MongoDB instance, the tool calls will always have the connection information.
119+
*
120+
* @param result - The result of the tool call.
121+
* @param args - The arguments passed to the tool
122+
* @returns The tool metadata
123+
*/
114124
protected resolveTelemetryMetadata(
115125
// eslint-disable-next-line @typescript-eslint/no-unused-vars
116-
result: CallToolResult,
126+
_result: CallToolResult,
117127
// eslint-disable-next-line @typescript-eslint/no-unused-vars
118-
args: ToolArgs<typeof this.argsShape>
119-
): AtlasMetadata {
120-
const metadata: AtlasMetadata = {};
121-
122-
// Add projectId to the metadata if running a MongoDB operation to an Atlas cluster
123-
if (this.session.connectedAtlasCluster?.projectId) {
124-
metadata.project_id = this.session.connectedAtlasCluster.projectId;
125-
}
126-
127-
return metadata;
128+
_args: Parameters<ToolCallback<typeof this.argsShape>>
129+
): ConnectionMetadata {
130+
return this.getConnectionInfoMetadata();
128131
}
129132
}

src/tools/tool.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { CallToolResult, ToolAnnotations } from "@modelcontextprotocol/sdk/
55
import type { Session } from "../common/session.js";
66
import { LogId } from "../common/logger.js";
77
import type { Telemetry } from "../telemetry/telemetry.js";
8-
import type { TelemetryToolMetadata, ToolEvent } from "../telemetry/types.js";
8+
import type { ConnectionMetadata, TelemetryToolMetadata, ToolEvent } from "../telemetry/types.js";
99
import type { UserConfig } from "../common/config.js";
1010
import type { Server } from "../server.js";
1111
import type { Elicitation } from "../elicitation.js";
@@ -303,6 +303,20 @@ export abstract class ToolBase {
303303
protected isFeatureEnabled(feature: PreviewFeature): boolean {
304304
return this.config.previewFeatures.includes(feature);
305305
}
306+
307+
protected getConnectionInfoMetadata(): ConnectionMetadata {
308+
const metadata: ConnectionMetadata = {};
309+
const connectionStringAuthType = this.session.connectionManager.currentConnectionState.connectionStringAuthType;
310+
if (connectionStringAuthType) {
311+
metadata.connection_auth_type = connectionStringAuthType;
312+
}
313+
314+
if (this.session.connectedAtlasCluster?.projectId) {
315+
metadata.project_id = this.session.connectedAtlasCluster.projectId;
316+
}
317+
318+
return metadata;
319+
}
306320
}
307321

308322
/**

0 commit comments

Comments
 (0)