From a25d3cd9a47349866e2695a3d71a79a622e52442 Mon Sep 17 00:00:00 2001 From: Bianca Lisle Date: Wed, 7 May 2025 17:30:24 +0100 Subject: [PATCH 1/2] chore: add more details for some api errors --- src/tools/atlas/atlasTool.ts | 52 +++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/src/tools/atlas/atlasTool.ts b/src/tools/atlas/atlasTool.ts index 6c74bb88..609dea13 100644 --- a/src/tools/atlas/atlasTool.ts +++ b/src/tools/atlas/atlasTool.ts @@ -1,7 +1,9 @@ -import { ToolBase, ToolCategory, TelemetryToolMetadata } from "../tool.js"; +import { ToolBase, ToolCategory, TelemetryToolMetadata, ToolArgs } from "../tool.js"; import { ToolCallback } from "@modelcontextprotocol/sdk/server/mcp.js"; +import { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; import logger, { LogId } from "../../logger.js"; import { z } from "zod"; +import { ApiClientError } from "../../common/atlas/apiClientError.js"; export abstract class AtlasToolBase extends ToolBase { protected category: ToolCategory = "atlas"; @@ -13,6 +15,54 @@ export abstract class AtlasToolBase extends ToolBase { return super.verifyAllowed(); } + protected handleError( + error: unknown, + args: ToolArgs + ): Promise | CallToolResult { + if (error instanceof ApiClientError) { + const statusCode = error.response.status; + + if (statusCode === 401) { + return { + content: [ + { + type: "text", + text: `Unable to authenticate with MongoDB Atlas. Your API credentials may be invalid, expired or lack permissions. Please check your Atlas API credentials and ensure they have the appropriate permissions. For more information on setting up API keys, visit: https://www.mongodb.com/docs/atlas/configure-api-access/`, + }, + ], + isError: true, + }; + } + + if (statusCode === 403) { + return { + content: [ + { + type: "text", + text: `You don't have sufficient permissions to perform this action in MongoDB Atlas. Please ensure your API key has the necessary roles assigned. For more information on Atlas API access roles, visit: https://www.mongodb.com/docs/atlas/api/service-accounts-overview/`, + }, + ], + isError: true, + }; + } + + if (statusCode === 429) { + return { + content: [ + { + type: "text", + text: `MongoDB Atlas API rate limit exceeded. Please wait before making additional requests. For more information on rate limits, visit: https://www.mongodb.com/docs/atlas/api/#rate-limits`, + }, + ], + isError: true, + }; + } + } + + // For other types of errors, use the default error handling from the base class + return super.handleError(error, args); + } + /** * * Resolves the tool metadata from the arguments passed to the tool From b79e53388ab8b442de7f29bd7e0157cc430a6db6 Mon Sep 17 00:00:00 2001 From: Bianca Lisle Date: Wed, 7 May 2025 17:46:37 +0100 Subject: [PATCH 2/2] update --- src/tools/atlas/atlasTool.ts | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/tools/atlas/atlasTool.ts b/src/tools/atlas/atlasTool.ts index 609dea13..2b93a5ec 100644 --- a/src/tools/atlas/atlasTool.ts +++ b/src/tools/atlas/atlasTool.ts @@ -27,31 +27,27 @@ export abstract class AtlasToolBase extends ToolBase { content: [ { type: "text", - text: `Unable to authenticate with MongoDB Atlas. Your API credentials may be invalid, expired or lack permissions. Please check your Atlas API credentials and ensure they have the appropriate permissions. For more information on setting up API keys, visit: https://www.mongodb.com/docs/atlas/configure-api-access/`, - }, - ], - isError: true, - }; - } + text: `Unable to authenticate with MongoDB Atlas, API error: ${error.message} - if (statusCode === 403) { - return { - content: [ - { - type: "text", - text: `You don't have sufficient permissions to perform this action in MongoDB Atlas. Please ensure your API key has the necessary roles assigned. For more information on Atlas API access roles, visit: https://www.mongodb.com/docs/atlas/api/service-accounts-overview/`, +Hint: Your API credentials may be invalid, expired or lack permissions. +Please check your Atlas API credentials and ensure they have the appropriate permissions. +For more information on setting up API keys, visit: https://www.mongodb.com/docs/atlas/configure-api-access/`, }, ], isError: true, }; } - if (statusCode === 429) { + if (statusCode === 403) { return { content: [ { type: "text", - text: `MongoDB Atlas API rate limit exceeded. Please wait before making additional requests. For more information on rate limits, visit: https://www.mongodb.com/docs/atlas/api/#rate-limits`, + text: `Received a Forbidden API Error: ${error.message} + +You don't have sufficient permissions to perform this action in MongoDB Atlas +Please ensure your API key has the necessary roles assigned. +For more information on Atlas API access roles, visit: https://www.mongodb.com/docs/atlas/api/service-accounts-overview/`, }, ], isError: true,