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";
22import { ToolBase } from "../tool.js" ;
33import { State } from "../../state.js" ;
44import { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver" ;
5- import { McpError } from "@modelcontextprotocol/sdk/types.js" ;
5+ import { CallToolResult , McpError } from "@modelcontextprotocol/sdk/types.js" ;
66import { ErrorCodes } from "../../errors.js" ;
77
88export type MongoDBToolState = { serviceProvider ?: NodeDriverServiceProvider } ;
@@ -18,9 +18,28 @@ export abstract class MongoDBToolBase<Args extends ZodRawShape = ZodRawShape> ex
1818 protected ensureConnected ( ) : NodeDriverServiceProvider {
1919 const provider = this . mongodbState . serviceProvider ;
2020 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" ) ;
2222 }
2323
2424 return provider ;
2525 }
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+ }
2645}
Original file line number Diff line number Diff line change @@ -39,15 +39,17 @@ export abstract class ToolBase<Args extends ZodRawShape> {
3939 } ;
4040 }
4141
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+ ) ;
5153 }
5254 } ;
5355
@@ -60,4 +62,8 @@ export abstract class ToolBase<Args extends ZodRawShape> {
6062 server . tool ( this . name , this . description , callback as any ) ;
6163 }
6264 }
65+
66+ protected handleError ( error : unknown ) : CallToolResult | undefined {
67+ return undefined ;
68+ }
6369}
You can’t perform that action at this time.
0 commit comments