1- import config from "../../config.js " ;
2- import createClient , { Client , FetchOptions , Middleware } from "openapi-fetch" ;
1+ import createClient , { Client , Middleware } from "openapi-fetch " ;
2+ import type { FetchOptions } from "openapi-fetch" ;
33import { AccessToken , ClientCredentials } from "simple-oauth2" ;
44import { ApiClientError } from "./apiClientError.js" ;
55import { paths , operations } from "./openapi.js" ;
6+ import { BaseEvent } from "../../telemetry/types.js" ;
7+ import { mongoLogId } from "mongodb-log-writer" ;
8+ import logger from "../../logger.js" ;
9+ import { packageInfo } from "../../packageInfo.js" ;
610
711const ATLAS_API_VERSION = "2025-03-12" ;
812
@@ -67,7 +71,7 @@ export class ApiClient {
6771 baseUrl : options ?. baseUrl || "https://cloud.mongodb.com/" ,
6872 userAgent :
6973 options ?. userAgent ||
70- `AtlasMCP/${ config . version } (${ process . platform } ; ${ process . arch } ; ${ process . env . HOSTNAME || "unknown" } )` ,
74+ `AtlasMCP/${ packageInfo . version } (${ process . platform } ; ${ process . arch } ; ${ process . env . HOSTNAME || "unknown" } )` ,
7175 } ;
7276
7377 this . client = createClient < paths > ( {
@@ -93,6 +97,15 @@ export class ApiClient {
9397 this . client . use ( this . errorMiddleware ) ;
9498 }
9599
100+ public hasCredentials ( ) : boolean {
101+ logger . info (
102+ mongoLogId ( 1_000_000 ) ,
103+ "api-client" ,
104+ `Checking if API client has credentials: ${ ! ! ( this . oauth2Client && this . accessToken ) } `
105+ ) ;
106+ return ! ! ( this . oauth2Client && this . accessToken ) ;
107+ }
108+
96109 public async getIpInfo ( ) : Promise < {
97110 currentIpv4Address : string ;
98111 } > {
@@ -118,6 +131,32 @@ export class ApiClient {
118131 } > ;
119132 }
120133
134+ async sendEvents ( events : BaseEvent [ ] ) : Promise < void > {
135+ let endpoint = "api/private/unauth/telemetry/events" ;
136+ const headers : Record < string , string > = {
137+ Accept : "application/json" ,
138+ "Content-Type" : "application/json" ,
139+ "User-Agent" : this . options . userAgent ,
140+ } ;
141+
142+ const accessToken = await this . getAccessToken ( ) ;
143+ if ( accessToken ) {
144+ endpoint = "api/private/v1.0/telemetry/events" ;
145+ headers [ "Authorization" ] = `Bearer ${ accessToken } ` ;
146+ }
147+
148+ const url = new URL ( endpoint , this . options . baseUrl ) ;
149+ const response = await fetch ( url , {
150+ method : "POST" ,
151+ headers,
152+ body : JSON . stringify ( events ) ,
153+ } ) ;
154+
155+ if ( ! response . ok ) {
156+ throw await ApiClientError . fromResponse ( response ) ;
157+ }
158+ }
159+
121160 // DO NOT EDIT. This is auto-generated code.
122161 async listClustersForAllProjects ( options ?: FetchOptions < operations [ "listClustersForAllProjects" ] > ) {
123162 const { data } = await this . client . GET ( "/api/atlas/v2/clusters" , options ) ;
0 commit comments