这是一个使用FastAPI、pyodbc和SQLAlchemy构建的轻量级MCP(模型上下文协议)服务器,适用于ODBC。该服务器兼容Virtuoso数据库管理系统以及其他实现SQLAlchemy提供者的数据库后端。
安装uv:
pip install uv
或者使用Homebrew:
brew install uv
unixODBC运行时环境检查:
运行以下命令检查安装配置(例如,关键INI文件的位置):odbcinst -j
运行以下命令列出可用的数据源名称:odbcinst -q -s
ODBC DSN设置:配置您的ODBC数据源名称(~/.odbc.ini),针对目标数据库。例如,对于Virtuoso DBMS:
[VOS]
Description = OpenLink Virtuoso
Driver = /path/to/virtodbcu_r.so
Database = Demo
Address = localhost:1111
WideAsUTF16 = Yes
SQLAlchemy URL绑定:使用以下格式:
virtuoso+pyodbc://user:password@VOS
克隆此仓库:
git clone https://github.com/OpenLinkSoftware/mcp-sqlalchemy-server.git
cd mcp-sqlalchemy-server
更新您的.env以覆盖默认值,使其符合您的偏好
ODBC_DSN=VOS
ODBC_USER=dba
ODBC_PASSWORD=dba
API_KEY=xxx
对于Claude Desktop用户:
在claude_desktop_config.json中添加以下内容:
{
"mcpServers": {
"my_database": {
"command": "uv",
"args": ["--directory", "/path/to/mcp-sqlalchemy-server", "run", "mcp-sqlalchemy-server"],
"env": {
"ODBC_DSN": "dsn_name",
"ODBC_USER": "username",
"ODBC_PASSWORD": "password",
"API_KEY": "sk-xxx"
}
}
}
}
以下是已测试使用的mcp-server连接到DBMS系统的pyodbc URL示例。
| 数据库 | URL 格式 |
|---|---|
| Virtuoso DBMS | virtuoso+pyodbc://user:password@ODBC_DSN |
| PostgreSQL | postgresql://user:password@localhost/dbname |
| MySQL | mysql+pymysql://user:password@localhost/dbname |
| SQLite | sqlite:///path/to/database.db |
| 一旦连接,您可以通过Claude与WhatsApp联系人互动,利用Claude的AI能力在您的WhatsApp对话中。 |
| 名称 | 描述 |
|---|---|
| podbc_get_schemas | 列出连接的数据库管理系统(DBMS)可访问的所有模式。 |
| podbc_get_tables | 列出与选定数据库模式关联的表。 |
| podbc_describe_table | 提供与指定数据库模式关联的表的描述。这包括列名、数据类型、空值处理、自动递增、主键和外键的信息。 |
| podbc_filter_table_names | 基于q输入字段中的子串模式,列出与选定数据库模式关联的表。 |
| podbc_query_database | 执行SQL查询,并以JSONL格式返回结果。 |
| podbc_execute_query | 执行SQL查询,并以JSONL格式返回结果。 |
| podbc_execute_query_md | 执行SQL查询,并以Markdown表格格式返回结果。 |
| podbc_spasql_query | 执行SPASQL查询并返回结果。 |
| podbc_sparql_query | 执行SPARQL查询并返回结果。 |
| podbc_virtuoso_support_ai | 与Virtuoso支持助手/代理交互——一个特定于Virtuoso的功能,用于与LLMs交互 |
podbc_get_schemas
user (字符串,可选):数据库用户名。默认为"demo"。password (字符串,可选):数据库密码。默认为"demo"。dsn (字符串,可选):ODBC数据源名称。默认为"Local Virtuoso"。podbc_get_tables
schema (字符串,可选):用于筛选表的数据库模式。默认为连接默认。user (字符串,可选):数据库用户名。默认为"demo"。password (字符串,可选):数据库密码。默认为"demo"。dsn (字符串,可选):ODBC数据源名称。默认为"Local Virtuoso"。podbc_filter_table_names
q (字符串,必需):要在表名中搜索的子串。schema (字符串,可选):用于筛选表的数据库模式。默认为连接默认。user (字符串,可选):数据库用户名。默认为"demo"。password (字符串,可选):数据库密码。默认为"demo"。dsn (字符串,可选):ODBC数据源名称。默认为"Local Virtuoso"。podbc_describe_table
schema (字符串,必需):包含表的数据库模式名称。table (字符串,必需):要描述的表名。user (字符串,可选):数据库用户名。默认为"demo"。password (字符串,可选):数据库密码。默认为"demo"。dsn (字符串,可选):ODBC数据源名称。默认为"Local Virtuoso"。podbc_query_database
query (字符串,必需):要执行的SQL查询字符串。user (字符串,可选):数据库用户名。默认为"demo"。password (字符串,可选):数据库密码。默认为"demo"。dsn (字符串,可选):ODBC数据源名称。默认为"Local Virtuoso"。podbc_query_database_md
query (字符串,必需):要执行的SQL查询字符串。user (字符串,可选):数据库用户名。默认为"demo"。password (字符串,可选):数据库密码。默认为"demo"。dsn (字符串,可选):ODBC数据源名称。默认为"Local Virtuoso"。podbc_query_database_jsonl
query (字符串,必需):要执行的SQL查询字符串。user (字符串,可选):数据库用户名。默认为"demo"。password (字符串,可选):数据库密码。默认为"demo"。dsn (字符串,可选):ODBC数据源名称。默认为"Local Virtuoso"。podbc_spasql_query
query (字符串,必需):SPASQL查询字符串。max_rows (数字,可选):要返回的最大行数。默认为20。timeout (数字,可选):查询超时时间(毫秒)。默认为30000。user (字符串,可选):数据库用户名。默认为"demo"。password (字符串,可选):数据库密码。默认为"demo"。dsn (字符串,可选):ODBC数据源名称。默认为"Local Virtuoso"。Demo.demo.execute_spasql_query)。podbc_sparql_query
query (字符串,必需):SPARQL查询字符串。format (字符串,可选):期望的结果格式。默认为'json'。timeout (数字,可选):查询超时时间(毫秒)。默认为30000。user (字符串,可选):数据库用户名。默认为"demo"。password (字符串,可选):数据库密码。默认为"demo"。dsn (字符串,可选):ODBC数据源名称。默认为"Local Virtuoso"。"UB".dba."sparqlQuery")。podbc_virtuoso_support_ai
prompt (字符串,必需):AI功能的提示文本。api_key (字符串,可选):AI服务的API密钥。默认为"none"。user (字符串,可选):数据库用户名。默认为"demo"。password (字符串,可选):数据库密码。默认为"demo"。dsn (字符串,可选):ODBC数据源名称。默认为"Local Virtuoso"。DEMO.DBA.OAI_VIRTUOSO_SUPPORT_AI)。为了更方便地进行故障排除:
安装MCP Inspector:
npm install -g @modelcontextprotocol/inspector
启动Inspector:
npx @modelcontextprotocol/inspector uv --directory /path/to/mcp-sqlalchemy-server run mcp-sqlalchemy-server
通过提供的URL访问以进行服务器交互的故障排除。