Skip to content

sderhy/php-mysql-mcp-server

Repository files navigation

PHP MySQL MCP Server for Claude Code

A portable, secure, and easy-to-use MySQL database interface for Claude Code using the Model Context Protocol (MCP). Built entirely in PHP with zero dependencies.

๐Ÿš€ Quick Start

./install-mysql-mcp.sh

Restart Claude Code, and you're done!

๐Ÿ“‹ What This Does

Gives Claude Code the ability to:

  • ๐Ÿ“Š List and explore your database tables
  • ๐Ÿ” Search for data across your database
  • ๐Ÿ“ˆ Execute read-only SQL queries
  • ๐Ÿ“ Analyze table structures and schemas
  • ๐Ÿ”ข Count rows and get statistics

All in natural language! Just ask Claude about your database.

๐ŸŽฏ Features

  • 6 Powerful Tools: Everything you need to explore and query databases
  • Read-Only & Safe: Only SELECT queries allowed, no data modification
  • Zero Dependencies: Pure PHP, no external packages required
  • Portable: One script installs everything
  • Multi-Database: Connect to multiple databases simultaneously
  • SQL Injection Protected: Built-in security measures
  • Team-Friendly: Easy to share and deploy

๐Ÿ“ฆ Installation

Prerequisites

  • PHP 7.4+ with PDO MySQL extension
  • MySQL database access
  • Claude Code installed

Install PHP

# macOS
brew install php

# Ubuntu/Debian
sudo apt-get install php-cli php-mysql

# Fedora/CentOS
sudo dnf install php-cli php-mysqlnd

# Verify
php --version
php -m | grep pdo_mysql

Automated Installation (Recommended)

# Clone the repository
git clone git@github.com:sderhy/php-mysql-mcp-server.git
cd php-mysql-mcp-server

# Run the installer
./install-mysql-mcp.sh

The installer will:

  1. โœ… Check prerequisites
  2. โœ… Ask for database credentials
  3. โœ… Test database connection
  4. โœ… Create configuration file
  5. โœ… Register with Claude Code
  6. โœ… Verify installation

Then restart Claude Code and you're ready!

Manual Installation

# 1. Create config
cp mysql-config.example.json mysql-config.json
nano mysql-config.json

# 2. Register with Claude Code
claude mcp add --scope user --transport stdio mysql-mydb -- \
  php $(pwd)/mysql-mcp-server.php

# 3. Verify
claude mcp list

๐ŸŽฎ Usage Examples

Once installed, interact naturally with Claude Code:

You: What tables are in my database?
Claude: [Lists all tables with descriptions]

You: Show me the users table structure
Claude: [Displays columns, types, indexes, and row count]

You: How many active users do I have?
Claude: [Counts rows where status = 'active']

You: Find all orders from last month
Claude: [Executes appropriate SELECT query]

You: Search for email john@example.com
Claude: [Searches across all columns in users table]

๐Ÿ”ง Available Tools

  1. list_tables - List all tables (with optional pattern filter)
  2. describe_table - Get complete schema information
  3. query_database - Execute SELECT queries (max 1000 rows)
  4. get_table_row_count - Count rows with optional WHERE clause
  5. search_tables - Search for value across all columns
  6. get_table_sample - Get sample rows to understand data

๐Ÿ”’ Security

  • Read-Only: Only SELECT statements allowed
  • SQL Injection Prevention: Input validation and prepared statements
  • Credential Protection: Config files are git-ignored
  • Rate Limiting: Automatic LIMIT enforcement
  • Query Validation: Blocks dangerous SQL keywords

Blocked operations: INSERT, UPDATE, DELETE, DROP, CREATE, ALTER, TRUNCATE, GRANT, REVOKE

๐ŸŒ Distribution & Sharing

Create Portable Package

./create-portable-package.sh

Creates a 12KB tarball with everything needed:

  • MCP server implementation
  • Installer script
  • Configuration template
  • Complete documentation

Share with Team

Method 1: Via Git

# Team members clone
git clone git@github.com:sderhy/php-mysql-mcp-server.git
cd php-mysql-mcp-server

# Each person runs
./install-mysql-mcp.sh

Method 2: Via Package

# Create package
./create-portable-package.sh

# Share the tarball
# Each person extracts and runs ./install-mysql-mcp.sh

Note: Credentials are never committed (git-ignored). Each user enters their own.

โš™๏ธ Configuration

Basic Configuration

mysql-config.json:

{
  "host": "localhost",
  "port": 3306,
  "database": "my_database",
  "username": "my_user",
  "password": "my_password",
  "charset": "utf8mb4"
}

Multiple Databases

Install multiple instances:

./install-mysql-mcp.sh  # Name: mysql-production
./install-mysql-mcp.sh  # Name: mysql-staging
./install-mysql-mcp.sh  # Name: mysql-development

Remote Database

Just configure the host:

{
  "host": "db.example.com",
  "port": 3306,
  "database": "production",
  ...
}

SSH Tunnel

# Create tunnel
ssh -L 3307:localhost:3306 user@remote-server

# Configure
{
  "host": "localhost",
  "port": 3307,
  ...
}

๐Ÿ› Troubleshooting

Common Issues

Problem Solution
claude command not found Ensure Claude Code is installed; check which claude
No MCP servers configured Use claude mcp add, not manual file editing
Server not appearing Completely quit and relaunch Claude Code
Connection failed Test: mysql -h HOST -u USER -p DATABASE
PDO extension missing Install: apt-get install php-mysql

Verification Commands

# Check PHP
php --version
php -m | grep pdo_mysql

# Check registration
claude mcp list

# Check Claude Code
# In Claude Code, run: /mcp

# Check logs
tail -f ~/.claude/debug/latest | grep -i mysql

Reset Installation

# Remove server
claude mcp remove mysql-mydb

# Clean config
rm mysql-config.json

# Reinstall
./install-mysql-mcp.sh

๐Ÿ“š Documentation

๐Ÿค Contributing

This is a reference implementation designed to be:

  • Simple: Easy to understand and modify
  • Portable: Works anywhere PHP runs
  • Secure: Read-only with built-in protections
  • Educational: Learn how MCP works

Feel free to:

  • Fork and customize for your needs
  • Add new tools for specific use cases
  • Adapt for other databases (PostgreSQL, SQLite, etc.)
  • Share improvements with the community

๐ŸŒŸ Why PHP?

PHP is an excellent choice for MCP servers:

  • Ubiquitous: Available on most systems
  • Zero Setup: No npm, no virtual environments, no build steps
  • Fast: Instant startup, perfect for stdio communication
  • Portable: Single file deployments
  • Battle-tested: Mature PDO library for database access

๐Ÿ“œ License

ISC - Free to use, modify, and distribute

๐Ÿ™ Acknowledgments

Built for the Claude Code community. Inspired by the need for easy, secure database access in AI-assisted development.


Need Help?

  1. Check START-HERE.md for quick start
  2. Read README-PORTABLE.md for installation
  3. Review README-MYSQL.md for detailed docs
  4. Check troubleshooting section above

Happy Querying! ๐ŸŽ‰

About

Portable mysql mcp php server, if you need to work with Claude-Code / php

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published