DatabaseMcpServer CLI
Drive DatabaseMcpServer from the shell safely and repeatably.
Mental Model
DatabaseMcpServer CLI has two state layers. Confusing them is the most common source of wasted time.
- Config file (
databases.json): a static, human-edited list of named connections plus anisDefaultflag. Changed byconfig add / update / remove / use / clone / rename / import. - CLI current-connection state (
%USERPROFILE%/.database-mcp/cli-state.json): a dynamic per-session pointer remembered acrosstoolinvocations. Changed bytool switch_database. Keyed by the resolved config path so two different config files keep independent current connections.
Consequences worth internalizing:
tool switch_databasedoes not rewritedatabases.json. Users who expect "switch" to persist into the config file will be surprised; point them atconfig useinstead.- A fresh
toolcall reads cli-state first. It only falls back toisDefaultwhen no state file exists or the saved name was removed from the config. tool reload_database_configreloads the config file but deliberately preserves the current connection when possible — this is a feature, not a bug.
When the user is confused about "which database am I actually on", run tool get_current_database and compare against config list / config show rather than trusting assumptions.
Start Here
- Confirm the user wants CLI behavior, not MCP stdio transport. If the intent is "connect Claude Desktop / Cursor / Cline to this server", CLI is the wrong answer.
DatabaseMcpServeris distributed as a .NET Global Tool. The user should have it on PATH via:
dotnet tool install --global DatabaseMcpServer
dotnet tool list --global | Select-String databasemcpserver
- Verify the installed version via
dotnet tool list --global, not via the binary. The CLI itself does not accept--version— it exits2with未知命令: '--version'. The .NET tool manifest is authoritative. - If the installed version is stale or the tool misbehaves, upgrade before debugging:
dotnet tool update --global DatabaseMcpServer
- If the
DatabaseMcpServercommand isn't found,%USERPROFILE%\.dotnet\tools(or the platform equivalent) is missing fromPATH— tell the user to add it rather than trying to invoke a binary by absolute path.
Command Families
| Family | Purpose | Typical use |
|---|---|---|
DatabaseMcpServer -web |
Start the localhost-only configuration UI. | Browser-based config maintenance, visual inspection, import/export |
DatabaseMcpServer tool <name> |
Invoke one MCP tool. Real work happens here. | Query, schema, DDL, export, health |
DatabaseMcpServer config <subcommand> |
Create, inspect, validate, import, export, repair connections inside a config file. | First-time setup, maintenance |
DatabaseMcpServer init |
Seed a default config file skeleton. | Bootstrapping a machine |
tool list / tool help <name> / config help <subcommand> / --help |
Discover exact parameter names before guessing. | Whenever naming is unclear |
Naming convention is strict: tool names are snake_case, options are kebab-case. When the CLI prints "最接近的命令" suggestions, prefer one of them — it means you got the naming form wrong.
Handle Config Deliberately
- Prefer explicit
--config <path>when the user has a target file. It makes behavior deterministic and keeps cli-state scoped correctly. - If the user only gives a connection string, create a one-entry
databases.jsonunder%TEMP%rather than editing their real config. - Keep secrets out of repo-tracked files unless the user explicitly asks to persist them.
- Delete temporary configs after testing if they contain credentials.
- For repeatable local usage, move the validated config to a stable path before handing it off.
Minimal ad-hoc config shape:
{
"databases": [
{
"name": "temp-db",
"connectionString": "<user-connection-string>",
"dbType": "<MySql|PostgreSQL|SqlServer|Sqlite|...>",
"description": "temporary cli test",
"isDefault": true
}
]
}
Config resolution rules (memorize or re-derive from help):
toolwithout--configsearches:./databases.json→./local-databases.json→DB_CONFIG_PATH→%USERPROFILE%/.database-mcp/databases.json.initandconfigdefault to%USERPROFILE%/.database-mcp/databases.jsonunless--configis supplied.-webuses the same discovery order astoolwhen locating an existing config. If nothing exists yet, it still starts and treats%USERPROFILE%/.database-mcp/databases.jsonas the writable target.-webis localhost-only by design. It is not the right answer for “publish a remote admin console” or “let another machine manage my config”.
Operating Principles
These are ordered from low to high risk. Don't skip ahead without a reason.
Read-only first
Establishing trust before touching anything saves retries later:
validate_configurationtest_connection(ortest_connection_by_namefor a specific connection)get_database_configlist_databasesandget_current_databasewhen connection identity mattershealth_checkwhen broader connectivity matters
Schema before SQL
Guessing table or column names produces wrong SQL or silent mismatches. Before querying or changing objects:
is_any_tableorget_table_info_listget_table_schemaorget_column_infos_by_table_nameget_primaries,get_index_list, orget_trigger_nameswhen relevant
Isolate writes and DDL
Write tools can damage real data, so isolate them:
- Never exercise write or schema tools on business tables unless the user explicitly asks.
- For verification loops, create uniquely prefixed temporary objects:
cli_<yyyyMMdd_HHmmss>_<shortid>. - Clean up created objects before finishing, and report cleanup status.
- Without explicit approval for writes, stop at read-only validation or deliver a plan only.
Respect the CLI Contract
Classifying a failure correctly is half the diagnosis. The contract:
- Successful tool calls write their payload to
stdoutas JSON. - Help text and CLI usage errors go to
stderr(this includes root help andtool list— don't misclassify). - Exit code
0: success. Parse stdout JSON. - Exit code
1: tool ran and returned a structured failure — usuallysuccess: false. Backend / database / permission / capability issues live here. - Exit code
2: CLI usage failure. Missing--yes, missing option, unknown option, malformed JSON argument, quoting bug, config not found. get_database_configcan return valid JSON on exit0without a top-levelsuccessfield. Treatexit 0 + parseable JSONas success.
When triaging, first ask: did the tool even run? Exit 2 + stderr usage hint = it didn't, fix the command. Exit 1 + success: false = it did, look at the backend.
Use --yes Correctly
--yes is an explicit acknowledgement that you accept the side effect. The CLI requires it for anything that mutates rows or schema:
- DML and batch execution
- Stored procedure calls (input-only and with-output)
- Table, column, index, constraint, remark, and default-value changes
- Drop, truncate, rename, backup, and similar destructive operations
config remove
If the CLI prints 需要显式确认。请追加 '--yes'。, the exit code is 2 and the diagnosis is CLI-layer: add --yes, don't chase a backend problem.
Quote PowerShell Arguments Carefully
PowerShell quoting eats more debugging time than the database ever will. Guardrails:
- Wrap SQL and JSON arguments in single quotes. That disables PowerShell variable expansion and preserves literal content.
- Keep each JSON object or array as one argument — never split them across multiple
--fooflags. - For
batch_execute_commands, pass one JSON array string:'["cmd1","cmd2"]'. - For
execute_command_with_go, pass one SQL argument containing embedded newlines andGO. - For SQL Server
add_default_value, the tool expects a SQL literal, not bare text. Escape the inner quotes:--default-value '''active'''(outer single quotes + doubled inner quotes). - For scripted automation, prefer
ProcessStartInfo.ArgumentListover composing a shell string — it sidesteps quoting entirely.
Don't Weaken Security Quietly
Some CLI "fixes" work by downgrading the connection's security posture. Downgrades need explicit, informed consent — urgency is not consent.
When SQL Server connections fail with Encrypt=True and the error says encryption is required but unsupported on the machine:
- Report the exact error first. The wording is misleading — on Microsoft.Data.SqlClient 4.0+ this is almost always a TLS 1.2 or server-cert-chain problem on the client, not literal "no crypto support".
- Don't silently swap in
Encrypt=False;TrustServerCertificate=True. That hides a real infrastructure problem and downgrades security. - Offer ranked options, not a single fix. Lead with the safest:
- Fix the client: enable TLS 1.2 in SCHANNEL, install the server's CA cert, update OS/driver.
- Keep encryption, skip cert check:
Encrypt=True;TrustServerCertificate=True. Traffic still encrypted; only the server identity check is skipped. Acceptable on trusted intranet. - Disable encryption entirely:
Encrypt=False. Diagnostic fallback only — requires explicit consent and shouldn't land in the user's real config.
- Keep the tradeoff visible. If the user says "just make it work", surface what they're giving up before changing the string. A temp config under
%TEMP%is a safer diagnostic vehicle than editing their realdatabases.json.
The same pattern generalizes: if the next CLI quirk has a "silently downgrade" shortcut, rank by safety, name the tradeoff, get consent.
Reference Index
Three bundled reference files, always available inside the skill folder. Route to the right one based on what the user is asking:
| User's question or symptom | Read first |
|---|---|
"How do I open the browser config page?" / "-web 怎么用?" |
references/cli.md §1.1 + this SKILL.md Command Families |
| "What's the exact command / option to X?" (syntax lookup) | references/cli.md §4 (config) / §8 (tool catalog) |
| "Walk me through installing and getting started." | references/cli.md §2 用户使用流程 |
"My PowerShell command has weird quoting" or "why does add_default_value fail" |
references/commands.md |
"CLI returned this error / exit 1 / exit 2 / 需要显式确认 / --port invalid" |
references/troubleshooting.md |
"switch_database didn't change my default" or "which DB am I actually on" |
Stay in this SKILL.md Mental Model + references/troubleshooting.md #10 |
| "Is this a CLI bug or a backend issue?" | references/troubleshooting.md Quick Triage table |
File summaries:
references/commands.md— PowerShell quoting gotchas, the single-quote convention,add_default_valueSQL-literal escape,execute_command_with_gonewline trick, broad verification workflow, config-vs-current-connection summary.references/troubleshooting.md— symptom → diagnosis matrix for--yes, unknown command, config not found, invalid--port, stdout/stderr mix, JSON argument parsing, SQL Server quoting, encryption,success:falsevs CLI failure, current-vs-default confusion, andreload_database_configsemantics.references/cli.md— the authoritative CLI spec mirrored from the source repo'sDoc/cli.md. §1.1 covers-web; §2 is the end-to-end user lifecycle; §3.6 covers the current-vs-default state model; §4 is config management; §6 is PowerShell traps; §7 lists every--yes-required command; §8 is the full tool catalog. When the skill lives inside the source repo, the upstreamDoc/cli.mdis canonical — keep this copy in sync if the repo file changes.
Report Results Clearly
A verification or troubleshooting report is useful only if someone else can reproduce it. Use this exact structure when writing up a CLI run:
**Executable**: `DatabaseMcpServer` on PATH, version <from `dotnet tool list --global`>
**Config**: <absolute path>, source: <--config flag | DB_CONFIG_PATH | ./databases.json | ./local-databases.json | %USERPROFILE% default>
**Current connection**: <name from get_current_database> (default in databases.json: <name>)
**Command**: <exact argv, one space per argument>
**Exit code**: <0 | 1 | 2>
**Stdout** (trimmed):
<JSON payload, or "(empty)">
**Stderr** (trimmed):
<help / warning / "(empty)">
**Classification**: <CLI-layer usage error | backend/tool failure (success: false) | success>
**Cleanup**: <temp config deleted at <path> | temp DB objects dropped: <list> | none required>
Fill every field, even with (empty) or none required. A blank field reads as "I forgot to check" and forces the reader to guess.
Source: ttcc666/DatabaseMcpServer — distributed by TomeVault.