connect
Establish a connection to a MySQL database using provided credentials or environment variables, enabling database interactions through the MCP Server MySQL.
Instructions
Connect to a MySQL database. If not called explicitly, will use environment variables for connection.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| host | No | Database host | |
| port | No | Database port | |
| user | No | Database user | |
| password | No | Database password | |
| database | No | Database name |
Implementation Reference
- src/index.ts:100-139 (handler)The handler function for the 'connect' tool. It creates a new MySQL connection pool using provided parameters or falling back to environment variables, closes any existing pool, tests the connection by acquiring and releasing it, and returns success information including connection details.async ({ host, port, user, password, database }) => { const config = { host: host || process.env.MYSQL_HOST || "localhost", port: port || parseInt(process.env.MYSQL_PORT || "3306", 10), user: user || process.env.MYSQL_USER || "root", password: password || process.env.MYSQL_PASSWORD || "", database: database, waitForConnections: true, connectionLimit: 10, queueLimit: 0, }; // Close existing pool if any if (pool) { await pool.end(); } pool = mysql.createPool(config); // Test connection const connection = await pool.getConnection(); connection.release(); const output = { success: true, host: config.host, port: config.port, database: config.database || null, }; return { content: [ { type: "text" as const, text: `Successfully connected to MySQL server at ${config.host}:${config.port}${config.database ? ` (database: ${config.database})` : ""}`, }, ], structuredContent: output, }; }
- src/index.ts:93-99 (schema)Zod input schema for the 'connect' tool, defining optional parameters for host, port, user, password, and database.{ host: z.string().optional().describe("Database host"), port: z.number().optional().describe("Database port"), user: z.string().optional().describe("Database user"), password: z.string().optional().describe("Database password"), database: z.string().optional().describe("Database name"), },
- src/index.ts:90-140 (registration)Registration of the 'connect' tool using the McpServer.tool method, including name, description, input schema, and handler function.server.tool( "connect", "Connect to a MySQL database. If not called explicitly, will use environment variables for connection.", { host: z.string().optional().describe("Database host"), port: z.number().optional().describe("Database port"), user: z.string().optional().describe("Database user"), password: z.string().optional().describe("Database password"), database: z.string().optional().describe("Database name"), }, async ({ host, port, user, password, database }) => { const config = { host: host || process.env.MYSQL_HOST || "localhost", port: port || parseInt(process.env.MYSQL_PORT || "3306", 10), user: user || process.env.MYSQL_USER || "root", password: password || process.env.MYSQL_PASSWORD || "", database: database, waitForConnections: true, connectionLimit: 10, queueLimit: 0, }; // Close existing pool if any if (pool) { await pool.end(); } pool = mysql.createPool(config); // Test connection const connection = await pool.getConnection(); connection.release(); const output = { success: true, host: config.host, port: config.port, database: config.database || null, }; return { content: [ { type: "text" as const, text: `Successfully connected to MySQL server at ${config.host}:${config.port}${config.database ? ` (database: ${config.database})` : ""}`, }, ], structuredContent: output, }; } );