NLSQL MCP Server是一个基于Node.js的生产级软件包,借助人工智能多智能体系统,它能将自然语言问题转换为SQL查询,为数据库操作带来了极大的便利。
# 全局安装
npm install -g nlsql-mcp-server
# 启动服务器
nlsql-mcp-server start
# 或者使用npx直接运行
npx nlsql-mcp-server start
npm install -g nlsql-mcp-server
npm install nlsql-mcp-server
该软件包将自动完成以下操作:
# 设置你的OpenAI API密钥
export OPENAI_API_KEY="your_api_key_here"
# 或者创建一个.env文件
echo "OPENAI_API_KEY=your_api_key_here" > .env
npm install -g nlsql-mcp-server
sk -开头)。Windows系统:
Windows + R。%APPDATA%\Claude。claude_desktop_config.json。Mac系统:
Cmd + Shift + G。~/Library/Application Support/Claude。claude_desktop_config.json。Linux系统:
~/.config/Claude。claude_desktop_config.json。如果文件已存在:打开文件,并将nlsql配置添加到现有的mcpServers部分。
如果文件不存在:创建一个名为 claude_desktop_config.json 的新文件,内容如下:
{
"mcpServers": {
"nlsql": {
"command": "npx",
"args": ["nlsql-mcp-server", "start"],
"env": {
"OPENAI_API_KEY": "sk-your-actual-api-key-here"
}
}
}
}
重要提示:请将 sk-your-actual-api-key-here 替换为你真实的OpenAI API密钥!
在Claude Desktop中,尝试询问:
"Connect to the sample database and show me what tables are available"
如果一切正常,你将看到Claude连接到NBA示例数据库!
# 启动MCP服务器
nlsql-mcp-server start
# 以调试模式启动
nlsql-mcp-server start --debug
# 测试安装
nlsql-mcp-server test
# 安装/重新安装Python依赖
nlsql-mcp-server install-deps
# 生成Claude Desktop配置
nlsql-mcp-server config
# 显示帮助信息
nlsql-mcp-server --help
const NLSQLMCPServer = require('nlsql-mcp-server');
const server = new NLSQLMCPServer({
debug: true,
pythonExecutable: 'python3',
env: {
OPENAI_API_KEY: 'your_key_here'
}
});
await server.start();
服务器运行时提供以下MCP工具:
| 工具 | 描述 |
|---|---|
connect_database |
连接到SQLite、PostgreSQL或MySQL数据库 |
connect_sample_database |
连接到内置的NBA示例数据库 |
natural_language_to_sql |
使用人工智能将问题转换为SQL |
execute_sql_query |
安全地执行SQL查询 |
analyze_schema |
通过人工智能进行数据库模式分析 |
get_database_info |
获取表和列的信息 |
validate_sql_query |
验证SQL语法 |
get_table_sample |
获取表中的示例数据 |
get_connection_status |
检查数据库连接状态 |
disconnect_database |
断开与数据库的连接 |
完成Claude Desktop集成设置后,你可以使用自然语言与数据库进行交互:
Connect to my sample database and show me the schema
Convert this to SQL: "How many teams are in the NBA?"
Show me sample data from the team table
Analyze my database structure and suggest useful queries
使用内置的NBA数据库进行测试(包含30支球队、15张包含球员、比赛、统计数据的表):
Use the connect_sample_database tool
然后可以提出以下问题:
# 测试Node.js包装器
npm test
# 测试底层Python服务器
nlsql-mcp-server test
# 使用示例数据库进行测试
nlsql-mcp-server start --debug
# 然后在Claude Desktop中使用
# 安装Python 3.8+
# Ubuntu/Debian系统:
sudo apt update && sudo apt install python3 python3-pip
# macOS系统:
brew install python3
# Windows系统:
# 从python.org下载
# 手动安装
nlsql-mcp-server install-deps
# 或者手动安装
pip3 install mcp crewai sqlalchemy pandas openai python-dotenv psycopg2-binary pymysql cryptography
# 设置环境变量
export OPENAI_API_KEY="your_key_here"
# 或者使用.env文件
echo "OPENAI_API_KEY=your_key_here" > .env
# 以调试模式启动以查看详细日志
nlsql-mcp-server start --debug
# 测试安装
nlsql-mcp-server test
以调试模式运行以获取详细日志:
nlsql-mcp-server start --debug
日志文件保存路径如下:
~/.config/nlsql-mcp-server/logs/%APPDATA%\nlsql-mcp-server\logs\将以下内容添加到你的Continue.dev配置中:
{
"mcpServers": {
"nlsql": {
"command": "npx",
"args": ["nlsql-mcp-server", "start"]
}
}
}
const { spawn } = require('child_process');
const mcpServer = spawn('npx', ['nlsql-mcp-server', 'start'], {
stdio: ['pipe', 'pipe', 'pipe'],
env: {
...process.env,
OPENAI_API_KEY: 'your_key_here'
}
});
// 处理MCP协议通信
mcpServer.stdout.on('data', handleMCPMessage);
mcpServer.stdin.write(JSON.stringify(mcpRequest));
npm test。本项目采用MIT许可证,详情请参阅 LICENSE 文件。