mcp-server-for-db

devsenweb/mcp-server-for-db

3.2

If you are the rightful owner of mcp-server-for-db and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.

The MCP Server for Databases is a lightweight implementation of the Model Context Protocol (MCP) designed to facilitate AI model interactions with SQL databases through a structured interface.

Tools
2
Resources
0
Prompts
0

šŸ—ƒļø MCP Server for Databases

MIT License Python Version

A lightweight implementation of the Model Context Protocol (MCP) for database operations. This server enables AI models to interact with SQL databases through a structured, tool-based interface, providing safe and efficient database access.


šŸ“š Table of Contents


šŸš€ Key Capabilities

šŸ› ļø MCP Tool Integration

  • Exposes database operations as MCP tools for AI model interaction
  • Supports natural language prompts for database exploration
  • Provides schema-aware query building assistance

šŸ’¾ Database Support

  • Works with any SQLAlchemy-compatible database (SQLite, PostgreSQL, MySQL, etc.)
  • Automatic schema inspection and reflection
  • Transaction support for multi-statement operations

šŸ“” MCP Features

  • Native MCP tools, resources, and prompts
  • Support for multiple transport protocols (http, WebSocket, stdio)
  • Structured error handling and validation

šŸ”§ Getting Started

āœ… Prerequisites

  • Python 3.8+
  • SQLite or any other SQLAlchemy-supported database

āš™ļø Installation

git clone https://github.com/devsenweb/mcp-server-for-db.git
cd mcp-server-for-db
python -m venv .venv
source .venv/bin/activate  # macOS/Linux
# OR
.venv\Scripts\activate  # Windows
pip install -r requirements.txt

šŸ› ļø Configuration

Edit config.yaml:

db:
  uri: sqlite:///./example.db  # Supports any SQLAlchemy URI

(Optional) Create a sample DB:

python create_database.py

ā–¶ļø Starting the MCP Server

python -m mcp_server.server --transport http --port 8080
# or
python -m mcp_server.server --transport ws --port 8080
# or
python -m mcp_server.server --transport stdio

🧠 MCP Interface

šŸ“Œ Tool Definitions

execute_query
{
  "jsonrpc": "2.0",
  "method": "tools/execute_query",
  "params": {
    "sql": "SELECT * FROM users WHERE age > 25"
  },
  "id": 1
}
validate_sql
{
  "jsonrpc": "2.0",
  "method": "tools/validate_sql",
  "params": {
    "sql": "SELECT * FROM users"
  },
  "id": 2
}

šŸ“ Resource Endpoints

db://schema
{
  "jsonrpc": "2.0",
  "method": "resources/get",
  "params": {
    "uri": "db://schema"
  },
  "id": 3
}
db://tables
{
  "jsonrpc": "2.0",
  "method": "resources/get",
  "params": {
    "uri": "db://tables"
  },
  "id": 4
}

šŸ› ļø IDE and Tool Integration

(Cursor IDE) .cursor/mcp.json

{
  "mcpServers": {
    "database-query": {
      "url": "http://localhost:8000/mcp",
      "transport": "http"
    }
  }
}

MCP Inspector

npm install -g @modelcontextprotocol/inspector
mcp-inspector --transport http --url http://localhost:8000/mcp
# Visit http://localhost:3000

āš™ļø Development

pip install -r requirements-dev.txt
pytest tests/

šŸ“ Project Structure

mcp-server-for-db/
ā”œā”€ā”€ mcp_server/
│   ā”œā”€ā”€ __init__.py
│   ā”œā”€ā”€ server.py
│   └── db_adapter.py
ā”œā”€ā”€ config.yaml
ā”œā”€ā”€ create_database.py
└── requirements.txt

šŸ¤ Contributing

Contributions are welcome! Feel free to submit a Pull Request.


šŸ“„ License

Licensed under the MIT License. See for details.


āš™ļø MCP Server Configuration Reference

python -m mcp_server.server   --host 0.0.0.0   --port 8000   --transport http   --config ./config.yaml

šŸ™ Acknowledgments

  • Powered by SQLAlchemy
  • Based on the Model Context Protocol (MCP) standard

āš ļø Disclaimer

This project is a demo implementation of an MCP-compatible server for database interaction. It does not include SQL safety mechanisms, input sanitization, authentication, or permission controls.

āš ļø Use at your own risk. This server executes raw SQL and is intended for experimentation, prototyping, and local development only.

It is the responsibility of the user to implement proper guardrails, validation, and security measures before using this in any production or sensitive environment.