connect_db
Establish connections to MySQL databases using URL or configuration parameters. Supports database operations such as queries, schema management, and CRUD through a standardized interface on the MCP-MongoDB-MySQL-Server.
Instructions
Connect to MySQL database using URL or config
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| database | No | ||
| host | No | ||
| password | No | ||
| url | No | Database URL (mysql://user:pass@host:port/db) | |
| user | No | ||
| workspace | No | Project workspace path |
Input Schema (JSON Schema)
{
"properties": {
"database": {
"optional": true,
"type": "string"
},
"host": {
"optional": true,
"type": "string"
},
"password": {
"optional": true,
"type": "string"
},
"url": {
"description": "Database URL (mysql://user:pass@host:port/db)",
"optional": true,
"type": "string"
},
"user": {
"optional": true,
"type": "string"
},
"workspace": {
"description": "Project workspace path",
"optional": true,
"type": "string"
}
},
"type": "object"
}
Implementation Reference
- src/index.ts:607-660 (handler)The core handler function for the 'connect_db' tool. Parses connection arguments (URL, workspace, or direct params), closes existing connection, sets config, establishes MySQL connection using ensureConnection(), and returns success message.private async handleConnectDb(args: any) { let config: ConnectionConfig | null = null; // Priority 1: Direct URL if (args.url) { config = this.parseConnectionUrl(args.url); } // Priority 2: Workspace config else if (args.workspace) { this.currentWorkspace = args.workspace; config = await this.loadWorkspaceConfig(args.workspace); } // Priority 3: Individual connection params else if (args.host && args.user && args.password && args.database) { config = { host: args.host, user: args.user, password: args.password, database: args.database }; } if (!config) { throw new McpError( ErrorCode.InvalidParams, 'No valid database configuration provided. Please provide either a URL, workspace path, or connection parameters.' ); } // Close existing connection if any if (this.connection) { await this.connection.end(); this.connection = null; } this.config = config; try { await this.ensureConnection(); return { content: [ { type: 'text', text: `Successfully connected to database ${config.database} at ${config.host}` } ] }; } catch (error) { throw new McpError( ErrorCode.InternalError, `Failed to connect to database: ${getErrorMessage(error)}` ); } }
- src/index.ts:208-232 (registration)Tool registration in ListToolsRequestSchema handler, defining name, description, and input schema for connect_db.{ name: 'connect_db', description: 'Connect to MySQL database using URL or config', inputSchema: { type: 'object', properties: { url: { type: 'string', description: 'Database URL (mysql://user:pass@host:port/db)', optional: true }, workspace: { type: 'string', description: 'Project workspace path', optional: true }, // Keep existing connection params as fallback host: { type: 'string', optional: true }, user: { type: 'string', optional: true }, password: { type: 'string', optional: true }, database: { type: 'string', optional: true } }, // No required fields - will try different connection methods }, },
- src/index.ts:537-538 (registration)Dispatch in CallToolRequestSchema handler that routes 'connect_db' calls to the handleConnectDb method.case 'connect_db': return await this.handleConnectDb(request.params.arguments);
- src/index.ts:211-231 (schema)Input schema definition for the connect_db tool, specifying optional parameters for URL, workspace, or individual connection details.inputSchema: { type: 'object', properties: { url: { type: 'string', description: 'Database URL (mysql://user:pass@host:port/db)', optional: true }, workspace: { type: 'string', description: 'Project workspace path', optional: true }, // Keep existing connection params as fallback host: { type: 'string', optional: true }, user: { type: 'string', optional: true }, password: { type: 'string', optional: true }, database: { type: 'string', optional: true } }, // No required fields - will try different connection methods },
- src/index.ts:662-688 (helper)Helper function parseConnectionUrl used by handleConnectDb to parse MySQL connection URL into config object.private parseConnectionUrl(url: string): ConnectionConfig { const parsed = parseUrl(url); if (!parsed.host || !parsed.auth) { throw new McpError( ErrorCode.InvalidParams, 'Invalid connection URL' ); } const [user, password] = parsed.auth.split(':'); const database = parsed.pathname?.slice(1); if (!database) { throw new McpError( ErrorCode.InvalidParams, 'Database name must be specified in URL' ); } return { host: parsed.hostname!, user, password: password || '', database, ssl: parsed.protocol === 'mysqls:' ? { rejectUnauthorized: true } : undefined }; }