update_database
Update database tables from CSV or Excel files by specifying file path, database type, connection string, and target table name.
Instructions
Update the database from a CSV or Excel file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filePath | Yes | Path to the CSV or Excel file | |
| databaseType | Yes | Type of database (e.g., PostgreSQL, MySQL, MongoDB, SQLite) | |
| connectionString | Yes | Connection string for the database | |
| tableName | Yes | Name of the table to update |
Implementation Reference
- src/index.ts:178-219 (handler)Handler for executing the 'update_database' tool: validates arguments, parses CSV/Excel file, parses connection string, logs update (placeholder for actual DB update), returns success message.case "update_database": { const filePath = String(request.params.arguments?.filePath); const databaseType = String(request.params.arguments?.databaseType); const connectionString = String(request.params.arguments?.connectionString); const tableName = String(request.params.arguments?.tableName); if (!filePath || !databaseType || !connectionString || !tableName) { throw new McpError(ErrorCode.InvalidParams, "File path, database type, connection string, and table name are required"); } try { const fileExtension = filePath.split('.').pop()?.toLowerCase(); let results: any[] = []; if (fileExtension === 'csv') { results = await parseCsvFile(filePath); } else if (fileExtension === 'xlsx' || fileExtension === 'xls') { results = await parseExcelFile(filePath); } else { throw new McpError(ErrorCode.InvalidParams, "Unsupported file type. Only CSV and Excel files are supported."); } // Placeholder for database interaction logic const connectionDetails = parseConnectionString(connectionString); console.log(`Updating database of type ${databaseType} with connection details ${JSON.stringify(connectionDetails)} and table name ${tableName} with data:`, results); // Add database update logic here based on databaseType and connectionDetails // For example, you might use a library like 'pg' for PostgreSQL, 'mysql' for MySQL, or 'mongodb' for MongoDB // This is a placeholder, so for now, we'll just log the data return { content: [{ type: "text", text: `Successfully updated database from ${filePath}` }] }; } catch (error: any) { console.error("Error updating database:", error); throw new McpError(ErrorCode.InternalError, `Error updating database: ${error.message}`); } }
- src/index.ts:125-150 (registration)Registration of the 'update_database' tool in ListToolsRequestSchema handler, including name, description, and input schema.{ name: "update_database", description: "Update the database from a CSV or Excel file", inputSchema: { type: "object", properties: { filePath: { type: "string", description: "Path to the CSV or Excel file" }, databaseType: { type: "string", description: "Type of database (e.g., PostgreSQL, MySQL, MongoDB, SQLite)" }, connectionString: { type: "string", description: "Connection string for the database" }, tableName: { type: "string", description: "Name of the table to update" } }, required: ["filePath", "databaseType", "connectionString", "tableName"] } }
- src/index.ts:226-235 (helper)Helper function to parse CSV files into array of objects, used by update_database handler.async function parseCsvFile(filePath: string): Promise<any[]> { return new Promise((resolve, reject) => { const results: any[] = []; fs.createReadStream(filePath) .pipe(csvParser()) .on('data', (data: any) => results.push(data)) .on('end', () => resolve(results)) .on('error', (error: any) => reject(error)); }); }
- src/index.ts:237-242 (helper)Helper function to parse Excel files into array of objects, used by update_database handler.async function parseExcelFile(filePath: string): Promise<any[]> { const workbook = XLSX.readFile(filePath); const sheetName = workbook.SheetNames[0]; const worksheet = workbook.Sheets[sheetName]; return XLSX.utils.sheet_to_json(worksheet); }
- src/index.ts:244-276 (helper)Helper function to parse database connection string into details object, used by update_database handler.function parseConnectionString(connectionString: string): any { // This is a basic example, you might need a more robust parser try { const url = new URL(connectionString); if (url.protocol === 'mongodb:') { return { type: 'mongodb', url: connectionString }; } else { const parts = connectionString.split(';'); const connectionDetails: any = {}; parts.forEach(part => { const [key, value] = part.split('='); if (key && value) { connectionDetails[key.trim()] = value.trim(); } }); return connectionDetails; } } catch (e) { // If it's not a URL, assume it's a semicolon-separated string const parts = connectionString.split(';'); const connectionDetails: any = {}; parts.forEach(part => { const [key, value] = part.split('='); if (key && value) { connectionDetails[key.trim()] = value.trim(); } }); return connectionDetails; } }