File tree Expand file tree Collapse file tree 2 files changed +36
-11
lines changed Expand file tree Collapse file tree 2 files changed +36
-11
lines changed Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ import { ZodRawShape } from "zod";
2
2
import { ToolBase } from "../tool.js" ;
3
3
import { State } from "../../state.js" ;
4
4
import { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver" ;
5
- import { McpError } from "@modelcontextprotocol/sdk/types.js" ;
5
+ import { CallToolResult , McpError } from "@modelcontextprotocol/sdk/types.js" ;
6
6
import { ErrorCodes } from "../../errors.js" ;
7
7
8
8
export type MongoDBToolState = { serviceProvider ?: NodeDriverServiceProvider } ;
@@ -18,9 +18,28 @@ export abstract class MongoDBToolBase<Args extends ZodRawShape = ZodRawShape> ex
18
18
protected ensureConnected ( ) : NodeDriverServiceProvider {
19
19
const provider = this . mongodbState . serviceProvider ;
20
20
if ( ! provider ) {
21
- throw new McpError ( ErrorCodes . NotConnectedToMongoDB , ` Not connected to MongoDB instance with name ${ name } ` ) ;
21
+ throw new McpError ( ErrorCodes . NotConnectedToMongoDB , " Not connected to MongoDB" ) ;
22
22
}
23
23
24
24
return provider ;
25
25
}
26
+
27
+ protected handleError ( error : unknown ) : CallToolResult | undefined {
28
+ if ( error instanceof McpError && error . code === ErrorCodes . NotConnectedToMongoDB ) {
29
+ return {
30
+ content : [
31
+ {
32
+ type : "text" ,
33
+ text : "You need to connect to a MongoDB instance before you can access its data." ,
34
+ } ,
35
+ {
36
+ type : "text" ,
37
+ text : "Please use the 'connect' tool to connect to a MongoDB instance." ,
38
+ } ,
39
+ ] ,
40
+ } ;
41
+ }
42
+
43
+ return undefined ;
44
+ }
26
45
}
Original file line number Diff line number Diff line change @@ -39,15 +39,17 @@ export abstract class ToolBase<Args extends ZodRawShape> {
39
39
} ;
40
40
}
41
41
42
- return {
43
- content : [
44
- {
45
- type : "text" ,
46
- text : `Error running ${ this . name } : ${ error instanceof Error ? error . message : String ( error ) } ` ,
47
- } ,
48
- ] ,
49
- isError : true ,
50
- } ;
42
+ return (
43
+ this . handleError ( error ) || {
44
+ content : [
45
+ {
46
+ type : "text" ,
47
+ text : `Error running ${ this . name } : ${ error instanceof Error ? error . message : String ( error ) } ` ,
48
+ } ,
49
+ ] ,
50
+ isError : true ,
51
+ }
52
+ ) ;
51
53
}
52
54
} ;
53
55
@@ -60,4 +62,8 @@ export abstract class ToolBase<Args extends ZodRawShape> {
60
62
server . tool ( this . name , this . description , callback as any ) ;
61
63
}
62
64
}
65
+
66
+ protected handleError ( error : unknown ) : CallToolResult | undefined {
67
+ return undefined ;
68
+ }
63
69
}
You can’t perform that action at this time.
0 commit comments