MCP-DbServer
MySQL Database MCP Server - AI-powered database introspection with 6 powerful tools
GitHubスター
0
ユーザー評価
未評価
お気に入り
0
閲覧数
7
フォーク
0
イシュー
0
MsDbServer - MySQL Database MCP Server
A powerful Model Context Protocol (MCP) server that provides MySQL database introspection capabilities for AI assistants like GitHub Copilot. Built with .NET 8 and the official Microsoft MCP SDK.
🎯 What is this?
MsDbServer is an MCP (Model Context Protocol) server that acts as a bridge between AI assistants and MySQL databases. It allows you to ask natural language questions about your database structure and get instant, detailed responses.
Example Usage with GitHub Copilot:
- 🗣️ "List all tables in my database"
- 🗣️ "Show me the structure of the users table"
- 🗣️ "What are the foreign key relationships?"
- 🗣️ "Execute this query: SELECT COUNT(*) FROM orders"
✨ Features
🛠️ 6 Powerful Database Tools
Tool | Description | Example |
---|---|---|
ListTables |
Get all tables in database | "Show me all tables" |
DescribeTable |
Detailed table schema with columns, keys, indexes | "Describe the users table" |
ExecuteQuery |
Run SELECT queries safely (with limits) | "Query the first 10 users" |
GetDatabaseStats |
Database size, table counts, statistics | "Get database statistics" |
GetSchemaInfo |
Multiple table schemas with pattern matching | "Show tables starting with 'user'" |
GetTableRelationships |
Foreign key dependencies and relationships | "Show table relationships" |
🔒 Safety Features
- ✅ Query Restrictions - Only SELECT and WITH statements allowed
- ✅ Row Limits - Maximum 1000 rows per query
- ✅ Timeout Protection - 30-second query timeout
- ✅ Input Validation - All inputs sanitized and validated
🏗️ Technical Features
- ✅ Built with Microsoft MCP SDK - Official ModelContextProtocol package
- ✅ MySQL Support - Full MySQL database introspection
- ✅ Async Operations - Non-blocking database operations
- ✅ Rich Formatting - Beautiful, readable output
- ✅ Error Handling - Comprehensive error messages
- ✅ VS Code Integration - Works seamlessly with GitHub Copilot
🚀 Quick Start
Prerequisites
- .NET 8.0 SDK or later
- MySQL database (accessible)
- Visual Studio Code with GitHub Copilot
1. Clone and Setup
git clone https://github.com/your-username/MsDbServer.git
cd MsDbServer
2. Configure Database Connection
Edit MsDbServer/appsettings.json
:
{
"ConnectionStrings": {
"DefaultConnection": "server=localhost;port=3306;database=your_database;user=your_user;password=your_password"
}
}
3. Test the Server
cd MsDbServer
dotnet build
dotnet run
4. Setup VS Code Integration
Create .vscode/mcp.json
in your project:
{
"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"
}
}
}
}
📋 Detailed Setup Instructions
Step 1: Install Prerequisites
Install .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
Install Visual Studio Code
- Download from: https://code.visualstudio.com/
- Install GitHub Copilot extension
- Install GitHub Copilot Chat extension
Ensure MySQL Access
- Have a MySQL database running
- Know the connection details (host, port, database, username, password)
Step 2: Project Setup
Clone the Repository
git clone https://github.com/your-username/MsDbServer.git cd MsDbServer
Configure Database Connection
Option A: Edit appsettings.json
{ "ConnectionStrings": { "DefaultConnection": "server=your_host;port=3306;database=your_db;user=your_user;password=your_password" } }
Option B: Use Environment Variables
# 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"
Test the Connection
cd MsDbServer dotnet build dotnet run
You should see:
info: Database connection test successful. Starting MCP server...
Step 3: VS Code Integration
Create MCP Configuration
Create
.vscode/mcp.json
in your workspace:{ "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" } } } }
Open Workspace in VS Code
code .
Test with GitHub Copilot
- Open GitHub Copilot Chat (Ctrl+Shift+I)
- Try: "List all tables in the database"
- Try: "Describe the users table"
💻 Usage Examples
In GitHub Copilot Chat:
🗣️ 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
Manual Testing (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
📁 Project Structure
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
🛠️ Development
Building
cd MsDbServer
dotnet build
Running
cd MsDbServer
dotnet run
Testing Tools
# 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
🔧 Troubleshooting
Common Issues
"Database connection test failed"
- Check your connection string in
appsettings.json
- Verify MySQL server is running
- Confirm database exists and credentials are correct
- Check your connection string in
"GitHub Copilot doesn't see the MCP server"
- Ensure
.vscode/mcp.json
exists in your workspace - Restart VS Code after creating/editing MCP configuration
- Check that GitHub Copilot and Copilot Chat extensions are installed and active
- Ensure
"Build failed"
- Ensure .NET 8 SDK is installed:
dotnet --version
- Try:
dotnet restore
thendotnet build
- Ensure .NET 8 SDK is installed:
"Permission denied" errors
- Check file permissions
- On Linux/macOS, you might need:
chmod +x
on script files
Debug Mode
# Run with detailed logging
dotnet run --configuration Debug
🤝 Contributing
- Fork the repository
- Create a feature branch:
git checkout -b feature-name
- Make your changes
- Test thoroughly
- Submit a pull request
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- Built with the Microsoft MCP SDK
- Uses MySql.Data for MySQL connectivity
- Powered by .NET 8
🚀 Ready to explore your database with AI? Clone, configure, and start asking questions!
- Dependency Injection - Proper service registration and lifetime management
- Async/Await - All database operations are asynchronous
- Comprehensive Error Handling - Proper error responses with meaningful messages
- Structured Logging - Configurable logging with Microsoft.Extensions.Logging
🗄️ Database Support
- MySQL - Full support for MySQL database introspection
- Connection String Configuration - Configurable via appsettings.json
- Safe Data Type Handling - Handles MySQL-specific data types and large values
Quick Start
Prerequisites
- .NET 8.0 SDK or later
- MySQL database (configured and accessible)
- Visual Studio Code with GitHub Copilot (for testing)
Installation
- Clone or download the project
- Configure your database connection in
appsettings.json
- Build and run the server
dotnet build
dotnet run
Configuration
Update appsettings.json
with your MySQL connection string:
{
"ConnectionStrings": {
"DefaultConnection": "server=localhost;port=3306;database=your_database;user=your_user;password=your_password"
}
}
VS Code Integration
Create or update .vscode/mcp.json
in your workspace:
{
"inputs": [],
"servers": {
"MsDbServer": {
"type": "stdio",
"command": "dotnet",
"args": ["run", "--project", "path/to/MsDbServer.csproj"]
}
}
}
Usage Examples
Using with GitHub Copilot
Once configured, you can use the tools in GitHub Copilot:
- "List all tables in the database"
- "Describe the structure of the users table"
- "Show me the schema for the orders table"
- "Get database statistics and table sizes"
- "Execute a query to show the first 10 users"
- "Show me all tables that start with 'user'"
- "What are the foreign key relationships for the orders table?"
Direct JSON-RPC Testing
# 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 Testing
Use the included test script:
.\test-mcp.ps1
Sample Output
ListTables Response
Database Tables:
================
addresses
orders
products
users
DescribeTable Response
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 Response
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 Response
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 Response
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
Project Structure
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
Dependencies
- ModelContextProtocol (0.3.0-preview.1) - Microsoft MCP SDK
- Microsoft.Extensions.Hosting (9.0.6) - Hosting infrastructure
- MySql.Data (9.3.0) - MySQL connectivity
Migration from Manual Implementation
This project replaces a previous manual JSON-RPC implementation with the official Microsoft MCP SDK, providing:
- Simplified Development - Attributes-based tool registration
- Better Integration - Official SDK support and updates
- Reduced Boilerplate - Automatic JSON-RPC handling
- Future-Proof - Follows Microsoft's recommended patterns
License
This project is built for educational and development purposes.
Built with ❤️ using .NET 8 and the Microsoft MCP SDK
0
フォロワー
0
リポジトリ
0
Gist
0
貢献数