connect_db
Establish a connection to a MySQL database using a URL or configuration parameters. Integrate database interactions into workflows via the MCP MySQL Server's standardized interface.
Instructions
Connect to MySQL database using URL or config
Input Schema
TableJSON 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 |
Implementation Reference
- src/index.ts:542-561 (handler)The main handler function for the 'connect_db' tool. Loads database configuration from input arguments using loadConfig, establishes and tests the connection with ensureConnection, returns a success message, or throws an MCP error on failure.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:70-77 (schema)TypeScript interface defining the expected input shape for connect_db tool arguments.interface ConnectionArgs { url?: string; workspace?: string; host?: string; user?: string; password?: string; database?: string; }
- src/index.ts:350-374 (registration)Registration of the 'connect_db' tool in the ListToolsRequestSchema handler, including name, description, and JSON inputSchema.{ 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:519-520 (registration)Dispatch routing in CallToolRequestSchema handler that casts arguments to ConnectionArgs and calls the handleConnectDb function.case 'connect_db': return await this.handleConnectDb(request.params.arguments as unknown as ConnectionArgs);
- src/index.ts:273-285 (helper)Key helper function used by the handler to derive ConnectionConfig from input args: tries URL parse, workspace .env load, or direct connection params.private async loadConfig(args: ConnectionArgs): Promise<ConnectionConfig> { if (args.url) return this.parseConnectionUrl(args.url); if (args.workspace) { const config = await this.loadWorkspaceConfig(args.workspace); if (config) return config; } if (this.hasDirectConfig(args)) return this.createDirectConfig(args); throw new McpError( ErrorCode.InvalidParams, 'No valid configuration provided. Please provide either a URL, workspace path, or connection parameters.' ); }