connect_db
Establish a connection to a MySQL database by providing host, user, password, and database name to enable database interactions through the MCP MySQL Server.
Instructions
Connect to MySQL database
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| host | Yes | Database host | |
| user | Yes | Database user | |
| password | Yes | Database password | |
| database | Yes | Database name |
Implementation Reference
- src/index.ts:214-251 (handler)The main handler function for the 'connect_db' tool. Validates arguments, closes existing connection, sets database config, establishes connection using ensureConnection, and returns success or throws error.private async handleConnectDb(args: any) { if (!args.host || !args.user || !args.password || !args.database) { throw new McpError( ErrorCode.InvalidParams, 'Missing required database configuration parameters' ); } // Close existing connection if any if (this.connection) { await this.connection.end(); this.connection = null; } this.config = { host: args.host, user: args.user, password: args.password, database: args.database, }; try { await this.ensureConnection(); return { content: [ { type: 'text', text: 'Successfully connected to database', }, ], }; } catch (error) { throw new McpError( ErrorCode.InternalError, `Failed to connect to database: ${getErrorMessage(error)}` ); } }
- src/index.ts:102-123 (schema)Input schema defining required parameters for connecting to MySQL: host, user, password, database.inputSchema: { type: 'object', properties: { host: { type: 'string', description: 'Database host', }, user: { type: 'string', description: 'Database user', }, password: { type: 'string', description: 'Database password', }, database: { type: 'string', description: 'Database name', }, }, required: ['host', 'user', 'password', 'database'], },
- src/index.ts:99-124 (registration)Tool registration in the ListTools response, including name, description, and input schema.{ name: 'connect_db', description: 'Connect to MySQL database', inputSchema: { type: 'object', properties: { host: { type: 'string', description: 'Database host', }, user: { type: 'string', description: 'Database user', }, password: { type: 'string', description: 'Database password', }, database: { type: 'string', description: 'Database name', }, }, required: ['host', 'user', 'password', 'database'], }, },
- src/index.ts:195-196 (registration)Dispatch in CallToolRequest handler switch statement routing 'connect_db' to its handler.case 'connect_db': return await this.handleConnectDb(request.params.arguments);
- src/index.ts:76-94 (helper)Helper method to ensure database connection exists, creates it if needed using the stored config, called by handleConnectDb.private async ensureConnection() { if (!this.config) { throw new McpError( ErrorCode.InvalidRequest, 'Database configuration not set. Use connect_db tool first.' ); } if (!this.connection) { try { this.connection = await mysql.createConnection(this.config); } catch (error) { throw new McpError( ErrorCode.InternalError, `Failed to connect to database: ${getErrorMessage(error)}` ); } } }