Skip to main content
Glama
nilsir

MCP Server MySQL

by nilsir

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
NameRequiredDescriptionDefault
hostNoDatabase host
portNoDatabase port
userNoDatabase user
passwordNoDatabase password
databaseNoDatabase name

Implementation Reference

  • 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, }; }
  • 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, }; } );

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/nilsir/mcp-server-mysql'

If you have feedback or need assistance with the MCP directory API, please join our Discord server