Skip to main content
Glama
IzumiSy

MCP Universal DB Client

connect_database

Connect to PostgreSQL, MySQL, or SQLite databases using connection strings to establish database connections for query execution.

Instructions

Connect to a database using connection string

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
connectionStringYesThe database connection string. e.g., postgresql://user:password@localhost:5432/dbname
dialectYesThe available database dialect (available: psql, mysql, sqlite)

Implementation Reference

  • Handler function for the "connect_database" tool. It adds the connection using DatabaseConnections.addConnection and returns a confirmation message with the connection name.
    async (input) => {
      databaseConnections.addConnection(input);
    
      return {
        content: [
          {
            type: "text",
            text: `Connected to ${input.dialect} database.`,
          },
          {
            type: "text",
            text: `Connection Name: ${input.name} (use this name for future queries)`,
          },
        ],
      };
    }
  • Zod input schema for the connect_database tool, defining name, dialect, and connectionString fields.
    export const connectDatabaseInputSchema = z.object({
      name: z
        .string()
        .describe("An unique name for this connection to be used as an ID"),
      dialect: dialectsSchema,
      connectionString: z
        .string()
        .describe(
          "The database connection string. e.g., postgresql://user:password@localhost:5432/dbname"
        ),
    });
  • src/index.ts:17-40 (registration)
    Registration of the "connect_database" tool on the MCP server, specifying title, description, input schema, and handler.
    mcpServer.registerTool(
      "connect_database",
      {
        title: "Connect Database",
        description: "Connect to a database using connection string",
        inputSchema: connectDatabaseInputSchema.shape,
      },
      async (input) => {
        databaseConnections.addConnection(input);
    
        return {
          content: [
            {
              type: "text",
              text: `Connected to ${input.dialect} database.`,
            },
            {
              type: "text",
              text: `Connection Name: ${input.name} (use this name for future queries)`,
            },
          ],
        };
      }
    );
  • addConnection method in DatabaseConnections class, called by the handler to create and store the Kysely database instance.
    public addConnection(input: ConnectDatabaseInput) {
      this.connections.set(input.name, {
        instance: new Kysely({
          dialect: createDialect(input),
        }),
        dialect: input.dialect,
        connectedAt: new Date(),
      });
    }
  • createDialect helper function that initializes the appropriate Kysely dialect (Postgres, MySQL, SQLite) based on input.
    export const createDialect = (input: ConnectDatabaseInput) => {
      switch (input.dialect) {
        case "psql":
          return new PostgresDialect({
            pool: new Pool({
              connectionString: input.connectionString,
            }),
          });
        case "mysql":
          return new MysqlDialect({
            pool: createPool({
              uri: input.connectionString,
            }),
          });
        case "sqlite":
          return new SqliteDialect({
            database: new Database(input.connectionString),
          });
        default:
          throw new Error(`Unsupported dialect: ${input.dialect}`);
      }
    };

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/IzumiSy/mcp-universal-db-client'

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