connect_db
Establish a connection to MySQL databases using URL parameters or configuration details to enable database operations through the MCP server.
Instructions
Connect to MySQL database using URL or config
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | No | Database URL (mysql://user:pass@host:port/db) | |
| workspace | No | Project workspace path | |
| host | No | ||
| user | No | ||
| password | No | ||
| database | No |
Implementation Reference
- src/index.ts:607-660 (handler)The primary handler function for the 'connect_db' tool. Parses connection arguments (URL, workspace, or direct params), closes any existing connection, sets the config, ensures connection, 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:211-231 (schema)Input schema definition for the 'connect_db' tool, specifying optional properties for URL, workspace, or individual connection parameters.
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:208-232 (registration)Tool registration in the ListTools response, including 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 registration in the CallToolRequestHandler switch statement, routing 'connect_db' calls to the handleConnectDb method.
case 'connect_db': return await this.handleConnectDb(request.params.arguments); - src/index.ts:662-688 (helper)Helper function to parse MySQL connection URL into config object, used by handleConnectDb.
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 }; }