Skip to main content
Glama
pickstar-2002

MySQL MCP Server

mysql_import_data

Import data from CSV or JSON files into MySQL tables. Specify table name, file path, format, and optional truncation to load external data into your database.

Instructions

从文件导入数据到表

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
tableNameYes表名称
filePathYes导入文件路径
formatYes文件格式
truncateFirstNo是否先清空表

Implementation Reference

  • The main handler function that executes the mysql_import_data tool: optionally truncates the table, parses the input file using importData helper, and inserts the data row-by-row into the MySQL table using parameterized queries.
    private async handleImportData(args: { tableName: string; filePath: string; format: 'csv' | 'json'; truncateFirst?: boolean }): Promise<any> { if (args.truncateFirst) { await this.dbManager.query(`TRUNCATE TABLE \`${args.tableName}\``); } const data = await importData({ format: args.format, filePath: args.filePath, tableName: args.tableName, }); if (data.length === 0) { return { content: [ { type: 'text', text: '导入文件为空,没有数据需要导入', }, ], }; } const columns = Object.keys(data[0]); const placeholders = columns.map(() => '?').join(', '); let importedRows = 0; for (const row of data) { const values = columns.map(col => row[col]); const sql = `INSERT INTO \`${args.tableName}\` (\`${columns.join('`, `')}\`) VALUES (${placeholders})`; const result = await this.dbManager.query(sql, values); importedRows += result.affectedRows || 0; } return { content: [ { type: 'text', text: `成功导入 ${importedRows} 行数据到表 ${args.tableName}`, }, ], }; }
  • Input schema definition for the mysql_import_data tool, specifying required parameters and types for validation.
    inputSchema: { type: 'object', properties: { tableName: { type: 'string', description: '表名称' }, filePath: { type: 'string', description: '导入文件路径' }, format: { type: 'string', enum: ['csv', 'json'], description: '文件格式' }, truncateFirst: { type: 'boolean', description: '是否先清空表', default: false }, }, required: ['tableName', 'filePath', 'format'], },
  • src/server.ts:210-223 (registration)
    Tool registration in the listTools response, defining name, description, and input schema for mysql_import_data.
    { name: 'mysql_import_data', description: '从文件导入数据到表', inputSchema: { type: 'object', properties: { tableName: { type: 'string', description: '表名称' }, filePath: { type: 'string', description: '导入文件路径' }, format: { type: 'string', enum: ['csv', 'json'], description: '文件格式' }, truncateFirst: { type: 'boolean', description: '是否先清空表', default: false }, }, required: ['tableName', 'filePath', 'format'], }, },
  • src/server.ts:261-262 (registration)
    Switch case in CallToolRequest handler that routes mysql_import_data calls to the handleImportData method.
    case 'mysql_import_data': return await this.handleImportData(args as any);
  • Supporting utility function that reads and parses data from JSON or CSV files, used by the handler to load import data.
    export async function importData(options: ImportOptions): Promise<any[]> { const { format, filePath } = options; if (format === 'json') { const content = await fs.readFile(filePath, 'utf-8'); return JSON.parse(content); } else if (format === 'csv') { return new Promise((resolve, reject) => { const results: any[] = []; createReadStream(filePath) .pipe(csvParser()) .on('data', (data: any) => results.push(data)) .on('end', () => resolve(results)) .on('error', reject); }); } throw new Error(`不支持的格式: ${format}`); }

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/pickstar-2002/mysql-mcp'

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