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
| Name | Required | Description | Default |
|---|---|---|---|
| connectionString | Yes | The database connection string. e.g., postgresql://user:password@localhost:5432/dbname | |
| dialect | Yes | The available database dialect (available: psql, mysql, sqlite) |
Input Schema (JSON Schema)
{
"properties": {
"connectionString": {
"description": "The database connection string. e.g., postgresql://user:password@localhost:5432/dbname",
"type": "string"
},
"dialect": {
"description": "The available database dialect (available: psql, mysql, sqlite)",
"enum": [
"psql",
"mysql",
"sqlite"
],
"type": "string"
}
},
"required": [
"dialect",
"connectionString"
],
"type": "object"
}
Implementation Reference
- src/index.ts:24-39 (handler)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)`, }, ], }; }
- src/connection.ts:11-21 (schema)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)`, }, ], }; } );
- src/connection.ts:63-71 (helper)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(), }); }
- src/connection.ts:25-46 (helper)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}`); } };