Skip to content

Commit a25d3cd

Browse files
committed
chore: add more details for some api errors
1 parent 3af84ca commit a25d3cd

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

src/tools/atlas/atlasTool.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { ToolBase, ToolCategory, TelemetryToolMetadata } from "../tool.js";
1+
import { ToolBase, ToolCategory, TelemetryToolMetadata, ToolArgs } from "../tool.js";
22
import { ToolCallback } from "@modelcontextprotocol/sdk/server/mcp.js";
3+
import { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
34
import logger, { LogId } from "../../logger.js";
45
import { z } from "zod";
6+
import { ApiClientError } from "../../common/atlas/apiClientError.js";
57

68
export abstract class AtlasToolBase extends ToolBase {
79
protected category: ToolCategory = "atlas";
@@ -13,6 +15,54 @@ 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. 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/`,
31+
},
32+
],
33+
isError: true,
34+
};
35+
}
36+
37+
if (statusCode === 403) {
38+
return {
39+
content: [
40+
{
41+
type: "text",
42+
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/`,
43+
},
44+
],
45+
isError: true,
46+
};
47+
}
48+
49+
if (statusCode === 429) {
50+
return {
51+
content: [
52+
{
53+
type: "text",
54+
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`,
55+
},
56+
],
57+
isError: true,
58+
};
59+
}
60+
}
61+
62+
// For other types of errors, use the default error handling from the base class
63+
return super.handleError(error, args);
64+
}
65+
1666
/**
1767
*
1868
* Resolves the tool metadata from the arguments passed to the tool

0 commit comments

Comments
 (0)