diff --git a/src/tools/atlas/atlasTool.ts b/src/tools/atlas/atlasTool.ts index 6c74bb8..2b93a5e 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,50 @@ 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, API error: ${error.message} + +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 === 403) { + return { + content: [ + { + type: "text", + 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, + }; + } + } + + // 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