Skip to main content
Glama
Malove86

MCP MySQL Server

by Malove86

connect_db

Establish a connection to a MySQL database using host, user, password, and database details. Allows integration with MCP MySQL Server for query execution, table listing, and automated connection management.

Instructions

Connect to MySQL database (optional if environment variables are set)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
databaseYesDatabase name
hostYesDatabase host
passwordYesDatabase password
portNoDatabase port (optional)
userYesDatabase user

Implementation Reference

  • The primary handler function that executes the 'connect_db' tool logic: validates required DB credentials, closes existing pool if any, sets new config, calls ensureConnection to establish MySQL connection pool, returns success response.
    private async handleConnectDb(requestId: string, args: any) {
      // 验证参数
      if (!args.host || !args.user || args.password === undefined || args.password === null || !args.database) {
        throw new McpError(
          ErrorCode.InvalidParams,
          'Missing required database configuration parameters'
        );
      }
    
      // 关闭现有连接池
      if (this.pool) {
        try {
          console.error(`[${requestId}] Closing existing connection pool`);
          await this.pool.end();
        } catch (error) {
          console.error(`[${requestId}] Error closing pool: ${getErrorMessage(error)}`);
        }
        this.pool = null;
      }
    
      this.config = {
        host: args.host,
        user: args.user,
        password: args.password,
        database: args.database,
        port: args.port || 3306, // 确保有默认端口
      };
    
      try {
        console.error(`[${requestId}] Connecting to database: ${this.config.host}:${this.config.port}/${this.config.database}`);
        await this.ensureConnection();
        return {
          content: [
            {
              type: 'text',
              text: 'Successfully connected to database',
            },
          ],
        };
      } catch (error) {
        throw new McpError(
          ErrorCode.InternalError,
          `Failed to connect to database: ${getErrorMessage(error)}`
        );
      }
  • Input schema definition for the 'connect_db' tool, specifying required properties (host, user, password, database) and optional port.
    inputSchema: {
      type: 'object',
      properties: {
        host: {
          type: 'string',
          description: 'Database host',
        },
        user: {
          type: 'string',
          description: 'Database user',
        },
        password: {
          type: 'string',
          description: 'Database password',
        },
        database: {
          type: 'string',
          description: 'Database name',
        },
        port: {
          type: 'number',
          description: 'Database port (optional)',
        },
      },
      required: ['host', 'user', 'password', 'database'],
  • src/index.ts:185-214 (registration)
    Registration of the 'connect_db' tool in the ListTools response, including name, description, and input schema.
    {
      name: 'connect_db',
      description: 'Connect to MySQL database (optional if environment variables are set)',
      inputSchema: {
        type: 'object',
        properties: {
          host: {
            type: 'string',
            description: 'Database host',
          },
          user: {
            type: 'string',
            description: 'Database user',
          },
          password: {
            type: 'string',
            description: 'Database password',
          },
          database: {
            type: 'string',
            description: 'Database name',
          },
          port: {
            type: 'number',
            description: 'Database port (optional)',
          },
        },
        required: ['host', 'user', 'password', 'database'],
      },
    },
Behavior2/5

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

No annotations are provided, so the description carries the full burden of behavioral disclosure. It mentions the optionality based on environment variables, which adds some context, but fails to describe critical behaviors such as what happens on successful/failed connections, whether the connection persists, authentication requirements beyond parameters, or any rate limits. For a tool that establishes a database connection with security implications, this is inadequate.

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?

The description is a single, efficient sentence that front-loads the core action ('Connect to MySQL database') and adds a useful note about environment variables. There is zero waste or redundancy, making it highly concise and well-structured for quick comprehension.

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?

Given the complexity of a database connection tool with no annotations and no output schema, the description is incomplete. It doesn't explain what the tool returns (e.g., a connection handle, success status), error handling, or behavioral nuances like timeouts or security constraints. This leaves significant gaps for an AI agent to understand how to use the 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 100%, meaning the input schema already documents all parameters clearly (e.g., 'Database name', 'Database host'). The description adds no additional meaning about parameters beyond implying that some might be optional via environment variables, but it doesn't specify which ones or how they interact. This meets the baseline of 3 since the schema does the heavy lifting.

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 verb ('Connect to') and resource ('MySQL database'), making the purpose specific and understandable. It distinguishes from siblings like 'describe_table' or 'query' by focusing on establishing a connection rather than operating on an already connected database. However, it doesn't explicitly differentiate from 'list_tables', which might also require a connection, so it's not a perfect 5.

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

Usage Guidelines3/5

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

The description provides implied usage guidance by noting that connection is 'optional if environment variables are set', suggesting when it might not be needed. However, it lacks explicit instructions on when to use this tool versus alternatives (e.g., whether to rely on env vars or manual input) or any prerequisites for successful connection, leaving gaps in decision-making context.

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

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

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