mysql_connect
Establish a connection to a MySQL database by providing host, user, password, and optional parameters like port, database name, and SSL settings.
Instructions
Connect to a MySQL database with provided connection parameters
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| host | Yes | MySQL server hostname or IP address | |
| port | No | MySQL server port (default: 3306) | |
| user | Yes | Database username | |
| password | Yes | Database password | |
| database | No | Database name (optional) | |
| ssl | No | Use SSL connection (default: false) |
Input Schema (JSON Schema)
{
"properties": {
"database": {
"description": "Database name (optional)",
"type": "string"
},
"host": {
"description": "MySQL server hostname or IP address",
"type": "string"
},
"password": {
"description": "Database password",
"type": "string"
},
"port": {
"default": 3306,
"description": "MySQL server port (default: 3306)",
"type": "number"
},
"ssl": {
"default": false,
"description": "Use SSL connection (default: false)",
"type": "boolean"
},
"user": {
"description": "Database username",
"type": "string"
}
},
"required": [
"host",
"user",
"password"
],
"type": "object"
}
Implementation Reference
- src/index.ts:281-304 (handler)The main handler function for the 'mysql_connect' tool. It validates input using ConfigSchema, closes any existing connection, creates a new MySQL connection pool using createConnection, tests it, and returns a success message.private async handleConnect(args: any) { try { const config = ConfigSchema.parse(args); this.config = config; // Close existing connection if any if (this.pool) { await this.pool.end(); } this.pool = await this.createConnection(config); return { content: [ { type: "text", text: `Successfully connected to MySQL server at ${config.host}:${config.port}${config.database ? ` (database: ${config.database})` : ""}`, }, ], }; } catch (error) { throw new Error(`Connection failed: ${error instanceof Error ? error.message : String(error)}`); } }
- src/index.ts:15-23 (schema)Zod schema used in handleConnect to parse and validate the input parameters for mysql_connect.const ConfigSchema = z.object({ host: z.string(), port: z.number().optional().default(3306), user: z.string(), password: z.string(), database: z.string().optional(), ssl: z.boolean().optional().default(false), connectionLimit: z.number().optional().default(10), });
- src/index.ts:100-135 (registration)Tool registration in the ListTools response, including name, description, and JSON inputSchema matching ConfigSchema.{ name: "mysql_connect", description: "Connect to a MySQL database with provided connection parameters", inputSchema: { type: "object", properties: { host: { type: "string", description: "MySQL server hostname or IP address", }, port: { type: "number", description: "MySQL server port (default: 3306)", default: 3306, }, user: { type: "string", description: "Database username", }, password: { type: "string", description: "Database password", }, database: { type: "string", description: "Database name (optional)", }, ssl: { type: "boolean", description: "Use SSL connection (default: false)", default: false, }, }, required: ["host", "user", "password"], }, },
- src/index.ts:249-250 (registration)Dispatch case in the CallToolRequest handler that routes 'mysql_connect' calls to the handleConnect function.case "mysql_connect": return await this.handleConnect(args);
- src/index.ts:67-94 (helper)Helper function called by handleConnect to create and test the MySQL connection pool.private async createConnection(config: Config): Promise<mysql.Pool> { try { const poolConfig: mysql.PoolOptions = { host: config.host, port: config.port, user: config.user, password: config.password, database: config.database, connectionLimit: config.connectionLimit, multipleStatements: false, }; if (config.ssl) { poolConfig.ssl = {}; } this.pool = mysql.createPool(poolConfig); // Test the connection const connection = await this.pool.getConnection(); await connection.ping(); connection.release(); return this.pool; } catch (error) { throw new Error(`Failed to connect to MySQL: ${error instanceof Error ? error.message : String(error)}`); } }