connect
Establish a connection to a MySQL database using provided credentials or environment variables, enabling database operations 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 MySQL connection pool using the provided parameters or falls back to environment variables, closes any existing pool, tests the new connection by acquiring and releasing it, and returns a success message with 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 schema defining the optional input parameters for the 'connect' tool: 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:89-140 (registration)Registration of the 'connect' tool on the MCP server using server.tool(), specifying the tool name, description, input schema, and handler function.// Tool: connect 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, }; } );