Bun Database MCP Server
A high-performance MCP server that enables AI assistants to safely interact with MySQL databases through secure CRUD operations, schema inspection, and parameterized queries with built-in SQL injection prevention.
README
🚀 Bun Database MCP Server
A high-performance Model Context Protocol (MCP) server built with Bun and TypeScript, providing secure database operations for MySQL databases. This server enables AI assistants to safely interact with MySQL databases through a standardized protocol.
📹 Video Tutorials
Watch these comprehensive tutorials to understand MCP development:
✨ Features
- 🔌 Database Connection Management - Connect, disconnect, and check connection status
- 🔍 Safe Query Execution - Execute SELECT queries with parameterized statements
- 📝 CRUD Operations - Create, Read, Update, and Delete records securely
- 📊 Schema Inspection - Read database schema and table structures
- 🤖 MCP Prompts - Pre-built prompts for common database operations
- 📚 MCP Resources - Access database documentation and schema information
- 🛡️ SQL Injection Prevention - Built-in validation and sanitization
- ⚡ Built with Bun - Lightning-fast runtime and package management
- 🔒 Environment-based Configuration - Secure credential management
📋 Prerequisites
- Bun v1.0 or higher
- MySQL 5.7+ or MySQL 8.0+
- Node.js 18+ (for compatibility)
🛠️ Installation
- Clone the repository:
git clone https://github.com/yourusername/bun-db-mcp.git
cd bun-db-mcp
- Install dependencies:
bun install
- Configure environment variables:
cp .env.example .env
Edit .env with your database credentials:
DB_HOST=localhost
DB_PORT=3306
DB_USER=your_user
DB_PASSWORD=your_password
DB_DATABASE=your_database
- Initialize the database with sample data:
The repository includes an simple_import_employees.sql file with sample employee data. Import it using one of these methods.
Option 1: Using mysql command-line client:
mysql -u your_user -p your_database < simple_import_employees.sql
Option 2: From within MySQL client:
mysql -u your_user -p your_database
Then run:
source simple_import_employees.sql;
Option 3: Using mysqldump (for backup/restore):
# To export (backup)
mysqldump -u your_user -p your_database > backup.sql
# To import (restore)
mysql -u your_user -p your_database < backup.sql
🚀 Usage
Transport Options
The MCP server supports three different transport protocols:
1. STDIO Transport (Default)
Standard input/output communication for MCP clients like Claude Desktop:
bun run src/index.ts
# or
bun run src/index.ts --transport stdio
2. SSE Transport (Server-Sent Events)
HTTP-based transport using Server-Sent Events for real-time streaming:
bun run src/index.ts --transport sse --port 3000
- Endpoints:
GET http://localhost:3000/mcp- Establish SSE streamPOST http://localhost:3000/messages- Send JSON-RPC requests
- Session Management: Via
sessionIdquery parameter
3. HTTP Transport (StreamableHTTP with OAuth)
Modern HTTP transport with OAuth authentication supporting both JSON and SSE responses:
bun run src/index.ts --transport http --port 3000 --oauth
- MCP Endpoint:
GET/POST http://localhost:3000/mcp - Auth Server:
http://localhost:3001(OAuth provider with demo flows) - Session Management: Via
Mcp-Session-Idheader - Authentication: Bearer token required in
Authorizationheader - Response Formats:
- JSON:
Accept: application/json, text/event-stream - SSE:
Accept: text/event-stream, application/json
- JSON:
Authentication Flow:
- OAuth server runs on port 3001 with demo authentication flows
- Supports both in-memory demo provider and Google OAuth
- MCP server validates Bearer tokens for protected resources
- Set
GOOGLE_CLIENT_IDandGOOGLE_CLIENT_SECRETenvironment variables for Google OAuth
Starting the Server
Run with default STDIO transport:
bun run start
Run with specific transport:
# SSE transport
bun run src/index.ts --transport sse --port 3000
# HTTP transport with OAuth
bun run src/index.ts --transport http --port 3000 --oauth
# HTTP transport without OAuth (not recommended)
bun run src/index.ts --transport http --port 3000
For development with auto-reload:
bun run dev
Available Tools
The server provides six powerful tools for database operations:
1. connection - Manage Database Connection
{
"action": "connect" | "disconnect" | "status"
}
2. query - Execute SELECT Queries
{
"sql": "SELECT * FROM employees WHERE hire_date > ?",
"params": ["2000-01-01"]
}
3. create - Insert Records
{
"table": "employees",
"data": {
"emp_no": 500000,
"birth_date": "1990-05-15",
"first_name": "John",
"last_name": "Doe",
"gender": "M",
"hire_date": "2024-01-15"
}
}
4. update - Update Records
{
"table": "employees",
"data": { "hire_date": "2024-02-01" },
"where": { "emp_no": 500000 }
}
5. delete - Delete Records
{
"table": "employees",
"where": { "emp_no": 500000 }
}
6. readSchema - Inspect Database Schema
{
"table": "employees"
}
Available Prompts
The server provides pre-built prompts for common database operations:
1. query-employees - Natural Language Queries
Query the employees table using natural language instructions.
- Arguments:
instructions- e.g., "count female employees", "show 10 recent hires"
2. insert-employee - Add New Employee
Insert a new employee with all related information (department, title, salary).
- Arguments:
employee_info- Employee details in natural language
3. delete-employee - Remove Employee
Delete an employee and all related records from the database.
- Arguments:
employee_identifier- Employee number or name
4. manage-departments - Department Operations
Insert a new department or delete an existing department.
- Arguments:
instructions- e.g., "add Marketing department", "delete department d005"
Available Resources
The server exposes the following MCP resources:
bun-db-mcp://general-database - Database Schema Documentation
- Type:
text/markdown - Description: Complete documentation of the employee database schema including:
- Table structures and columns
- Entity relationships
- Key design patterns
- Common query patterns
- Mermaid ER diagram
🧪 Testing
Run the test suite:
bun test
Run specific test files:
bun test:db # Database connection tests
bun test:tools # Tool validation tests
Watch mode for development:
bun test:watch
🔧 Configuration
MCP Client Configuration
STDIO Transport (Claude Desktop)
To use with Claude Desktop or other MCP clients, add to your configuration:
{
"mcpServers": {
"bun-db-mcp": {
"command": "bun",
"args": [
"run",
"<root path>/src/index.ts",
"--transport",
"stdio"
],
"env": {
"DB_HOST": "127.0.0.1",
"DB_PORT": "3306",
"DB_USER": "root",
"DB_PASSWORD": "<your_password>",
"DB_DATABASE": "employees"
}
}
}
}
HTTP/SSE Transport (Web Clients)
For HTTP-based transports, use curl or web clients:
SSE Transport Example:
# Establish SSE stream
curl -N -H "Accept: text/event-stream" \
http://localhost:3000/mcp
# Send requests (in another terminal)
curl -X POST http://localhost:3000/messages?sessionId=<session-id> \
-H "Content-Type: application/json" \
-d '{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}'
HTTP Transport with OAuth Example:
# First, get an access token from the auth server
curl -X POST http://localhost:3001/oauth/token \
-H "Content-Type: application/json" \
-d '{"grant_type": "client_credentials", "client_id": "demo-client", "client_secret": "demo-secret"}'
# Use the token to make MCP requests
curl -X POST http://localhost:3000/mcp \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-H "Authorization: Bearer <access-token>" \
-d '{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}'
# SSE response with authentication
curl -X POST http://localhost:3000/mcp \
-H "Content-Type: application/json" \
-H "Accept: text/event-stream, application/json" \
-H "Authorization: Bearer <access-token>" \
-d '{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}' \
--no-buffer
Environment Variables
| Variable | Description | Default |
|---|---|---|
DB_HOST |
MySQL host address | localhost |
DB_PORT |
MySQL port | 3306 |
DB_USER |
Database user | root |
DB_PASSWORD |
Database password | - |
DB_DATABASE |
Database name | mcp_test |
GOOGLE_CLIENT_ID |
Google OAuth client ID (optional) | - |
GOOGLE_CLIENT_SECRET |
Google OAuth client secret (optional) | - |
🏗️ Project Structure
bun-db-mcp/
├── src/
│ ├── index.ts # Main MCP server with transport selection
│ ├── handlers.ts # Shared MCP request handlers
│ ├── transports/ # Transport implementations
│ │ ├── stdio.ts # STDIO transport (default)
│ │ ├── sse.ts # Server-Sent Events transport
│ │ └── http.ts # StreamableHTTP transport with OAuth support
│ ├── auth/ # OAuth authentication providers
│ │ ├── demoInMemoryOAuthProvider.ts # Demo OAuth provider
│ │ └── googleOAuthProvider.ts # Google OAuth provider
│ ├── db/
│ │ ├── connection.ts # Database connection manager
│ │ └── types.ts # TypeScript type definitions
│ ├── tools/
│ │ └── index.ts # Tool implementations
│ ├── specs/
│ │ ├── database-schema.md # Database schema documentation
│ │ ├── query-employees.md # Query prompt specification
│ │ ├── insert-employee-info.md # Insert prompt specification
│ │ ├── delete-employee.md # Delete prompt specification
│ │ └── manage-departments.md # Department management prompt
│ └── utils/
│ └── validation.ts # Input validation & sanitization
├── tests/
│ ├── db.test.ts # Database tests
│ └── tools.test.ts # Tool tests
├── .env.example # Environment template
└── package.json # Project configuration
🔒 Security Features
- OAuth Authentication - Bearer token authentication for HTTP transport
- Protected Resources - Access control for sensitive database operations
- Parameterized Queries - All queries use prepared statements to prevent SQL injection
- Input Validation - Table and column names are validated against strict patterns
- Identifier Escaping - Database identifiers are properly escaped
- SELECT-only Queries - Query tool restricted to SELECT statements only
- Environment Variables - Sensitive credentials stored in environment files
- CORS Protection - Configurable cross-origin resource sharing policies
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- Built with Bun - The fast all-in-one JavaScript runtime
- Uses MCP SDK for protocol implementation
- Database connectivity via mysql2
📊 Performance
Thanks to Bun's optimized runtime:
- 🚀 Fast Startup - Server starts in milliseconds
- ⚡ Low Memory - Efficient memory usage
- 🔥 High Throughput - Handle multiple database operations efficiently
🐛 Troubleshooting
Common Issues
-
Connection Refused
- Verify MySQL is running
- Check host and port in
.env - Ensure user has proper permissions
-
Authentication Failed
- Verify credentials in
.env - Check MySQL user permissions
- Ensure database exists
- Verify credentials in
-
Module Not Found
- Run
bun installto install dependencies - Verify Bun version with
bun --version
- Run
📞 Support
For issues and questions:
- Open an issue on GitHub Issues
- Check existing issues for solutions
- Provide detailed error messages and steps to reproduce
Built with ❤️ using Bun and TypeScript
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。



