Skip to content

Commit 40c2293

Browse files
chore: adds vscode specific connection error handler
1 parent 5e6e677 commit 40c2293

File tree

2 files changed

+79
-4
lines changed

2 files changed

+79
-4
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import {
2+
ErrorCodes,
3+
type ConnectionErrorHandler,
4+
} from '@himanshusinghs/mongodb-mcp-server';
5+
import type ConnectionController from '../connectionController';
6+
7+
export const createMCPConnectionErrorHandler = (
8+
connectionController: Pick<
9+
ConnectionController,
10+
'getSavedConnections' | 'getActiveConnectionName'
11+
>,
12+
): ConnectionErrorHandler => {
13+
return (error) => {
14+
const activeConnectionName = connectionController.getActiveConnectionName();
15+
const availableConnections = connectionController
16+
.getSavedConnections()
17+
.map(({ name }) => ({ name }));
18+
19+
const connectionGuidance: string[] = availableConnections.length
20+
? [
21+
`Following connections are available in the Sidebar for the user to connect to: ${JSON.stringify(availableConnections)}. Note to LLM: Provide user with the list of connections available and ask them to select a connection in the Sidebar.`,
22+
'Note to LLM: Alternatively, if the user wish to establish a new MongoDB connection, then ask them to follow our official guide on the following link: https://www.mongodb.com/docs/mongodb-vscode/connect/#create-a-connection',
23+
]
24+
: [
25+
'The user has no connections saved in their MongoDB Extension sidebar.',
26+
'Note to LLM: Ask the user to follow our official guide on the following link: https://www.mongodb.com/docs/mongodb-vscode/connect/#create-a-connection to connect to a MongoDB cluster.',
27+
];
28+
29+
switch (error.code) {
30+
case ErrorCodes.NotConnectedToMongoDB:
31+
return {
32+
errorHandled: true,
33+
result: {
34+
content: [
35+
{
36+
type: 'text',
37+
text: 'You need to connect to a MongoDB instance before you can access its data.',
38+
},
39+
...connectionGuidance.map<{ type: 'text'; text: string }>(
40+
(text) => ({ type: 'text', text }),
41+
),
42+
],
43+
},
44+
};
45+
case ErrorCodes.MisconfiguredConnectionString: {
46+
const resolutionGuidance: string[] = [
47+
'Note to LLM: Ask the user to reconnect to the desired connection. If the problem persist even after that then advice them to raise an issue on the VSCode project board - https://github.com/mongodb-js/vscode/issues',
48+
...connectionGuidance,
49+
];
50+
return {
51+
errorHandled: true,
52+
result: {
53+
content: [
54+
{
55+
type: 'text',
56+
text: `MCP server is having trouble connecting to the ${activeConnectionName ?? 'selected connection in the MongoDB VSCode extension'}.`,
57+
},
58+
...resolutionGuidance.map<{ type: 'text'; text: string }>(
59+
(text) => ({ type: 'text', text }),
60+
),
61+
],
62+
},
63+
};
64+
}
65+
default:
66+
return {
67+
errorHandled: false,
68+
};
69+
}
70+
};
71+
};

src/mcp/mcpController.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import type ConnectionController from '../connectionController';
1515
import { createLogger } from '../logging';
1616
import type { MCPConnectParams } from './mcpConnectionManager';
1717
import { MCPConnectionManager } from './mcpConnectionManager';
18+
import { createMCPConnectionErrorHandler } from './mcpConnectionErrorHandler';
1819

1920
type mcpServerStartupConfig = 'ask' | 'enabled' | 'disabled';
2021

@@ -90,11 +91,14 @@ export class MCPController {
9091
return connectionManager;
9192
};
9293

93-
const runner = new StreamableHttpRunner(
94-
mcpConfig,
94+
const runner = new StreamableHttpRunner({
95+
userConfig: mcpConfig,
9596
createConnectionManager,
96-
[new VSCodeMCPLogger()],
97-
);
97+
connectionErrorHandler: createMCPConnectionErrorHandler(
98+
this.connectionController,
99+
),
100+
additionalLoggers: [new VSCodeMCPLogger()],
101+
});
98102
await runner.start();
99103

100104
this.server = {

0 commit comments

Comments
 (0)