mcpField
D
Database MCP Server

Database MCP Server

本地服务
github.com/georgi-terziyski数据库384 周前更新

关于 Database MCP Server

Database MCP Server 为开发者和 LLM 数据库工作流连接 SQLite、PostgreSQL、MySQL/MariaDB 与 SQL Server。它为不同数据库提供统一操作,并在需要时提供特定数据库工具。

主要功能

  1. 01连接四种数据库系统
  2. 02执行 SQL 和结构化查询
  3. 03管理表和索引
  4. 04开启、提交和回滚事务
  5. 05运行时配置连接

适用场景

  1. 01查询受支持数据库中的记录
  2. 02创建或修改数据库架构
  3. 03管理数据库连接
  4. 04通过事务组织数据库更改

MCP 相关

向 MCP 客户端提供数据库连接、查询、架构和事务工具。它也可作为独立 Web 服务运行,通过 HTTP 端点列出和调用工具。

配置

本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。

暂未收录安装方式

我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。

代码仓库

工具

还没有获取到工具列表

本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。

详细介绍

Database MCP Server

一个模型上下文协议(MCP)服务,提供用于连接和交互各种数据库系统的工具。

功能

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

安装

先决条件
  • Python 3.8 或更高版本
  • 必需的 Python 包(使用 pip 自动安装):
    • SQLAlchemy
    • 各种数据库驱动,具体取决于要使用的数据库:
      • SQLite(Python 内置)
      • PostgreSQL:psycopg2-binary
      • MySQL/MariaDB:mysql-connector-python
      • SQL Server:pyodbc
从源代码安装
# Clone the repository
git clone <repository-url>

# 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 服务(适用于任何大模型)
# 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": "[email protected]"
  }
}

开发

运行测试
# Run all tests
python -m unittest discover

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

从其他大模型连接

作为独立 Web 服务运行时,其他大模型(如 Llama 3)可以通过 HTTP 连接到 Database MCP 服务。该服务提供以下端点:

端点
  • /list_tools - GET 或 POST:返回所有可用工具的列表,以及其说明和输入架构
  • /call_tool - POST:执行指定的数据库工具
示例:从其他大模型调用

要将此服务与其他大模型搭配使用,请让大模型向服务发送 HTTP 请求。以下示例展示了如何为 Llama 3 之类的大模型编写提示词:

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 许可证

评论

支持 Markdown

第一次接触 MCP?

从协议是什么,到在客户端里装上第一个服务,这几条回答覆盖了大多数入门问题。

一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。