connect_db
Establish a connection to a MySQL database using host, user, password, and database details. Allows integration with MCP MySQL Server for query execution, table listing, and automated connection management.
Instructions
Connect to MySQL database (optional if environment variables are set)
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| database | Yes | Database name | |
| host | Yes | Database host | |
| password | Yes | Database password | |
| port | No | Database port (optional) | |
| user | Yes | Database user |
Input Schema (JSON Schema)
{
"properties": {
"database": {
"description": "Database name",
"type": "string"
},
"host": {
"description": "Database host",
"type": "string"
},
"password": {
"description": "Database password",
"type": "string"
},
"port": {
"description": "Database port (optional)",
"type": "number"
},
"user": {
"description": "Database user",
"type": "string"
}
},
"required": [
"host",
"user",
"password",
"database"
],
"type": "object"
}
Implementation Reference
- src/index.ts:318-362 (handler)The primary handler function that executes the 'connect_db' tool logic: validates required DB credentials, closes existing pool if any, sets new config, calls ensureConnection to establish MySQL connection pool, returns success response.private async handleConnectDb(requestId: string, args: any) { // 验证参数 if (!args.host || !args.user || args.password === undefined || args.password === null || !args.database) { throw new McpError( ErrorCode.InvalidParams, 'Missing required database configuration parameters' ); } // 关闭现有连接池 if (this.pool) { try { console.error(`[${requestId}] Closing existing connection pool`); await this.pool.end(); } catch (error) { console.error(`[${requestId}] Error closing pool: ${getErrorMessage(error)}`); } this.pool = null; } this.config = { host: args.host, user: args.user, password: args.password, database: args.database, port: args.port || 3306, // 确保有默认端口 }; try { console.error(`[${requestId}] Connecting to database: ${this.config.host}:${this.config.port}/${this.config.database}`); 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:188-212 (schema)Input schema definition for the 'connect_db' tool, specifying required properties (host, user, password, database) and optional port.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', }, port: { type: 'number', description: 'Database port (optional)', }, }, required: ['host', 'user', 'password', 'database'],
- src/index.ts:185-214 (registration)Registration of the 'connect_db' tool in the ListTools response, including name, description, and input schema.{ name: 'connect_db', description: 'Connect to MySQL database (optional if environment variables are set)', 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', }, port: { type: 'number', description: 'Database port (optional)', }, }, required: ['host', 'user', 'password', 'database'], }, },