Skip to content

Commit 696a5f9

Browse files
committed
fix: error handling
1 parent a2d44ad commit 696a5f9

File tree

3 files changed

+17
-29
lines changed

3 files changed

+17
-29
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ The MongoDB MCP Server can be configured using multiple methods, with the follow
174174
To use the Atlas API tools, you'll need to create a service account in MongoDB Atlas:
175175

176176
1. **Create a Service Account:**
177+
177178
- Log in to MongoDB Atlas at [cloud.mongodb.com](https://cloud.mongodb.com)
178179
- Navigate to Access Manager > Organization Access
179180
- Click Add New > Applications > Service Accounts
@@ -182,10 +183,12 @@ To use the Atlas API tools, you'll need to create a service account in MongoDB A
182183
- Click "Create"
183184

184185
2. **Save Client Credentials:**
186+
185187
- After creation, you'll be shown the Client ID and Client Secret
186188
- **Important:** Copy and save the Client Secret immediately as it won't be displayed again
187189

188190
3. **Add Access List Entry (Optional but recommended):**
191+
189192
- Add your IP address to the API access list
190193

191194
4. **Configure the MCP Server:**

src/tools/mongodb/mongodbTool.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export abstract class MongoDBToolBase extends ToolBase {
2828
return provider;
2929
}
3030

31-
protected handleError(error: unknown): CallToolResult | undefined {
31+
protected handleError(error: unknown): Promise<CallToolResult> | CallToolResult {
3232
if (error instanceof MongoDBError && error.code === ErrorCodes.NotConnectedToMongoDB) {
3333
return {
3434
content: [
@@ -41,9 +41,10 @@ export abstract class MongoDBToolBase extends ToolBase {
4141
text: "Please use the 'connect' tool to connect to a MongoDB instance.",
4242
},
4343
],
44+
isError: true,
4445
};
4546
}
4647

47-
return undefined;
48+
return super.handleError(error);
4849
}
4950
}

src/tools/tool.ts

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,7 @@ export abstract class ToolBase {
3232
} catch (error) {
3333
logger.error(mongoLogId(1_000_000), "tool", `Error executing ${this.name}: ${error}`);
3434

35-
// If the error is authentication related, suggest using auth tool
36-
if (error instanceof Error && error.message.includes("Not authenticated")) {
37-
return {
38-
content: [
39-
{ type: "text", text: "You need to authenticate before accessing Atlas data." },
40-
{
41-
type: "text",
42-
text: "Please use the 'auth' tool to log in to your MongoDB Atlas account.",
43-
},
44-
],
45-
};
46-
}
47-
48-
return (
49-
this.handleError(error) || {
50-
content: [
51-
{
52-
type: "text",
53-
text: `Error running ${this.name}: ${error instanceof Error ? error.message : String(error)}`,
54-
},
55-
],
56-
isError: true,
57-
}
58-
);
35+
return await this.handleError(error);
5936
}
6037
};
6138

@@ -70,8 +47,15 @@ export abstract class ToolBase {
7047
}
7148

7249
// This method is intended to be overridden by subclasses to handle errors
73-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
74-
protected handleError(error: unknown): CallToolResult | undefined {
75-
return undefined;
50+
protected handleError(error: unknown): Promise<CallToolResult> | CallToolResult {
51+
return {
52+
content: [
53+
{
54+
type: "text",
55+
text: `Error running ${this.name}: ${error instanceof Error ? error.message : String(error)}`,
56+
},
57+
],
58+
isError: true,
59+
};
7660
}
7761
}

0 commit comments

Comments
 (0)