Skip to main content
Glama

connect_db

Establish a connection to a MySQL database by providing host, user, password, and database name to enable database interactions through the MCP MySQL Server.

Instructions

Connect to MySQL database

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
hostYesDatabase host
userYesDatabase user
passwordYesDatabase password
databaseYesDatabase name

Implementation Reference

  • The main handler function for the 'connect_db' tool. Validates arguments, closes existing connection, sets database config, establishes connection using ensureConnection, and returns success or throws error.
    private async handleConnectDb(args: any) { if (!args.host || !args.user || !args.password || !args.database) { throw new McpError( ErrorCode.InvalidParams, 'Missing required database configuration parameters' ); } // Close existing connection if any if (this.connection) { await this.connection.end(); this.connection = null; } this.config = { host: args.host, user: args.user, password: args.password, database: args.database, }; try { 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)}` ); } }
  • Input schema defining required parameters for connecting to MySQL: host, user, password, database.
    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', }, }, required: ['host', 'user', 'password', 'database'], },
  • src/index.ts:99-124 (registration)
    Tool registration in the ListTools response, including name, description, and input schema.
    { name: 'connect_db', description: 'Connect to MySQL database', 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', }, }, required: ['host', 'user', 'password', 'database'], }, },
  • src/index.ts:195-196 (registration)
    Dispatch in CallToolRequest handler switch statement routing 'connect_db' to its handler.
    case 'connect_db': return await this.handleConnectDb(request.params.arguments);
  • Helper method to ensure database connection exists, creates it if needed using the stored config, called by handleConnectDb.
    private async ensureConnection() { if (!this.config) { throw new McpError( ErrorCode.InvalidRequest, 'Database configuration not set. Use connect_db tool first.' ); } if (!this.connection) { try { this.connection = await mysql.createConnection(this.config); } catch (error) { throw new McpError( ErrorCode.InternalError, `Failed to connect to database: ${getErrorMessage(error)}` ); } } }

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/Atomzzm/mcp-mysql-server'

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