创建mysql

This commit is contained in:
gpu 2026-01-03 23:56:07 +08:00
parent e7a66185b6
commit 35ef62a571
4 changed files with 283 additions and 19 deletions

View File

@ -1,12 +1,8 @@
{
"mcpServers": {
"mysql": {
"command": "uvx",
"args": [
"--from",
"mysql-mcp-server",
"mysql_mcp_server"
],
"command": "node",
"args": ["D:/CodeManage/HaniBlindBox/server/scripts/mysql-mcp-server/index.js"],
"env": {
"MYSQL_HOST": "192.168.195.16",
"MYSQL_PORT": "1887",
@ -14,20 +10,11 @@
"MYSQL_PASSWORD": "Dbt@com@123",
"MYSQL_DATABASE": "youdas"
},
"autoApprove": [
"list_tables",
"describe_table",
"read_query",
"write_query",
"create_table",
"alter_table",
"drop_table",
"execute_sql"
]
"autoApprove": ["execute_sql"]
},
"sqlserver": {
"command": "node",
"args": ["server/scripts/mssql-mcp-server/index.js"],
"args": ["D:/CodeManage/HaniBlindBox/server/scripts/mssql-mcp-server/index.js"],
"env": {
"MSSQL_SERVER": "192.168.195.15",
"MSSQL_PORT": "1433",
@ -39,7 +26,7 @@
},
"admin-sqlserver": {
"command": "node",
"args": ["server/scripts/mssql-mcp-server/index.js"],
"args": ["D:/CodeManage/HaniBlindBox/server/scripts/mssql-mcp-server/index.js"],
"env": {
"MSSQL_SERVER": "192.168.195.15",
"MSSQL_PORT": "1433",
@ -50,4 +37,4 @@
"autoApprove": ["execute_sql"]
}
}
}
}

View File

@ -0,0 +1,125 @@
#!/usr/bin/env node
/**
* MySQL MCP Server - Node.js 实现
* 提供 execute_sql 工具用于执行 SQL 语句
*/
const mysql = require('mysql2/promise');
const readline = require('readline');
const config = {
host: process.env.MYSQL_HOST || 'localhost',
port: parseInt(process.env.MYSQL_PORT) || 3306,
user: process.env.MYSQL_USER || 'root',
password: process.env.MYSQL_PASSWORD || '',
database: process.env.MYSQL_DATABASE || 'test'
};
let pool = null;
async function getPool() {
if (!pool) {
pool = mysql.createPool(config);
}
return pool;
}
async function executeSQL(query) {
const p = await getPool();
const [rows, fields] = await p.query(query);
return { rows, fields };
}
function sendResponse(id, result) {
const response = { jsonrpc: '2.0', id, result };
process.stdout.write(JSON.stringify(response) + '\n');
}
function sendError(id, code, message) {
const response = { jsonrpc: '2.0', id, error: { code, message } };
process.stdout.write(JSON.stringify(response) + '\n');
}
async function handleRequest(request) {
const { id, method, params } = request;
try {
switch (method) {
case 'initialize':
sendResponse(id, {
protocolVersion: '2024-11-05',
capabilities: { tools: {} },
serverInfo: { name: 'mysql-node-mcp', version: '1.0.0' }
});
break;
case 'tools/list':
sendResponse(id, {
tools: [{
name: 'execute_sql',
description: 'Execute SQL query on MySQL database',
inputSchema: {
type: 'object',
properties: {
query: { type: 'string', description: 'SQL query to execute' }
},
required: ['query']
}
}]
});
break;
case 'tools/call':
if (params.name === 'execute_sql') {
const query = params.arguments.query;
const result = await executeSQL(query);
let content;
if (Array.isArray(result.rows) && result.rows.length > 0) {
content = JSON.stringify(result.rows, null, 2);
} else if (result.rows && result.rows.affectedRows !== undefined) {
content = `Rows affected: ${result.rows.affectedRows}`;
} else {
content = 'Query executed successfully';
}
sendResponse(id, {
content: [{ type: 'text', text: content }]
});
} else {
sendError(id, -32601, `Unknown tool: ${params.name}`);
}
break;
case 'notifications/initialized':
case 'notifications/cancelled':
// 忽略通知
break;
default:
if (id !== undefined) {
sendError(id, -32601, `Method not found: ${method}`);
}
}
} catch (err) {
if (id !== undefined) {
sendError(id, -32000, err.message);
}
}
}
const rl = readline.createInterface({ input: process.stdin });
rl.on('line', async (line) => {
try {
const request = JSON.parse(line);
await handleRequest(request);
} catch (err) {
// 忽略解析错误
}
});
process.on('SIGINT', async () => {
if (pool) await pool.end();
process.exit(0);
});

View File

@ -0,0 +1,140 @@
{
"name": "mysql-mcp-server",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "mysql-mcp-server",
"version": "1.0.0",
"dependencies": {
"mysql2": "^3.9.0"
},
"bin": {
"mysql-mcp-server": "index.js"
}
},
"node_modules/aws-ssl-profiles": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/aws-ssl-profiles/-/aws-ssl-profiles-1.1.2.tgz",
"integrity": "sha512-NZKeq9AfyQvEeNlN0zSYAaWrmBffJh3IELMZfRpJVWgrpEbtEpnjvzqBPf+mxoI287JohRDoa+/nsfqqiZmF6g==",
"license": "MIT",
"engines": {
"node": ">= 6.0.0"
}
},
"node_modules/denque": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/denque/-/denque-2.1.0.tgz",
"integrity": "sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw==",
"license": "Apache-2.0",
"engines": {
"node": ">=0.10"
}
},
"node_modules/generate-function": {
"version": "2.3.1",
"resolved": "https://registry.npmjs.org/generate-function/-/generate-function-2.3.1.tgz",
"integrity": "sha512-eeB5GfMNeevm/GRYq20ShmsaGcmI81kIX2K9XQx5miC8KdHaC6Jm0qQ8ZNeGOi7wYB8OsdxKs+Y2oVuTFuVwKQ==",
"license": "MIT",
"dependencies": {
"is-property": "^1.0.2"
}
},
"node_modules/iconv-lite": {
"version": "0.7.1",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.7.1.tgz",
"integrity": "sha512-2Tth85cXwGFHfvRgZWszZSvdo+0Xsqmw8k8ZwxScfcBneNUraK+dxRxRm24nszx80Y0TVio8kKLt5sLE7ZCLlw==",
"license": "MIT",
"dependencies": {
"safer-buffer": ">= 2.1.2 < 3.0.0"
},
"engines": {
"node": ">=0.10.0"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/express"
}
},
"node_modules/is-property": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/is-property/-/is-property-1.0.2.tgz",
"integrity": "sha512-Ks/IoX00TtClbGQr4TWXemAnktAQvYB7HzcCxDGqEZU6oCmb2INHuOoKxbtR+HFkmYWBKv/dOZtGRiAjDhj92g==",
"license": "MIT"
},
"node_modules/long": {
"version": "5.3.2",
"resolved": "https://registry.npmjs.org/long/-/long-5.3.2.tgz",
"integrity": "sha512-mNAgZ1GmyNhD7AuqnTG3/VQ26o760+ZYBPKjPvugO8+nLbYfX6TVpJPseBvopbdY+qpZ/lKUnmEc1LeZYS3QAA==",
"license": "Apache-2.0"
},
"node_modules/lru.min": {
"version": "1.1.3",
"resolved": "https://registry.npmjs.org/lru.min/-/lru.min-1.1.3.tgz",
"integrity": "sha512-Lkk/vx6ak3rYkRR0Nhu4lFUT2VDnQSxBe8Hbl7f36358p6ow8Bnvr8lrLt98H8J1aGxfhbX4Fs5tYg2+FTwr5Q==",
"license": "MIT",
"engines": {
"bun": ">=1.0.0",
"deno": ">=1.30.0",
"node": ">=8.0.0"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/wellwelwel"
}
},
"node_modules/mysql2": {
"version": "3.16.0",
"resolved": "https://registry.npmjs.org/mysql2/-/mysql2-3.16.0.tgz",
"integrity": "sha512-AEGW7QLLSuSnjCS4pk3EIqOmogegmze9h8EyrndavUQnIUcfkVal/sK7QznE+a3bc6rzPbAiui9Jcb+96tPwYA==",
"license": "MIT",
"dependencies": {
"aws-ssl-profiles": "^1.1.1",
"denque": "^2.1.0",
"generate-function": "^2.3.1",
"iconv-lite": "^0.7.0",
"long": "^5.2.1",
"lru.min": "^1.0.0",
"named-placeholders": "^1.1.3",
"seq-queue": "^0.0.5",
"sqlstring": "^2.3.2"
},
"engines": {
"node": ">= 8.0"
}
},
"node_modules/named-placeholders": {
"version": "1.1.6",
"resolved": "https://registry.npmjs.org/named-placeholders/-/named-placeholders-1.1.6.tgz",
"integrity": "sha512-Tz09sEL2EEuv5fFowm419c1+a/jSMiBjI9gHxVLrVdbUkkNUUfjsVYs9pVZu5oCon/kmRh9TfLEObFtkVxmY0w==",
"license": "MIT",
"dependencies": {
"lru.min": "^1.1.0"
},
"engines": {
"node": ">=8.0.0"
}
},
"node_modules/safer-buffer": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==",
"license": "MIT"
},
"node_modules/seq-queue": {
"version": "0.0.5",
"resolved": "https://registry.npmjs.org/seq-queue/-/seq-queue-0.0.5.tgz",
"integrity": "sha512-hr3Wtp/GZIc/6DAGPDcV4/9WoZhjrkXsi5B/07QgX8tsdc6ilr7BFM6PM6rbdAX1kFSDYeZGLipIZZKyQP0O5Q=="
},
"node_modules/sqlstring": {
"version": "2.3.3",
"resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.3.tgz",
"integrity": "sha512-qC9iz2FlN7DQl3+wjwn3802RTyjCx7sDvfQEXchwa6CWOx07/WVfh91gBmQ9fahw8snwGEWU3xGzOt4tFyHLxg==",
"license": "MIT",
"engines": {
"node": ">= 0.6"
}
}
}
}

View File

@ -0,0 +1,12 @@
{
"name": "mysql-mcp-server",
"version": "1.0.0",
"description": "MySQL MCP Server for Kiro",
"main": "index.js",
"bin": {
"mysql-mcp-server": "./index.js"
},
"dependencies": {
"mysql2": "^3.9.0"
}
}