Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion src/tools/atlas/atlasTool.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -13,6 +15,50 @@ export abstract class AtlasToolBase extends ToolBase {
return super.verifyAllowed();
}

protected handleError(
error: unknown,
args: ToolArgs<typeof this.argsShape>
): Promise<CallToolResult> | 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
Expand Down
Loading