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

MCP Dbserver

MCP Server

一个基于Model Context Protocol (MCP)的服务器,为GitHub Copilot等AI助手提供MySQL数据库内省功能,支持自然语言查询数据库结构和执行安全查询。

工具数

6

提示词数

0

GitHub Stars

0

资源数

0
C#VS Code自然语言查询VS Code

安装说明

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

作者 / 组织

DashrathYadav

提供方

DashrathYadav

最后核验

2026/5/17 20:23

快速接入

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

详细介绍

MsDbServer-MySQL数据库MCP服务器

一个强大的模型上下文协议(MCP)服务器,为GitHub Copilot等AI助手提供MySQL数据库自检功能。用。NET 8和微软官方MCP SDK。

🎯 这是什么?

MsDbServer 是一个MCP(模型上下文协议)服务器,充当AI助手和MySQL数据库之间的桥梁。它允许您询问有关数据库结构的自然语言问题,并获得即时、详细的回复。

GitHub Copilot使用示例:

  • 🗣️ “列出数据库中的所有表”
  • 🗣️ “显示用户表的结构”
  • 🗣️ “什么是对外关键关系?”
  • 🗣️ “执行此查询:从订单中选择COUNT(\*)”

✨ 特性

🛠️ 6强大的数据库工具

工具说明示例
ListTables获取数据库中的所有表“显示所有表”
DescribeTable包含列、键、索引的详细表架构“描述用户表”
ExecuteQuery安全运行SELECT查询(有限制)“查询前10个用户”
GetDatabaseStats数据库大小、表计数、统计信息“获取数据库统计信息”
GetSchemaInfo具有模式匹配的多个表架构“显示以'user'开头的表”
GetTableRelationships外键依赖关系和关系“显示表关系”

🔒 安全功能

  • 查询限制 -只允许SELECT和WITH语句
  • 行限制 -每个查询最多1000行
  • 超时保护 -30秒查询超时
  • 输入验证 -所有输入都经过消毒和验证

🏗️ 技术特性

  • 使用Microsoft MCP SDK构建 -官方ModelContextProtocol包
  • MySQL支持 -MySQL数据库全面自检
  • 异步操作 -非阻塞数据库操作
  • 丰富的格式 -美观、易读的输出
  • 错误处理 -全面的错误消息
  • VS代码集成 -与GitHub Copilot无缝协作

🚀 快速开始

先决条件

1.克隆和设置

git clone https://github.com/your-username/MsDbServer.git
cd MsDbServer

2.配置数据库连接

编辑 MsDbServer/appsettings.json:

{
  "ConnectionStrings": {
    "DefaultConnection": "server=localhost;port=3306;database=your_database;user=your_user;password=your_password"
  }
}

3.测试服务器

cd MsDbServer
dotnet build
dotnet run

4.设置VS代码集成

创建 .vscode/mcp.json 在您的项目中:

{
  "servers": {
    "MsDbServer": {
      "command": "dotnet",
      "args": ["run", "--project", "path/to/MsDbServer/MsDbServer.csproj"],
      "cwd": "${workspaceFolder}",
      "env": {
        "ConnectionStrings__DefaultConnection": "server=localhost;port=3306;database=your_database;user=your_user;password=your_password"
      }
    }
  }
}

📋 详细的设置说明

步骤1:安装先决条件

  1. 安装。NET 8 SDK
   # Windows (using winget)
   winget install Microsoft.DotNet.SDK.8

   # macOS (using brew)
   brew install dotnet

   # Or download from: https://dotnet.microsoft.com/download/dotnet/8.0
  1. 安装Visual Studio代码

- 下载地址:https://code.visualstudio.com/ - 安装GitHub Copilot扩展 - 安装GitHub Copilot聊天扩展

  1. 确保MySQL访问

- 运行MySQL数据库 - 了解连接详细信息(主机、端口、数据库、用户名、密码)

步骤2:项目设置

  1. 克隆存储库
   git clone https://github.com/your-username/MsDbServer.git
   cd MsDbServer
  1. 配置数据库连接

选项A:编辑应用程序设置

   {
     "ConnectionStrings": {
       "DefaultConnection": "server=your_host;port=3306;database=your_db;user=your_user;password=your_password"
     }
   }

选项B:使用环境变量

   # Windows
   set ConnectionStrings__DefaultConnection=server=localhost;port=3306;database=your_db;user=your_user;password=your_password

   # Linux/macOS
   export ConnectionStrings__DefaultConnection="server=localhost;port=3306;database=your_db;user=your_user;password=your_password"
  1. 测试连接
   cd MsDbServer
   dotnet build
   dotnet run

您应该看到:

   info: Database connection test successful. Starting MCP server...

步骤3:VS代码集成

  1. 创建MCP配置

创建 .vscode/mcp.json 在您的工作空间中:

   {
     "servers": {
       "MsDbServer": {
         "command": "dotnet",
         "args": ["run", "--project", "MsDbServer/MsDbServer.csproj"],
         "cwd": "${workspaceFolder}",
         "env": {
           "ConnectionStrings__DefaultConnection": "server=localhost;port=3306;database=your_database;user=your_user;password=your_password"
         }
       }
     }
   }
  1. 在VS代码中打开工作区
   code .
  1. 使用GitHub Copilot进行测试

- 打开GitHub Copilot聊天(Ctrl+Shift+I) - 尝试:“列出数据库中的所有表” - 尝试:“描述用户表”

💻 使用示例

在GitHub Copilot聊天中:

🗣️ List all tables in the database
📋 Response: Shows all table names

🗣️ Describe the users table structure
📋 Response: Detailed schema with columns, types, constraints

🗣️ Show me the first 5 records from the orders table
📋 Response: Formatted table output

🗣️ What are the foreign key relationships in my database?
📋 Response: Visual relationship mapping

🗣️ Get database statistics and table sizes
📋 Response: Database overview with sizes and counts

手动测试(JSON-RPC):

# List all tables
echo '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"ListTables","arguments":{}}}' | dotnet run

# Describe a table
echo '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"DescribeTable","arguments":{"tableName":"users"}}}' | dotnet run

📁 项目结构

MsDbServer/
├── .vscode/
│   └── mcp.json              # VS Code MCP configuration
├── MsDbServer/
│   ├── Program.cs            # Application entry point
│   ├── DatabaseTools.cs     # 6 MCP tools implementation
│   ├── IDatabaseService.cs  # Service interface & data models
│   ├── MySqlDatabaseService.cs # MySQL implementation
│   ├── appsettings.json     # Configuration
│   └── MsDbServer.csproj    # Project file
└── README.md                # This file

🛠️ 发展

建筑

cd MsDbServer
dotnet build

跑步

cd MsDbServer
dotnet run

测试工具

# Test ListTables
echo '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"ListTables","arguments":{}}}' | dotnet run

# Test DescribeTable
echo '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"DescribeTable","arguments":{"tableName":"your_table"}}}' | dotnet run

🔧 故障排除

常见问题

  1. “数据库连接测试失败”

- 检查您的连接字符串 appsettings.json - 验证MySQL服务器是否正在运行 - 确认数据库存在且凭据正确

  1. “GitHub Copilot看不到MCP服务器”

- 确保 .vscode/mcp.json 存在于您的工作区中 - 创建/编辑MCP配置后重新启动VS代码 - 检查GitHub Copilot和Copilot Chat扩展是否已安装并处于活动状态

  1. “构建失败”

- 确保。NET 8 SDK已安装: dotnet --version - 尝试: dotnet restore 然后 dotnet build

  1. “权限被拒绝”错误

- 检查文件权限 - 在Linux/macOS上,您可能需要: chmod +x 关于脚本文件

调试模式

# Run with detailed logging
dotnet run --configuration Debug

🤝 贡献

  1. 分叉存储库
  2. 创建要素分支: git checkout -b feature-name
  3. 进行更改
  4. 彻底测试
  5. 提交拉取请求

📄 许可证

此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。

🙏 致谢

______________________________________________________________________

🚀 准备好用AI探索你的数据库了吗?克隆、配置并开始提问!

  • 依赖注入 -正确的服务注册和终身管理
  • 异步/等待 -所有数据库操作都是异步的
  • 全面的错误处理 -正确的错误响应和有意义的消息
  • 结构化日志记录 -使用Microsoft可配置日志记录。扩展。日志记录

🗄️ 数据库支持

  • MySQL -完全支持MySQL数据库自检
  • 连接字符串配置 -可通过应用程序配置
  • 安全数据类型处理 -处理MySQL特定的数据类型和大值

快速开始

先决条件

  • .NET 8.0 SDK或更高版本
  • MySQL数据库(已配置且可访问)
  • 使用GitHub Copilot的Visual Studio代码(用于测试)

安装

  1. 克隆或下载项目
  2. 在中配置数据库连接 appsettings.json
  3. 构建并运行服务器
dotnet build
dotnet run

配置

更新 appsettings.json 使用MySQL连接字符串:

{
  "ConnectionStrings": {
    "DefaultConnection": "server=localhost;port=3306;database=your_database;user=your_user;password=your_password"
  }
}

VS代码集成

创建或更新 .vscode/mcp.json 在您的工作空间中:

{
  "inputs": [],
  "servers": {
    "MsDbServer": {
      "type": "stdio",
      "command": "dotnet",
      "args": ["run", "--project", "path/to/MsDbServer.csproj"]
    }
  }
}

使用示例

使用GitHub Copilot

配置后,您可以使用GitHub Copilot中的工具:

  • “列出数据库中的所有表”
  • “描述用户表的结构”
  • “显示订单表的架构”
  • “获取数据库统计信息和表大小”
  • “执行查询以显示前10个用户”
  • “显示所有以“user”开头的表”
  • “订单表的外键关系是什么?”

直接JSON-RPC测试

# List all tables
echo '{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"ListTables","arguments":{}}}' | dotnet run

# Describe a specific table
echo '{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"DescribeTable","arguments":{"tableName":"users"}}}' | dotnet run

# Execute a query
echo '{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"ExecuteQuery","arguments":{"query":"SELECT * FROM users LIMIT 5","maxRows":5}}}' | dotnet run

# Get database statistics
echo '{"jsonrpc":"2.0","id":4,"method":"tools/call","params":{"name":"GetDatabaseStats","arguments":{}}}' | dotnet run

# Get schema for tables starting with 'user'
echo '{"jsonrpc":"2.0","id":5,"method":"tools/call","params":{"name":"GetSchemaInfo","arguments":{"tablePattern":"user%"}}}' | dotnet run

# Get relationships for a specific table
echo '{"jsonrpc":"2.0","id":6,"method":"tools/call","params":{"name":"GetTableRelationships","arguments":{"tableName":"orders"}}}' | dotnet run

PowerShell测试

使用附带的测试脚本:

.\test-mcp.ps1

样本输出

ListTables响应

Database Tables:
================
  addresses
  orders
  products
  users

DescribeTable 响应

Table: default.users

Columns:
--------
  UserId bigint(19,0) NOT NULL IDENTITY PRIMARY KEY
  Username varchar(50) NOT NULL
  Email varchar(100) NULL
  CreatedDate datetime NOT NULL
    Default: CURRENT_TIMESTAMP

Primary Keys:
-------------
  UserId

Foreign Keys:
-------------
  (none)

Indexes:
--------
  IX_Users_Email: Email
  IX_Users_Username: Username

ExecuteQuery响应

Query: SELECT UserId, Username, Email FROM users LIMIT 3
Execution Time: 12.45ms
Rows: 3

-----------------------------------------------------------------------
UserId | Username     | Email
-----------------------------------------------------------------------
1      | john_doe     | john.doe@example.com
2      | jane_smith   | jane.smith@example.com
3      | bob_wilson   | bob.wilson@example.com
-----------------------------------------------------------------------

GetDatabaseStats响应

Database Statistics: rent_wizard
================================
Database Version: 8.0.35
Generated At: 2025-06-27 10:30:15 UTC

Overview:
---------
Total Tables: 6
Database Size: 2.4MB

Table Statistics:
-----------------
Table Name               Rows   Data Size Index Size  Columns Indexes
----------------------------------------------------------------------
users                    1,250     125.6KB    45.2KB        8       3
orders                     847      98.3KB    32.1KB       12       4
products                   156      23.4KB    12.8KB        9       2
addresses                  892      67.8KB    28.3KB        7       2
owners                      45       8.2KB     3.1KB        6       1
properties                 234      45.6KB    18.7KB       15       5

GetTableRelationships响应

Table Relationships (8 found)
=========================

Parent Table: users
Children:
  orders.UserId -> users.UserId
    Constraint: FK_orders_users
  addresses.UserId -> users.UserId
    Constraint: FK_addresses_users

Parent Table: products
Children:
  order_items.ProductId -> products.ProductId
    Constraint: FK_order_items_products

Dependency Summary:
------------------
orders depends on: users
addresses depends on: users
order_items depends on: products, orders

项目结构

MsDbServer/
├── Program.cs                  # Application entry point and MCP server setup
├── IDatabaseService.cs         # Database service interface and models
├── MySqlDatabaseService.cs     # MySQL database implementation
├── DatabaseTools.cs           # MCP tools with [McpServerTool] attributes
├── appsettings.json           # Configuration file
└── MsDbServer.csproj          # Project file with dependencies

依赖项

  • 模型上下文协议 (0.3.0-preview.1)-微软MCP SDK
  • 微软。扩展。托管 (9.0.6)-托管基础设施
  • MySql。数据 (9.3.0)-MySQL连接

从手动实施迁移

该项目用官方的Microsoft MCP SDK取代了之前的手动JSON-RPC实现,提供:

  • 简化开发 -基于属性的工具注册
  • 更好的集成 -官方SDK支持和更新
  • 减少沸腾板 -自动JSON-RPC处理
  • 未来考验 -遵循微软推荐的模式

许可证

该项目旨在教育和发展。

______________________________________________________________________

_内置于❤️ 使用。NET 8和微软MCP SDK_

目录标签

目录标签

C#VS Code自然语言查询MySQL数据库本地部署AI助手集成数据库内省GitHubCopilot

支持客户端

VS Code

接入字段

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

未说明

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

none

工具数量(toolCount,工具数)

6

资源数量(resourceCount,资源数)

0

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

0

权限和风险

未说明none部署方式未说明

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

安装前确认

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

仍需确认:installCommand

来源信息

继续浏览同类 MCP