From d55b813c4f4b1bc058e8b29b7db078e48b56d6d6 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Tue, 30 Dec 2025 01:25:04 +0100 Subject: [PATCH] fix: preserve config imports defaults --- CHANGELOG.md | 3 ++- src/cli/config/shared.ts | 4 ++-- tests/config-add-imports.test.ts | 28 ++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 tests/config-add-imports.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 067b92d..518da05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ ## [Unreleased] -- Nothing yet. +### CLI +- Preserve default imports when `mcporter config add` writes a config file, instead of forcing `"imports": []`. ## [0.7.3] - 2025-12-29 diff --git a/src/cli/config/shared.ts b/src/cli/config/shared.ts index 4356a97..cb438bf 100644 --- a/src/cli/config/shared.ts +++ b/src/cli/config/shared.ts @@ -20,7 +20,7 @@ export type ConfigLocationSummary = { export function cloneConfig(config: RawConfig): RawConfig { return { mcpServers: config.mcpServers ? { ...config.mcpServers } : {}, - imports: config.imports ? [...config.imports] : [], + imports: config.imports ? [...config.imports] : undefined, }; } @@ -32,7 +32,7 @@ export async function loadOrCreateConfig(loadOptions: LoadConfigOptions): Promis if (isErrno(error, 'ENOENT')) { const rootDir = loadOptions.rootDir ?? process.cwd(); const resolved = resolveConfigPath(loadOptions.configPath, rootDir); - return { config: { mcpServers: {}, imports: [] }, path: resolved.path }; + return { config: { mcpServers: {} }, path: resolved.path }; } throw error; } diff --git a/tests/config-add-imports.test.ts b/tests/config-add-imports.test.ts new file mode 100644 index 0000000..aa17d79 --- /dev/null +++ b/tests/config-add-imports.test.ts @@ -0,0 +1,28 @@ +import fs from 'node:fs/promises'; +import { describe, expect, it } from 'vitest'; +import { handleAddCommand } from '../src/cli/config/add.js'; +import { createTempConfig } from './fixtures/config-fixture.js'; + +describe('config add imports preservation', () => { + it('keeps imports undefined when config omits the key', async () => { + const ctx = await createTempConfig({ mcpServers: {} }); + + await handleAddCommand({ loadOptions: ctx.loadOptions } as never, ['local', 'https://local.example/mcp']); + + const buffer = await fs.readFile(ctx.configPath, 'utf8'); + const parsed = JSON.parse(buffer) as { imports?: string[] }; + expect(Object.hasOwn(parsed, 'imports')).toBe(false); + await ctx.cleanup(); + }); + + it('keeps imports undefined when creating a new config file', async () => { + const ctx = await createTempConfig(); + + await handleAddCommand({ loadOptions: ctx.loadOptions } as never, ['fresh', 'https://fresh.example/mcp']); + + const buffer = await fs.readFile(ctx.configPath, 'utf8'); + const parsed = JSON.parse(buffer) as { imports?: string[] }; + expect(Object.hasOwn(parsed, 'imports')).toBe(false); + await ctx.cleanup(); + }); +});