Token导航 LogoToken导航TokenDH.com
Database MCP Server logo
数据服务stdio官方级别未说明来源级核验

Database MCP Server

MCP Server

一个提供连接和操作多种数据库系统的工具的服务,支持SQLite、PostgreSQL、MySQL/MariaDB和SQL Server等数据库。

工具数

19

提示词数

0

GitHub Stars

3

资源数

0
PythonClaude多数据库支持Claude

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

作者 / 组织

georgi-terziyski

提供方

georgi-terziyski

最后核验

2026/5/17 20:21

运行时

Python

快速接入

先看主来源和安装命令,再打开仓库或文档;下面只保留这个条目的关键接入事实。

命令预览

pip install -e .

详细介绍

数据库MCP服务器

一种模型上下文协议(MCP)服务器,提供连接到各种数据库系统并与之交互的工具。

特性

  • 多数据库支持:连接到SQLite、PostgreSQL、MySQL/MariaDB和SQL Server数据库
  • 统一接口:用于跨所有支持的数据库类型进行数据库操作的通用工具
  • 数据库特定扩展:必要时,针对数据库特定功能的特定工具
  • 模式管理:创建、更改和删除表和索引
  • 查询执行:执行原始SQL查询或使用结构化查询工具
  • 事务支持:开始、提交和回滚事务

安装

先决条件

  • Python 3.8或更高版本
  • 所需的Python包(使用pip自动安装):

- SQL炼金术 - 各种数据库驱动程序,具体取决于您要使用的数据库: - SQLite(包含在Python中) - PostgreSQL: psycopg2-binary - MySQL/MariaDB: mysql-connector-python - SQL服务器: pyodbc

从源安装

# Clone the repository
git clone 

# Install the package
pip install -e .

配置

可以使用环境变量、配置文件或在运行时提供连接详细信息来配置服务器。

环境变量

  • DB_CONFIG_PATH:JSON配置文件的路径
  • DB_CONNECTIONS:逗号分隔的连接ID列表或包含连接详细信息的JSON字符串

配置文件格式

{
  "connections": {
    "sqlite_conn": {
      "type": "sqlite",
      "db_path": "/path/to/database.db"
    },
    "postgres_conn": {
      "type": "postgres",
      "host": "localhost",
      "port": 5432,
      "database": "mydatabase",
      "user": "myuser",
      "password": "mypassword"
    }
  }
}

用法

运行服务器

作为Claude的MCP服务器

# Run with default settings
python -m db_mcp_server

# Specify a configuration file
python -m db_mcp_server --config /path/to/config.json

# Set logging level
python -m db_mcp_server --log-level DEBUG

作为独立Web服务器(适用于任何LLM)

# Run as a web server
python -m db_mcp_server.web_server

# Specify host and port
python -m db_mcp_server.web_server --host 0.0.0.0 --port 8000

# Specify configuration file and logging level
python -m db_mcp_server.web_server --config /path/to/config.json --log-level DEBUG

可用的MCP工具

连接管理

  • add_connection:添加新的数据库连接
  • test_connection:测试数据库连接
  • list_connections:列出所有数据库连接
  • remove_connection:删除数据库连接

查询执行

  • execute_query:执行SQL查询
  • get_records:从表中获取记录
  • insert_record:将记录插入表中
  • update_record:更新表中的记录
  • delete_record:从表中删除记录

模式管理

  • list_tables:列出数据库中的所有表
  • get_table_schema:获取表的架构
  • create_table:创建新表
  • drop_table:放下一张桌子
  • create_index:在表上创建索引
  • drop_index:删除索引
  • alter_table:更改表结构

事务管理

  • begin_transaction:开始交易
  • commit_transaction:提交交易
  • rollback_transaction:回滚交易

例子

添加连接

{
  "connection_id": "my_sqlite_db",
  "type": "sqlite",
  "db_path": "/path/to/database.db"
}

执行查询

{
  "connection_id": "my_sqlite_db",
  "query": "SELECT * FROM users WHERE age > ?",
  "params": [21]
}

创建表

{
  "connection_id": "my_sqlite_db",
  "table": "users",
  "columns": [
    {
      "name": "id",
      "type": "INTEGER",
      "primary_key": true,
      "nullable": false
    },
    {
      "name": "name",
      "type": "TEXT",
      "nullable": false
    },
    {
      "name": "email",
      "type": "TEXT",
      "nullable": true
    }
  ]
}

插入记录

{
  "connection_id": "my_sqlite_db",
  "table": "users",
  "data": {
    "name": "John Doe",
    "email": "john@example.com"
  }
}

发展

运行测试

# Run all tests
python -m unittest discover

# Run specific test file
python -m unittest tests.test_sqlite

从其他LLM连接

当作为独立的web服务器运行时,其他LLM(如Llama 3)可以通过HTTP连接到数据库MCP服务器。服务器公开以下端点:

端点

  • /list_tools -GET或POST:返回所有可用工具及其描述和输入模式的列表
  • /call_tool -POST:执行特定的数据库工具

示例:从另一个LLM调用

要将此服务器与另一个LLM一起使用,请让LLM向服务器生成HTTP请求。下面是一个如何为像《Llama 3》这样的LLM构建提示的示例:

You can interact with a database by making HTTP requests to a database service at http://localhost:8000. 
The service provides the following endpoints:

1. To get a list of available tools:
   Make a POST request to: http://localhost:8000/list_tools
   
2. To execute a database tool:
   Make a POST request to: http://localhost:8000/call_tool
   with a JSON body like:
   {
     "name": "tool_name",
     "arguments": {
       "param1": "value1",
       "param2": "value2"
     }
   }

For example, to execute a SQL query, you would make a request like:
POST http://localhost:8000/call_tool
Content-Type: application/json

{
  "name": "execute_query",
  "arguments": {
    "connection_id": "my_db",
    "query": "SELECT * FROM users"
  }
}

用于客户端集成的Python代码示例

import requests
import json

# Base URL of the database MCP server
BASE_URL = "http://localhost:8000"

# List available tools
def list_tools():
    response = requests.post(f"{BASE_URL}/list_tools")
    return response.json()

# Execute a database tool
def call_tool(tool_name, arguments):
    payload = {
        "name": tool_name,
        "arguments": arguments
    }
    response = requests.post(f"{BASE_URL}/call_tool", json=payload)
    return response.json()

# Example: List tables in a database
def list_tables(connection_id):
    return call_tool("list_tables", {"connection_id": connection_id})

# Example: Execute a SQL query
def execute_query(connection_id, query, params=None):
    return call_tool("execute_query", {
        "connection_id": connection_id,
        "query": query,
        "params": params
    })

# Example: Add a new connection
def add_connection(connection_id, db_type, **kwargs):
    args = {"connection_id": connection_id, "type": db_type}
    args.update(kwargs)
    return call_tool("add_connection", args)

许可证

MIT许可证

目录标签

目录标签

PythonClaude多数据库支持本地部署统一接口模式管理查询执行事务支持

支持客户端

Claude

接入字段

传输方式(transport,传输协议)

stdio

鉴权方式(authType,认证方式)

none

运行时(runtime,运行环境)

Python

工具数量(toolCount,工具数)

19

资源数量(resourceCount,资源数)

0

提示词数量(promptCount,提示词数)

0

权限和风险

stdionone部署方式未说明

接入前请确认传输方式、认证方式和部署位置,并根据实际工具能力限制访问范围。

安装前确认

不要直接授予不必要的文件、网络或账号权限;先核对安装命令和配置内容。

来源信息

继续浏览同类 MCP