connect_db
Establish and manage secure connections to MySQL databases using a URL or configuration settings, enabling AI models to execute queries and manage schema effectively.
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:594-613 (handler)The core handler function for the 'connect_db' tool. Loads the database configuration from provided arguments and establishes a MySQL connection pool, returning a success message.private async handleConnectDb(args: ConnectionArgs) { this.config = await this.loadConfig(args); try { await this.ensureConnection(); return { content: [ { type: 'text', text: `Successfully connected to database ${this.config.database} at ${this.config.host}` } ] }; } catch (error) { throw new McpError( ErrorCode.InternalError, `Failed to connect to database: ${getErrorMessage(error)}` ); } }
- src/index.ts:396-420 (registration)Tool registration in ListToolsRequestHandler, 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:571-572 (registration)Dispatcher case in CallToolRequestHandler that routes 'connect_db' calls to the handleConnectDb method.case 'connect_db': return await this.handleConnectDb(request.params.arguments as unknown as ConnectionArgs);
- src/index.ts:70-77 (schema)TypeScript interface defining the input parameters for the connect_db tool.interface ConnectionArgs { url?: string; workspace?: string; host?: string; user?: string; password?: string; database?: string; }
- src/index.ts:273-301 (helper)Key helper function called by the handler to load and parse database connection configuration from URL, workspace .env files, or direct parameters.private async loadConfig(args: ConnectionArgs): Promise<ConnectionConfig> { console.error('Loading config with args:', args); if (args.url) { console.error('Using URL configuration'); return this.parseConnectionUrl(args.url); } if (args.workspace) { console.error('Attempting workspace configuration from:', args.workspace); const config = await this.loadWorkspaceConfig(args.workspace); if (config) { console.error('Successfully loaded workspace config'); return config; } console.error('Failed to load workspace config'); } if (this.hasDirectConfig(args)) { console.error('Using direct configuration parameters'); return this.createDirectConfig(args); } console.error('No valid configuration method found'); throw new McpError( ErrorCode.InvalidParams, 'No valid configuration provided. Please provide either a URL, workspace path, or connection parameters.' ); }