Skip to main content
Glama

MCP-MongoDB-MySQL-Server

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

NameRequiredDescriptionDefault
databaseNo
hostNo
passwordNo
urlNoDatabase URL (mysql://user:pass@host:port/db)
userNo
workspaceNoProject 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

  • 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);
  • 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 },
  • 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 }; }

Other Tools

Related Tools

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

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