|
| 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 | +}; |
0 commit comments