| 1 | import { |
| 2 | converMcpServersJsonConfigFileToYamlBlocks, |
| 3 | convertJsonMcpConfigToYamlMcpConfig, |
| 4 | convertYamlMcpConfigToJsonMcpConfig, |
| 5 | } from "./convertJson.js"; |
| 6 | import type { HttpMcpServer, SseMcpServer, StdioMcpServer } from "./index.js"; |
| 7 | import { |
| 8 | claudeDesktopLikeConfigFileSchema, |
| 9 | mcpServersJsonSchema, |
| 10 | type HttpMcpJsonConfig, |
| 11 | type McpServersJsonConfigFile, |
| 12 | type SseMcpJsonConfig, |
| 13 | type StdioMcpJsonConfig, |
| 14 | } from "./json.js"; |
| 15 | |
| 16 | describe("convertJsonMcpConfigToYamlMcpConfig", () => { |
| 17 | describe("STDIO configurations", () => { |
| 18 | test("converts basic stdio config", () => { |
| 19 | const jsonConfig: StdioMcpJsonConfig = { |
| 20 | command: "node", |
| 21 | args: ["server.js"], |
| 22 | }; |
| 23 | |
| 24 | const result = convertJsonMcpConfigToYamlMcpConfig( |
| 25 | "test-server", |
| 26 | jsonConfig, |
| 27 | ); |
| 28 | |
| 29 | expect(result.yamlConfig).toEqual({ |
| 30 | name: "test-server", |
| 31 | type: "stdio", |
| 32 | command: "node", |
| 33 | args: ["server.js"], |
| 34 | }); |
| 35 | expect(result.warnings).toHaveLength(0); |
| 36 | }); |
| 37 | |
| 38 | test("converts stdio config with all fields", () => { |
| 39 | const jsonConfig: StdioMcpJsonConfig = { |
| 40 | type: "stdio", |
| 41 | command: "python", |
| 42 | args: ["-m", "server"], |
| 43 | env: { |
| 44 | API_KEY: "test-key", |
| 45 | DEBUG: "true", |
| 46 | }, |
| 47 | }; |
| 48 | |
| 49 | const result = convertJsonMcpConfigToYamlMcpConfig( |
| 50 | "python-server", |
| 51 | jsonConfig, |
| 52 | ); |
| 53 | |
| 54 | expect(result.yamlConfig).toEqual({ |
| 55 | name: "python-server", |
| 56 | type: "stdio", |
| 57 | command: "python", |
| 58 | args: ["-m", "server"], |
| 59 | env: { |
| 60 | API_KEY: "test-key", |
| 61 | DEBUG: "true", |
| 62 | }, |
| 63 | }); |
| 64 | expect(result.warnings).toHaveLength(0); |
| 65 | }); |
| 66 | |
| 67 | test("warns about unsupported envFile", () => { |
| 68 | const jsonConfig: StdioMcpJsonConfig = { |
| 69 | command: "node", |
| 70 | args: ["server.js"], |
| 71 | envFile: ".env", |
| 72 | }; |
| 73 | |
| 74 | const result = convertJsonMcpConfigToYamlMcpConfig( |
| 75 | "env-server", |
| 76 | jsonConfig, |
| 77 | ); |
| 78 | |
| 79 | expect(result.yamlConfig).toEqual({ |
| 80 | name: "env-server", |
| 81 | type: "stdio", |
| 82 | command: "node", |
| 83 | args: ["server.js"], |
| 84 | }); |
| 85 | expect(result.warnings).toHaveLength(1); |
| 86 | expect(result.warnings[0]).toContain("envFile is not supported"); |
| 87 | }); |
| 88 | |
| 89 | test("converts stdio config from parsed JSON string", () => { |
| 90 | const jsonString = JSON.stringify({ |
| 91 | command: "deno", |
| 92 | args: ["run", "server.ts"], |
| 93 | env: { PORT: "3000" }, |
| 94 | }); |
| 95 | const parsed = mcpServersJsonSchema.parse(JSON.parse(jsonString)); |
| 96 | |
| 97 | const result = convertJsonMcpConfigToYamlMcpConfig("deno-server", parsed); |
| 98 | |
| 99 | expect(result.yamlConfig).toEqual({ |
| 100 | name: "deno-server", |
| 101 | type: "stdio", |
| 102 | command: "deno", |
| 103 | args: ["run", "server.ts"], |
| 104 | env: { PORT: "3000" }, |
| 105 | }); |
| 106 | }); |
| 107 | }); |
| 108 | |
| 109 | describe("SSE/HTTP configurations", () => { |
| 110 | test("converts basic SSE config", () => { |
| 111 | const jsonConfig: SseMcpJsonConfig = { |
| 112 | url: "https://api.example.com/sse", |
| 113 | }; |
| 114 | |
| 115 | const result = convertJsonMcpConfigToYamlMcpConfig( |
| 116 | "sse-server", |
| 117 | jsonConfig, |
| 118 | ); |
| 119 | |
| 120 | expect(result.yamlConfig).toEqual({ |
| 121 | name: "sse-server", |
| 122 | url: "https://api.example.com/sse", |
| 123 | }); |
| 124 | expect(result.warnings).toHaveLength(0); |
| 125 | }); |
| 126 | |
| 127 | test("converts SSE config with type and headers", () => { |
| 128 | const jsonConfig: SseMcpJsonConfig = { |
| 129 | type: "sse", |
| 130 | url: "https://api.example.com/sse", |
| 131 | headers: { |
| 132 | Authorization: "Bearer token", |
| 133 | "X-Custom-Header": "value", |
| 134 | }, |
| 135 | }; |
| 136 | |
| 137 | const result = convertJsonMcpConfigToYamlMcpConfig( |
| 138 | "sse-auth", |
| 139 | jsonConfig, |
| 140 | ); |
| 141 | |
| 142 | expect(result.yamlConfig).toEqual({ |
| 143 | name: "sse-auth", |
| 144 | type: "sse", |
| 145 | url: "https://api.example.com/sse", |
| 146 | requestOptions: { |
| 147 | headers: { |
| 148 | Authorization: "Bearer token", |
| 149 | "X-Custom-Header": "value", |
| 150 | }, |
| 151 | }, |
| 152 | }); |
| 153 | expect(result.warnings).toHaveLength(0); |
| 154 | }); |
| 155 | |
| 156 | test("converts HTTP config", () => { |
| 157 | const jsonConfig: HttpMcpJsonConfig = { |
| 158 | type: "http", |
| 159 | url: "https://api.example.com/http", |
| 160 | headers: { |
| 161 | "Content-Type": "application/json", |
| 162 | }, |
| 163 | }; |
| 164 | |
| 165 | const result = convertJsonMcpConfigToYamlMcpConfig( |
| 166 | "http-server", |
| 167 | jsonConfig, |
| 168 | ); |
| 169 | |
| 170 | expect(result.yamlConfig).toEqual({ |
| 171 | name: "http-server", |
| 172 | type: "streamable-http", |
| 173 | url: "https://api.example.com/http", |
| 174 | requestOptions: { |
| 175 | headers: { |
| 176 | "Content-Type": "application/json", |
| 177 | }, |
| 178 | }, |
| 179 | }); |
| 180 | expect(result.warnings).toHaveLength(0); |
| 181 | }); |
| 182 | |
| 183 | test("converts HTTP config from parsed JSON string", () => { |
| 184 | const jsonString = JSON.stringify({ |
| 185 | type: "http", |
| 186 | url: "https://test.com/api", |
| 187 | headers: { "API-Key": "secret" }, |
| 188 | }); |
| 189 | const parsed = mcpServersJsonSchema.parse(JSON.parse(jsonString)); |
| 190 | |
| 191 | const result = convertJsonMcpConfigToYamlMcpConfig("parsed-http", parsed); |
| 192 | |
| 193 | expect(result.yamlConfig).toEqual({ |
| 194 | name: "parsed-http", |
| 195 | type: "streamable-http", |
| 196 | url: "https://test.com/api", |
| 197 | requestOptions: { |
| 198 | headers: { "API-Key": "secret" }, |
| 199 | }, |
| 200 | }); |
| 201 | }); |
| 202 | }); |
| 203 | |
| 204 | test("throws error for invalid config", () => { |
| 205 | const invalidConfig = { |
| 206 | invalid: "config", |
| 207 | } as any; |
| 208 | |
| 209 | expect(() => |
| 210 | convertJsonMcpConfigToYamlMcpConfig("invalid", invalidConfig), |
| 211 | ).toThrowError("Invalid MCP server configuration"); |
| 212 | }); |
| 213 | }); |
| 214 | |
| 215 | describe("convertYamlMcpConfigToJsonMcpConfig", () => { |
| 216 | describe("STDIO configurations", () => { |
| 217 | test("converts basic stdio config", () => { |
| 218 | const yamlConfig: StdioMcpServer = { |
| 219 | name: "test-server", |
| 220 | type: "stdio", |
| 221 | command: "node", |
| 222 | args: ["server.js"], |
| 223 | }; |
| 224 | |
| 225 | const result = convertYamlMcpConfigToJsonMcpConfig(yamlConfig); |
| 226 | |
| 227 | expect(result.name).toBe("test-server"); |
| 228 | expect(result.jsonConfig).toEqual({ |
| 229 | type: "stdio", |
| 230 | command: "node", |
| 231 | args: ["server.js"], |
| 232 | }); |
| 233 | expect(result.MCP_TIMEOUT).toBeUndefined(); |
| 234 | expect(result.warnings).toHaveLength(0); |
| 235 | }); |
| 236 | |
| 237 | test("converts stdio config with env and timeout", () => { |
| 238 | const yamlConfig: StdioMcpServer = { |
| 239 | name: "python-server", |
| 240 | command: "python", |
| 241 | args: ["-m", "server"], |
| 242 | env: { |
| 243 | API_KEY: "test-key", |
| 244 | DEBUG: "true", |
| 245 | }, |
| 246 | connectionTimeout: 30000, |
| 247 | }; |
| 248 | |
| 249 | const result = convertYamlMcpConfigToJsonMcpConfig(yamlConfig); |
| 250 | |
| 251 | expect(result.name).toBe("python-server"); |
| 252 | expect(result.jsonConfig).toEqual({ |
| 253 | type: "stdio", |
| 254 | command: "python", |
| 255 | args: ["-m", "server"], |
| 256 | env: { |
| 257 | API_KEY: "test-key", |
| 258 | DEBUG: "true", |
| 259 | }, |
| 260 | }); |
| 261 | expect(result.MCP_TIMEOUT).toBe("30000"); |
| 262 | expect(result.warnings).toHaveLength(0); |
| 263 | }); |
| 264 | |
| 265 | test("warns about unsupported cwd field", () => { |
| 266 | const yamlConfig: StdioMcpServer = { |
| 267 | name: "cwd-server", |
| 268 | command: "node", |
| 269 | cwd: "/path/to/dir", |
| 270 | }; |
| 271 | |
| 272 | const result = convertYamlMcpConfigToJsonMcpConfig(yamlConfig); |
| 273 | |
| 274 | expect(result.jsonConfig).toEqual({ |
| 275 | type: "stdio", |
| 276 | command: "node", |
| 277 | }); |
| 278 | expect(result.warnings).toHaveLength(1); |
| 279 | expect(result.warnings[0]).toBe( |
| 280 | "`cwd` from YAML MCP config not supported in Claude-style JSON, will be removed from server cwd-server", |
| 281 | ); |
| 282 | }); |
| 283 | |
| 284 | test("warns about unsupported faviconUrl", () => { |
| 285 | const yamlConfig: StdioMcpServer = { |
| 286 | name: "icon-server", |
| 287 | command: "node", |
| 288 | faviconUrl: "https://example.com/icon.png", |
| 289 | }; |
| 290 | |
| 291 | const result = convertYamlMcpConfigToJsonMcpConfig(yamlConfig); |
| 292 | |
| 293 | expect(result.warnings).toHaveLength(1); |
| 294 | expect(result.warnings[0]).toBe( |
| 295 | "`faviconUrl` from YAML MCP config not supported in Claude-style JSON, will be removed from server icon-server", |
| 296 | ); |
| 297 | }); |
| 298 | |
| 299 | test("converts parsed YAML stdio config", () => { |
| 300 | const yamlConfig: StdioMcpServer = { |
| 301 | name: "parsed-stdio", |
| 302 | type: "stdio", |
| 303 | command: "bun", |
| 304 | args: ["run", "server.ts"], |
| 305 | env: { NODE_ENV: "production" }, |
| 306 | }; |
| 307 | |
| 308 | const result = convertYamlMcpConfigToJsonMcpConfig(yamlConfig); |
| 309 | |
| 310 | expect(result.jsonConfig).toEqual({ |
| 311 | type: "stdio", |
| 312 | command: "bun", |
| 313 | args: ["run", "server.ts"], |
| 314 | env: { NODE_ENV: "production" }, |
| 315 | }); |
| 316 | }); |
| 317 | }); |
| 318 | |
| 319 | describe("SSE/HTTP configurations", () => { |
| 320 | test("converts basic SSE config", () => { |
| 321 | const yamlConfig: SseMcpServer = { |
| 322 | name: "sse-server", |
| 323 | url: "https://api.example.com/sse", |
| 324 | }; |
| 325 | |
| 326 | const result = convertYamlMcpConfigToJsonMcpConfig(yamlConfig); |
| 327 | |
| 328 | expect(result.name).toBe("sse-server"); |
| 329 | expect(result.jsonConfig).toEqual({ |
| 330 | url: "https://api.example.com/sse", |
| 331 | }); |
| 332 | expect(result.warnings).toHaveLength(0); |
| 333 | }); |
| 334 | |
| 335 | test("converts SSE config with type and headers", () => { |
| 336 | const yamlConfig: SseMcpServer = { |
| 337 | name: "sse-auth", |
| 338 | type: "sse", |
| 339 | url: "https://api.example.com/sse", |
| 340 | requestOptions: { |
| 341 | headers: { |
| 342 | Authorization: "Bearer token", |
| 343 | "X-Custom-Header": "value", |
| 344 | }, |
| 345 | }, |
| 346 | }; |
| 347 | |
| 348 | const result = convertYamlMcpConfigToJsonMcpConfig(yamlConfig); |
| 349 | |
| 350 | expect(result.jsonConfig).toEqual({ |
| 351 | type: "sse", |
| 352 | url: "https://api.example.com/sse", |
| 353 | headers: { |
| 354 | Authorization: "Bearer token", |
| 355 | "X-Custom-Header": "value", |
| 356 | }, |
| 357 | }); |
| 358 | expect(result.warnings).toHaveLength(0); |
| 359 | }); |
| 360 | |
| 361 | test("converts HTTP config", () => { |
| 362 | const yamlConfig: HttpMcpServer = { |
| 363 | name: "http-server", |
| 364 | type: "streamable-http", |
| 365 | url: "https://api.example.com/http", |
| 366 | requestOptions: { |
| 367 | headers: { |
| 368 | "Content-Type": "application/json", |
| 369 | }, |
| 370 | }, |
| 371 | }; |
| 372 | |
| 373 | const result = convertYamlMcpConfigToJsonMcpConfig(yamlConfig); |
| 374 | |
| 375 | expect(result.jsonConfig).toEqual({ |
| 376 | type: "http", |
| 377 | url: "https://api.example.com/http", |
| 378 | headers: { |
| 379 | "Content-Type": "application/json", |
| 380 | }, |
| 381 | }); |
| 382 | expect(result.warnings).toHaveLength(0); |
| 383 | }); |
| 384 | |
| 385 | test("warns about unsupported requestOptions fields", () => { |
| 386 | const yamlConfig: SseMcpServer = { |
| 387 | name: "complex-server", |
| 388 | url: "https://api.example.com", |
| 389 | requestOptions: { |
| 390 | headers: { "API-Key": "secret" }, |
| 391 | timeout: 5000, |
| 392 | proxy: "http://proxy.com", |
| 393 | verifySsl: false, |
| 394 | }, |
| 395 | }; |
| 396 | |
| 397 | const result = convertYamlMcpConfigToJsonMcpConfig(yamlConfig); |
| 398 | |
| 399 | expect(result.jsonConfig).toEqual({ |
| 400 | url: "https://api.example.com", |
| 401 | headers: { "API-Key": "secret" }, |
| 402 | }); |
| 403 | expect(result.warnings).toHaveLength(3); |
| 404 | expect(result.warnings).toContain( |
| 405 | "timeout requestOption from YAML MCP config not supported in Claude-style JSON, will be ignored in server complex-server", |
| 406 | ); |
| 407 | expect(result.warnings).toContain( |
| 408 | "proxy requestOption from YAML MCP config not supported in Claude-style JSON, will be ignored in server complex-server", |
| 409 | ); |
| 410 | expect(result.warnings).toContain( |
| 411 | "verifySsl requestOption from YAML MCP config not supported in Claude-style JSON, will be ignored in server complex-server", |
| 412 | ); |
| 413 | }); |
| 414 | |
| 415 | test("converts parsed YAML HTTP config", () => { |
| 416 | const yamlConfig: HttpMcpServer = { |
| 417 | name: "parsed-http", |
| 418 | type: "streamable-http", |
| 419 | url: "https://test.com/api", |
| 420 | requestOptions: { |
| 421 | headers: { "API-Key": "secret" }, |
| 422 | }, |
| 423 | connectionTimeout: 10000, |
| 424 | }; |
| 425 | |
| 426 | const result = convertYamlMcpConfigToJsonMcpConfig(yamlConfig); |
| 427 | |
| 428 | expect(result.jsonConfig).toEqual({ |
| 429 | type: "http", |
| 430 | url: "https://test.com/api", |
| 431 | headers: { "API-Key": "secret" }, |
| 432 | }); |
| 433 | expect(result.MCP_TIMEOUT).toBe("10000"); |
| 434 | }); |
| 435 | }); |
| 436 | |
| 437 | test("throws error for invalid config", () => { |
| 438 | const invalidConfig = { |
| 439 | name: "invalid", |
| 440 | invalid: "config", |
| 441 | } as any; |
| 442 | |
| 443 | expect(() => |
| 444 | convertYamlMcpConfigToJsonMcpConfig(invalidConfig), |
| 445 | ).toThrowError("Invalid MCP server configuration"); |
| 446 | }); |
| 447 | }); |
| 448 | |
| 449 | describe("converMcpServersJsonConfigFileToYamlBlocks", () => { |
| 450 | test("converts empty file", () => { |
| 451 | const jsonFile: McpServersJsonConfigFile = { |
| 452 | mcpServers: {}, |
| 453 | }; |
| 454 | |
| 455 | const result = converMcpServersJsonConfigFileToYamlBlocks(jsonFile); |
| 456 | |
| 457 | expect(result.yamlConfigs).toEqual([]); |
| 458 | expect(result.warnings).toHaveLength(0); |
| 459 | }); |
| 460 | |
| 461 | test("converts file with multiple servers", () => { |
| 462 | const jsonFile: McpServersJsonConfigFile = { |
| 463 | mcpServers: { |
| 464 | "weather-server": { |
| 465 | command: "npx", |
| 466 | args: ["@example/weather-server"], |
| 467 | env: { |
| 468 | WEATHER_API_KEY: "key123", |
| 469 | }, |
| 470 | }, |
| 471 | "database-server": { |
| 472 | type: "stdio", |
| 473 | command: "python", |
| 474 | args: ["-m", "db_server"], |
| 475 | }, |
| 476 | "api-server": { |
| 477 | type: "http", |
| 478 | url: "https://api.example.com", |
| 479 | headers: { |
| 480 | Authorization: "Bearer token", |
| 481 | }, |
| 482 | }, |
| 483 | "sse-server": { |
| 484 | type: "sse", |
| 485 | url: "https://sse.example.com/stream", |
| 486 | }, |
| 487 | }, |
| 488 | }; |
| 489 | |
| 490 | const result = converMcpServersJsonConfigFileToYamlBlocks(jsonFile); |
| 491 | |
| 492 | expect(result.yamlConfigs).toHaveLength(4); |
| 493 | expect(result.warnings).toHaveLength(0); |
| 494 | |
| 495 | // Check each converted server |
| 496 | expect(result.yamlConfigs[0]).toEqual({ |
| 497 | name: "weather-server", |
| 498 | type: "stdio", |
| 499 | command: "npx", |
| 500 | args: ["@example/weather-server"], |
| 501 | env: { |
| 502 | WEATHER_API_KEY: "key123", |
| 503 | }, |
| 504 | }); |
| 505 | |
| 506 | expect(result.yamlConfigs[1]).toEqual({ |
| 507 | name: "database-server", |
| 508 | type: "stdio", |
| 509 | command: "python", |
| 510 | args: ["-m", "db_server"], |
| 511 | }); |
| 512 | |
| 513 | expect(result.yamlConfigs[2]).toEqual({ |
| 514 | name: "api-server", |
| 515 | type: "streamable-http", |
| 516 | url: "https://api.example.com", |
| 517 | requestOptions: { |
| 518 | headers: { |
| 519 | Authorization: "Bearer token", |
| 520 | }, |
| 521 | }, |
| 522 | }); |
| 523 | |
| 524 | expect(result.yamlConfigs[3]).toEqual({ |
| 525 | name: "sse-server", |
| 526 | type: "sse", |
| 527 | url: "https://sse.example.com/stream", |
| 528 | }); |
| 529 | }); |
| 530 | |
| 531 | test("collects warnings from multiple servers", () => { |
| 532 | const jsonFile: McpServersJsonConfigFile = { |
| 533 | mcpServers: { |
| 534 | server1: { |
| 535 | command: "node", |
| 536 | envFile: ".env", |
| 537 | }, |
| 538 | server2: { |
| 539 | command: "python", |
| 540 | args: ["app.py"], |
| 541 | envFile: ".env.production", |
| 542 | }, |
| 543 | }, |
| 544 | }; |
| 545 | |
| 546 | const result = converMcpServersJsonConfigFileToYamlBlocks(jsonFile); |
| 547 | |
| 548 | expect(result.yamlConfigs).toHaveLength(2); |
| 549 | expect(result.warnings).toHaveLength(2); |
| 550 | expect(result.warnings[0]).toContain("server1"); |
| 551 | expect(result.warnings[1]).toContain("server2"); |
| 552 | }); |
| 553 | |
| 554 | test("converts parsed JSON file", () => { |
| 555 | const jsonString = JSON.stringify({ |
| 556 | mcpServers: { |
| 557 | "test-stdio": { |
| 558 | command: "node", |
| 559 | args: ["index.js"], |
| 560 | }, |
| 561 | "test-http": { |
| 562 | type: "http", |
| 563 | url: "https://test.com", |
| 564 | headers: { "X-Test": "value" }, |
| 565 | }, |
| 566 | }, |
| 567 | }); |
| 568 | const parsed = claudeDesktopLikeConfigFileSchema.parse( |
| 569 | JSON.parse(jsonString), |
| 570 | ); |
| 571 | |
| 572 | const result = converMcpServersJsonConfigFileToYamlBlocks(parsed); |
| 573 | |
| 574 | expect(result.yamlConfigs).toHaveLength(2); |
| 575 | expect(result.yamlConfigs[0].name).toBe("test-stdio"); |
| 576 | expect(result.yamlConfigs[1].name).toBe("test-http"); |
| 577 | }); |
| 578 | |
| 579 | test("handles mixed valid and problematic configurations", () => { |
| 580 | const jsonFile: McpServersJsonConfigFile = { |
| 581 | mcpServers: { |
| 582 | "good-server": { |
| 583 | command: "node", |
| 584 | args: ["server.js"], |
| 585 | }, |
| 586 | "warning-server": { |
| 587 | command: "python", |
| 588 | envFile: ".env", |
| 589 | env: { |
| 590 | PORT: "3000", |
| 591 | }, |
| 592 | }, |
| 593 | }, |
| 594 | }; |
| 595 | |
| 596 | const result = converMcpServersJsonConfigFileToYamlBlocks(jsonFile); |
| 597 | |
| 598 | expect(result.yamlConfigs).toHaveLength(2); |
| 599 | expect(result.warnings).toHaveLength(1); |
| 600 | |
| 601 | // Check that both servers were converted |
| 602 | expect( |
| 603 | result.yamlConfigs.find((c) => c.name === "good-server"), |
| 604 | ).toBeTruthy(); |
| 605 | expect( |
| 606 | result.yamlConfigs.find((c) => c.name === "warning-server"), |
| 607 | ).toBeTruthy(); |
| 608 | |
| 609 | // Check that the warning server still has its env vars |
| 610 | const warningServer = result.yamlConfigs.find( |
| 611 | (c) => c.name === "warning-server", |
| 612 | ); |
| 613 | expect(warningServer).toMatchObject({ |
| 614 | env: { PORT: "3000" }, |
| 615 | }); |
| 616 | }); |
| 617 | |
| 618 | test("handles environment variable templating in JSON file", () => { |
| 619 | const jsonFile: McpServersJsonConfigFile = { |
| 620 | mcpServers: { |
| 621 | "weather-server": { |
| 622 | command: "npx", |
| 623 | args: ["@example/weather-server"], |
| 624 | env: { |
| 625 | WEATHER_API_KEY: "${WEATHER_API_KEY_ENV_VAR}", |
| 626 | STATIC_VALUE: "production", |
| 627 | COMPLEX: "https://${API_HOST}:${API_PORT}/v1", |
| 628 | }, |
| 629 | }, |
| 630 | }, |
| 631 | }; |
| 632 | |
| 633 | const result = converMcpServersJsonConfigFileToYamlBlocks(jsonFile); |
| 634 | |
| 635 | expect(result.yamlConfigs).toHaveLength(1); |
| 636 | expect(result.yamlConfigs[0]).toEqual({ |
| 637 | name: "weather-server", |
| 638 | type: "stdio", |
| 639 | command: "npx", |
| 640 | args: ["@example/weather-server"], |
| 641 | env: { |
| 642 | WEATHER_API_KEY: "${{ secrets.WEATHER_API_KEY_ENV_VAR }}", |
| 643 | STATIC_VALUE: "production", |
| 644 | COMPLEX: "https://${{ secrets.API_HOST }}:${{ secrets.API_PORT }}/v1", |
| 645 | }, |
| 646 | }); |
| 647 | }); |
| 648 | }); |
| 649 | |
| 650 | describe("Environment variable conversion", () => { |
| 651 | describe("JSON to YAML conversion", () => { |
| 652 | test("converts ${VAR} to ${{ secrets.VAR }}", () => { |
| 653 | const jsonConfig: StdioMcpJsonConfig = { |
| 654 | command: "node", |
| 655 | args: ["server.js"], |
| 656 | env: { |
| 657 | API_KEY: "${WEATHER_API_KEY}", |
| 658 | PORT: "3000", |
| 659 | DEBUG: "${DEBUG_MODE}", |
| 660 | }, |
| 661 | }; |
| 662 | |
| 663 | const result = convertJsonMcpConfigToYamlMcpConfig( |
| 664 | "test-server", |
| 665 | jsonConfig, |
| 666 | ); |
| 667 | |
| 668 | expect(result.yamlConfig).toEqual({ |
| 669 | name: "test-server", |
| 670 | type: "stdio", |
| 671 | command: "node", |
| 672 | args: ["server.js"], |
| 673 | env: { |
| 674 | API_KEY: "${{ secrets.WEATHER_API_KEY }}", |
| 675 | PORT: "3000", |
| 676 | DEBUG: "${{ secrets.DEBUG_MODE }}", |
| 677 | }, |
| 678 | }); |
| 679 | }); |
| 680 | |
| 681 | test("handles multiple variables in one value", () => { |
| 682 | const jsonConfig: StdioMcpJsonConfig = { |
| 683 | command: "node", |
| 684 | env: { |
| 685 | CONNECTION_STRING: |
| 686 | "postgres://${DB_USER}:${DB_PASS}@${DB_HOST}:5432/mydb", |
| 687 | API_URL: "https://${API_HOST}/v1/${API_VERSION}", |
| 688 | }, |
| 689 | }; |
| 690 | |
| 691 | const result = convertJsonMcpConfigToYamlMcpConfig( |
| 692 | "test-server", |
| 693 | jsonConfig, |
| 694 | ); |
| 695 | |
| 696 | expect(result.yamlConfig).toEqual({ |
| 697 | name: "test-server", |
| 698 | type: "stdio", |
| 699 | command: "node", |
| 700 | env: { |
| 701 | CONNECTION_STRING: |
| 702 | "postgres://${{ secrets.DB_USER }}:${{ secrets.DB_PASS }}@${{ secrets.DB_HOST }}:5432/mydb", |
| 703 | API_URL: |
| 704 | "https://${{ secrets.API_HOST }}/v1/${{ secrets.API_VERSION }}", |
| 705 | }, |
| 706 | }); |
| 707 | }); |
| 708 | |
| 709 | test("preserves non-template values", () => { |
| 710 | const jsonConfig: StdioMcpJsonConfig = { |
| 711 | command: "node", |
| 712 | env: { |
| 713 | STATIC_VALUE: "production", |
| 714 | MIXED: "prefix-${DYNAMIC}-suffix", |
| 715 | NUMBER: "8080", |
| 716 | }, |
| 717 | }; |
| 718 | |
| 719 | const result = convertJsonMcpConfigToYamlMcpConfig( |
| 720 | "test-server", |
| 721 | jsonConfig, |
| 722 | ); |
| 723 | |
| 724 | expect(result.yamlConfig).toEqual({ |
| 725 | name: "test-server", |
| 726 | type: "stdio", |
| 727 | command: "node", |
| 728 | env: { |
| 729 | STATIC_VALUE: "production", |
| 730 | MIXED: "prefix-${{ secrets.DYNAMIC }}-suffix", |
| 731 | NUMBER: "8080", |
| 732 | }, |
| 733 | }); |
| 734 | }); |
| 735 | }); |
| 736 | |
| 737 | describe("YAML to JSON conversion", () => { |
| 738 | test("converts ${{ secrets.VAR }} to ${VAR}", () => { |
| 739 | const yamlConfig: StdioMcpServer = { |
| 740 | name: "test-server", |
| 741 | type: "stdio", |
| 742 | command: "node", |
| 743 | args: ["server.js"], |
| 744 | env: { |
| 745 | API_KEY: "${{ secrets.WEATHER_API_KEY }}", |
| 746 | PORT: "3000", |
| 747 | DEBUG: "${{ secrets.DEBUG_MODE }}", |
| 748 | }, |
| 749 | }; |
| 750 | |
| 751 | const result = convertYamlMcpConfigToJsonMcpConfig(yamlConfig); |
| 752 | |
| 753 | expect(result.jsonConfig).toEqual({ |
| 754 | type: "stdio", |
| 755 | command: "node", |
| 756 | args: ["server.js"], |
| 757 | env: { |
| 758 | API_KEY: "${WEATHER_API_KEY}", |
| 759 | PORT: "3000", |
| 760 | DEBUG: "${DEBUG_MODE}", |
| 761 | }, |
| 762 | }); |
| 763 | }); |
| 764 | |
| 765 | test("converts ${{ inputs.VAR }} to ${VAR}", () => { |
| 766 | const yamlConfig: StdioMcpServer = { |
| 767 | name: "test-server", |
| 768 | type: "stdio", |
| 769 | command: "node", |
| 770 | env: { |
| 771 | USER_INPUT: "${{ inputs.USER_NAME }}", |
| 772 | API_KEY: "${{ inputs.API_KEY }}", |
| 773 | }, |
| 774 | }; |
| 775 | |
| 776 | const result = convertYamlMcpConfigToJsonMcpConfig(yamlConfig); |
| 777 | |
| 778 | expect(result.jsonConfig).toEqual({ |
| 779 | type: "stdio", |
| 780 | command: "node", |
| 781 | env: { |
| 782 | USER_INPUT: "${USER_NAME}", |
| 783 | API_KEY: "${API_KEY}", |
| 784 | }, |
| 785 | }); |
| 786 | }); |
| 787 | |
| 788 | test("handles mixed secrets and inputs", () => { |
| 789 | const yamlConfig: StdioMcpServer = { |
| 790 | name: "test-server", |
| 791 | type: "stdio", |
| 792 | command: "node", |
| 793 | env: { |
| 794 | SECRET_KEY: "${{ secrets.API_SECRET }}", |
| 795 | USER_INPUT: "${{ inputs.USER_NAME }}", |
| 796 | MIXED: "${{ secrets.PART1 }}-${{ inputs.PART2 }}", |
| 797 | }, |
| 798 | }; |
| 799 | |
| 800 | const result = convertYamlMcpConfigToJsonMcpConfig(yamlConfig); |
| 801 | |
| 802 | expect(result.jsonConfig).toEqual({ |
| 803 | type: "stdio", |
| 804 | command: "node", |
| 805 | env: { |
| 806 | SECRET_KEY: "${API_SECRET}", |
| 807 | USER_INPUT: "${USER_NAME}", |
| 808 | MIXED: "${PART1}-${PART2}", |
| 809 | }, |
| 810 | }); |
| 811 | }); |
| 812 | |
| 813 | test("handles whitespace in templates", () => { |
| 814 | const yamlConfig: StdioMcpServer = { |
| 815 | name: "test-server", |
| 816 | type: "stdio", |
| 817 | command: "node", |
| 818 | env: { |
| 819 | SPACED: "${{ secrets.VAR_WITH_SPACES }}", |
| 820 | MIXED_SPACE: "${{ secrets.VAR1 }}-${{inputs.VAR2}}", |
| 821 | }, |
| 822 | }; |
| 823 | |
| 824 | const result = convertYamlMcpConfigToJsonMcpConfig(yamlConfig); |
| 825 | |
| 826 | expect(result.jsonConfig).toEqual({ |
| 827 | type: "stdio", |
| 828 | command: "node", |
| 829 | env: { |
| 830 | SPACED: "${VAR_WITH_SPACES}", |
| 831 | MIXED_SPACE: "${VAR1}-${VAR2}", |
| 832 | }, |
| 833 | }); |
| 834 | }); |
| 835 | |
| 836 | test("handles multiple variables in one value", () => { |
| 837 | const yamlConfig: StdioMcpServer = { |
| 838 | name: "test-server", |
| 839 | type: "stdio", |
| 840 | command: "node", |
| 841 | env: { |
| 842 | CONNECTION_STRING: |
| 843 | "postgres://${{ secrets.DB_USER }}:${{ secrets.DB_PASS }}@${{ secrets.DB_HOST }}:5432/mydb", |
| 844 | API_URL: |
| 845 | "https://${{ inputs.API_HOST }}/v1/${{ secrets.API_VERSION }}", |
| 846 | }, |
| 847 | }; |
| 848 | |
| 849 | const result = convertYamlMcpConfigToJsonMcpConfig(yamlConfig); |
| 850 | |
| 851 | expect(result.jsonConfig).toEqual({ |
| 852 | type: "stdio", |
| 853 | command: "node", |
| 854 | env: { |
| 855 | CONNECTION_STRING: |
| 856 | "postgres://${DB_USER}:${DB_PASS}@${DB_HOST}:5432/mydb", |
| 857 | API_URL: "https://${API_HOST}/v1/${API_VERSION}", |
| 858 | }, |
| 859 | }); |
| 860 | }); |
| 861 | |
| 862 | test("preserves non-template values", () => { |
| 863 | const yamlConfig: StdioMcpServer = { |
| 864 | name: "test-server", |
| 865 | type: "stdio", |
| 866 | command: "node", |
| 867 | env: { |
| 868 | STATIC_VALUE: "production", |
| 869 | MIXED: "prefix-${{ secrets.DYNAMIC }}-suffix", |
| 870 | NUMBER: "8080", |
| 871 | }, |
| 872 | }; |
| 873 | |
| 874 | const result = convertYamlMcpConfigToJsonMcpConfig(yamlConfig); |
| 875 | |
| 876 | expect(result.jsonConfig).toEqual({ |
| 877 | type: "stdio", |
| 878 | command: "node", |
| 879 | env: { |
| 880 | STATIC_VALUE: "production", |
| 881 | MIXED: "prefix-${DYNAMIC}-suffix", |
| 882 | NUMBER: "8080", |
| 883 | }, |
| 884 | }); |
| 885 | }); |
| 886 | }); |
| 887 | |
| 888 | describe("Roundtrip conversion", () => { |
| 889 | test("JSON -> YAML -> JSON preserves values", () => { |
| 890 | const originalJson: StdioMcpJsonConfig = { |
| 891 | command: "node", |
| 892 | args: ["server.js"], |
| 893 | env: { |
| 894 | API_KEY: "${WEATHER_API_KEY}", |
| 895 | STATIC: "production", |
| 896 | COMPLEX: "prefix-${VAR1}-middle-${VAR2}-suffix", |
| 897 | }, |
| 898 | }; |
| 899 | |
| 900 | // Convert to YAML |
| 901 | const yamlResult = convertJsonMcpConfigToYamlMcpConfig( |
| 902 | "test", |
| 903 | originalJson, |
| 904 | ); |
| 905 | |
| 906 | // Convert back to JSON |
| 907 | const jsonResult = convertYamlMcpConfigToJsonMcpConfig( |
| 908 | yamlResult.yamlConfig as StdioMcpServer, |
| 909 | ); |
| 910 | |
| 911 | expect(jsonResult.jsonConfig).toEqual({ |
| 912 | type: "stdio", |
| 913 | command: "node", |
| 914 | args: ["server.js"], |
| 915 | env: { |
| 916 | API_KEY: "${WEATHER_API_KEY}", |
| 917 | STATIC: "production", |
| 918 | COMPLEX: "prefix-${VAR1}-middle-${VAR2}-suffix", |
| 919 | }, |
| 920 | }); |
| 921 | }); |
| 922 | }); |
| 923 | }); |