setup_atlas_network_access
Configure network access for MongoDB Atlas projects by specifying allowed IP addresses or CIDR blocks to control database connectivity.
Instructions
Sets up network access for an existing Atlas project. Accepts list of IP addresses or CIDR blocks.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| projectId | Yes | The ID of the Atlas project. | |
| ipAddresses | Yes | An array of IP addresses or CIDR blocks for network access. |
Implementation Reference
- src/index.ts:161-185 (handler)The main handler function that implements the tool logic: constructs the Atlas API request to add the provided IP addresses to the project's IP access list and returns the result or error.private async setupAtlasNetworkAccess(input: NetworkAccessInput) { try { const url = `https://cloud.mongodb.com/api/atlas/v1.0/groups/${input.projectId}/accessList`; const body = input.ipAddresses.map(ip => ({ ipAddress: ip, comment: "Added via Atlas Project Manager MCP" })); const result = await this.makeAtlasRequest(url, 'POST', body); return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] }; } catch (error: any) { return { content: [{ type: 'text', text: error.message }], isError: true }; } }
- src/index.ts:410-430 (registration)Registration of the tool in the ListTools handler, defining name, description, and JSON input schema.{ name: 'setup_atlas_network_access', description: 'Sets up network access for an existing Atlas project. Accepts list of IP addresses or CIDR blocks.', inputSchema: { type: 'object', properties: { projectId: { type: 'string', description: 'The ID of the Atlas project.', }, ipAddresses: { type: 'array', items: { type: 'string', }, description: 'An array of IP addresses or CIDR blocks for network access.', }, }, required: ['projectId', 'ipAddresses'], }, },
- src/index.ts:20-23 (schema)TypeScript interface defining the expected input shape for the handler function.interface NetworkAccessInput { projectId: string; ipAddresses: string[]; }
- src/index.ts:552-554 (registration)Dispatch to the handler function in the CallToolRequestSchema switch statement.case 'setup_atlas_network_access': result = await this.setupAtlasNetworkAccess(input as unknown as NetworkAccessInput); break;
- src/index.ts:55-113 (helper)Shared helper method for making authenticated requests to the Atlas API using Digest authentication.private async makeAtlasRequest(url: string, method: string, body?: any) { // Step 1: Make initial request to get digest challenge const initialResponse = await fetch(url, { method, headers: { 'Content-Type': 'application/json' }, body: body ? JSON.stringify(body) : undefined }); // Check if we got a 401 with WWW-Authenticate header (digest challenge) if (initialResponse.status === 401) { const wwwAuthHeader = initialResponse.headers.get('WWW-Authenticate'); if (!wwwAuthHeader || !wwwAuthHeader.startsWith('Digest ')) { throw new Error('Expected Digest authentication challenge not received'); } // Parse the digest challenge const authDetails: Record<string, string> = {}; wwwAuthHeader.substring(7).split(',').forEach(part => { const [key, value] = part.trim().split('='); // Remove quotes if present authDetails[key] = value.startsWith('"') ? value.slice(1, -1) : value; }); // Generate a random client nonce (cnonce) const cnonce = Math.random().toString(36).substring(2, 15); const nc = '00000001'; // nonce count, incremented for each request with the same nonce // Calculate the response hash const ha1 = this.md5(`${this.apiKey}:${authDetails.realm}:${this.privateKey}`); const ha2 = this.md5(`${method}:${new URL(url).pathname}`); const response = this.md5(`${ha1}:${authDetails.nonce}:${nc}:${cnonce}:${authDetails.qop}:${ha2}`); // Build the Authorization header const authHeader = `Digest username="${this.apiKey}", realm="${authDetails.realm}", nonce="${authDetails.nonce}", uri="${new URL(url).pathname}", qop=${authDetails.qop}, nc=${nc}, cnonce="${cnonce}", response="${response}", algorithm=${authDetails.algorithm || 'MD5'}`; // Make the actual request with the digest authentication const digestResponse = await fetch(url, { method, headers: { 'Content-Type': 'application/json', 'Authorization': authHeader }, body: body ? JSON.stringify(body) : undefined }); if (!digestResponse.ok) { throw new Error(`Atlas API error: ${digestResponse.statusText}`); } return digestResponse.json(); } else if (initialResponse.ok) { // If the initial request succeeded without authentication (unlikely) return initialResponse.json(); } else { throw new Error(`Atlas API error: ${initialResponse.statusText}`); } }