@@ -3,6 +3,7 @@ import { CallToolResult, McpError } from "@modelcontextprotocol/sdk/types.js";
33import { NodeDriverServiceProvider } from "@mongosh/service-provider-node-driver" ;
44import { MongoDBToolBase } from "./mongodbTool.js" ;
55import { ToolArgs } from "../tool" ;
6+ import { ErrorCodes } from "../../errors.js" ;
67
78const argsShape = {
89 connectionStringOrClusterName : z
@@ -17,55 +18,48 @@ export class ConnectTool extends MongoDBToolBase<typeof argsShape> {
1718 protected argsShape = argsShape ;
1819
1920 protected async execute ( { connectionStringOrClusterName } : ToolArgs < typeof argsShape > ) : Promise < CallToolResult > {
20- try {
21- if ( ! connectionStringOrClusterName ) {
22- // TODO: try reconnecting to the default connection
21+ if ( ! connectionStringOrClusterName ) {
22+ // TODO: try reconnecting to the default connection
23+ return {
24+ content : [
25+ { type : "text" , text : "No connection details provided." } ,
26+ { type : "text" , text : "Please provide either a connection string or a cluster name" } ,
27+ {
28+ type : "text" ,
29+ text : "Alternatively, you can use the default deployment at mongodb://localhost:27017" ,
30+ } ,
31+ ] ,
32+ } ;
33+ }
34+
35+ let connectionString : string ;
36+
37+ if ( typeof connectionStringOrClusterName === "string" ) {
38+ if (
39+ connectionStringOrClusterName . startsWith ( "mongodb://" ) ||
40+ connectionStringOrClusterName . startsWith ( "mongodb+srv://" )
41+ ) {
42+ connectionString = connectionStringOrClusterName ;
43+ } else {
44+ // TODO:
2345 return {
2446 content : [
25- { type : "text" , text : "No connection details provided." } ,
26- { type : "text" , text : "Please provide either a connection string or a cluster name" } ,
2747 {
2848 type : "text" ,
29- text : "Alternatively, you can use the default deployment at mongodb://localhost:27017" ,
49+ text : `Connecting via cluster name not supported yet. Please provide a connection string.` ,
3050 } ,
3151 ] ,
3252 } ;
3353 }
54+ } else {
55+ throw new McpError ( ErrorCodes . InvalidParams , "Invalid connection options" ) ;
56+ }
3457
35- let connectionString : string ;
36-
37- if ( typeof connectionStringOrClusterName === "string" ) {
38- if (
39- connectionStringOrClusterName . startsWith ( "mongodb://" ) ||
40- connectionStringOrClusterName . startsWith ( "mongodb+srv://" )
41- ) {
42- connectionString = connectionStringOrClusterName ;
43- } else {
44- // TODO:
45- return {
46- content : [
47- {
48- type : "text" ,
49- text : `Connecting via cluster name not supported yet. Please provide a connection string.` ,
50- } ,
51- ] ,
52- } ;
53- }
54- } else {
55- throw new McpError ( 2 , "Invalid connection options" ) ;
56- }
57-
58- await this . connect ( connectionString ) ;
58+ await this . connect ( connectionString ) ;
5959
60- return {
61- content : [ { type : "text" , text : `Successfully connected to ${ connectionString } .` } ] ,
62- } ;
63- } catch ( error ) {
64- return {
65- content : [ { type : "text" , text : "Failed to get cluster connection string" } ] ,
66- isError : true ,
67- } ;
68- }
60+ return {
61+ content : [ { type : "text" , text : `Successfully connected to ${ connectionString } .` } ] ,
62+ } ;
6963 }
7064
7165 private async connect ( connectionString : string ) : Promise < void > {
0 commit comments