Skip to main content
Glama
yaoxiaolinglong

MCP-MongoDB-MySQL-Server

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

TableJSON Schema
NameRequiredDescriptionDefault
urlNoDatabase URL (mysql://user:pass@host:port/db)
workspaceNoProject workspace path
hostNo
userNo
passwordNo
databaseNo

Implementation Reference

  • 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)}`
        );
      }
    }
  • 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);
  • 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
      };
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden but only states the basic action. It doesn't disclose whether this establishes a persistent connection, requires authentication, has timeout/rate limits, returns a connection object, or what happens on failure. For a connection tool with zero annotation coverage, this leaves critical behavioral aspects unspecified.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Extremely concise single sentence with zero wasted words. Front-loaded with the core purpose. Every word earns its place in this minimal description.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a connection tool with 6 parameters, 0% required parameters, no annotations, and no output schema, the description is inadequate. It doesn't explain what a successful connection yields, error conditions, or how parameters interact. The agent lacks sufficient context to use this tool effectively.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is low (33%), with only 'url' parameter documented. The description adds value by clarifying the two connection methods ('URL or config'), which helps interpret the parameter set, but doesn't explain the relationship between URL and individual config parameters (host, user, etc.) or which approach is preferred.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('Connect to MySQL database') and resource ('MySQL database'), specifying the connection method ('using URL or config'). It distinguishes from sibling tools like 'connect_mongodb' by specifying MySQL, but doesn't explicitly contrast with other database tools beyond the name.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No guidance on when to use this tool versus alternatives like 'connect_mongodb' or other database operations. The description implies it's for establishing a connection, but doesn't specify prerequisites, timing, or when to choose URL vs config parameters.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other 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