@codegiveness/mssql-mcp 0.2.0 → 0.3.0
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.
- package/bin/mssql-mcp.js +370 -0
- package/package.json +11 -7
- package/bin/mssql-mcp +0 -14
- package/install.js +0 -255
package/bin/mssql-mcp.js
ADDED
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
// Shim: resolves the per-platform optionalDependency and execs the native binary.
|
|
5
|
+
// Postinstall-independent — npm's dependency resolution delivers the binary,
|
|
6
|
+
// not a lifecycle script. When the optional dep is absent (stripped by
|
|
7
|
+
// --no-optional, corporate npm mirrors), the shim self-heals by downloading
|
|
8
|
+
// from GitHub Releases into ~/.mssql-mcp/bin/<version>/<rid>/ (ADR-0028 §2).
|
|
9
|
+
|
|
10
|
+
const { spawnSync } = require('child_process');
|
|
11
|
+
const crypto = require('crypto');
|
|
12
|
+
const fs = require('fs');
|
|
13
|
+
const https = require('https');
|
|
14
|
+
const os = require('os');
|
|
15
|
+
const path = require('path');
|
|
16
|
+
|
|
17
|
+
const REPO = 'codegiveness/mssql-mcp';
|
|
18
|
+
const NO_DOWNLOAD_ENV = 'MSSQL_MCP_NO_DOWNLOAD';
|
|
19
|
+
const CACHE_ROOT = path.join(os.homedir(), '.mssql-mcp', 'bin');
|
|
20
|
+
|
|
21
|
+
// (platform, arch) -> RID. win-x64 is framework-dependent per ADR-0002.
|
|
22
|
+
function ridFor(platform, arch) {
|
|
23
|
+
if (platform === 'linux' && arch === 'x64') return 'linux-x64';
|
|
24
|
+
if (platform === 'linux' && arch === 'arm64') return 'linux-arm64';
|
|
25
|
+
if (platform === 'darwin' && arch === 'x64') return 'osx-x64';
|
|
26
|
+
if (platform === 'darwin' && arch === 'arm64') return 'osx-arm64';
|
|
27
|
+
if (platform === 'win32' && arch === 'x64') return 'win-x64';
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function archiveExt(rid) {
|
|
32
|
+
return rid === 'win-x64' ? '.zip' : '.tar.gz';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function sha256Hex(buf) {
|
|
36
|
+
return crypto.createHash('sha256').update(buf).digest('hex');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Classify a download error into an actionable reason string.
|
|
40
|
+
// Inspects the error message for HTTP status codes and the error code for
|
|
41
|
+
// network-level failures (ECONNRESET, ENOTFOUND, ECONNREFUSED).
|
|
42
|
+
function classifyDownloadError(err) {
|
|
43
|
+
if (err && err.message && /HTTP 404\b/.test(err.message)) return 'release not published yet';
|
|
44
|
+
if (err && (err.code === 'ECONNRESET' || err.code === 'ENOTFOUND' || err.code === 'ECONNREFUSED')) {
|
|
45
|
+
return 'network or proxy blocked';
|
|
46
|
+
}
|
|
47
|
+
if (err && err.message && /timeout/i.test(err.message)) return 'timed out';
|
|
48
|
+
return 'download failed';
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Parse a .sha256 sidecar file. Accepts "<hex> <filename>" or bare "<hex>".
|
|
52
|
+
function parseChecksumFile(text) {
|
|
53
|
+
const trimmed = text.trim();
|
|
54
|
+
if (/^[0-9a-fA-F]{64}$/.test(trimmed)) return trimmed.toLowerCase();
|
|
55
|
+
// "<hex> <filename>" form (sha256sum output).
|
|
56
|
+
const match = trimmed.match(/^([0-9a-fA-F]{64})\s+\*?\S/);
|
|
57
|
+
if (match) return match[1].toLowerCase();
|
|
58
|
+
// Last resort: first 64-hex-char run anywhere in the file.
|
|
59
|
+
const any = trimmed.match(/[0-9a-fA-F]{64}/);
|
|
60
|
+
if (any) return any[0].toLowerCase();
|
|
61
|
+
throw new Error('checksum file has no 64-hex SHA256 token');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Follow up to 3 HTTP redirects (GitHub releases redirect to S3/CDN).
|
|
65
|
+
// Hard depth limit prevents redirect loops from hanging or stack-overflowing.
|
|
66
|
+
const MAX_REDIRECTS = 3;
|
|
67
|
+
|
|
68
|
+
function fetchUrl(url, depth) {
|
|
69
|
+
if (depth === undefined) depth = 0;
|
|
70
|
+
return new Promise((resolve, reject) => {
|
|
71
|
+
if (depth > MAX_REDIRECTS) {
|
|
72
|
+
reject(new Error('too many redirects (>' + MAX_REDIRECTS + ') starting from ' + url));
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const req = https.get(url, (res) => {
|
|
76
|
+
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
|
|
77
|
+
res.resume();
|
|
78
|
+
fetchUrl(res.headers.location, depth + 1).then(resolve, reject);
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
if (res.statusCode !== 200) {
|
|
82
|
+
res.resume();
|
|
83
|
+
reject(new Error('HTTP ' + res.statusCode + ' for ' + url));
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const chunks = [];
|
|
87
|
+
res.on('data', (c) => chunks.push(c));
|
|
88
|
+
res.on('end', () => resolve(Buffer.concat(chunks)));
|
|
89
|
+
});
|
|
90
|
+
req.on('error', reject);
|
|
91
|
+
req.setTimeout(60000, () => {
|
|
92
|
+
req.destroy(new Error('timeout downloading ' + url));
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function extractTarGz(archivePath, outDir) {
|
|
98
|
+
const r = spawnSync('tar', ['-xzf', archivePath, '-C', outDir], { stdio: 'inherit' });
|
|
99
|
+
if (r.status !== 0) {
|
|
100
|
+
throw new Error('tar exited with status ' + r.status);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function extractZip(archivePath, outDir) {
|
|
105
|
+
// Validate entry names before extraction to prevent path traversal (Zip Slip).
|
|
106
|
+
// `unzip -o` alone does not reject entries with ../ or absolute paths.
|
|
107
|
+
const list = spawnSync('unzip', ['-l', archivePath], { encoding: 'utf8' });
|
|
108
|
+
if (list.status !== 0) {
|
|
109
|
+
throw new Error('unzip -l failed with status ' + list.status);
|
|
110
|
+
}
|
|
111
|
+
const resolvedOut = path.resolve(outDir);
|
|
112
|
+
for (const line of list.stdout.split('\n')) {
|
|
113
|
+
const m = line.match(/^\s+\d+\s+\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}\s+(.+)$/);
|
|
114
|
+
if (!m) continue;
|
|
115
|
+
const entryPath = m[1].trim();
|
|
116
|
+
const resolvedEntry = path.resolve(outDir, entryPath);
|
|
117
|
+
if (!resolvedEntry.startsWith(resolvedOut + path.sep) && resolvedEntry !== resolvedOut) {
|
|
118
|
+
throw new Error('Refusing to extract zip entry outside target directory: ' + entryPath);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const r = spawnSync('unzip', ['-o', archivePath, '-d', outDir], { stdio: 'inherit' });
|
|
123
|
+
if (r.status !== 0) {
|
|
124
|
+
throw new Error('unzip exited with status ' + r.status + ' (is unzip installed?)');
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// --- Entry point ---
|
|
129
|
+
|
|
130
|
+
async function main() {
|
|
131
|
+
const rid = ridFor(process.platform, process.arch);
|
|
132
|
+
if (!rid) {
|
|
133
|
+
failUnsupportedPlatform();
|
|
134
|
+
return; // unreachable; failUnsupportedPlatform exits
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// Happy path: resolve the per-platform optional dependency.
|
|
138
|
+
const pkgName = '@codegiveness/mssql-mcp-' + rid;
|
|
139
|
+
const binaryName = process.platform === 'win32' ? 'mssql-mcp.exe' : 'mssql-mcp';
|
|
140
|
+
|
|
141
|
+
let binaryPath = null;
|
|
142
|
+
try {
|
|
143
|
+
binaryPath = require.resolve(pkgName + '/' + binaryName);
|
|
144
|
+
} catch (_) {
|
|
145
|
+
// Optional dep absent — fall through to self-heal path.
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (!binaryPath) {
|
|
149
|
+
binaryPath = resolveCachedBinary(rid);
|
|
150
|
+
if (!binaryPath) {
|
|
151
|
+
// No cached binary — attempt self-heal download or fail with guidance.
|
|
152
|
+
binaryPath = await selfHealOrDie(rid);
|
|
153
|
+
if (!binaryPath) return; // unreachable; selfHealOrDie exits on failure
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Exec the binary, forwarding stdio.
|
|
158
|
+
const result = spawnSync(binaryPath, process.argv.slice(2), { stdio: 'inherit' });
|
|
159
|
+
|
|
160
|
+
// Windows framework-dependent binary without .NET 10 runtime fails to spawn.
|
|
161
|
+
// Detect and provide actionable guidance.
|
|
162
|
+
if (result.error && process.platform === 'win32') {
|
|
163
|
+
failWindowsDotNetMissing(result.error);
|
|
164
|
+
return; // unreachable
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (result.error) {
|
|
168
|
+
console.error('mssql-mcp: failed to launch binary: ' + result.error.message);
|
|
169
|
+
process.exit(1);
|
|
170
|
+
}
|
|
171
|
+
process.exit(result.status ?? 1);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Check if a cached binary exists at ~/.mssql-mcp/bin/<version>/<rid>/mssql-mcp[.exe]
|
|
175
|
+
function resolveCachedBinary(rid) {
|
|
176
|
+
const pkg = require('../package.json');
|
|
177
|
+
const version = pkg.version;
|
|
178
|
+
const binaryName = process.platform === 'win32' ? 'mssql-mcp.exe' : 'mssql-mcp';
|
|
179
|
+
const cachedPath = path.join(CACHE_ROOT, version, rid, binaryName);
|
|
180
|
+
if (fs.existsSync(cachedPath)) {
|
|
181
|
+
return cachedPath;
|
|
182
|
+
}
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Attempt self-heal download. Returns the binary path on success, or exits
|
|
187
|
+
// with an actionable error message on failure.
|
|
188
|
+
async function selfHealOrDie(rid) {
|
|
189
|
+
const pkg = require('../package.json');
|
|
190
|
+
const version = pkg.version;
|
|
191
|
+
const tag = 'v' + version;
|
|
192
|
+
const ext = archiveExt(rid);
|
|
193
|
+
// Release archives are named with the tag (including the 'v' prefix):
|
|
194
|
+
// mssql-mcp-v0.1.0-linux-x64.tar.gz — see release.yml Archive steps.
|
|
195
|
+
const archiveName = 'mssql-mcp-' + tag + '-' + rid + ext;
|
|
196
|
+
const archiveUrl = 'https://github.com/' + REPO + '/releases/download/' + tag + '/' + archiveName;
|
|
197
|
+
const checksumUrl = archiveUrl + '.sha256';
|
|
198
|
+
|
|
199
|
+
const noDownload = process.env[NO_DOWNLOAD_ENV] === '1';
|
|
200
|
+
if (noDownload) {
|
|
201
|
+
failDownloadDisabled(rid, archiveUrl);
|
|
202
|
+
return null; // unreachable
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const cacheDir = path.join(CACHE_ROOT, version, rid);
|
|
206
|
+
const binaryName = process.platform === 'win32' ? 'mssql-mcp.exe' : 'mssql-mcp';
|
|
207
|
+
const cachedBinaryPath = path.join(cacheDir, binaryName);
|
|
208
|
+
|
|
209
|
+
console.error('mssql-mcp: native binary not found via npm; attempting self-heal download.');
|
|
210
|
+
console.error('mssql-mcp: downloading ' + archiveUrl);
|
|
211
|
+
|
|
212
|
+
let archiveBuf;
|
|
213
|
+
try {
|
|
214
|
+
archiveBuf = await fetchUrl(archiveUrl);
|
|
215
|
+
} catch (e) {
|
|
216
|
+
failDownloadError(rid, 'archive', classifyDownloadError(e), e.message, archiveUrl);
|
|
217
|
+
return null; // unreachable
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
let checksumBuf;
|
|
221
|
+
try {
|
|
222
|
+
checksumBuf = await fetchUrl(checksumUrl);
|
|
223
|
+
} catch (e) {
|
|
224
|
+
failDownloadError(rid, 'checksum', classifyDownloadError(e), e.message, checksumUrl);
|
|
225
|
+
return null; // unreachable
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const expected = parseChecksumFile(checksumBuf.toString('utf8'));
|
|
229
|
+
const actual = sha256Hex(archiveBuf);
|
|
230
|
+
if (expected !== actual) {
|
|
231
|
+
failChecksumMismatch(rid, expected, actual, archiveUrl);
|
|
232
|
+
return null; // unreachable
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// Extract into cache dir.
|
|
236
|
+
fs.mkdirSync(cacheDir, { recursive: true });
|
|
237
|
+
const tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'mssql-mcp-heal-'));
|
|
238
|
+
try {
|
|
239
|
+
const archivePath = path.join(tmpRoot, archiveName);
|
|
240
|
+
fs.writeFileSync(archivePath, archiveBuf);
|
|
241
|
+
|
|
242
|
+
const extractDir = path.join(tmpRoot, 'extract');
|
|
243
|
+
fs.mkdirSync(extractDir, { recursive: true });
|
|
244
|
+
if (ext === '.zip') {
|
|
245
|
+
extractZip(archivePath, extractDir);
|
|
246
|
+
} else {
|
|
247
|
+
extractTarGz(archivePath, extractDir);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// Flat archive: binary at archive root, named `mssql-mcp` (Unix) or
|
|
251
|
+
// `mssql-mcp.exe` (Windows). Look for both to be defensive.
|
|
252
|
+
const candidates = [
|
|
253
|
+
path.join(extractDir, 'mssql-mcp'),
|
|
254
|
+
path.join(extractDir, 'mssql-mcp.exe'),
|
|
255
|
+
].filter((p) => fs.existsSync(p));
|
|
256
|
+
if (candidates.length === 0) {
|
|
257
|
+
failExtractionFailed(rid, archiveUrl);
|
|
258
|
+
return null; // unreachable
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
fs.copyFileSync(candidates[0], cachedBinaryPath);
|
|
262
|
+
if (process.platform !== 'win32') {
|
|
263
|
+
fs.chmodSync(cachedBinaryPath, 0o755);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
console.error('mssql-mcp: cached binary to ' + cachedBinaryPath);
|
|
267
|
+
} finally {
|
|
268
|
+
try {
|
|
269
|
+
fs.rmSync(tmpRoot, { recursive: true, force: true });
|
|
270
|
+
} catch (_) {
|
|
271
|
+
// ignored
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return cachedBinaryPath;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// --- Error message contract ---
|
|
279
|
+
// Every failure mode prints: (1) the problem and RID, (2) the direct GitHub
|
|
280
|
+
// Releases URL for manual download, (3) the `dotnet tool install -g mssql-mcp`
|
|
281
|
+
// fallback. No dead ends. See ADR-0028 §5.
|
|
282
|
+
|
|
283
|
+
function failUnsupportedPlatform() {
|
|
284
|
+
console.error('mssql-mcp: unsupported platform/architecture.');
|
|
285
|
+
console.error(' detected: platform=' + process.platform + ', arch=' + process.arch);
|
|
286
|
+
console.error(' supported RIDs: linux-x64, linux-arm64, osx-x64, osx-arm64, win-x64');
|
|
287
|
+
console.error('');
|
|
288
|
+
console.error('Install the .NET tool instead:');
|
|
289
|
+
console.error(' dotnet tool install -g mssql-mcp');
|
|
290
|
+
process.exit(1);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function failDownloadDisabled(rid, archiveUrl) {
|
|
294
|
+
console.error('mssql-mcp: native binary not found and download is disabled');
|
|
295
|
+
console.error(' (MSSQL_MCP_NO_DOWNLOAD=1 is set).');
|
|
296
|
+
console.error(' RID: ' + rid);
|
|
297
|
+
console.error('');
|
|
298
|
+
console.error('Manual download:');
|
|
299
|
+
console.error(' ' + archiveUrl);
|
|
300
|
+
console.error('');
|
|
301
|
+
console.error('Or install the .NET tool:');
|
|
302
|
+
console.error(' dotnet tool install -g mssql-mcp');
|
|
303
|
+
process.exit(1);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function failDownloadError(rid, kind, reason, message, url) {
|
|
307
|
+
console.error('mssql-mcp: failed to download ' + kind + ' [' + reason + '].');
|
|
308
|
+
console.error(' RID: ' + rid);
|
|
309
|
+
console.error(' error: ' + message);
|
|
310
|
+
console.error(' URL: ' + url);
|
|
311
|
+
console.error('');
|
|
312
|
+
console.error('Manual download:');
|
|
313
|
+
console.error(' ' + url);
|
|
314
|
+
console.error('');
|
|
315
|
+
console.error('Or install the .NET tool:');
|
|
316
|
+
console.error(' dotnet tool install -g mssql-mcp');
|
|
317
|
+
process.exit(1);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function failChecksumMismatch(rid, expected, actual, archiveUrl) {
|
|
321
|
+
console.error('mssql-mcp: SHA256 verification failed for downloaded archive.');
|
|
322
|
+
console.error(' RID: ' + rid);
|
|
323
|
+
console.error(' expected: ' + expected);
|
|
324
|
+
console.error(' actual: ' + actual);
|
|
325
|
+
console.error(' archive: ' + archiveUrl);
|
|
326
|
+
console.error('');
|
|
327
|
+
console.error('Manual download (re-verify the checksum yourself):');
|
|
328
|
+
console.error(' ' + archiveUrl);
|
|
329
|
+
console.error('');
|
|
330
|
+
console.error('Or install the .NET tool:');
|
|
331
|
+
console.error(' dotnet tool install -g mssql-mcp');
|
|
332
|
+
process.exit(1);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function failExtractionFailed(rid, archiveUrl) {
|
|
336
|
+
console.error('mssql-mcp: download succeeded but extraction produced no binary.');
|
|
337
|
+
console.error(' RID: ' + rid);
|
|
338
|
+
console.error(' archive: ' + archiveUrl);
|
|
339
|
+
console.error('');
|
|
340
|
+
console.error('Manual download:');
|
|
341
|
+
console.error(' ' + archiveUrl);
|
|
342
|
+
console.error('');
|
|
343
|
+
console.error('Or install the .NET tool:');
|
|
344
|
+
console.error(' dotnet tool install -g mssql-mcp');
|
|
345
|
+
process.exit(1);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function failWindowsDotNetMissing(spawnError) {
|
|
349
|
+
console.error('mssql-mcp: failed to launch the Windows binary.');
|
|
350
|
+
console.error(' The Windows build is framework-dependent and requires the .NET 10 runtime.');
|
|
351
|
+
console.error(' error: ' + spawnError.message);
|
|
352
|
+
console.error('');
|
|
353
|
+
console.error('Download the .NET 10 runtime:');
|
|
354
|
+
console.error(' https://dotnet.microsoft.com/download');
|
|
355
|
+
console.error('');
|
|
356
|
+
console.error('Or install the .NET tool (includes the runtime):');
|
|
357
|
+
console.error(' dotnet tool install -g mssql-mcp');
|
|
358
|
+
process.exit(1);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// Exposed for smoke tests (npm/test.js). Not part of the public API.
|
|
362
|
+
module.exports = { ridFor, archiveExt, parseChecksumFile, classifyDownloadError, sha256Hex };
|
|
363
|
+
|
|
364
|
+
// Run main() only when invoked directly (not when required by test.js).
|
|
365
|
+
if (require.main === module) {
|
|
366
|
+
main().catch((e) => {
|
|
367
|
+
console.error('mssql-mcp: unexpected error: ' + (e && e.stack ? e.stack : String(e)));
|
|
368
|
+
process.exit(1);
|
|
369
|
+
});
|
|
370
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codegiveness/mssql-mcp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "A Model Context Protocol (MCP) server for Microsoft SQL Server, built in C#/.NET 10",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -8,15 +8,19 @@
|
|
|
8
8
|
"url": "https://github.com/codegiveness/mssql-mcp"
|
|
9
9
|
},
|
|
10
10
|
"bin": {
|
|
11
|
-
"mssql-mcp": "bin/mssql-mcp"
|
|
12
|
-
},
|
|
13
|
-
"scripts": {
|
|
14
|
-
"postinstall": "node install.js"
|
|
11
|
+
"mssql-mcp": "bin/mssql-mcp.js"
|
|
15
12
|
},
|
|
13
|
+
"scripts": {},
|
|
16
14
|
"files": [
|
|
17
|
-
"bin/"
|
|
18
|
-
"install.js"
|
|
15
|
+
"bin/mssql-mcp.js"
|
|
19
16
|
],
|
|
17
|
+
"optionalDependencies": {
|
|
18
|
+
"@codegiveness/mssql-mcp-linux-x64": "0.3.0",
|
|
19
|
+
"@codegiveness/mssql-mcp-linux-arm64": "0.3.0",
|
|
20
|
+
"@codegiveness/mssql-mcp-osx-x64": "0.3.0",
|
|
21
|
+
"@codegiveness/mssql-mcp-osx-arm64": "0.3.0",
|
|
22
|
+
"@codegiveness/mssql-mcp-win-x64": "0.3.0"
|
|
23
|
+
},
|
|
20
24
|
"engines": {
|
|
21
25
|
"node": ">=18"
|
|
22
26
|
}
|
package/bin/mssql-mcp
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
// Shim: replaced by the real binary during npm install (postinstall).
|
|
3
|
-
// If you see this message, the postinstall did not run or the download failed.
|
|
4
|
-
const { spawnSync } = require('child_process');
|
|
5
|
-
const path = require('path');
|
|
6
|
-
const fs = require('fs');
|
|
7
|
-
const binPath = path.join(__dirname, 'mssql-mcp');
|
|
8
|
-
if (fs.existsSync(binPath) && !fs.readFileSync(binPath, 'utf8').includes('Shim')) {
|
|
9
|
-
const result = spawnSync(binPath, process.argv.slice(2), { stdio: 'inherit' });
|
|
10
|
-
process.exit(result.status ?? 1);
|
|
11
|
-
} else {
|
|
12
|
-
console.error('mssql-mcp: binary not installed. Run "npm install" or use "dotnet tool install -g mssql-mcp" instead.');
|
|
13
|
-
process.exit(1);
|
|
14
|
-
}
|
package/install.js
DELETED
|
@@ -1,255 +0,0 @@
|
|
|
1
|
-
// npm postinstall: download the prebuilt mssql-mcp binary for this platform,
|
|
2
|
-
// verify its SHA256 checksum, and replace the Node shim at bin/mssql-mcp with
|
|
3
|
-
// the real native executable.
|
|
4
|
-
//
|
|
5
|
-
// Contract (ADR-0014):
|
|
6
|
-
// - Fail loudly on ANY error (no network, 404, checksum mismatch, extraction
|
|
7
|
-
// failure, unsupported platform). Exit non-zero. No silent fallback to the
|
|
8
|
-
// Node shim — the shim is NOT a real server.
|
|
9
|
-
// - No retry logic in v1. The user retries manually.
|
|
10
|
-
// - Detect unsupported platform/arch BEFORE download; print supported RIDs.
|
|
11
|
-
// - Verify SHA256 after download; fail on mismatch.
|
|
12
|
-
// - Print final binary path on success.
|
|
13
|
-
//
|
|
14
|
-
// Fall-back command suggested in every error message:
|
|
15
|
-
// dotnet tool install -g mssql-mcp
|
|
16
|
-
'use strict';
|
|
17
|
-
|
|
18
|
-
const crypto = require('crypto');
|
|
19
|
-
const fs = require('fs');
|
|
20
|
-
const https = require('https');
|
|
21
|
-
const os = require('os');
|
|
22
|
-
const path = require('path');
|
|
23
|
-
const { spawnSync } = require('child_process');
|
|
24
|
-
|
|
25
|
-
const REPO = 'codegiveness/mssql-mcp';
|
|
26
|
-
const BIN_DIR = path.join(__dirname, 'bin');
|
|
27
|
-
const BIN_PATH = path.join(BIN_DIR, 'mssql-mcp');
|
|
28
|
-
|
|
29
|
-
// (platform, arch) -> RID. win-x64 is framework-dependent per ADR-0002.
|
|
30
|
-
function ridFor(platform, arch) {
|
|
31
|
-
if (platform === 'linux' && arch === 'x64') return 'linux-x64';
|
|
32
|
-
if (platform === 'linux' && arch === 'arm64') return 'linux-arm64';
|
|
33
|
-
if (platform === 'darwin' && arch === 'x64') return 'osx-x64';
|
|
34
|
-
if (platform === 'darwin' && arch === 'arm64') return 'osx-arm64';
|
|
35
|
-
if (platform === 'win32' && arch === 'x64') return 'win-x64';
|
|
36
|
-
return null;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function archiveExt(rid) {
|
|
40
|
-
return rid === 'win-x64' ? '.zip' : '.tar.gz';
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
function die(message) {
|
|
44
|
-
console.error('mssql-mcp: ' + message);
|
|
45
|
-
console.error('');
|
|
46
|
-
console.error('Falling back is not automatic. You can install the .NET tool instead:');
|
|
47
|
-
console.error(' dotnet tool install -g mssql-mcp');
|
|
48
|
-
process.exit(1);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// Follow up to 3 HTTP redirects (GitHub releases redirect to S3/CDN).
|
|
52
|
-
// Hard depth limit prevents redirect loops from hanging or stack-overflowing.
|
|
53
|
-
const MAX_REDIRECTS = 3;
|
|
54
|
-
|
|
55
|
-
function fetchUrl(url, depth) {
|
|
56
|
-
if (depth === undefined) depth = 0;
|
|
57
|
-
return new Promise((resolve, reject) => {
|
|
58
|
-
if (depth > MAX_REDIRECTS) {
|
|
59
|
-
reject(new Error('too many redirects (>' + MAX_REDIRECTS + ') starting from ' + url));
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
const req = https.get(url, (res) => {
|
|
63
|
-
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
|
|
64
|
-
res.resume();
|
|
65
|
-
fetchUrl(res.headers.location, depth + 1).then(resolve, reject);
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
if (res.statusCode !== 200) {
|
|
69
|
-
res.resume();
|
|
70
|
-
reject(new Error('HTTP ' + res.statusCode + ' for ' + url));
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
const chunks = [];
|
|
74
|
-
res.on('data', (c) => chunks.push(c));
|
|
75
|
-
res.on('end', () => resolve(Buffer.concat(chunks)));
|
|
76
|
-
});
|
|
77
|
-
req.on('error', reject);
|
|
78
|
-
req.setTimeout(60000, () => {
|
|
79
|
-
req.destroy(new Error('timeout downloading ' + url));
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function sha256Hex(buf) {
|
|
85
|
-
return crypto.createHash('sha256').update(buf).digest('hex');
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// Classify a download error from fetchUrl into an actionable reason string.
|
|
89
|
-
// Inspects the error message for HTTP status codes and the error code for
|
|
90
|
-
// network-level failures (ECONNRESET, ENOTFOUND, ECONNREFUSED).
|
|
91
|
-
function classifyDownloadError(err) {
|
|
92
|
-
if (err && err.message && /HTTP 404\b/.test(err.message)) return 'release not published yet';
|
|
93
|
-
if (err && (err.code === 'ECONNRESET' || err.code === 'ENOTFOUND' || err.code === 'ECONNREFUSED')) {
|
|
94
|
-
return 'network or proxy blocked';
|
|
95
|
-
}
|
|
96
|
-
if (err && err.message && /timeout/i.test(err.message)) return 'timed out';
|
|
97
|
-
return 'download failed';
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// Parse a .sha256 sidecar file. Accepts "<hex> <filename>" or bare "<hex>".
|
|
101
|
-
function parseChecksumFile(text) {
|
|
102
|
-
const trimmed = text.trim();
|
|
103
|
-
if (/^[0-9a-fA-F]{64}$/.test(trimmed)) return trimmed.toLowerCase();
|
|
104
|
-
// "<hex> <filename>" form (sha256sum output).
|
|
105
|
-
const match = trimmed.match(/^([0-9a-fA-F]{64})\s+\*?\S/);
|
|
106
|
-
if (match) return match[1].toLowerCase();
|
|
107
|
-
// Last resort: first 64-hex-char run anywhere in the file.
|
|
108
|
-
const any = trimmed.match(/[0-9a-fA-F]{64}/);
|
|
109
|
-
if (any) return any[0].toLowerCase();
|
|
110
|
-
throw new Error('checksum file has no 64-hex SHA256 token');
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function extractTarGz(archivePath, outDir) {
|
|
114
|
-
const r = spawnSync('tar', ['-xzf', archivePath, '-C', outDir], { stdio: 'inherit' });
|
|
115
|
-
if (r.status !== 0) {
|
|
116
|
-
throw new Error('tar exited with status ' + r.status);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
function extractZip(archivePath, outDir) {
|
|
121
|
-
// Validate entry names before extraction to prevent path traversal (Zip Slip).
|
|
122
|
-
// `unzip -o` alone does not reject entries with ../ or absolute paths.
|
|
123
|
-
const list = spawnSync('unzip', ['-l', archivePath], { encoding: 'utf8' });
|
|
124
|
-
if (list.status !== 0) {
|
|
125
|
-
throw new Error('unzip -l failed with status ' + list.status);
|
|
126
|
-
}
|
|
127
|
-
const resolvedOut = path.resolve(outDir);
|
|
128
|
-
for (const line of list.stdout.split('\n')) {
|
|
129
|
-
const m = line.match(/^\s+\d+\s+\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}\s+(.+)$/);
|
|
130
|
-
if (!m) continue;
|
|
131
|
-
const entryPath = m[1].trim();
|
|
132
|
-
const resolvedEntry = path.resolve(outDir, entryPath);
|
|
133
|
-
if (!resolvedEntry.startsWith(resolvedOut + path.sep) && resolvedEntry !== resolvedOut) {
|
|
134
|
-
throw new Error('Refusing to extract zip entry outside target directory: ' + entryPath);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
const r = spawnSync('unzip', ['-o', archivePath, '-d', outDir], { stdio: 'inherit' });
|
|
139
|
-
if (r.status !== 0) {
|
|
140
|
-
throw new Error('unzip exited with status ' + r.status + ' (is unzip installed?)');
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
async function main() {
|
|
145
|
-
const rid = ridFor(process.platform, process.arch);
|
|
146
|
-
if (!rid) {
|
|
147
|
-
console.error('mssql-mcp: unsupported platform/architecture.');
|
|
148
|
-
console.error(' detected: platform=' + process.platform + ', arch=' + process.arch);
|
|
149
|
-
console.error(' supported RIDs: linux-x64, linux-arm64, osx-x64, osx-arm64, win-x64');
|
|
150
|
-
die('cannot resolve a binary for this platform.');
|
|
151
|
-
return; // unreachable; die exits
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
// Resolve version from this package's package.json.
|
|
155
|
-
const pkg = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'), 'utf8'));
|
|
156
|
-
const version = pkg.version;
|
|
157
|
-
if (!version) {
|
|
158
|
-
die('package.json has no version field; cannot determine which release to download.');
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
const tag = 'v' + version;
|
|
162
|
-
|
|
163
|
-
const ext = archiveExt(rid);
|
|
164
|
-
const basename = 'mssql-mcp-' + version + '-' + rid + ext;
|
|
165
|
-
const archiveUrl = 'https://github.com/' + REPO + '/releases/download/' + tag + '/' + basename;
|
|
166
|
-
const checksumUrl = archiveUrl + '.sha256';
|
|
167
|
-
|
|
168
|
-
console.log('mssql-mcp: installing v' + version + ' for ' + rid);
|
|
169
|
-
console.log('mssql-mcp: downloading ' + archiveUrl);
|
|
170
|
-
|
|
171
|
-
let archiveBuf;
|
|
172
|
-
try {
|
|
173
|
-
archiveBuf = await fetchUrl(archiveUrl);
|
|
174
|
-
} catch (e) {
|
|
175
|
-
die('download failed for archive [' + classifyDownloadError(e) + ']: ' + e.message + '\n URL: ' + archiveUrl);
|
|
176
|
-
return;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
console.log('mssql-mcp: downloading ' + checksumUrl);
|
|
180
|
-
let checksumBuf;
|
|
181
|
-
try {
|
|
182
|
-
checksumBuf = await fetchUrl(checksumUrl);
|
|
183
|
-
} catch (e) {
|
|
184
|
-
die('download failed for checksum [' + classifyDownloadError(e) + ']: ' + e.message + '\n URL: ' + checksumUrl);
|
|
185
|
-
return;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
const expected = parseChecksumFile(checksumBuf.toString('utf8'));
|
|
189
|
-
const actual = sha256Hex(archiveBuf);
|
|
190
|
-
if (expected !== actual) {
|
|
191
|
-
die(
|
|
192
|
-
'SHA256 mismatch.\n' +
|
|
193
|
-
' expected: ' + expected + '\n' +
|
|
194
|
-
' actual: ' + actual + '\n' +
|
|
195
|
-
' archive: ' + archiveUrl
|
|
196
|
-
);
|
|
197
|
-
return;
|
|
198
|
-
}
|
|
199
|
-
console.log('mssql-mcp: checksum verified (' + expected + ')');
|
|
200
|
-
|
|
201
|
-
// Stage the archive in a temp dir, extract, then move the binary into bin/.
|
|
202
|
-
if (!fs.existsSync(BIN_DIR)) fs.mkdirSync(BIN_DIR, { recursive: true });
|
|
203
|
-
|
|
204
|
-
const tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'mssql-mcp-install-'));
|
|
205
|
-
try {
|
|
206
|
-
const archivePath = path.join(tmpRoot, basename);
|
|
207
|
-
fs.writeFileSync(archivePath, archiveBuf);
|
|
208
|
-
|
|
209
|
-
const extractDir = path.join(tmpRoot, 'extract');
|
|
210
|
-
fs.mkdirSync(extractDir, { recursive: true });
|
|
211
|
-
if (ext === '.zip') {
|
|
212
|
-
extractZip(archivePath, extractDir);
|
|
213
|
-
} else {
|
|
214
|
-
extractTarGz(archivePath, extractDir);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
// Flat archive per sqz: binary at archive root, named `mssql-mcp` (Unix) or
|
|
218
|
-
// `mssql-mcp.exe` (Windows). Look for both to be defensive.
|
|
219
|
-
const candidates = [
|
|
220
|
-
path.join(extractDir, 'mssql-mcp'),
|
|
221
|
-
path.join(extractDir, 'mssql-mcp.exe'),
|
|
222
|
-
].filter((p) => fs.existsSync(p));
|
|
223
|
-
if (candidates.length === 0) {
|
|
224
|
-
die('extraction produced no `mssql-mcp` binary. Archive: ' + archiveUrl);
|
|
225
|
-
return;
|
|
226
|
-
}
|
|
227
|
-
const extractedBin = candidates[0];
|
|
228
|
-
|
|
229
|
-
// Overwrite the shim with the real binary.
|
|
230
|
-
fs.copyFileSync(extractedBin, BIN_PATH);
|
|
231
|
-
if (process.platform !== 'win32') {
|
|
232
|
-
fs.chmodSync(BIN_PATH, 0o755);
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
console.log('mssql-mcp: installed binary to ' + BIN_PATH);
|
|
236
|
-
} finally {
|
|
237
|
-
// Best-effort cleanup; never fail the install over tempdir removal.
|
|
238
|
-
try {
|
|
239
|
-
fs.rmSync(tmpRoot, { recursive: true, force: true });
|
|
240
|
-
} catch (_) {
|
|
241
|
-
// ignored
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
// Run main() only when invoked directly as `node install.js` (npm postinstall),
|
|
247
|
-
// not when required by the smoke test (npm/test.js).
|
|
248
|
-
if (require.main === module) {
|
|
249
|
-
main().catch((e) => {
|
|
250
|
-
die('unexpected error: ' + (e && e.stack ? e.stack : String(e)));
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
// Exposed for the smoke test in npm/test.js. Not part of the public API.
|
|
255
|
-
module.exports = { ridFor, archiveExt, parseChecksumFile, classifyDownloadError };
|