mcp-win-stdio-db 0.2.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mcp_win_stdio/db/__init__.py +5 -0
- mcp_win_stdio/db/__main__.py +8 -0
- mcp_win_stdio/db/cli.py +87 -0
- mcp_win_stdio/db/guide.py +165 -0
- mcp_win_stdio/db/server.py +2568 -0
- mcp_win_stdio_db-0.2.3.dist-info/METADATA +118 -0
- mcp_win_stdio_db-0.2.3.dist-info/RECORD +9 -0
- mcp_win_stdio_db-0.2.3.dist-info/WHEEL +4 -0
- mcp_win_stdio_db-0.2.3.dist-info/entry_points.txt +3 -0
mcp_win_stdio/db/cli.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""
|
|
2
|
+
CLI entry point for mcp-win-stdio-db.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import sys
|
|
7
|
+
from mcp_win_stdio.db import __version__
|
|
8
|
+
from mcp_win_stdio.db.server import mcp
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def cmd_run(args: argparse.Namespace) -> None:
|
|
12
|
+
"""Run Database MCP server over stdio."""
|
|
13
|
+
mcp.run()
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def cmd_doctor(args: argparse.Namespace) -> None:
|
|
17
|
+
"""Check Database MCP health and dependencies."""
|
|
18
|
+
print(f"\n=== mcp-win-stdio-db Doctor Diagnostic (v{__version__}) ===\n")
|
|
19
|
+
print(f"[OK] Python: {sys.version.split()[0]} ({sys.executable})")
|
|
20
|
+
|
|
21
|
+
for dep in ("mcp", "psycopg2", "pymysql"):
|
|
22
|
+
try:
|
|
23
|
+
__import__(dep)
|
|
24
|
+
print(f"[OK] Driver: {dep:<12}")
|
|
25
|
+
except ImportError:
|
|
26
|
+
print(f"[FAIL] Driver: {dep:<12} (MISSING - run `pip install {dep}`)")
|
|
27
|
+
|
|
28
|
+
print("\n--- Native DB Dump Utilities ---")
|
|
29
|
+
import shutil
|
|
30
|
+
for util in ("pg_dump", "psql", "mysqldump", "mysql"):
|
|
31
|
+
loc = shutil.which(util)
|
|
32
|
+
if loc:
|
|
33
|
+
print(f"[OK] Tool: {util:<12} -> {loc}")
|
|
34
|
+
else:
|
|
35
|
+
print(f"[INFO] Tool: {util:<12} -> (Not on PATH, fallback SQL execution used)")
|
|
36
|
+
|
|
37
|
+
print("\nDiagnostic complete.\n")
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def cmd_guide(args: argparse.Namespace) -> None:
|
|
41
|
+
"""Print guide and prompt recipes."""
|
|
42
|
+
print("""
|
|
43
|
+
=== mcp-win-stdio-db Guide & Recipes ===
|
|
44
|
+
|
|
45
|
+
Tools Provided:
|
|
46
|
+
- list_connections, use_database, list_databases, list_schemas
|
|
47
|
+
- describe_table, schema_overview, get_table_sample, search_schema
|
|
48
|
+
- read_query, execute_query, explain_query, get_database_stats
|
|
49
|
+
- create_database, drop_database, clone_database, terminate_connections
|
|
50
|
+
- list_active_queries, dump_database, restore_database
|
|
51
|
+
|
|
52
|
+
Configuration:
|
|
53
|
+
Pass SERVERS env var as JSON:
|
|
54
|
+
SERVERS='{"showreel":"postgresql://...","dsr":"postgresql://...","ijitest":"mysql://..."}'
|
|
55
|
+
|
|
56
|
+
Run in Claude / Copilot:
|
|
57
|
+
python -m mcp_win_stdio.db.server
|
|
58
|
+
""")
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def main() -> None:
|
|
62
|
+
parser = argparse.ArgumentParser(
|
|
63
|
+
prog="mcp-win-stdio-db",
|
|
64
|
+
description="Unified Database MCP Server CLI (PostgreSQL & MySQL)",
|
|
65
|
+
)
|
|
66
|
+
parser.add_argument("--version", "-v", action="version", version=f"%(prog)s {__version__}")
|
|
67
|
+
subparsers = parser.add_subparsers(dest="command", help="Command to execute")
|
|
68
|
+
|
|
69
|
+
sub_guide = subparsers.add_parser("guide", help="View usage guide and recipes")
|
|
70
|
+
sub_guide.set_defaults(func=cmd_guide)
|
|
71
|
+
|
|
72
|
+
sub_doctor = subparsers.add_parser("doctor", help="Check database drivers and native tools")
|
|
73
|
+
sub_doctor.set_defaults(func=cmd_doctor)
|
|
74
|
+
|
|
75
|
+
sub_run = subparsers.add_parser("run", help="Run Database MCP server over stdio")
|
|
76
|
+
sub_run.set_defaults(func=cmd_run)
|
|
77
|
+
|
|
78
|
+
args = parser.parse_args()
|
|
79
|
+
if hasattr(args, "func"):
|
|
80
|
+
args.func(args)
|
|
81
|
+
else:
|
|
82
|
+
# Default action is run
|
|
83
|
+
cmd_run(args)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
if __name__ == "__main__":
|
|
87
|
+
main()
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Usage guide and prompt recipes for Database MCP server (mcp-win-stdio.db).
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
def print_db_guide() -> None:
|
|
6
|
+
guide_text = """
|
|
7
|
+
================================================================================
|
|
8
|
+
Polyglot Database MCP Server (mcp-win-stdio.db)
|
|
9
|
+
================================================================================
|
|
10
|
+
|
|
11
|
+
Description:
|
|
12
|
+
Unified Model Context Protocol server for PostgreSQL and MySQL databases.
|
|
13
|
+
Supports cross-database switching, cross-schema auto-resolution, fast JSON queries,
|
|
14
|
+
DBA administrative tools (safe DROP, CREATE, CLONE), native DUMP / RESTORE,
|
|
15
|
+
DDL reconstruction, PII-safe sampling, database-wide health auditing, and
|
|
16
|
+
schema diff with auto-generated migration SQL.
|
|
17
|
+
|
|
18
|
+
Available Tools (23 Tools):
|
|
19
|
+
--------------------------------------------------------------------------------
|
|
20
|
+
CONNECTION MANAGEMENT
|
|
21
|
+
1. list_connections
|
|
22
|
+
- Lists all active and configured database connections from SERVERS env.
|
|
23
|
+
|
|
24
|
+
2. use_database
|
|
25
|
+
- Switches active connection or switches database on current server.
|
|
26
|
+
- Args: name (str), database (optional str)
|
|
27
|
+
|
|
28
|
+
3. add_connection
|
|
29
|
+
- Dynamically adds a new PostgreSQL or MySQL connection string at runtime.
|
|
30
|
+
- Args: name (str), url (str)
|
|
31
|
+
|
|
32
|
+
SCHEMA INSPECTION
|
|
33
|
+
4. list_databases
|
|
34
|
+
- Lists all databases on the active server.
|
|
35
|
+
|
|
36
|
+
5. list_schemas
|
|
37
|
+
- Lists all schemas in the active PostgreSQL database.
|
|
38
|
+
|
|
39
|
+
6. describe_table
|
|
40
|
+
- Full schema inspection: columns, data types, nullability, defaults, PKs,
|
|
41
|
+
foreign keys, and indexes. Auto-resolves cross-schema tables in PostgreSQL.
|
|
42
|
+
- Args: tableName (str), schemaName (optional str)
|
|
43
|
+
|
|
44
|
+
7. schema_overview
|
|
45
|
+
- Compact overview of all user tables and views.
|
|
46
|
+
- Args: schemaName (optional str)
|
|
47
|
+
|
|
48
|
+
8. compact_schema_overview [NEW v0.2.3]
|
|
49
|
+
- Ultra-compact one-liner per table: schema.table (col: type PK, col2: FK->ref)
|
|
50
|
+
- Ideal for large databases — fits entire schema in minimal tokens.
|
|
51
|
+
- Args: schemaName (optional str), connection (optional str)
|
|
52
|
+
|
|
53
|
+
9. get_table_ddl [NEW v0.2.3]
|
|
54
|
+
- Reconstructs the full CREATE TABLE DDL with column types, NOT NULL,
|
|
55
|
+
defaults, all constraints (PK, FK, UNIQUE, CHECK), and extra indexes.
|
|
56
|
+
- PostgreSQL only (MySQL falls back to SHOW CREATE TABLE).
|
|
57
|
+
- Args: tableName (str), schema (optional str), connection (optional str)
|
|
58
|
+
|
|
59
|
+
10. search_schema
|
|
60
|
+
- Case-insensitive regex search for table, view, or column names.
|
|
61
|
+
- Args: query (str)
|
|
62
|
+
|
|
63
|
+
DATA ACCESS
|
|
64
|
+
11. get_table_sample [NEW v0.2.3]
|
|
65
|
+
- Returns sample rows (default: 5) and estimated row count for quick inspection.
|
|
66
|
+
- mask_sensitive=True (default) auto-redacts PII columns (password, token,
|
|
67
|
+
secret, ssn, api_key, credit_card, etc.) as [REDACTED_SENSITIVE].
|
|
68
|
+
- Args: tableName (str), schemaName (optional str), limit (optional int),
|
|
69
|
+
mask_sensitive (optional bool, default: True)
|
|
70
|
+
|
|
71
|
+
12. read_query
|
|
72
|
+
- Safe SELECT queries returning structured JSON with row counts.
|
|
73
|
+
- Args: sql (str), limit (optional int)
|
|
74
|
+
|
|
75
|
+
13. execute_query
|
|
76
|
+
- Executes DML / DDL statements (INSERT, UPDATE, DELETE, CREATE, ALTER) with
|
|
77
|
+
transaction commit/rollback and affected row counts.
|
|
78
|
+
- Args: sql (str), dry_run (optional bool)
|
|
79
|
+
|
|
80
|
+
14. explain_query
|
|
81
|
+
- Generates EXPLAIN query execution plan.
|
|
82
|
+
- Args: sql (str), analyze (optional bool)
|
|
83
|
+
|
|
84
|
+
DIAGNOSTICS & HEALTH
|
|
85
|
+
15. get_database_stats
|
|
86
|
+
- Table size, row estimates, index sizes, and total database size.
|
|
87
|
+
|
|
88
|
+
16. list_active_queries
|
|
89
|
+
- Lists running queries, connection duration, and process IDs.
|
|
90
|
+
|
|
91
|
+
17. audit_database_health [NEW v0.2.3]
|
|
92
|
+
- Database-wide health audit (PostgreSQL only). Returns three categories:
|
|
93
|
+
• unindexed_foreign_keys: FK columns lacking a supporting index (slow JOINs).
|
|
94
|
+
• unused_indexes: Indexes with zero scans — candidates for DROP.
|
|
95
|
+
• bloated_tables: Tables with >20% dead tuples needing VACUUM.
|
|
96
|
+
- Each finding includes a 'fix' field with the recommended SQL statement.
|
|
97
|
+
- Args: schema (optional str, default: 'public', use '*' for all schemas),
|
|
98
|
+
connection (optional str)
|
|
99
|
+
|
|
100
|
+
18. compare_schemas [ENHANCED]
|
|
101
|
+
- Compares two connections: missing tables, missing columns, type mismatches.
|
|
102
|
+
- Now includes migration_sql: auto-generated ALTER/CREATE statements to bring
|
|
103
|
+
target schema up to source.
|
|
104
|
+
- Args: source_connection (str), target_connection (str), schema (optional str)
|
|
105
|
+
|
|
106
|
+
DBA MANAGEMENT
|
|
107
|
+
19. create_database
|
|
108
|
+
- Creates a new database on the active server.
|
|
109
|
+
- Args: name (str), template (optional str, Postgres only), encoding (optional str)
|
|
110
|
+
|
|
111
|
+
20. drop_database
|
|
112
|
+
- Safety-guarded DROP DATABASE (requires confirmName matching target name).
|
|
113
|
+
- Args: name (str), confirmName (str), force (optional bool)
|
|
114
|
+
|
|
115
|
+
21. clone_database
|
|
116
|
+
- Fast database cloning using PostgreSQL TEMPLATE mechanism.
|
|
117
|
+
- Args: sourceDb (str), targetDb (str)
|
|
118
|
+
|
|
119
|
+
22. terminate_connections
|
|
120
|
+
- Terminates active connections to a specific database (PostgreSQL only).
|
|
121
|
+
- Args: dbName (str)
|
|
122
|
+
|
|
123
|
+
23. dump_database / restore_database
|
|
124
|
+
- dump_database: Dumps database to SQL file using native pg_dump or mysqldump.
|
|
125
|
+
Args: outputPath (str), tables (optional list)
|
|
126
|
+
- restore_database: Restores database from SQL dump file using native psql or mysql.
|
|
127
|
+
Args: inputPath (str), targetDb (optional str)
|
|
128
|
+
|
|
129
|
+
--------------------------------------------------------------------------------
|
|
130
|
+
Error Handling:
|
|
131
|
+
--------------------------------------------------------------------------------
|
|
132
|
+
All tools return rich PostgreSQL error details when a query fails:
|
|
133
|
+
• pgcode — PostgreSQL error code (e.g. 23503 for FK violation)
|
|
134
|
+
• severity — ERROR / FATAL / WARNING
|
|
135
|
+
• detail — Full PostgreSQL DETAIL message
|
|
136
|
+
• hint — PostgreSQL HINT for resolution
|
|
137
|
+
• constraint — Constraint name (for constraint violations)
|
|
138
|
+
• table — Table name from error context
|
|
139
|
+
• suggestion — Human-readable fix suggestion from the built-in suggestion map
|
|
140
|
+
|
|
141
|
+
--------------------------------------------------------------------------------
|
|
142
|
+
Environment Variables:
|
|
143
|
+
--------------------------------------------------------------------------------
|
|
144
|
+
* SERVERS: JSON dictionary mapping server names to connection strings.
|
|
145
|
+
Example:
|
|
146
|
+
{
|
|
147
|
+
"showreel": "postgresql://postgres:pass@localhost:5432/showreel",
|
|
148
|
+
"dsr": "postgresql://postgres:pass@localhost:5432/dsr",
|
|
149
|
+
"ijitest": "mysql://user:pass@srv604.hstgr.io:3306/db_name"
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
--------------------------------------------------------------------------------
|
|
153
|
+
Example Prompts for Claude:
|
|
154
|
+
--------------------------------------------------------------------------------
|
|
155
|
+
* "What databases are configured and what tables are in the active database?"
|
|
156
|
+
* "Switch to the ijitest database and describe the users table."
|
|
157
|
+
* "Show me the full DDL for the orders table."
|
|
158
|
+
* "Show a sample of the users table — mask any sensitive columns."
|
|
159
|
+
* "Run a health audit on the showreel database and tell me what to fix."
|
|
160
|
+
* "Compare the dev and prod schemas and generate migration SQL for differences."
|
|
161
|
+
* "Create a clone of the showreel database named showreel_backup."
|
|
162
|
+
* "Show the active queries running on PostgreSQL right now."
|
|
163
|
+
================================================================================
|
|
164
|
+
"""
|
|
165
|
+
print(guide_text)
|