mcp-sql-querystore 0.1.0__tar.gz
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_sql_querystore-0.1.0/.gitignore +21 -0
- mcp_sql_querystore-0.1.0/API Token.txt +0 -0
- mcp_sql_querystore-0.1.0/PKG-INFO +224 -0
- mcp_sql_querystore-0.1.0/README.md +199 -0
- mcp_sql_querystore-0.1.0/claude_desktop_config.example.json +16 -0
- mcp_sql_querystore-0.1.0/provisioning/create_readonly_login.sql +38 -0
- mcp_sql_querystore-0.1.0/pyproject.toml +47 -0
- mcp_sql_querystore-0.1.0/src/mcp_sql_querystore/__init__.py +3 -0
- mcp_sql_querystore-0.1.0/src/mcp_sql_querystore/db.py +251 -0
- mcp_sql_querystore-0.1.0/src/mcp_sql_querystore/plan_parser.py +101 -0
- mcp_sql_querystore-0.1.0/src/mcp_sql_querystore/queries.py +261 -0
- mcp_sql_querystore-0.1.0/src/mcp_sql_querystore/server.py +504 -0
- mcp_sql_querystore-0.1.0/tests/conftest.py +6 -0
- mcp_sql_querystore-0.1.0/tests/test_audit.py +70 -0
- mcp_sql_querystore-0.1.0/tests/test_db_guards.py +64 -0
- mcp_sql_querystore-0.1.0/tests/test_integration.py +114 -0
- mcp_sql_querystore-0.1.0/tests/test_plan_parser.py +82 -0
- mcp_sql_querystore-0.1.0/tests/test_secrets.py +124 -0
- mcp_sql_querystore-0.1.0/tests/test_sweep.py +66 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Secrets — never commit
|
|
2
|
+
pwd.txt
|
|
3
|
+
*.pwd
|
|
4
|
+
*.secret
|
|
5
|
+
.env
|
|
6
|
+
claude_desktop_config.json
|
|
7
|
+
|
|
8
|
+
# Virtual environments
|
|
9
|
+
.venv/
|
|
10
|
+
venv/
|
|
11
|
+
|
|
12
|
+
# Python
|
|
13
|
+
__pycache__/
|
|
14
|
+
*.pyc
|
|
15
|
+
.pytest_cache/
|
|
16
|
+
*.egg-info/
|
|
17
|
+
build/
|
|
18
|
+
dist/
|
|
19
|
+
|
|
20
|
+
# Local plan captures
|
|
21
|
+
*.sqlplan
|
|
File without changes
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: mcp-sql-querystore
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Read-only MCP server for SQL Server performance diagnostics over Query Store, DMVs, and execution plans
|
|
5
|
+
Project-URL: Homepage, https://github.com/deepeshd87/mcp-sql-querystore
|
|
6
|
+
Project-URL: Repository, https://github.com/deepeshd87/mcp-sql-querystore
|
|
7
|
+
Project-URL: Issues, https://github.com/deepeshd87/mcp-sql-querystore/issues
|
|
8
|
+
Author: Deepesh Dhake
|
|
9
|
+
License: MIT
|
|
10
|
+
Keywords: database,dba,mcp,model-context-protocol,mssql,performance-tuning,query-store,sql-server
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Intended Audience :: System Administrators
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Topic :: Database
|
|
18
|
+
Classifier: Topic :: System :: Monitoring
|
|
19
|
+
Requires-Python: >=3.10
|
|
20
|
+
Requires-Dist: mcp<2,>=1.2.0
|
|
21
|
+
Requires-Dist: pyodbc>=5.1.0
|
|
22
|
+
Provides-Extra: test
|
|
23
|
+
Requires-Dist: pytest>=8.0; extra == 'test'
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
|
|
26
|
+
# mcp-sql-querystore
|
|
27
|
+
|
|
28
|
+
Read-only MCP server exposing SQL Server Query Store diagnostics to LLM agents.
|
|
29
|
+
|
|
30
|
+
Six read-only diagnostic tools over Query Store, DMVs, and execution plans. The
|
|
31
|
+
tools have been validated against a live SQL Server instance and are covered by a
|
|
32
|
+
unit + integration test suite. Still validate against a non-prod instance of your
|
|
33
|
+
own before pointing it at production, especially on SQL Server versions other than
|
|
34
|
+
those noted under caveats.
|
|
35
|
+
|
|
36
|
+
## Quickstart
|
|
37
|
+
|
|
38
|
+
1. **Provision a read-only login.** Run `provisioning/create_readonly_login.sql`
|
|
39
|
+
against your instance (edit names first). This login's permissions are the
|
|
40
|
+
read-only guarantee — see the security model below.
|
|
41
|
+
2. **Install.** `pip install -e .` in a virtual environment. ODBC Driver 18 for
|
|
42
|
+
SQL Server must be installed on the host.
|
|
43
|
+
3. **Store the password outside the repo.** Put it in a plain-text file somewhere
|
|
44
|
+
the repo can't reach (not under the project folder):
|
|
45
|
+
|
|
46
|
+
# Windows PowerShell, UTF-8, password only, no quotes/newline
|
|
47
|
+
New-Item -ItemType Directory -Force C:\Users\you\secrets | Out-Null
|
|
48
|
+
Set-Content -NoNewline -Encoding utf8 C:\Users\you\secrets\mcp_sql.pwd 'your-password'
|
|
49
|
+
|
|
50
|
+
Or skip the password entirely with integrated auth (`MCP_SQL_TRUSTED=yes`) —
|
|
51
|
+
preferred for CJIS/PCI. See **Secret handling** below for all options.
|
|
52
|
+
4. **Configure your MCP client.** Copy the `sql-querystore` block from
|
|
53
|
+
`claude_desktop_config.example.json` into your real Claude Desktop config
|
|
54
|
+
(Windows: `%APPDATA%\Claude\claude_desktop_config.json`), then replace the
|
|
55
|
+
placeholder paths, server name, and `MCP_SQL_PWD_FILE`. Set
|
|
56
|
+
`MCP_SQL_TRUST_CERT=yes` only for a self-signed/local cert; leave it `no`
|
|
57
|
+
against instances with proper certificates.
|
|
58
|
+
5. **Restart your MCP client** and confirm the server shows as running.
|
|
59
|
+
|
|
60
|
+
Never commit your real config or your password file. `.gitignore` already
|
|
61
|
+
excludes `*.pwd`, `.env`, and `claude_desktop_config.json`.
|
|
62
|
+
|
|
63
|
+
## Security model (read this first)
|
|
64
|
+
|
|
65
|
+
The read-only guarantee comes from **the SQL login's permissions**, not from any
|
|
66
|
+
code in this repo:
|
|
67
|
+
|
|
68
|
+
- Provision a dedicated login with `VIEW DATABASE STATE` (and `VIEW SERVER STATE`
|
|
69
|
+
only if you use server-scoped DMVs) and **nothing else** — no `db_datareader`,
|
|
70
|
+
no `SELECT` on user tables. See `provisioning/create_readonly_login.sql`.
|
|
71
|
+
- The keyword screen in `db.py` and the fixed SELECT-only query text are
|
|
72
|
+
**defense-in-depth**, not the primary control.
|
|
73
|
+
- `ApplicationIntent=ReadOnly` in the connection string only routes to a readable
|
|
74
|
+
secondary in an availability group. On a standalone instance it does not make
|
|
75
|
+
the session read-only. Do not rely on it for safety.
|
|
76
|
+
- Credentials never belong in code. The simplest setup uses the
|
|
77
|
+
`MCP_SQL_CONNECTION_STRING` env var, but for CJIS/PCI environments prefer
|
|
78
|
+
integrated auth or a file/secret-store-sourced password — see the
|
|
79
|
+
**Secret handling** section below.
|
|
80
|
+
- Every query is recorded via the audit logger — see **Audit logging** below. In
|
|
81
|
+
a regulated environment, route that logger to a durable file or SIEM.
|
|
82
|
+
|
|
83
|
+
## Setup
|
|
84
|
+
|
|
85
|
+
ODBC Driver 18 for SQL Server must be installed on the host. Install the package,
|
|
86
|
+
then configure the connection via environment variables (see **Secret handling**
|
|
87
|
+
for all options). The recommended form keeps the password in a file, not inline:
|
|
88
|
+
|
|
89
|
+
```powershell
|
|
90
|
+
pip install -e .
|
|
91
|
+
|
|
92
|
+
# PowerShell — connection assembled from parts, password read from a file
|
|
93
|
+
$env:MCP_SQL_SERVER = "yourhost\INSTANCE"
|
|
94
|
+
$env:MCP_SQL_DATABASE = "master"
|
|
95
|
+
$env:MCP_SQL_UID = "mcp_readonly"
|
|
96
|
+
$env:MCP_SQL_PWD_FILE = "C:\path\to\your\secret.pwd"
|
|
97
|
+
$env:MCP_SQL_TRUST_CERT = "no" # "yes" only for a self-signed/local cert
|
|
98
|
+
|
|
99
|
+
python -m mcp_sql_querystore.server
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Or use integrated auth with no stored password at all (`MCP_SQL_TRUSTED=yes`).
|
|
103
|
+
A full `MCP_SQL_CONNECTION_STRING` is also accepted for simple cases — see
|
|
104
|
+
**Secret handling**.
|
|
105
|
+
|
|
106
|
+
Register it with your MCP client (e.g. Claude Desktop) as an stdio server
|
|
107
|
+
invoking `python -m mcp_sql_querystore.server`; see
|
|
108
|
+
`claude_desktop_config.example.json`.
|
|
109
|
+
|
|
110
|
+
## Tools
|
|
111
|
+
|
|
112
|
+
All tools are read-only and take a `database_name` (except `sweep_regressions`,
|
|
113
|
+
which can sweep all databases). Each returns JSON, or a structured error dict on
|
|
114
|
+
failure rather than raising.
|
|
115
|
+
|
|
116
|
+
- **get_regressed_queries** — compares a recent window against an earlier
|
|
117
|
+
baseline window per query and flags those worse by at least
|
|
118
|
+
`regression_threshold`. A real baseline-vs-recent comparison, not a top-CPU list.
|
|
119
|
+
- **get_query_execution_plan** — returns compiled plans for a `query_id` with a
|
|
120
|
+
compact JSON summary (missing indexes, warnings incl. implicit conversions,
|
|
121
|
+
key lookups) and optional raw XML.
|
|
122
|
+
- **analyze_parameter_sniffing** — finds queries with multiple compiled plans and
|
|
123
|
+
ranks them by the ratio of slowest to fastest plan mean duration — the classic
|
|
124
|
+
parameter-sniffing signature.
|
|
125
|
+
- **get_missing_index_impact** — scans recent plans containing missing-index
|
|
126
|
+
recommendations, parses the impact score from the plan XML, and aggregates
|
|
127
|
+
duplicate recommendations across queries, ranked by impact then recurrence.
|
|
128
|
+
- **get_wait_stats** — aggregates query wait time by wait category over a window,
|
|
129
|
+
showing *why* queries are slow (CPU, blocking/locks, IO, memory) rather than
|
|
130
|
+
which. De-duplicates flushed vs in-memory rows per Microsoft guidance.
|
|
131
|
+
- **sweep_regressions** — runs regression detection across all Query Store-enabled
|
|
132
|
+
online databases (or an explicit `database_names` list) and returns the worst
|
|
133
|
+
per database, ranked. One failing database does not abort the sweep; its error
|
|
134
|
+
is collected and reported.
|
|
135
|
+
|
|
136
|
+
## Example prompts
|
|
137
|
+
|
|
138
|
+
Once the server is connected to your MCP client, you drive the tools in plain
|
|
139
|
+
language. Name the target database in the prompt (except `sweep_regressions`,
|
|
140
|
+
which can scan all of them). Replace `YourDB` with your database name.
|
|
141
|
+
|
|
142
|
+
**Wait stats — why queries are slow**
|
|
143
|
+
- "What are the top wait categories in YourDB over the last week?"
|
|
144
|
+
- "Is YourDB waiting on CPU, memory, or IO?"
|
|
145
|
+
- "Show me wait stats for YourDB over the last 24 hours."
|
|
146
|
+
|
|
147
|
+
**Execution plans**
|
|
148
|
+
- "Get the execution plan for query_id 10 in YourDB and summarize it."
|
|
149
|
+
- "Does query_id 13 in YourDB have missing index recommendations?"
|
|
150
|
+
- "Are there implicit conversion warnings in query 12's plan in YourDB?"
|
|
151
|
+
|
|
152
|
+
**Regression analysis**
|
|
153
|
+
- "Check YourDB for CPU regressions over the last 24 hours."
|
|
154
|
+
- "Which queries in YourDB regressed by more than 30%?"
|
|
155
|
+
- "Find duration regressions in YourDB, ignoring anything with fewer than 10 executions."
|
|
156
|
+
|
|
157
|
+
**Parameter sniffing**
|
|
158
|
+
- "Check YourDB for parameter sniffing."
|
|
159
|
+
- "Which queries in YourDB have unstable plans?"
|
|
160
|
+
|
|
161
|
+
**Missing indexes**
|
|
162
|
+
- "What missing indexes does YourDB need most?"
|
|
163
|
+
- "Show me the top 10 index recommendations for YourDB by impact."
|
|
164
|
+
|
|
165
|
+
**Multi-database sweep (no database name needed)**
|
|
166
|
+
- "Sweep all my databases for CPU regressions."
|
|
167
|
+
- "Which database has the worst regressions this week?"
|
|
168
|
+
|
|
169
|
+
**Combined — chaining tools in one turn**
|
|
170
|
+
- "Find the biggest CPU regression in YourDB, pull its plan, and tell me why it might have regressed."
|
|
171
|
+
- "YourDB feels slow — diagnose it." (wait stats → regressions → plans)
|
|
172
|
+
- "Full performance triage of YourDB: wait stats, top regressions, and missing indexes."
|
|
173
|
+
|
|
174
|
+
## Known caveats / TODO
|
|
175
|
+
|
|
176
|
+
- **Version differences.** Query Store column names assume SQL Server 2019+/2022
|
|
177
|
+
and Azure SQL MI. Verify against 2016/2017 if you target those.
|
|
178
|
+
- **Regression semantics.** Current logic uses execution-weighted averages. You
|
|
179
|
+
may prefer percentile-based comparison (Query Store doesn't store percentiles
|
|
180
|
+
directly, so that needs `*_stdev` columns and assumptions).
|
|
181
|
+
- **Not time-windowed:** `analyze_parameter_sniffing` aggregates across all Query
|
|
182
|
+
Store history; on busy databases consider adding a `recent_hours` filter like
|
|
183
|
+
the other tools have.
|
|
184
|
+
- **Remaining hardening:** connection retry with backoff, and version-aware column
|
|
185
|
+
handling for mixed 2016/2017/2019/2022 fleets.
|
|
186
|
+
|
|
187
|
+
## Secret handling
|
|
188
|
+
|
|
189
|
+
The connection string is resolved in this order, so the password need not sit in
|
|
190
|
+
plaintext config:
|
|
191
|
+
|
|
192
|
+
1. `MCP_SQL_CONNECTION_STRING` — the full string (simplest; back-compat).
|
|
193
|
+
2. `MCP_SQL_CONNECTION_STRING_FILE` — path to a file holding the full string
|
|
194
|
+
(Docker/K8s secret-mount style).
|
|
195
|
+
3. Assembled from parts: `MCP_SQL_SERVER` (+ `MCP_SQL_DATABASE`, `MCP_SQL_DRIVER`,
|
|
196
|
+
`MCP_SQL_ENCRYPT`, `MCP_SQL_TRUST_CERT`, `MCP_SQL_EXTRA`). Auth is either:
|
|
197
|
+
- **Integrated** (preferred for CJIS/PCI — no password stored): `MCP_SQL_TRUSTED=yes`.
|
|
198
|
+
- **SQL auth**: `MCP_SQL_UID` plus the password from `MCP_SQL_PWD_FILE` (a
|
|
199
|
+
vault-mounted file), `MCP_SQL_PWD_ENV` (name of another env var), or
|
|
200
|
+
`MCP_SQL_PWD` (direct; least preferred).
|
|
201
|
+
|
|
202
|
+
Timeouts: `MCP_SQL_CONNECT_TIMEOUT` (default 10s) and `MCP_SQL_QUERY_TIMEOUT`
|
|
203
|
+
(default 30s, 0 disables).
|
|
204
|
+
|
|
205
|
+
## Audit logging
|
|
206
|
+
|
|
207
|
+
Every query attempt is logged via the `mcp_sql_querystore.audit` logger: tool,
|
|
208
|
+
database, a 12-char hash of the SQL (not the text), row count, elapsed ms, and
|
|
209
|
+
outcome. Connection strings, SQL text, and parameter values are never logged.
|
|
210
|
+
Configure a handler for that logger to route the audit trail to a file or SIEM.
|
|
211
|
+
|
|
212
|
+
## Testing
|
|
213
|
+
|
|
214
|
+
Unit tests (no database, safe in CI):
|
|
215
|
+
|
|
216
|
+
pip install -e ".[test]"
|
|
217
|
+
pytest
|
|
218
|
+
|
|
219
|
+
Integration tests (real instance, opt-in):
|
|
220
|
+
|
|
221
|
+
# set a working connection (any form above), then:
|
|
222
|
+
$env:MCP_SQL_TEST_DATABASE = "RAG"
|
|
223
|
+
$env:MCP_SQL_RUN_INTEGRATION = "1"
|
|
224
|
+
pytest tests/test_integration.py -v
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
# mcp-sql-querystore
|
|
2
|
+
|
|
3
|
+
Read-only MCP server exposing SQL Server Query Store diagnostics to LLM agents.
|
|
4
|
+
|
|
5
|
+
Six read-only diagnostic tools over Query Store, DMVs, and execution plans. The
|
|
6
|
+
tools have been validated against a live SQL Server instance and are covered by a
|
|
7
|
+
unit + integration test suite. Still validate against a non-prod instance of your
|
|
8
|
+
own before pointing it at production, especially on SQL Server versions other than
|
|
9
|
+
those noted under caveats.
|
|
10
|
+
|
|
11
|
+
## Quickstart
|
|
12
|
+
|
|
13
|
+
1. **Provision a read-only login.** Run `provisioning/create_readonly_login.sql`
|
|
14
|
+
against your instance (edit names first). This login's permissions are the
|
|
15
|
+
read-only guarantee — see the security model below.
|
|
16
|
+
2. **Install.** `pip install -e .` in a virtual environment. ODBC Driver 18 for
|
|
17
|
+
SQL Server must be installed on the host.
|
|
18
|
+
3. **Store the password outside the repo.** Put it in a plain-text file somewhere
|
|
19
|
+
the repo can't reach (not under the project folder):
|
|
20
|
+
|
|
21
|
+
# Windows PowerShell, UTF-8, password only, no quotes/newline
|
|
22
|
+
New-Item -ItemType Directory -Force C:\Users\you\secrets | Out-Null
|
|
23
|
+
Set-Content -NoNewline -Encoding utf8 C:\Users\you\secrets\mcp_sql.pwd 'your-password'
|
|
24
|
+
|
|
25
|
+
Or skip the password entirely with integrated auth (`MCP_SQL_TRUSTED=yes`) —
|
|
26
|
+
preferred for CJIS/PCI. See **Secret handling** below for all options.
|
|
27
|
+
4. **Configure your MCP client.** Copy the `sql-querystore` block from
|
|
28
|
+
`claude_desktop_config.example.json` into your real Claude Desktop config
|
|
29
|
+
(Windows: `%APPDATA%\Claude\claude_desktop_config.json`), then replace the
|
|
30
|
+
placeholder paths, server name, and `MCP_SQL_PWD_FILE`. Set
|
|
31
|
+
`MCP_SQL_TRUST_CERT=yes` only for a self-signed/local cert; leave it `no`
|
|
32
|
+
against instances with proper certificates.
|
|
33
|
+
5. **Restart your MCP client** and confirm the server shows as running.
|
|
34
|
+
|
|
35
|
+
Never commit your real config or your password file. `.gitignore` already
|
|
36
|
+
excludes `*.pwd`, `.env`, and `claude_desktop_config.json`.
|
|
37
|
+
|
|
38
|
+
## Security model (read this first)
|
|
39
|
+
|
|
40
|
+
The read-only guarantee comes from **the SQL login's permissions**, not from any
|
|
41
|
+
code in this repo:
|
|
42
|
+
|
|
43
|
+
- Provision a dedicated login with `VIEW DATABASE STATE` (and `VIEW SERVER STATE`
|
|
44
|
+
only if you use server-scoped DMVs) and **nothing else** — no `db_datareader`,
|
|
45
|
+
no `SELECT` on user tables. See `provisioning/create_readonly_login.sql`.
|
|
46
|
+
- The keyword screen in `db.py` and the fixed SELECT-only query text are
|
|
47
|
+
**defense-in-depth**, not the primary control.
|
|
48
|
+
- `ApplicationIntent=ReadOnly` in the connection string only routes to a readable
|
|
49
|
+
secondary in an availability group. On a standalone instance it does not make
|
|
50
|
+
the session read-only. Do not rely on it for safety.
|
|
51
|
+
- Credentials never belong in code. The simplest setup uses the
|
|
52
|
+
`MCP_SQL_CONNECTION_STRING` env var, but for CJIS/PCI environments prefer
|
|
53
|
+
integrated auth or a file/secret-store-sourced password — see the
|
|
54
|
+
**Secret handling** section below.
|
|
55
|
+
- Every query is recorded via the audit logger — see **Audit logging** below. In
|
|
56
|
+
a regulated environment, route that logger to a durable file or SIEM.
|
|
57
|
+
|
|
58
|
+
## Setup
|
|
59
|
+
|
|
60
|
+
ODBC Driver 18 for SQL Server must be installed on the host. Install the package,
|
|
61
|
+
then configure the connection via environment variables (see **Secret handling**
|
|
62
|
+
for all options). The recommended form keeps the password in a file, not inline:
|
|
63
|
+
|
|
64
|
+
```powershell
|
|
65
|
+
pip install -e .
|
|
66
|
+
|
|
67
|
+
# PowerShell — connection assembled from parts, password read from a file
|
|
68
|
+
$env:MCP_SQL_SERVER = "yourhost\INSTANCE"
|
|
69
|
+
$env:MCP_SQL_DATABASE = "master"
|
|
70
|
+
$env:MCP_SQL_UID = "mcp_readonly"
|
|
71
|
+
$env:MCP_SQL_PWD_FILE = "C:\path\to\your\secret.pwd"
|
|
72
|
+
$env:MCP_SQL_TRUST_CERT = "no" # "yes" only for a self-signed/local cert
|
|
73
|
+
|
|
74
|
+
python -m mcp_sql_querystore.server
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Or use integrated auth with no stored password at all (`MCP_SQL_TRUSTED=yes`).
|
|
78
|
+
A full `MCP_SQL_CONNECTION_STRING` is also accepted for simple cases — see
|
|
79
|
+
**Secret handling**.
|
|
80
|
+
|
|
81
|
+
Register it with your MCP client (e.g. Claude Desktop) as an stdio server
|
|
82
|
+
invoking `python -m mcp_sql_querystore.server`; see
|
|
83
|
+
`claude_desktop_config.example.json`.
|
|
84
|
+
|
|
85
|
+
## Tools
|
|
86
|
+
|
|
87
|
+
All tools are read-only and take a `database_name` (except `sweep_regressions`,
|
|
88
|
+
which can sweep all databases). Each returns JSON, or a structured error dict on
|
|
89
|
+
failure rather than raising.
|
|
90
|
+
|
|
91
|
+
- **get_regressed_queries** — compares a recent window against an earlier
|
|
92
|
+
baseline window per query and flags those worse by at least
|
|
93
|
+
`regression_threshold`. A real baseline-vs-recent comparison, not a top-CPU list.
|
|
94
|
+
- **get_query_execution_plan** — returns compiled plans for a `query_id` with a
|
|
95
|
+
compact JSON summary (missing indexes, warnings incl. implicit conversions,
|
|
96
|
+
key lookups) and optional raw XML.
|
|
97
|
+
- **analyze_parameter_sniffing** — finds queries with multiple compiled plans and
|
|
98
|
+
ranks them by the ratio of slowest to fastest plan mean duration — the classic
|
|
99
|
+
parameter-sniffing signature.
|
|
100
|
+
- **get_missing_index_impact** — scans recent plans containing missing-index
|
|
101
|
+
recommendations, parses the impact score from the plan XML, and aggregates
|
|
102
|
+
duplicate recommendations across queries, ranked by impact then recurrence.
|
|
103
|
+
- **get_wait_stats** — aggregates query wait time by wait category over a window,
|
|
104
|
+
showing *why* queries are slow (CPU, blocking/locks, IO, memory) rather than
|
|
105
|
+
which. De-duplicates flushed vs in-memory rows per Microsoft guidance.
|
|
106
|
+
- **sweep_regressions** — runs regression detection across all Query Store-enabled
|
|
107
|
+
online databases (or an explicit `database_names` list) and returns the worst
|
|
108
|
+
per database, ranked. One failing database does not abort the sweep; its error
|
|
109
|
+
is collected and reported.
|
|
110
|
+
|
|
111
|
+
## Example prompts
|
|
112
|
+
|
|
113
|
+
Once the server is connected to your MCP client, you drive the tools in plain
|
|
114
|
+
language. Name the target database in the prompt (except `sweep_regressions`,
|
|
115
|
+
which can scan all of them). Replace `YourDB` with your database name.
|
|
116
|
+
|
|
117
|
+
**Wait stats — why queries are slow**
|
|
118
|
+
- "What are the top wait categories in YourDB over the last week?"
|
|
119
|
+
- "Is YourDB waiting on CPU, memory, or IO?"
|
|
120
|
+
- "Show me wait stats for YourDB over the last 24 hours."
|
|
121
|
+
|
|
122
|
+
**Execution plans**
|
|
123
|
+
- "Get the execution plan for query_id 10 in YourDB and summarize it."
|
|
124
|
+
- "Does query_id 13 in YourDB have missing index recommendations?"
|
|
125
|
+
- "Are there implicit conversion warnings in query 12's plan in YourDB?"
|
|
126
|
+
|
|
127
|
+
**Regression analysis**
|
|
128
|
+
- "Check YourDB for CPU regressions over the last 24 hours."
|
|
129
|
+
- "Which queries in YourDB regressed by more than 30%?"
|
|
130
|
+
- "Find duration regressions in YourDB, ignoring anything with fewer than 10 executions."
|
|
131
|
+
|
|
132
|
+
**Parameter sniffing**
|
|
133
|
+
- "Check YourDB for parameter sniffing."
|
|
134
|
+
- "Which queries in YourDB have unstable plans?"
|
|
135
|
+
|
|
136
|
+
**Missing indexes**
|
|
137
|
+
- "What missing indexes does YourDB need most?"
|
|
138
|
+
- "Show me the top 10 index recommendations for YourDB by impact."
|
|
139
|
+
|
|
140
|
+
**Multi-database sweep (no database name needed)**
|
|
141
|
+
- "Sweep all my databases for CPU regressions."
|
|
142
|
+
- "Which database has the worst regressions this week?"
|
|
143
|
+
|
|
144
|
+
**Combined — chaining tools in one turn**
|
|
145
|
+
- "Find the biggest CPU regression in YourDB, pull its plan, and tell me why it might have regressed."
|
|
146
|
+
- "YourDB feels slow — diagnose it." (wait stats → regressions → plans)
|
|
147
|
+
- "Full performance triage of YourDB: wait stats, top regressions, and missing indexes."
|
|
148
|
+
|
|
149
|
+
## Known caveats / TODO
|
|
150
|
+
|
|
151
|
+
- **Version differences.** Query Store column names assume SQL Server 2019+/2022
|
|
152
|
+
and Azure SQL MI. Verify against 2016/2017 if you target those.
|
|
153
|
+
- **Regression semantics.** Current logic uses execution-weighted averages. You
|
|
154
|
+
may prefer percentile-based comparison (Query Store doesn't store percentiles
|
|
155
|
+
directly, so that needs `*_stdev` columns and assumptions).
|
|
156
|
+
- **Not time-windowed:** `analyze_parameter_sniffing` aggregates across all Query
|
|
157
|
+
Store history; on busy databases consider adding a `recent_hours` filter like
|
|
158
|
+
the other tools have.
|
|
159
|
+
- **Remaining hardening:** connection retry with backoff, and version-aware column
|
|
160
|
+
handling for mixed 2016/2017/2019/2022 fleets.
|
|
161
|
+
|
|
162
|
+
## Secret handling
|
|
163
|
+
|
|
164
|
+
The connection string is resolved in this order, so the password need not sit in
|
|
165
|
+
plaintext config:
|
|
166
|
+
|
|
167
|
+
1. `MCP_SQL_CONNECTION_STRING` — the full string (simplest; back-compat).
|
|
168
|
+
2. `MCP_SQL_CONNECTION_STRING_FILE` — path to a file holding the full string
|
|
169
|
+
(Docker/K8s secret-mount style).
|
|
170
|
+
3. Assembled from parts: `MCP_SQL_SERVER` (+ `MCP_SQL_DATABASE`, `MCP_SQL_DRIVER`,
|
|
171
|
+
`MCP_SQL_ENCRYPT`, `MCP_SQL_TRUST_CERT`, `MCP_SQL_EXTRA`). Auth is either:
|
|
172
|
+
- **Integrated** (preferred for CJIS/PCI — no password stored): `MCP_SQL_TRUSTED=yes`.
|
|
173
|
+
- **SQL auth**: `MCP_SQL_UID` plus the password from `MCP_SQL_PWD_FILE` (a
|
|
174
|
+
vault-mounted file), `MCP_SQL_PWD_ENV` (name of another env var), or
|
|
175
|
+
`MCP_SQL_PWD` (direct; least preferred).
|
|
176
|
+
|
|
177
|
+
Timeouts: `MCP_SQL_CONNECT_TIMEOUT` (default 10s) and `MCP_SQL_QUERY_TIMEOUT`
|
|
178
|
+
(default 30s, 0 disables).
|
|
179
|
+
|
|
180
|
+
## Audit logging
|
|
181
|
+
|
|
182
|
+
Every query attempt is logged via the `mcp_sql_querystore.audit` logger: tool,
|
|
183
|
+
database, a 12-char hash of the SQL (not the text), row count, elapsed ms, and
|
|
184
|
+
outcome. Connection strings, SQL text, and parameter values are never logged.
|
|
185
|
+
Configure a handler for that logger to route the audit trail to a file or SIEM.
|
|
186
|
+
|
|
187
|
+
## Testing
|
|
188
|
+
|
|
189
|
+
Unit tests (no database, safe in CI):
|
|
190
|
+
|
|
191
|
+
pip install -e ".[test]"
|
|
192
|
+
pytest
|
|
193
|
+
|
|
194
|
+
Integration tests (real instance, opt-in):
|
|
195
|
+
|
|
196
|
+
# set a working connection (any form above), then:
|
|
197
|
+
$env:MCP_SQL_TEST_DATABASE = "RAG"
|
|
198
|
+
$env:MCP_SQL_RUN_INTEGRATION = "1"
|
|
199
|
+
pytest tests/test_integration.py -v
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"_comment": "Copy the sql-querystore block into your real Claude Desktop config (on Windows: %APPDATA%\\Claude\\claude_desktop_config.json). Replace every placeholder. Do NOT commit your real config or password. See the Quickstart in README.md.",
|
|
3
|
+
"mcpServers": {
|
|
4
|
+
"sql-querystore": {
|
|
5
|
+
"command": "C:\\path\\to\\mcp-sql-querystore\\.venv\\Scripts\\python.exe",
|
|
6
|
+
"args": ["-m", "mcp_sql_querystore.server"],
|
|
7
|
+
"env": {
|
|
8
|
+
"MCP_SQL_SERVER": "YOUR_SERVER\\INSTANCE",
|
|
9
|
+
"MCP_SQL_DATABASE": "master",
|
|
10
|
+
"MCP_SQL_UID": "mcp_readonly",
|
|
11
|
+
"MCP_SQL_PWD_FILE": "C:\\path\\to\\your\\secret.pwd",
|
|
12
|
+
"MCP_SQL_TRUST_CERT": "no"
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
-- Provision a dedicated least-privilege login for mcp-sql-querystore.
|
|
2
|
+
-- This login's permissions ARE the read-only guarantee. Run once per instance,
|
|
3
|
+
-- then run the per-database section against each DB you want to inspect.
|
|
4
|
+
--
|
|
5
|
+
-- Adjust names, and prefer a strong password from your secret store or use a
|
|
6
|
+
-- contained/AAD login on Azure SQL MI.
|
|
7
|
+
|
|
8
|
+
-------------------------------------------------------------------------------
|
|
9
|
+
-- 1. Server-level login (SQL auth shown; swap for Windows/AAD as appropriate)
|
|
10
|
+
-------------------------------------------------------------------------------
|
|
11
|
+
USE [master];
|
|
12
|
+
GO
|
|
13
|
+
IF NOT EXISTS (SELECT 1 FROM sys.server_principals WHERE name = N'mcp_readonly')
|
|
14
|
+
BEGIN
|
|
15
|
+
CREATE LOGIN [mcp_readonly] WITH PASSWORD = N'<set-from-secret-store>',
|
|
16
|
+
CHECK_POLICY = ON;
|
|
17
|
+
END
|
|
18
|
+
GO
|
|
19
|
+
|
|
20
|
+
-- Needed for server-scoped DMVs (sys.dm_exec_*). VIEW SERVER STATE is broad;
|
|
21
|
+
-- if you only use DB-scoped Query Store views you can rely on VIEW DATABASE
|
|
22
|
+
-- STATE per database instead and skip this grant.
|
|
23
|
+
GRANT VIEW SERVER STATE TO [mcp_readonly];
|
|
24
|
+
GO
|
|
25
|
+
|
|
26
|
+
-------------------------------------------------------------------------------
|
|
27
|
+
-- 2. Per-database: run this block in EACH database to be inspected
|
|
28
|
+
-------------------------------------------------------------------------------
|
|
29
|
+
-- USE [YourDatabase];
|
|
30
|
+
-- GO
|
|
31
|
+
-- CREATE USER [mcp_readonly] FOR LOGIN [mcp_readonly];
|
|
32
|
+
-- GO
|
|
33
|
+
-- GRANT VIEW DATABASE STATE TO [mcp_readonly]; -- Query Store + DB DMVs
|
|
34
|
+
-- GO
|
|
35
|
+
--
|
|
36
|
+
-- Deliberately NOT granted: db_datareader, SELECT on user tables, any write role.
|
|
37
|
+
-- The server only reads sys.query_store_* and sys.dm_* which VIEW DATABASE STATE
|
|
38
|
+
-- (plus VIEW SERVER STATE for server DMVs) covers.
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "mcp-sql-querystore"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Read-only MCP server for SQL Server performance diagnostics over Query Store, DMVs, and execution plans"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.10"
|
|
7
|
+
license = { text = "MIT" }
|
|
8
|
+
authors = [{ name = "Deepesh Dhake" }]
|
|
9
|
+
keywords = [
|
|
10
|
+
"mcp", "model-context-protocol", "sql-server", "mssql",
|
|
11
|
+
"query-store", "dba", "performance-tuning", "database",
|
|
12
|
+
]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 4 - Beta",
|
|
15
|
+
"Intended Audience :: Developers",
|
|
16
|
+
"Intended Audience :: System Administrators",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.10",
|
|
20
|
+
"Topic :: Database",
|
|
21
|
+
"Topic :: System :: Monitoring",
|
|
22
|
+
]
|
|
23
|
+
dependencies = [
|
|
24
|
+
"mcp>=1.2.0,<2",
|
|
25
|
+
"pyodbc>=5.1.0",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[project.urls]
|
|
29
|
+
Homepage = "https://github.com/deepeshd87/mcp-sql-querystore"
|
|
30
|
+
Repository = "https://github.com/deepeshd87/mcp-sql-querystore"
|
|
31
|
+
Issues = "https://github.com/deepeshd87/mcp-sql-querystore/issues"
|
|
32
|
+
|
|
33
|
+
[project.scripts]
|
|
34
|
+
mcp-sql-querystore = "mcp_sql_querystore.server:run"
|
|
35
|
+
|
|
36
|
+
[build-system]
|
|
37
|
+
requires = ["hatchling"]
|
|
38
|
+
build-backend = "hatchling.build"
|
|
39
|
+
|
|
40
|
+
[tool.hatch.build.targets.wheel]
|
|
41
|
+
packages = ["src/mcp_sql_querystore"]
|
|
42
|
+
|
|
43
|
+
[project.optional-dependencies]
|
|
44
|
+
test = ["pytest>=8.0"]
|
|
45
|
+
|
|
46
|
+
[tool.pytest.ini_options]
|
|
47
|
+
testpaths = ["tests"]
|