mirror of
https://github.com/executeautomation/mcp-database-server.git
synced 2025-12-09 21:12:57 +08:00
Add MySQL support to MCP Database Server
Updated package.json and package-lock.json to include MySQL dependencies. Enhanced README with MySQL usage instructions and configuration details. Modified index.ts to handle MySQL connection parameters and logging. Added MysqlAdapter for database interactions.
This commit is contained in:
@@ -54,6 +54,7 @@ export interface DbAdapter {
|
||||
import { SqliteAdapter } from './sqlite-adapter.js';
|
||||
import { SqlServerAdapter } from './sqlserver-adapter.js';
|
||||
import { PostgresqlAdapter } from './postgresql-adapter.js';
|
||||
import { MysqlAdapter } from './mysql-adapter.js';
|
||||
|
||||
/**
|
||||
* Factory function to create the appropriate database adapter
|
||||
@@ -72,6 +73,8 @@ export function createDbAdapter(type: string, connectionInfo: any): DbAdapter {
|
||||
case 'postgresql':
|
||||
case 'postgres':
|
||||
return new PostgresqlAdapter(connectionInfo);
|
||||
case 'mysql':
|
||||
return new MysqlAdapter(connectionInfo);
|
||||
default:
|
||||
throw new Error(`Unsupported database type: ${type}`);
|
||||
}
|
||||
|
||||
135
src/db/mysql-adapter.ts
Normal file
135
src/db/mysql-adapter.ts
Normal file
@@ -0,0 +1,135 @@
|
||||
import { DbAdapter } from "./adapter.js";
|
||||
import mysql from "mysql2/promise";
|
||||
|
||||
/**
|
||||
* MySQL database adapter implementation
|
||||
*/
|
||||
export class MysqlAdapter implements DbAdapter {
|
||||
private connection: mysql.Connection | null = null;
|
||||
private config: mysql.ConnectionOptions;
|
||||
private host: string;
|
||||
private database: string;
|
||||
|
||||
constructor(connectionInfo: {
|
||||
host: string;
|
||||
database: string;
|
||||
user?: string;
|
||||
password?: string;
|
||||
port?: number;
|
||||
ssl?: boolean | object;
|
||||
connectionTimeout?: number;
|
||||
}) {
|
||||
this.host = connectionInfo.host;
|
||||
this.database = connectionInfo.database;
|
||||
this.config = {
|
||||
host: connectionInfo.host,
|
||||
database: connectionInfo.database,
|
||||
port: connectionInfo.port || 3306,
|
||||
user: connectionInfo.user,
|
||||
password: connectionInfo.password,
|
||||
connectTimeout: connectionInfo.connectionTimeout || 30000,
|
||||
multipleStatements: true,
|
||||
};
|
||||
if (typeof connectionInfo.ssl === 'object' || typeof connectionInfo.ssl === 'string') {
|
||||
this.config.ssl = connectionInfo.ssl;
|
||||
} else if (connectionInfo.ssl === true) {
|
||||
this.config.ssl = {};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize MySQL connection
|
||||
*/
|
||||
async init(): Promise<void> {
|
||||
try {
|
||||
console.error(`[INFO] Connecting to MySQL: ${this.host}, Database: ${this.database}`);
|
||||
this.connection = await mysql.createConnection(this.config);
|
||||
console.error(`[INFO] MySQL connection established successfully`);
|
||||
} catch (err) {
|
||||
console.error(`[ERROR] MySQL connection error: ${(err as Error).message}`);
|
||||
throw new Error(`Failed to connect to MySQL: ${(err as Error).message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a SQL query and get all results
|
||||
*/
|
||||
async all(query: string, params: any[] = []): Promise<any[]> {
|
||||
if (!this.connection) {
|
||||
throw new Error("Database not initialized");
|
||||
}
|
||||
try {
|
||||
const [rows] = await this.connection.execute(query, params);
|
||||
return Array.isArray(rows) ? rows : [];
|
||||
} catch (err) {
|
||||
throw new Error(`MySQL query error: ${(err as Error).message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a SQL query that modifies data
|
||||
*/
|
||||
async run(query: string, params: any[] = []): Promise<{ changes: number, lastID: number }> {
|
||||
if (!this.connection) {
|
||||
throw new Error("Database not initialized");
|
||||
}
|
||||
try {
|
||||
const [result]: any = await this.connection.execute(query, params);
|
||||
const changes = result.affectedRows || 0;
|
||||
const lastID = result.insertId || 0;
|
||||
return { changes, lastID };
|
||||
} catch (err) {
|
||||
throw new Error(`MySQL query error: ${(err as Error).message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute multiple SQL statements
|
||||
*/
|
||||
async exec(query: string): Promise<void> {
|
||||
if (!this.connection) {
|
||||
throw new Error("Database not initialized");
|
||||
}
|
||||
try {
|
||||
await this.connection.query(query);
|
||||
} catch (err) {
|
||||
throw new Error(`MySQL batch error: ${(err as Error).message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the database connection
|
||||
*/
|
||||
async close(): Promise<void> {
|
||||
if (this.connection) {
|
||||
await this.connection.end();
|
||||
this.connection = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get database metadata
|
||||
*/
|
||||
getMetadata(): { name: string; type: string; server: string; database: string } {
|
||||
return {
|
||||
name: "MySQL",
|
||||
type: "mysql",
|
||||
server: this.host,
|
||||
database: this.database,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get database-specific query for listing tables
|
||||
*/
|
||||
getListTablesQuery(): string {
|
||||
return "SHOW TABLES";
|
||||
}
|
||||
|
||||
/**
|
||||
* Get database-specific query for describing a table
|
||||
*/
|
||||
getDescribeTableQuery(tableName: string): string {
|
||||
return `DESCRIBE \`${tableName}\``;
|
||||
}
|
||||
}
|
||||
42
src/index.ts
42
src/index.ts
@@ -45,6 +45,7 @@ if (args.length === 0) {
|
||||
logger.error("Usage for SQLite: node index.js <database_file_path>");
|
||||
logger.error("Usage for SQL Server: node index.js --sqlserver --server <server> --database <database> [--user <user> --password <password>]");
|
||||
logger.error("Usage for PostgreSQL: node index.js --postgresql --host <host> --database <database> [--user <user> --password <password> --port <port>]");
|
||||
logger.error("Usage for MySQL: node index.js --mysql --host <host> --database <database> [--user <user> --password <password> --port <port>]");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
@@ -120,6 +121,45 @@ else if (args.includes('--postgresql') || args.includes('--postgres')) {
|
||||
logger.error("Error: PostgreSQL requires --host and --database parameters");
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
// Check if using MySQL
|
||||
else if (args.includes('--mysql')) {
|
||||
dbType = 'mysql';
|
||||
connectionInfo = {
|
||||
host: '',
|
||||
database: '',
|
||||
user: undefined,
|
||||
password: undefined,
|
||||
port: undefined,
|
||||
ssl: undefined,
|
||||
connectionTimeout: undefined
|
||||
};
|
||||
// Parse MySQL connection parameters
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
if (args[i] === '--host' && i + 1 < args.length) {
|
||||
connectionInfo.host = args[i + 1];
|
||||
} else if (args[i] === '--database' && i + 1 < args.length) {
|
||||
connectionInfo.database = args[i + 1];
|
||||
} else if (args[i] === '--user' && i + 1 < args.length) {
|
||||
connectionInfo.user = args[i + 1];
|
||||
} else if (args[i] === '--password' && i + 1 < args.length) {
|
||||
connectionInfo.password = args[i + 1];
|
||||
} else if (args[i] === '--port' && i + 1 < args.length) {
|
||||
connectionInfo.port = parseInt(args[i + 1], 10);
|
||||
} else if (args[i] === '--ssl' && i + 1 < args.length) {
|
||||
const sslVal = args[i + 1];
|
||||
if (sslVal === 'true') connectionInfo.ssl = true;
|
||||
else if (sslVal === 'false') connectionInfo.ssl = false;
|
||||
else connectionInfo.ssl = sslVal;
|
||||
} else if (args[i] === '--connection-timeout' && i + 1 < args.length) {
|
||||
connectionInfo.connectionTimeout = parseInt(args[i + 1], 10);
|
||||
}
|
||||
}
|
||||
// Validate MySQL connection info
|
||||
if (!connectionInfo.host || !connectionInfo.database) {
|
||||
logger.error("Error: MySQL requires --host and --database parameters");
|
||||
process.exit(1);
|
||||
}
|
||||
} else {
|
||||
// SQLite mode (default)
|
||||
dbType = 'sqlite';
|
||||
@@ -178,6 +218,8 @@ async function runServer() {
|
||||
logger.info(`Server: ${connectionInfo.server}, Database: ${connectionInfo.database}`);
|
||||
} else if (dbType === 'postgresql') {
|
||||
logger.info(`Host: ${connectionInfo.host}, Database: ${connectionInfo.database}`);
|
||||
} else if (dbType === 'mysql') {
|
||||
logger.info(`Host: ${connectionInfo.host}, Database: ${connectionInfo.database}`);
|
||||
}
|
||||
|
||||
// Initialize the database
|
||||
|
||||
Reference in New Issue
Block a user