From e20ff20cada2574007a36975b9a21c7da6f2c686 Mon Sep 17 00:00:00 2001 From: Peter Vyboch Date: Fri, 27 Mar 2026 19:38:49 +0100 Subject: [PATCH] fix: preserve quoted args with spaces when launching via CLI Args containing spaces (e.g. --description 'get todays date') were silently split into multiple tokens by the proxy, breaking MCP servers that rely on multi-word argument values. Co-Authored-By: Claude Sonnet 4.6 --- client/bin/start.js | 2 +- package-lock.json | 348 +++++++++++++++++++++++++- server/__tests__/args-parsing.test.ts | 176 +++++++++++++ server/package.json | 6 +- server/src/index.ts | 26 +- server/vitest.config.ts | 9 + 6 files changed, 560 insertions(+), 7 deletions(-) create mode 100644 server/__tests__/args-parsing.test.ts create mode 100644 server/vitest.config.ts diff --git a/client/bin/start.js b/client/bin/start.js index 7e7e013af..8d44f0fb2 100755 --- a/client/bin/start.js +++ b/client/bin/start.js @@ -106,7 +106,7 @@ async function startProdServer(serverOptions) { inspectorServerPath, ...(command ? [`--command=${command}`] : []), ...(mcpServerArgs && mcpServerArgs.length > 0 - ? [`--args=${mcpServerArgs.join(" ")}`] + ? [`--args=${JSON.stringify(mcpServerArgs)}`] : []), ...(transport ? [`--transport=${transport}`] : []), ...(serverUrl ? [`--server-url=${serverUrl}`] : []), diff --git a/package-lock.json b/package-lock.json index 19b73655b..2728aa5d0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5632,6 +5632,16 @@ "node": ">= 0.8" } }, + "node_modules/cac": { + "version": "6.7.14", + "resolved": "https://registry.npmjs.org/cac/-/cac-6.7.14.tgz", + "integrity": "sha512-b6Ilus+c3RrdDk+JhLKUAQfzzgLEPy6wcXqS7f/xe1EETvsDP6GORG7SFuOs6cID5YkqchW/LXZbX5bc8j7ZcQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8" + } + }, "node_modules/call-bind-apply-helpers": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.2.tgz", @@ -5748,6 +5758,16 @@ "node": ">=10" } }, + "node_modules/check-error": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/check-error/-/check-error-2.1.3.tgz", + "integrity": "sha512-PAJdDJusoxnwm1VwW07VWwUN1sl7smmC3OKggvndJFadxxDRyFJBX/ggnu/KE4kQAB7a3Dp8f/YXC1FlUprWmA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 16" + } + }, "node_modules/chokidar": { "version": "3.6.0", "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.6.0.tgz", @@ -6262,6 +6282,16 @@ } } }, + "node_modules/deep-eql": { + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-5.0.2.tgz", + "integrity": "sha512-h5k/5U50IJJFpzfL6nO9jaaumfjO/f2NjK/oYB2Djzm4p9L+3T9qWpZqZ2hAbLPuuYq9wrU08WQyBTL5GbPk5Q==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6" + } + }, "node_modules/deep-is": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/deep-is/-/deep-is-0.1.4.tgz", @@ -9835,6 +9865,13 @@ "loose-envify": "cli.js" } }, + "node_modules/loupe": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/loupe/-/loupe-3.2.1.tgz", + "integrity": "sha512-CdzqowRJCeLU72bHvWqwRBBlLcMEtIvGrlvef74kMnV2AolS9Y8xUv1I0U/MNAWMhBlKIoyuEgoJ0t/bbwHbLQ==", + "dev": true, + "license": "MIT" + }, "node_modules/lru-cache": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-5.1.1.tgz", @@ -10537,6 +10574,16 @@ "dev": true, "license": "MIT" }, + "node_modules/pathval": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/pathval/-/pathval-2.0.1.tgz", + "integrity": "sha512-//nshmD55c46FuFw26xV/xFAaB5HF9Xdap7HJBBnrKdAd6/GxDBaNA1870O79+9ueg61cZLSVc+OaFlfmObYVQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 14.16" + } + }, "node_modules/picocolors": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz", @@ -12144,6 +12191,26 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/strip-literal": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/strip-literal/-/strip-literal-3.1.0.tgz", + "integrity": "sha512-8r3mkIM/2+PpjHoOtiAW8Rg3jJLHaV7xPwG+YRGrv6FP0wwk/toTpATxWYOW0BKdWwl82VT2tFYi5DlROa0Mxg==", + "dev": true, + "license": "MIT", + "dependencies": { + "js-tokens": "^9.0.1" + }, + "funding": { + "url": "https://github.com/sponsors/antfu" + } + }, + "node_modules/strip-literal/node_modules/js-tokens": { + "version": "9.0.1", + "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-9.0.1.tgz", + "integrity": "sha512-mxa9E9ITFOt0ban3j6L5MpjwegGz6lBQmM1IJkWeBZGcMxto50+eWdjC/52xDbS2vy0k7vIMK0Fe2wfL9OQSpQ==", + "dev": true, + "license": "MIT" + }, "node_modules/sucrase": { "version": "3.35.1", "resolved": "https://registry.npmjs.org/sucrase/-/sucrase-3.35.1.tgz", @@ -12369,6 +12436,16 @@ "url": "https://github.com/sponsors/jonschlinkert" } }, + "node_modules/tinypool": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/tinypool/-/tinypool-1.1.1.tgz", + "integrity": "sha512-Zba82s87IFq9A9XmjiX5uZA/ARWDrB03OHlq+Vw1fSdt0I+4/Kutwy8BP4Y/y/aORMo61FQ0vIb5j44vSo5Pkg==", + "dev": true, + "license": "MIT", + "engines": { + "node": "^18.0.0 || >=20.0.0" + } + }, "node_modules/tinyrainbow": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/tinyrainbow/-/tinyrainbow-3.0.3.tgz", @@ -12379,6 +12456,16 @@ "node": ">=14.0.0" } }, + "node_modules/tinyspy": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/tinyspy/-/tinyspy-4.0.4.tgz", + "integrity": "sha512-azl+t0z7pw/z958Gy9svOTuzqIk6xq+NSheJzn5MMWtWTFywIacg2wUlzKFGtt3cthx0r2SxMK0yzJOR0IES7Q==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=14.0.0" + } + }, "node_modules/tldts": { "version": "6.1.86", "resolved": "https://registry.npmjs.org/tldts/-/tldts-6.1.86.tgz", @@ -13458,6 +13545,29 @@ } } }, + "node_modules/vite-node": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/vite-node/-/vite-node-3.2.4.tgz", + "integrity": "sha512-EbKSKh+bh1E1IFxeO0pg1n4dvoOTt0UDiXMd/qn++r98+jPO1xtJilvXldeuQ8giIB5IkpjCgMleHMNEsGH6pg==", + "dev": true, + "license": "MIT", + "dependencies": { + "cac": "^6.7.14", + "debug": "^4.4.1", + "es-module-lexer": "^1.7.0", + "pathe": "^2.0.3", + "vite": "^5.0.0 || ^6.0.0 || ^7.0.0-0" + }, + "bin": { + "vite-node": "vite-node.mjs" + }, + "engines": { + "node": "^18.0.0 || ^20.0.0 || >=22.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, "node_modules/vite/node_modules/fdir": { "version": "6.5.0", "resolved": "https://registry.npmjs.org/fdir/-/fdir-6.5.0.tgz", @@ -14025,7 +14135,243 @@ "@types/shell-quote": "^1.7.5", "@types/ws": "^8.5.12", "tsx": "^4.19.0", - "typescript": "^5.6.2" + "typescript": "^5.6.2", + "vitest": "^3.0.0" + } + }, + "server/node_modules/@vitest/expect": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/@vitest/expect/-/expect-3.2.4.tgz", + "integrity": "sha512-Io0yyORnB6sikFlt8QW5K7slY4OjqNX9jmJQ02QDda8lyM6B5oNgVWoSoKPac8/kgnCUzuHQKrSLtu/uOqqrig==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/chai": "^5.2.2", + "@vitest/spy": "3.2.4", + "@vitest/utils": "3.2.4", + "chai": "^5.2.0", + "tinyrainbow": "^2.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "server/node_modules/@vitest/pretty-format": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/@vitest/pretty-format/-/pretty-format-3.2.4.tgz", + "integrity": "sha512-IVNZik8IVRJRTr9fxlitMKeJeXFFFN0JaB9PHPGQ8NKQbGpfjlTx9zO4RefN8gp7eqjNy8nyK3NZmBzOPeIxtA==", + "dev": true, + "license": "MIT", + "dependencies": { + "tinyrainbow": "^2.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "server/node_modules/@vitest/runner": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/@vitest/runner/-/runner-3.2.4.tgz", + "integrity": "sha512-oukfKT9Mk41LreEW09vt45f8wx7DordoWUZMYdY/cyAk7w5TWkTRCNZYF7sX7n2wB7jyGAl74OxgwhPgKaqDMQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/utils": "3.2.4", + "pathe": "^2.0.3", + "strip-literal": "^3.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "server/node_modules/@vitest/snapshot": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/@vitest/snapshot/-/snapshot-3.2.4.tgz", + "integrity": "sha512-dEYtS7qQP2CjU27QBC5oUOxLE/v5eLkGqPE0ZKEIDGMs4vKWe7IjgLOeauHsR0D5YuuycGRO5oSRXnwnmA78fQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/pretty-format": "3.2.4", + "magic-string": "^0.30.17", + "pathe": "^2.0.3" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "server/node_modules/@vitest/spy": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/@vitest/spy/-/spy-3.2.4.tgz", + "integrity": "sha512-vAfasCOe6AIK70iP5UD11Ac4siNUNJ9i/9PZ3NKx07sG6sUxeag1LWdNrMWeKKYBLlzuK+Gn65Yd5nyL6ds+nw==", + "dev": true, + "license": "MIT", + "dependencies": { + "tinyspy": "^4.0.3" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "server/node_modules/@vitest/utils": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/@vitest/utils/-/utils-3.2.4.tgz", + "integrity": "sha512-fB2V0JFrQSMsCo9HiSq3Ezpdv4iYaXRG1Sx8edX3MwxfyNn83mKiGzOcH+Fkxt4MHxr3y42fQi1oeAInqgX2QA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/pretty-format": "3.2.4", + "loupe": "^3.1.4", + "tinyrainbow": "^2.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + } + }, + "server/node_modules/chai": { + "version": "5.3.3", + "resolved": "https://registry.npmjs.org/chai/-/chai-5.3.3.tgz", + "integrity": "sha512-4zNhdJD/iOjSH0A05ea+Ke6MU5mmpQcbQsSOkgdaUMJ9zTlDTD/GYlwohmIE2u0gaxHYiVHEn1Fw9mZ/ktJWgw==", + "dev": true, + "license": "MIT", + "dependencies": { + "assertion-error": "^2.0.1", + "check-error": "^2.1.1", + "deep-eql": "^5.0.1", + "loupe": "^3.1.0", + "pathval": "^2.0.0" + }, + "engines": { + "node": ">=18" + } + }, + "server/node_modules/picomatch": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.4.tgz", + "integrity": "sha512-QP88BAKvMam/3NxH6vj2o21R6MjxZUAd6nlwAS/pnGvN9IVLocLHxGYIzFhg6fUQ+5th6P4dv4eW9jX3DSIj7A==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, + "server/node_modules/tinyexec": { + "version": "0.3.2", + "resolved": "https://registry.npmjs.org/tinyexec/-/tinyexec-0.3.2.tgz", + "integrity": "sha512-KQQR9yN7R5+OSwaK0XQoj22pwHoTlgYqmUscPYoknOoWCWfj/5/ABTMRi69FrKU5ffPVh5QcFikpWJI/P1ocHA==", + "dev": true, + "license": "MIT" + }, + "server/node_modules/tinyrainbow": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/tinyrainbow/-/tinyrainbow-2.0.0.tgz", + "integrity": "sha512-op4nsTR47R6p0vMUUoYl/a+ljLFVtlfaXkLQmqfLR1qHma1h/ysYk4hEXZ880bf2CYgTskvTa/e196Vd5dDQXw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=14.0.0" + } + }, + "server/node_modules/vitest": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/vitest/-/vitest-3.2.4.tgz", + "integrity": "sha512-LUCP5ev3GURDysTWiP47wRRUpLKMOfPh+yKTx3kVIEiu5KOMeqzpnYNsKyOoVrULivR8tLcks4+lga33Whn90A==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/chai": "^5.2.2", + "@vitest/expect": "3.2.4", + "@vitest/mocker": "3.2.4", + "@vitest/pretty-format": "^3.2.4", + "@vitest/runner": "3.2.4", + "@vitest/snapshot": "3.2.4", + "@vitest/spy": "3.2.4", + "@vitest/utils": "3.2.4", + "chai": "^5.2.0", + "debug": "^4.4.1", + "expect-type": "^1.2.1", + "magic-string": "^0.30.17", + "pathe": "^2.0.3", + "picomatch": "^4.0.2", + "std-env": "^3.9.0", + "tinybench": "^2.9.0", + "tinyexec": "^0.3.2", + "tinyglobby": "^0.2.14", + "tinypool": "^1.1.1", + "tinyrainbow": "^2.0.0", + "vite": "^5.0.0 || ^6.0.0 || ^7.0.0-0", + "vite-node": "3.2.4", + "why-is-node-running": "^2.3.0" + }, + "bin": { + "vitest": "vitest.mjs" + }, + "engines": { + "node": "^18.0.0 || ^20.0.0 || >=22.0.0" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "@edge-runtime/vm": "*", + "@types/debug": "^4.1.12", + "@types/node": "^18.0.0 || ^20.0.0 || >=22.0.0", + "@vitest/browser": "3.2.4", + "@vitest/ui": "3.2.4", + "happy-dom": "*", + "jsdom": "*" + }, + "peerDependenciesMeta": { + "@edge-runtime/vm": { + "optional": true + }, + "@types/debug": { + "optional": true + }, + "@types/node": { + "optional": true + }, + "@vitest/browser": { + "optional": true + }, + "@vitest/ui": { + "optional": true + }, + "happy-dom": { + "optional": true + }, + "jsdom": { + "optional": true + } + } + }, + "server/node_modules/vitest/node_modules/@vitest/mocker": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/@vitest/mocker/-/mocker-3.2.4.tgz", + "integrity": "sha512-46ryTE9RZO/rfDd7pEqFl7etuyzekzEhUbTW3BvmeO/BcCMEgq59BKhek3dXDWgAj4oMK6OZi+vRr1wPW6qjEQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@vitest/spy": "3.2.4", + "estree-walker": "^3.0.3", + "magic-string": "^0.30.17" + }, + "funding": { + "url": "https://opencollective.com/vitest" + }, + "peerDependencies": { + "msw": "^2.4.9", + "vite": "^5.0.0 || ^6.0.0 || ^7.0.0-0" + }, + "peerDependenciesMeta": { + "msw": { + "optional": true + }, + "vite": { + "optional": true + } } } } diff --git a/server/__tests__/args-parsing.test.ts b/server/__tests__/args-parsing.test.ts new file mode 100644 index 000000000..4ab8b6787 --- /dev/null +++ b/server/__tests__/args-parsing.test.ts @@ -0,0 +1,176 @@ +import { describe, it, expect } from "vitest"; +import { parse as shellParseArgs, quote as shellQuoteArgs } from "shell-quote"; + +// Mirrors the logic in the /config endpoint +function processArgsForClient(rawArgs: string): string { + if (!rawArgs) return rawArgs; + try { + const parsed = JSON.parse(rawArgs); + if (Array.isArray(parsed)) { + return shellQuoteArgs(parsed); + } + } catch { + // Not JSON — legacy shell string, pass through unchanged + } + return rawArgs; +} + +// Mirrors the logic in createTransport +function parseArgsForTransport(rawArgs: string): string[] { + if (!rawArgs) return []; + try { + const parsed = JSON.parse(rawArgs); + if (Array.isArray(parsed)) { + return parsed as string[]; + } + } catch { + // Not JSON + } + return shellParseArgs(rawArgs) as string[]; +} + +describe("start.js serialization: args array → JSON string", () => { + it("preserves args with spaces when serialized as JSON array", () => { + const mcpServerArgs = [ + "--tool", + "date", + "--description", + "get todays date", + "--command", + "date", + ]; + const serialized = JSON.stringify(mcpServerArgs); + expect(serialized).toBe( + '["--tool","date","--description","get todays date","--command","date"]', + ); + }); + + it("round-trips args with spaces through JSON without corruption", () => { + const original = ["--description", "get todays date", "--command", "date"]; + const deserialized = JSON.parse(JSON.stringify(original)); + expect(deserialized).toEqual(original); + }); +}); + +describe("/config endpoint: JSON array → shell-quoted string for client UI", () => { + it("converts JSON array to properly quoted shell string", () => { + const rawArgs = JSON.stringify([ + "--tool", + "date", + "--description", + "get todays date", + "--command", + "date", + ]); + const result = processArgsForClient(rawArgs); + expect(result).toBe( + "--tool date --description 'get todays date' --command date", + ); + }); + + it("passes legacy shell string through unchanged", () => { + const legacyArgs = + "--tool date --description 'get todays date' --command date"; + expect(processArgsForClient(legacyArgs)).toBe(legacyArgs); + }); + + it("handles empty args string", () => { + expect(processArgsForClient("")).toBe(""); + }); + + it("handles args with no spaces (no quoting needed)", () => { + const rawArgs = JSON.stringify(["--verbose", "--port", "8080"]); + expect(processArgsForClient(rawArgs)).toBe("--verbose --port 8080"); + }); + + it("handles args with embedded single quotes", () => { + const rawArgs = JSON.stringify(["--message", "it's working"]); + const result = processArgsForClient(rawArgs); + // shell-quote uses double-quotes when value contains a single quote + const parsed = shellParseArgs(result) as string[]; + expect(parsed).toEqual(["--message", "it's working"]); + }); + + it("handles args with embedded double quotes", () => { + const rawArgs = JSON.stringify(["--message", 'say "hello"']); + const result = processArgsForClient(rawArgs); + const parsed = shellParseArgs(result) as string[]; + expect(parsed).toEqual(["--message", 'say "hello"']); + }); +}); + +describe("createTransport: args string → parsed args array", () => { + it("accepts JSON array directly and returns elements as-is", () => { + const jsonArgs = JSON.stringify(["--description", "get todays date"]); + const result = parseArgsForTransport(jsonArgs); + expect(result).toEqual(["--description", "get todays date"]); + expect(result).toHaveLength(2); + }); + + it("falls back to shellParseArgs for shell-quoted string", () => { + const shellArgs = "--description 'get todays date'"; + const result = parseArgsForTransport(shellArgs); + expect(result).toEqual(["--description", "get todays date"]); + expect(result).toHaveLength(2); + }); + + it("parses plain args without quotes correctly", () => { + const result = parseArgsForTransport("--verbose --port 8080"); + expect(result).toEqual(["--verbose", "--port", "8080"]); + }); + + it("handles empty string", () => { + expect(parseArgsForTransport("")).toEqual([]); + }); +}); + +describe("full round-trip: start.js → /config → createTransport", () => { + it("args with spaces survive the complete pipeline", () => { + // Step 1: start.js serializes the args array as JSON + const originalArgs = [ + "--tool", + "date", + "--description", + "get todays date", + "--command", + "date", + ]; + const serialized = JSON.stringify(originalArgs); + + // Step 2: /config converts JSON array → shell-quoted string for UI + const clientDisplayString = processArgsForClient(serialized); + expect(clientDisplayString).toBe( + "--tool date --description 'get todays date' --command date", + ); + + // Step 3: client sends display string; createTransport parses it + const finalArgs = parseArgsForTransport(clientDisplayString); + expect(finalArgs).toEqual(originalArgs); + expect(finalArgs).toHaveLength(6); + }); + + it("args without spaces survive the complete pipeline unchanged", () => { + const originalArgs = ["--verbose", "--port", "8080"]; + const serialized = JSON.stringify(originalArgs); + const clientDisplayString = processArgsForClient(serialized); + expect(clientDisplayString).toBe("--verbose --port 8080"); + const finalArgs = parseArgsForTransport(clientDisplayString); + expect(finalArgs).toEqual(originalArgs); + }); + + it("legacy manually-typed shell string (no JSON) still works", () => { + // User types args manually in the UI text box — never goes through start.js + const manuallyTyped = "--tool date --description 'get todays date'"; + // /config would return this unchanged (it's not JSON) + const fromConfig = processArgsForClient(manuallyTyped); + expect(fromConfig).toBe(manuallyTyped); + // createTransport parses it correctly + const finalArgs = parseArgsForTransport(fromConfig); + expect(finalArgs).toEqual([ + "--tool", + "date", + "--description", + "get todays date", + ]); + }); +}); diff --git a/server/package.json b/server/package.json index c674018a8..7b7da8d3d 100644 --- a/server/package.json +++ b/server/package.json @@ -19,7 +19,8 @@ "build": "tsc && shx cp -R static build", "start": "node build/index.js", "dev": "tsx watch --clear-screen=false src/index.ts", - "dev:windows": "tsx watch --clear-screen=false src/index.ts < NUL" + "dev:windows": "tsx watch --clear-screen=false src/index.ts < NUL", + "test": "vitest run" }, "devDependencies": { "@types/cors": "^2.8.19", @@ -27,7 +28,8 @@ "@types/shell-quote": "^1.7.5", "@types/ws": "^8.5.12", "tsx": "^4.19.0", - "typescript": "^5.6.2" + "typescript": "^5.6.2", + "vitest": "^3.0.0" }, "dependencies": { "@modelcontextprotocol/sdk": "^1.25.2", diff --git a/server/src/index.ts b/server/src/index.ts index 4d1fffa29..852d5002a 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -2,7 +2,7 @@ import cors from "cors"; import { parseArgs } from "node:util"; -import { parse as shellParseArgs } from "shell-quote"; +import { parse as shellParseArgs, quote as shellQuoteArgs } from "shell-quote"; import nodeFetch, { Headers as NodeHeaders } from "node-fetch"; // Type-compatible wrappers for node-fetch to work with browser-style types @@ -375,7 +375,16 @@ const createTransport = async ( if (transportType === "stdio") { const command = (query.command as string).trim(); - const origArgs = shellParseArgs(query.args as string) as string[]; + const rawArgs = query.args as string; + let origArgs: string[]; + try { + const parsed = JSON.parse(rawArgs); + origArgs = Array.isArray(parsed) + ? (parsed as string[]) + : (shellParseArgs(rawArgs) as string[]); + } catch { + origArgs = shellParseArgs(rawArgs) as string[]; + } const queryEnv = query.env ? JSON.parse(query.env as string) : {}; const env = { ...defaultEnvironment, ...process.env, ...queryEnv }; @@ -786,7 +795,18 @@ app.get("/config", originValidationMiddleware, authMiddleware, (req, res) => { res.json({ defaultEnvironment, defaultCommand: values.command, - defaultArgs: values.args, + defaultArgs: (() => { + if (!values.args) return values.args; + try { + const parsed = JSON.parse(values.args); + if (Array.isArray(parsed)) { + return shellQuoteArgs(parsed); + } + } catch { + // Not JSON — legacy shell string, pass through unchanged + } + return values.args; + })(), defaultTransport: values.transport, defaultServerUrl: values["server-url"], }); diff --git a/server/vitest.config.ts b/server/vitest.config.ts new file mode 100644 index 000000000..8c3ee959e --- /dev/null +++ b/server/vitest.config.ts @@ -0,0 +1,9 @@ +import { defineConfig } from "vitest/config"; + +export default defineConfig({ + test: { + globals: true, + environment: "node", + include: ["__tests__/**/*.test.ts"], + }, +});