mssql

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

MSSQL Read-Only Query Skill

MSSQL 只读查询技能

Execute safe, read-only queries against configured Microsoft SQL Server databases.
针对已配置的Microsoft SQL Server数据库执行安全的只读查询。

Requirements

要求

  • Python 3.8+
  • pymssql:
    pip install -r requirements.txt
  • Python 3.8+
  • pymssql:
    pip install -r requirements.txt

Setup

配置

Create
connections.json
in the skill directory or
~/.config/claude/mssql-connections.json
.
Security: Set file permissions to
600
since it contains credentials:
bash
chmod 600 connections.json
json
{
  "databases": [
    {
      "name": "production",
      "description": "Main app database - users, orders, transactions",
      "host": "db.example.com",
      "port": 1433,
      "database": "app_prod",
      "user": "readonly_user",
      "password": "your-password",
      "encrypt": true,
      "tds_version": "7.3"
    }
  ]
}
在技能目录或
~/.config/claude/mssql-connections.json
中创建
connections.json
文件。
安全提示:由于文件包含凭据,请将文件权限设置为
600
bash
chmod 600 connections.json
json
{
  "databases": [
    {
      "name": "production",
      "description": "Main app database - users, orders, transactions",
      "host": "db.example.com",
      "port": 1433,
      "database": "app_prod",
      "user": "readonly_user",
      "password": "your-password",
      "encrypt": true,
      "tds_version": "7.3"
    }
  ]
}

Config Fields

配置字段

FieldRequiredDescription
nameYesIdentifier for the database (case-insensitive)
descriptionYesWhat data this database contains (used for auto-selection)
hostYesDatabase hostname
portNoPort number (default: 1433)
databaseYesDatabase name
userYesUsername
passwordYesPassword
encryptNoEnable TLS encryption (default: false)
tds_versionNoTDS protocol version: 7.0, 7.1, 7.2, 7.3, 7.4 (default: auto)
字段必填描述
name数据库标识符(不区分大小写)
description该数据库包含的数据内容(用于自动选择)
host数据库主机名
port端口号(默认:1433)
database数据库名称
user用户名
password密码
encrypt启用TLS加密(默认:false)
tds_versionTDS协议版本:7.0、7.1、7.2、7.3、7.4(默认:自动)

Usage

使用方法

List configured databases

列出已配置的数据库

bash
python3 scripts/query.py --list
bash
python3 scripts/query.py --list

Query a database

查询数据库

bash
python3 scripts/query.py --db production --query "SELECT TOP 10 * FROM users"
bash
python3 scripts/query.py --db production --query "SELECT TOP 10 * FROM users"

List tables

列出表

bash
python3 scripts/query.py --db production --tables
bash
python3 scripts/query.py --db production --tables

Show schema

查看架构

bash
python3 scripts/query.py --db production --schema
bash
python3 scripts/query.py --db production --schema

Limit results

限制结果数量

bash
python3 scripts/query.py --db production --query "SELECT * FROM orders" --limit 100
Note: MSSQL uses
TOP N
instead of
LIMIT
. The
--limit
flag automatically inserts
TOP N
after SELECT.
bash
python3 scripts/query.py --db production --query "SELECT * FROM orders" --limit 100
注意:MSSQL使用
TOP N
而非
LIMIT
--limit
标志会自动在SELECT后插入
TOP N

Database Selection

数据库选择

Match user intent to database
description
:
User asks aboutLook for description containing
users, accountsusers, accounts, customers
orders, salesorders, transactions, sales
analytics, metricsanalytics, metrics, reports
logs, eventslogs, events, audit
If unclear, run
--list
and ask user which database.
根据用户意图匹配数据库
description
用户询问内容查找包含以下关键词的描述
用户、账户users、accounts、customers
订单、销售orders、transactions、sales
分析、指标analytics、metrics、reports
日志、事件logs、events、audit
如果意图不明确,运行
--list
并询问用户要使用哪个数据库。

Safety Features

安全特性

  • Read-only enforcement: Query validation blocks write operations (use a
    db_datareader
    role user for server-side protection)
  • Query validation: Only SELECT, SHOW, EXPLAIN, WITH, SP_HELP queries allowed
  • Single statement: Multiple statements per query rejected
  • TLS support: Configurable encryption for secure connections
  • Query timeout: 30-second timeout enforced via pymssql
  • Connection timeout: 10-second login timeout
  • Memory protection: Max 10,000 rows per query to prevent OOM
  • Column width cap: 100 char max per column for readable output
  • Credential sanitization: Error messages don't leak passwords
  • 只读强制:查询验证会阻止写入操作(使用
    db_datareader
    角色用户可实现服务器端保护)
  • 查询验证:仅允许SELECT、SHOW、EXPLAIN、WITH、SP_HELP查询
  • 单语句限制:拒绝每个查询包含多条语句
  • TLS支持:可配置加密以实现安全连接
  • 查询超时:通过pymssql强制30秒超时
  • 连接超时:10秒登录超时
  • 内存保护:每个查询最多返回10,000行,防止内存不足
  • 列宽度限制:每列最多100字符,保证输出可读性
  • 凭据清理:错误信息不会泄露密码

Troubleshooting

故障排除

ErrorSolution
Config not foundCreate
connections.json
in skill directory
Authentication failedCheck username/password in config
Connection timeoutVerify host/port, check firewall/VPN
TDS version errorTry
"tds_version": "7.3"
or
"7.4"
Encryption errorSet
"encrypt": true
for Azure SQL
Permission warningRun
chmod 600 connections.json
错误解决方案
未找到配置文件在技能目录中创建
connections.json
认证失败检查配置中的用户名/密码
连接超时验证主机/端口,检查防火墙/VPN
TDS版本错误尝试设置
"tds_version": "7.3"
"7.4"
加密错误针对Azure SQL设置
"encrypt": true
权限警告运行
chmod 600 connections.json

Exit Codes

退出码

  • 0: Success
  • 1: Error (config missing, auth failed, invalid query, database error)
  • 0:成功
  • 1:错误(配置缺失、认证失败、无效查询、数据库错误)

Workflow

工作流程

  1. Run
    --list
    to show available databases
  2. Match user intent to database description
  3. Run
    --tables
    or
    --schema
    to explore structure
  4. Execute query with appropriate
    --limit
    (auto-converts to TOP N)
  1. 运行
    --list
    查看可用数据库
  2. 根据用户意图匹配数据库描述
  3. 运行
    --tables
    --schema
    探索数据库结构
  4. 使用合适的
    --limit
    执行查询(自动转换为TOP N)