1- import { ToolBase , ToolCategory , TelemetryToolMetadata } from "../tool.js" ;
1+ import { ToolBase , ToolCategory , TelemetryToolMetadata , ToolArgs } from "../tool.js" ;
22import { ToolCallback } from "@modelcontextprotocol/sdk/server/mcp.js" ;
3+ import { CallToolResult } from "@modelcontextprotocol/sdk/types.js" ;
34import logger , { LogId } from "../../logger.js" ;
45import { z } from "zod" ;
6+ import { ApiClientError } from "../../common/atlas/apiClientError.js" ;
57
68export abstract class AtlasToolBase extends ToolBase {
79 protected category : ToolCategory = "atlas" ;
@@ -13,6 +15,50 @@ export abstract class AtlasToolBase extends ToolBase {
1315 return super . verifyAllowed ( ) ;
1416 }
1517
18+ protected handleError (
19+ error : unknown ,
20+ args : ToolArgs < typeof this . argsShape >
21+ ) : Promise < CallToolResult > | CallToolResult {
22+ if ( error instanceof ApiClientError ) {
23+ const statusCode = error . response . status ;
24+
25+ if ( statusCode === 401 ) {
26+ return {
27+ content : [
28+ {
29+ type : "text" ,
30+ text : `Unable to authenticate with MongoDB Atlas, API error: ${ error . message }
31+
32+ Hint: Your API credentials may be invalid, expired or lack permissions.
33+ Please check your Atlas API credentials and ensure they have the appropriate permissions.
34+ For more information on setting up API keys, visit: https://www.mongodb.com/docs/atlas/configure-api-access/` ,
35+ } ,
36+ ] ,
37+ isError : true ,
38+ } ;
39+ }
40+
41+ if ( statusCode === 403 ) {
42+ return {
43+ content : [
44+ {
45+ type : "text" ,
46+ text : `Received a Forbidden API Error: ${ error . message }
47+
48+ You don't have sufficient permissions to perform this action in MongoDB Atlas
49+ Please ensure your API key has the necessary roles assigned.
50+ For more information on Atlas API access roles, visit: https://www.mongodb.com/docs/atlas/api/service-accounts-overview/` ,
51+ } ,
52+ ] ,
53+ isError : true ,
54+ } ;
55+ }
56+ }
57+
58+ // For other types of errors, use the default error handling from the base class
59+ return super . handleError ( error , args ) ;
60+ }
61+
1662 /**
1763 *
1864 * Resolves the tool metadata from the arguments passed to the tool
0 commit comments