Skip to content

Commit 1a42981

Browse files
committed
feat: add MySQL MCP server package
- Implement MySQL MCP server with connection pooling - Add tools for query execution, database/table listing, and schema inspection - Support both SELECT queries and data modification operations - Include comprehensive documentation and TypeScript types - Add basic test structure Closes #6201
1 parent d62a260 commit 1a42981

File tree

8 files changed

+1071
-17
lines changed

8 files changed

+1071
-17
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Dependencies
2+
node_modules/
3+
4+
# Build output
5+
dist/
6+
*.tsbuildinfo
7+
8+
# IDE
9+
.vscode/
10+
.idea/
11+
12+
# Logs
13+
*.log
14+
npm-debug.log*
15+
yarn-debug.log*
16+
yarn-error.log*
17+
18+
# Environment files
19+
.env
20+
.env.local
21+
.env.*.local
22+
23+
# OS files
24+
.DS_Store
25+
Thumbs.db
26+
27+
# Test coverage
28+
coverage/
29+
.nyc_output/
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
# MySQL MCP Server
2+
3+
A Model Context Protocol (MCP) server that provides MySQL database operations.
4+
5+
## Features
6+
7+
- Execute SELECT queries safely
8+
- Execute INSERT, UPDATE, DELETE, and DDL operations
9+
- List databases and tables
10+
- Describe table structures
11+
- Test database connections
12+
- Connection pooling for efficient resource usage
13+
14+
## Installation
15+
16+
```bash
17+
npm install @roo-code/mcp-server-mysql
18+
```
19+
20+
## Configuration
21+
22+
The MySQL MCP server requires the following environment variables:
23+
24+
- `MYSQL_HOST` - MySQL server host (default: `localhost`)
25+
- `MYSQL_PORT` - MySQL server port (default: `3306`)
26+
- `MYSQL_USER` - MySQL username (default: `root`)
27+
- `MYSQL_PASSWORD` - MySQL password (default: empty string)
28+
- `MYSQL_DATABASE` - Default database to use (optional)
29+
30+
## Available Tools
31+
32+
### `query`
33+
34+
Execute SELECT queries on the MySQL database.
35+
36+
**Parameters:**
37+
38+
- `query` (string, required): The SELECT query to execute
39+
- `database` (string, optional): Database to use (overrides default)
40+
41+
**Example:**
42+
43+
```json
44+
{
45+
"query": "SELECT * FROM users WHERE active = 1",
46+
"database": "myapp"
47+
}
48+
```
49+
50+
### `execute`
51+
52+
Execute non-SELECT queries (INSERT, UPDATE, DELETE, CREATE, etc.).
53+
54+
**Parameters:**
55+
56+
- `query` (string, required): The SQL query to execute
57+
- `database` (string, optional): Database to use (overrides default)
58+
59+
**Example:**
60+
61+
```json
62+
{
63+
"query": "INSERT INTO users (name, email) VALUES ('John Doe', '[email protected]')",
64+
"database": "myapp"
65+
}
66+
```
67+
68+
### `list_databases`
69+
70+
List all databases in the MySQL server.
71+
72+
**No parameters required.**
73+
74+
### `list_tables`
75+
76+
List all tables in a specific database.
77+
78+
**Parameters:**
79+
80+
- `database` (string, required): The database name
81+
82+
**Example:**
83+
84+
```json
85+
{
86+
"database": "myapp"
87+
}
88+
```
89+
90+
### `describe_table`
91+
92+
Get the structure of a table.
93+
94+
**Parameters:**
95+
96+
- `table` (string, required): The table name
97+
- `database` (string, optional): The database name (uses default if not specified)
98+
99+
**Example:**
100+
101+
```json
102+
{
103+
"table": "users",
104+
"database": "myapp"
105+
}
106+
```
107+
108+
### `test_connection`
109+
110+
Test the MySQL connection and get server information.
111+
112+
**No parameters required.**
113+
114+
## Usage with Roo Code
115+
116+
To use this MCP server with Roo Code, add it to your MCP settings configuration:
117+
118+
```json
119+
{
120+
"mcpServers": {
121+
"mysql": {
122+
"command": "node",
123+
"args": ["path/to/@roo-code/mcp-server-mysql/dist/index.js"],
124+
"env": {
125+
"MYSQL_HOST": "localhost",
126+
"MYSQL_PORT": "3306",
127+
"MYSQL_USER": "your_username",
128+
"MYSQL_PASSWORD": "your_password",
129+
"MYSQL_DATABASE": "your_default_db"
130+
}
131+
}
132+
}
133+
}
134+
```
135+
136+
## Security Considerations
137+
138+
- The `query` tool only allows SELECT, SHOW, and DESCRIBE queries
139+
- The `execute` tool is for all other operations (INSERT, UPDATE, DELETE, etc.)
140+
- Always use parameterized queries when building dynamic SQL
141+
- Ensure proper access controls are in place at the MySQL server level
142+
- Store credentials securely and never commit them to version control
143+
144+
## Development
145+
146+
```bash
147+
# Install dependencies
148+
npm install
149+
150+
# Build the project
151+
npm run build
152+
153+
# Run in development mode
154+
npm run dev
155+
156+
# Run tests
157+
npm test
158+
159+
# Type checking
160+
npm run typecheck
161+
162+
# Linting
163+
npm run lint
164+
```
165+
166+
## License
167+
168+
MIT
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { config } from "@roo-code/config-eslint/base"
2+
3+
/** @type {import("eslint").Linter.Config} */
4+
export default [...config]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "@roo-code/mcp-server-mysql",
3+
"version": "0.1.0",
4+
"description": "MySQL MCP server for Roo Code",
5+
"type": "module",
6+
"main": "dist/index.js",
7+
"types": "dist/index.d.ts",
8+
"scripts": {
9+
"build": "tsc",
10+
"dev": "tsx src/index.ts",
11+
"start": "node dist/index.js",
12+
"test": "vitest run",
13+
"test:watch": "vitest",
14+
"lint": "eslint src --ext .ts",
15+
"typecheck": "tsc --noEmit"
16+
},
17+
"keywords": [
18+
"mcp",
19+
"mysql",
20+
"database",
21+
"model-context-protocol"
22+
],
23+
"author": "Roo Code Inc",
24+
"license": "MIT",
25+
"dependencies": {
26+
"@modelcontextprotocol/sdk": "^1.9.0",
27+
"mysql2": "^3.11.5",
28+
"zod": "^3.24.1"
29+
},
30+
"devDependencies": {
31+
"@roo-code/config-eslint": "workspace:*",
32+
"@types/node": "^22.10.5",
33+
"@typescript-eslint/eslint-plugin": "^8.20.0",
34+
"@typescript-eslint/parser": "^8.20.0",
35+
"eslint": "^9.18.0",
36+
"tsx": "^4.19.2",
37+
"typescript": "^5.7.3",
38+
"vitest": "^2.1.8"
39+
},
40+
"engines": {
41+
"node": ">=18"
42+
}
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { describe, it, expect, beforeAll, afterAll, vi } from "vitest"
2+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"
3+
4+
// Mock the mysql2/promise module
5+
vi.mock("mysql2/promise", () => ({
6+
default: {
7+
createPool: vi.fn(() => ({
8+
getConnection: vi.fn(() => ({
9+
execute: vi.fn(() => [[{ Database: "test_db" }], [{ name: "Database" }]]),
10+
ping: vi.fn(),
11+
changeUser: vi.fn(),
12+
release: vi.fn(),
13+
})),
14+
})),
15+
},
16+
}))
17+
18+
describe("MySQL MCP Server", () => {
19+
it("should create server instance", () => {
20+
const server = new McpServer({
21+
name: "mysql-server",
22+
version: "0.1.0",
23+
description: "MySQL database operations via MCP",
24+
})
25+
26+
expect(server).toBeDefined()
27+
})
28+
29+
it("should have required tools", async () => {
30+
const { server } = await import("./index.js")
31+
32+
// Check if tools are registered
33+
const tools = server.getTools()
34+
const toolNames = tools.map((tool) => tool.name)
35+
36+
expect(toolNames).toContain("query")
37+
expect(toolNames).toContain("execute")
38+
expect(toolNames).toContain("list_databases")
39+
expect(toolNames).toContain("list_tables")
40+
expect(toolNames).toContain("describe_table")
41+
expect(toolNames).toContain("test_connection")
42+
})
43+
})

0 commit comments

Comments
 (0)