Skip to main content
Glama
sql-server-mcp

sql-server-mcp

by tungminsoft
README.md
# sql-server-mcp

An MCP server (Streamable HTTP) for SQL Server. It stores several connections (connection strings encrypted
on disk with AES-256-GCM) and grants access and keeps an audit log **per API key**: each key is bound to a list of
connections, read-only or read-write. Administration happens in a web page that only opens on the server itself.

- Listens on `127.0.0.1:47201`. MCP endpoint: `http://127.0.0.1:47201/mcp`, header `Authorization: Bearer <key>`.
- MCP tools: `sql_list_connections`, `sql_schema`, `sql_query` (+ `sql_execute` for read-write keys).
  Results over 200 rows are paged; call again with `offset`.
- Admin page: `http://127.0.0.1:47201/` (you create the admin password on first visit).

## Install on Windows

PowerShell:

```powershell
irm https://github.com/tungminsoft/sql-server-mcp/releases/latest/download/install.ps1 | iex
```

CMD:

```cmd
curl -fsSL https://github.com/tungminsoft/sql-server-mcp/releases/latest/download/install.cmd -o install.cmd && install.cmd && del install.cmd
```

The installer asks for admin rights, ships its own `node.exe` (no Node install needed), then:

- puts the program in `%ProgramData%\sql-server-mcp\app` and data in `%ProgramData%\sql-server-mcp\data`
  (readable only by Administrators and SYSTEM);
- registers a Task Scheduler task `sql-server-mcp` that runs as SYSTEM at boot (no one needs to sign in)
  and restarts after 5 seconds if it dies;
- stops with a clear message if port 47201 is already taken; it never picks another port.

Running the installer again upgrades in place and keeps the data. Offline machine: copy
`sql-server-mcp-<version>-win-x64.zip` and `install.ps1` over, then in an elevated window run
`powershell -ExecutionPolicy Bypass -File install.ps1 -ArchivePath <package.zip>`.

Uninstall: `powershell -ExecutionPolicy Bypass -File "%ProgramData%\sql-server-mcp\uninstall.ps1"` (asks before deleting
data; pass `-KeepData` or `-RemoveData` to skip the question).

## Install on Linux (systemd)

Requires Node.js 22 or newer (the installer checks and tells you how to install it; it does not install Node itself).

```bash
curl -fsSL https://github.com/tungminsoft/sql-server-mcp/releases/latest/download/install.sh | sudo bash
```

The program lives in `/opt/sql-server-mcp`, data in `/var/lib/sql-server-mcp` (mode `700`, owned by the system user
`sql-server-mcp`), and the service is `sql-server-mcp.service` (`Restart=always`). On a server without a browser, reach
the admin page through an SSH tunnel: `ssh -L 47201:127.0.0.1:47201 <user>@<server>`, then open `http://127.0.0.1:47201/`.

Uninstall: `curl -fsSL https://github.com/tungminsoft/sql-server-mcp/releases/latest/download/uninstall.sh | sudo bash`.

## Docker

```bash
docker run -d --name sql-server-mcp --restart unless-stopped -v sql-server-mcp-data:/data -p 127.0.0.1:47201:47201 ghcr.io/tungminsoft/sql-server-mcp:latest
```

To run next to an app on the same Docker network, see [`docker-compose.example.yml`](docker-compose.example.yml). The app
calls `http://sql-server-mcp:47201/mcp`; `SQL_SERVER_MCP_ALLOWED_HOSTS=sql-server-mcp` allows that host name on `/mcp`.
The admin page still accepts only `127.0.0.1`/`localhost` as `Host`, so publish the port on `127.0.0.1` only.

## Using it from an MCP client

1. Admin page › **Connections**: add a connection (a SQL login with only `db_datareader` is recommended for the read string).
2. **Access keys**: create a key, pick its connections and whether it may write. The key is shown once; copy it right away.
3. **Settings**: copy the MCP configuration into your client and add the header `Authorization: Bearer <key>`.
4. **Audit log**: every statement, filterable by key and connection.

Deleting a key in the admin page rejects the next call made with it immediately.

## Import from an older `sql-gateway` data directory

If you ran the earlier `sql-gateway` service (same `connections.json` + `key` layout), copy its data directory and run:

```bash
sql-server-mcp import-gateway <old data directory>
```

- Windows (elevated): `"%ProgramData%\sql-server-mcp\app\node.exe" "%ProgramData%\sql-server-mcp\app\out\index.js" import-gateway <dir>`
- Linux: `sudo -u sql-server-mcp SQL_SERVER_MCP_DATA=/var/lib/sql-server-mcp node /opt/sql-server-mcp/out/index.js import-gateway <dir>`
- Docker: mount the old directory read-only (e.g. `-v <old>:/old:ro`) and run `node out/index.js import-gateway /old`.

The old directory is only read. Connections are re-encrypted with this server's key; IDs that already exist are skipped,
so running it twice is safe. The output lists connection IDs and counts only, never strings or passwords. Restart the
service afterwards, then create access keys in the admin page (keys are not imported).

## Forgot the admin password

Clear the password (connections and keys are kept), then open the page to set a new one:

- Windows (elevated): `"%ProgramData%\sql-server-mcp\app\node.exe" "%ProgramData%\sql-server-mcp\app\out\index.js" reset-admin`
- Linux: `sudo -u sql-server-mcp SQL_SERVER_MCP_DATA=/var/lib/sql-server-mcp node /opt/sql-server-mcp/out/index.js reset-admin && sudo systemctl restart sql-server-mcp`
- Docker: `docker exec sql-server-mcp node out/index.js reset-admin && docker restart sql-server-mcp`

## Environment variables

| Variable | Default | Notes |
|---|---|---|
| `SQL_SERVER_MCP_HOST` | `127.0.0.1` | Windows accepts loopback only. Docker sets `0.0.0.0` |
| `SQL_SERVER_MCP_PORT` | `47201` | |
| `SQL_SERVER_MCP_DATA` | Windows `%ProgramData%\sql-server-mcp\data`, Linux `/var/lib/sql-server-mcp` | |
| `SQL_SERVER_MCP_ALLOWED_HOSTS` | (empty) | Extra host names allowed on `/mcp`, comma-separated |
| `SQL_SERVER_MCP_QUERY_TIMEOUT_MS` | `300000` | |

## Development

```bash
npm ci
npm test            # tests (fake SQL Server, no real server needed)
npm run typecheck
npm run dev         # run from source
npm run release     # build packages into release/ and smoke-test each one
```

Releasing: push a tag `vX.Y.Z` matching `version` in `package.json`; GitHub Actions
(`.github/workflows/release.yml`) runs the tests, builds the packages, creates the Release and pushes the image to `ghcr.io`.

## License

MIT

## Tiếng Việt

`sql-server-mcp` là MCP server (HTTP) cho SQL Server: giữ nhiều kết nối mã hoá trên đĩa, cấp quyền và ghi nhật ký
theo từng khoá (chỉ đọc hoặc đọc-ghi). Nghe `127.0.0.1:47201`; trang quản trị `http://127.0.0.1:47201/` chỉ mở được
trên chính máy chủ, lần đầu vào thì tạo mật khẩu.

- Cài: Windows chạy lệnh `irm ... | iex` ở mục *Install on Windows*; Linux chạy `curl ... | sudo bash` ở mục
  *Install on Linux*; Docker xem mục *Docker*. Chạy lại lệnh cài là cập nhật, dữ liệu giữ nguyên.
- Dùng: thêm kết nối → tạo khoá → chép cấu hình MCP vào app, thêm header `Authorization: Bearer <khoá>`.
- Chuyển từ cổng `sql-gateway` cũ: `sql-server-mcp import-gateway <thư mục dữ liệu cũ>` (chỉ đọc thư mục cũ,
  không in chuỗi kết nối hay mật khẩu; khoá truy cập tạo lại ở trang quản trị).
- Quên mật khẩu quản trị: lệnh `reset-admin` ở mục *Forgot the admin password*.