• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mongodb-js / mongodb-mcp-server / 14590701503

22 Apr 2025 08:50AM UTC coverage: 50.118% (-0.9%) from 50.98%
14590701503

Pull #82

github

blva
lint
Pull Request #82: add create project tool

23 of 150 branches covered (15.33%)

Branch coverage included in aggregate %.

8 of 31 new or added lines in 3 files covered. (25.81%)

4 existing lines in 1 file now uncovered.

401 of 696 relevant lines covered (57.61%)

36.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

8.0
/src/common/atlas/apiClient.ts
1
import config from "../../config.js";
6✔
2
import createClient, { Client, FetchOptions, Middleware } from "openapi-fetch";
6✔
3
import { AccessToken, ClientCredentials } from "simple-oauth2";
6✔
4
import { ApiClientError } from "./apiClientError.js";
6✔
5
import { paths, operations } from "./openapi.js";
6

7
const ATLAS_API_VERSION = "2025-03-12";
6✔
8

9
export interface ApiClientOptions {
10
    credentials?: {
11
        clientId: string;
12
        clientSecret: string;
13
    };
14
    baseUrl?: string;
15
    userAgent?: string;
16
}
17

18
export class ApiClient {
6✔
19
    private options: {
20
        baseUrl: string;
21
        userAgent: string;
22
        credentials?: {
23
            clientId: string;
24
            clientSecret: string;
25
        };
26
    };
27
    private client: Client<paths>;
28
    private oauth2Client?: ClientCredentials;
29
    private accessToken?: AccessToken;
30

31
    private getAccessToken = async () => {
×
32
        if (this.oauth2Client && (!this.accessToken || this.accessToken.expired())) {
×
33
            this.accessToken = await this.oauth2Client.getToken({});
×
34
        }
35
        return this.accessToken?.token.access_token as string | undefined;
×
36
    };
37

38
    private authMiddleware: Middleware = {
×
39
        onRequest: async ({ request, schemaPath }) => {
40
            if (schemaPath.startsWith("/api/private/unauth") || schemaPath.startsWith("/api/oauth")) {
×
41
                return undefined;
×
42
            }
43

44
            try {
×
45
                const accessToken = await this.getAccessToken();
×
46
                request.headers.set("Authorization", `Bearer ${accessToken}`);
×
47
                return request;
×
48
            } catch {
49
                // ignore not availble tokens, API will return 401
50
            }
51
        },
52
    };
53

54
    private readonly errorMiddleware: Middleware = {
×
55
        async onResponse({ response }) {
56
            if (!response.ok) {
×
57
                throw await ApiClientError.fromResponse(response);
×
58
            }
59
        },
60
    };
61

62
    constructor(options?: ApiClientOptions) {
63
        this.options = {
×
64
            ...options,
65
            baseUrl: options?.baseUrl || "https://cloud.mongodb.com/",
×
66
            userAgent:
67
                options?.userAgent ||
×
68
                `AtlasMCP/${config.version} (${process.platform}; ${process.arch}; ${process.env.HOSTNAME || "unknown"})`,
×
69
        };
70

71
        this.client = createClient<paths>({
×
72
            baseUrl: this.options.baseUrl,
73
            headers: {
74
                "User-Agent": this.options.userAgent,
75
                Accept: `application/vnd.atlas.${ATLAS_API_VERSION}+json`,
76
            },
77
        });
78
        if (this.options.credentials?.clientId && this.options.credentials?.clientSecret) {
×
79
            this.oauth2Client = new ClientCredentials({
×
80
                client: {
81
                    id: this.options.credentials.clientId,
82
                    secret: this.options.credentials.clientSecret,
83
                },
84
                auth: {
85
                    tokenHost: this.options.baseUrl,
86
                    tokenPath: "/api/oauth/token",
87
                },
88
            });
89
            this.client.use(this.authMiddleware);
×
90
        }
91
        this.client.use(this.errorMiddleware);
×
92
    }
93

94
    public async getIpInfo(): Promise<{
95
        currentIpv4Address: string;
96
    }> {
97
        const accessToken = await this.getAccessToken();
×
98

99
        const endpoint = "api/private/ipinfo";
×
100
        const url = new URL(endpoint, this.options.baseUrl);
×
101
        const response = await fetch(url, {
×
102
            method: "GET",
103
            headers: {
104
                Accept: "application/json",
105
                Authorization: `Bearer ${accessToken}`,
106
                "User-Agent": this.options.userAgent,
107
            },
108
        });
109

110
        if (!response.ok) {
×
111
            throw await ApiClientError.fromResponse(response);
×
112
        }
113

114
        return (await response.json()) as Promise<{
×
115
            currentIpv4Address: string;
116
        }>;
117
    }
118

119
    // DO NOT EDIT. This is auto-generated code.
120
    async listOrganizations(options?: FetchOptions<operations["listOrganizations"]>) {
NEW
121
        const { data } = await this.client.GET("/api/atlas/v2/orgs", options);
×
UNCOV
122
        return data;
×
123
    }
124

125
    async listProjects(options?: FetchOptions<operations["listProjects"]>) {
126
        const { data } = await this.client.GET("/api/atlas/v2/groups", options);
×
127
        return data;
×
128
    }
129

130
    async createProject(options: FetchOptions<operations["createProject"]>) {
131
        const { data } = await this.client.POST("/api/atlas/v2/groups", options);
×
132
        return data;
×
133
    }
134

135
    async listClustersForAllProjects(options?: FetchOptions<operations["listClustersForAllProjects"]>) {
NEW
136
        const { data } = await this.client.GET("/api/atlas/v2/clusters", options);
×
UNCOV
137
        return data;
×
138
    }
139

140
    async getProject(options: FetchOptions<operations["getProject"]>) {
NEW
141
        const { data } = await this.client.GET("/api/atlas/v2/groups/{groupId}", options);
×
UNCOV
142
        return data;
×
143
    }
144

145
    async listClusters(options: FetchOptions<operations["listClusters"]>) {
146
        const { data } = await this.client.GET("/api/atlas/v2/groups/{groupId}/clusters", options);
×
147
        return data;
×
148
    }
149

150
    async createCluster(options: FetchOptions<operations["createCluster"]>) {
151
        const { data } = await this.client.POST("/api/atlas/v2/groups/{groupId}/clusters", options);
×
152
        return data;
×
153
    }
154

155
    async listProjectIpAccessLists(options: FetchOptions<operations["listProjectIpAccessLists"]>) {
NEW
156
        const { data } = await this.client.GET("/api/atlas/v2/groups/{groupId}/accessList", options);
×
NEW
157
        return data;
×
158
    }
159

160
    async createProjectIpAccessList(options: FetchOptions<operations["createProjectIpAccessList"]>) {
NEW
161
        const { data } = await this.client.POST("/api/atlas/v2/groups/{groupId}/accessList", options);
×
UNCOV
162
        return data;
×
163
    }
164

165
    async listDatabaseUsers(options: FetchOptions<operations["listDatabaseUsers"]>) {
166
        const { data } = await this.client.GET("/api/atlas/v2/groups/{groupId}/databaseUsers", options);
×
167
        return data;
×
168
    }
169

170
    async createDatabaseUser(options: FetchOptions<operations["createDatabaseUser"]>) {
171
        const { data } = await this.client.POST("/api/atlas/v2/groups/{groupId}/databaseUsers", options);
×
172
        return data;
×
173
    }
174

175
    async getCluster(options: FetchOptions<operations["getCluster"]>) {
NEW
176
        const { data } = await this.client.GET("/api/atlas/v2/groups/{groupId}/clusters/{clusterName}", options);
×
NEW
177
        return data;
×
178
    }
179
    // DO NOT EDIT. This is auto-generated code.
180
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc