diff --git a/docs/getting-started/client-configuration.md b/docs/getting-started/client-configuration.md index 5112bae90e6..99c3d937ddd 100644 --- a/docs/getting-started/client-configuration.md +++ b/docs/getting-started/client-configuration.md @@ -954,13 +954,13 @@ removal are covered by `tests/acceptance/setup/clineClientSetup.test.ts`. Setup, doctor, update, uninstall and `rea mcp add` share one definition for each of these clients. Setup offers a client when its directory exists. -| Client | `--client` | User configuration | Server table | -| ----------------------- | -------------- | ------------------------------------------------------------------------------------------------------------------------------- | ----------------- | -| Cline VS Code extension | `cline_vscode` | `/globalStorage/saoudrizwan.claude-dev/settings/cline_mcp_settings.json` | `mcpServers` | -| Kiro | `kiro` | `~/.kiro/settings/mcp.json`, shared by the IDE and CLI | `mcpServers` | -| Mastra Code | `mastracode` | `~/.mastracode/mcp.json` | `mcpServers` | -| Zed | `zed` | `~/.config/zed/settings.json` on macOS, `%APPDATA%\Zed\settings.json` on Windows, `$XDG_CONFIG_HOME/zed/settings.json` on Linux | `context_servers` | -| Amp | `amp` | `~/.config/amp/settings.json`, or `settings.jsonc` when that is the only one | `amp.mcpServers` | +| Client | `--client` | User configuration | Server table | +| ----------------------- | -------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------- | +| Cline VS Code extension | `cline_vscode` | `/globalStorage/saoudrizwan.claude-dev/settings/cline_mcp_settings.json` | `mcpServers` | +| Kiro | `kiro` | `~/.kiro/settings/mcp.json`, or `$KIRO_HOME/settings/mcp.json` when `KIRO_HOME` is absolute and not a filesystem root, the home folder, or an ancestor of it; shared by the IDE and CLI | `mcpServers` | +| Mastra Code | `mastracode` | `~/.mastracode/mcp.json` | `mcpServers` | +| Zed | `zed` | `~/.config/zed/settings.json` on macOS, `%APPDATA%\Zed\settings.json` on Windows, `$XDG_CONFIG_HOME/zed/settings.json` on Linux | `context_servers` | +| Amp | `amp` | `~/.config/amp/settings.json`, or `settings.jsonc` when that is the only one | `amp.mcpServers` | The Cline extension keeps a separate settings file in each editor that runs it. `cline_vscode_insiders`, `cline_vscodium`, `cline_cursor` and diff --git a/src/application/McpRegistrationCatalog.test.ts b/src/application/McpRegistrationCatalog.test.ts index 0b494bbc32e..55d32142d3a 100644 --- a/src/application/McpRegistrationCatalog.test.ts +++ b/src/application/McpRegistrationCatalog.test.ts @@ -1,4 +1,4 @@ -import { mkdir, rm, writeFile } from "node:fs/promises"; +import { mkdir, rm, symlink, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { expect, it } from "vitest"; import { z } from "zod"; @@ -691,6 +691,164 @@ it.each([ expect(client?.configPathError).toContain("FX_HOME"); }); +it.each([ + { + name: "unset", + env: {}, + configPath: "/home/alice/.kiro/settings/mcp.json", + markerPath: "/home/alice/.kiro", + }, + { + name: "absolute KIRO_HOME", + env: { KIRO_HOME: "/work/kiro-profile" }, + configPath: "/work/kiro-profile/settings/mcp.json", + markerPath: "/work/kiro-profile", + }, + { + name: "trailing slash", + env: { KIRO_HOME: "/work/kiro-profile/" }, + configPath: "/work/kiro-profile/settings/mcp.json", + markerPath: "/work/kiro-profile", + }, + { + name: "folder inside home", + env: { KIRO_HOME: "/home/alice/profile" }, + configPath: "/home/alice/profile/settings/mcp.json", + markerPath: "/home/alice/profile", + }, + { + name: "sibling sharing a name prefix", + env: { KIRO_HOME: "/home/al" }, + configPath: "/home/al/settings/mcp.json", + markerPath: "/home/al", + }, + { + name: "empty", + env: { KIRO_HOME: "" }, + configPath: "/home/alice/.kiro/settings/mcp.json", + markerPath: "/home/alice/.kiro", + }, + { + name: "relative", + env: { KIRO_HOME: "relative/kiro" }, + configPath: "/home/alice/.kiro/settings/mcp.json", + markerPath: "/home/alice/.kiro", + }, + { + name: "filesystem root", + env: { KIRO_HOME: "/" }, + configPath: "/home/alice/.kiro/settings/mcp.json", + markerPath: "/home/alice/.kiro", + }, + { + name: "home folder", + env: { KIRO_HOME: "/home/alice" }, + configPath: "/home/alice/.kiro/settings/mcp.json", + markerPath: "/home/alice/.kiro", + }, + { + name: "ancestor of home", + env: { KIRO_HOME: "/home" }, + configPath: "/home/alice/.kiro/settings/mcp.json", + markerPath: "/home/alice/.kiro", + }, + { + name: "home alias through ..", + env: { KIRO_HOME: "/home/alice/profile/.." }, + configPath: "/home/alice/.kiro/settings/mcp.json", + markerPath: "/home/alice/.kiro", + }, +])( + "resolves the Kiro root Kiro reads ($name)", + ({ env, configPath, markerPath }) => { + const [client] = supportedClients("/home/alice", "linux", env, {}, "kiro"); + expect(client).toMatchObject({ name: "kiro", configPath, markerPath }); + expect(client?.configPathError).toBeUndefined(); + }, +); + +it.each([ + { + name: "absolute KIRO_HOME", + env: { KIRO_HOME: "C:\\work\\kiro" }, + configPath: "C:\\work\\kiro\\settings\\mcp.json", + markerPath: "C:\\work\\kiro", + }, + { + name: "forward slashes", + env: { KIRO_HOME: "C:/work/kiro/" }, + configPath: "C:\\work\\kiro\\settings\\mcp.json", + markerPath: "C:\\work\\kiro", + }, + { + name: "another drive", + env: { KIRO_HOME: "D:\\kiro" }, + configPath: "D:\\kiro\\settings\\mcp.json", + markerPath: "D:\\kiro", + }, + { + name: "drive root", + env: { KIRO_HOME: "C:\\" }, + configPath: "C:\\Users\\alice\\.kiro\\settings\\mcp.json", + markerPath: "C:\\Users\\alice\\.kiro", + }, + { + name: "root of another drive", + env: { KIRO_HOME: "D:\\" }, + configPath: "C:\\Users\\alice\\.kiro\\settings\\mcp.json", + markerPath: "C:\\Users\\alice\\.kiro", + }, + { + name: "UNC share root", + env: { KIRO_HOME: "\\\\server\\share\\" }, + configPath: "C:\\Users\\alice\\.kiro\\settings\\mcp.json", + markerPath: "C:\\Users\\alice\\.kiro", + }, + { + name: "folder under a UNC share", + env: { KIRO_HOME: "\\\\server\\share\\kiro" }, + configPath: "\\\\server\\share\\kiro\\settings\\mcp.json", + markerPath: "\\\\server\\share\\kiro", + }, + { + name: "home folder in another case", + env: { KIRO_HOME: "c:\\users\\ALICE" }, + configPath: "C:\\Users\\alice\\.kiro\\settings\\mcp.json", + markerPath: "C:\\Users\\alice\\.kiro", + }, + { + name: "ancestor of home", + env: { KIRO_HOME: "C:\\Users" }, + configPath: "C:\\Users\\alice\\.kiro\\settings\\mcp.json", + markerPath: "C:\\Users\\alice\\.kiro", + }, + { + name: "drive-less rooted path", + env: { KIRO_HOME: "\\kiro" }, + configPath: "C:\\Users\\alice\\.kiro\\settings\\mcp.json", + markerPath: "C:\\Users\\alice\\.kiro", + }, + { + name: "drive-relative path", + env: { KIRO_HOME: "C:kiro" }, + configPath: "C:\\Users\\alice\\.kiro\\settings\\mcp.json", + markerPath: "C:\\Users\\alice\\.kiro", + }, +])( + "resolves the Windows Kiro root Kiro reads ($name)", + ({ env, configPath, markerPath }) => { + const [client] = supportedClients( + "C:\\Users\\alice", + "win32", + env, + {}, + "kiro", + ); + expect(client).toMatchObject({ name: "kiro", configPath, markerPath }); + expect(client?.configPathError).toBeUndefined(); + }, +); + it.each([ { name: "legacy default", @@ -793,3 +951,32 @@ it("refuses a relative MCPORTER_CONFIG whose file depends on each command's dire error: { message: expect.stringContaining("MCPORTER_CONFIG is relative") }, }); }); + +it("selects the default Kiro configuration through aliases of home and its ancestor", async () => { + const root = await createTestTempDirectory("rea-kiro-home-identity-"); + const home = join(root, "home"); + const homeAlias = join(root, "home-alias"); + const ancestorAlias = join(root, "ancestor-alias"); + const alternative = join(root, "alternative"); + await mkdir(home); + await mkdir(alternative); + const linkType = process.platform === "win32" ? "junction" : "dir"; + await symlink(home, homeAlias, linkType); + await symlink(root, ancestorAlias, linkType); + for (const selected of [homeAlias, ancestorAlias, alternative]) { + const [client] = supportedClients( + home, + process.platform, + { KIRO_HOME: selected }, + {}, + "kiro", + ); + expect(client?.configPath).toBe( + join( + selected === alternative ? alternative : join(home, ".kiro"), + "settings", + "mcp.json", + ), + ); + } +}); diff --git a/src/application/SupportedClientLocations.ts b/src/application/SupportedClientLocations.ts index 88dd20e59b7..c950f9b500a 100644 --- a/src/application/SupportedClientLocations.ts +++ b/src/application/SupportedClientLocations.ts @@ -1,3 +1,4 @@ +import { HOST_PLATFORM } from "../platform/HostPlatform.js"; import { isAbsentClientConfigPath, isEnoentError } from "./ClientConfigPath.js"; import type { ClientRegistrationConnectionProfile, @@ -5,7 +6,7 @@ import type { } from "./ClientRegistrationProfile.js"; import { dirname, join, posix, resolve, win32 } from "node:path"; import { fileURLToPath } from "node:url"; -import { lstatSync, statSync } from "node:fs"; +import { lstatSync, realpathSync, statSync } from "node:fs"; import { err, ok, type Result } from "../domain/result.js"; import { absoluteCallerPath, @@ -705,6 +706,68 @@ export const fxLocation = ({ }); }; +/** + * Kiro's global root: an absolute `KIRO_HOME` moves it, including + * `settings/mcp.json`. Kiro ignores the variable and falls back to `~/.kiro` + * when it is relative (or drive-less on Windows), the filesystem root, the + * home folder, or an ancestor of home, so REA does the same instead of + * refusing the value. + */ +export const kiroLocation = ({ + home, + platform, + env, +}: ClientPathContext): Result => { + const pathApi = platform === "win32" ? win32 : posix; + const fallback = pathApi.join(home, ".kiro"); + const at = (directory: string): ClientLocation => ({ + configPath: pathApi.join(directory, "settings", "mcp.json"), + markerPath: directory, + }); + const value = env.KIRO_HOME; + if (!value || clientPathDependence(value, platform) !== undefined) + return ok(at(fallback)); + const normalized = pathApi.normalize(value); + const root = pathApi.parse(normalized).root; + const selected = + normalized.length > root.length + ? normalized.replace(platform === "win32" ? /[\\/]+$/u : /\/+$/u, "") + : normalized; + const comparable = (path: string): string => + platform === "win32" ? path.toLowerCase() : path; + const isRootOrAncestor = (directory: string, userHome: string): boolean => { + const relativeHome = pathApi.relative( + comparable(directory), + comparable(userHome), + ); + return ( + comparable(directory) === comparable(pathApi.parse(directory).root) || + relativeHome === "" || + (relativeHome !== ".." && + !relativeHome.startsWith(`..${pathApi.sep}`) && + !pathApi.isAbsolute(relativeHome)) + ); + }; + if (isRootOrAncestor(selected, home)) return ok(at(fallback)); + if (platform === HOST_PLATFORM.platform) { + let inspecting = selected; + try { + const physicalDirectory = realpathSync.native(selected); + inspecting = home; + const physicalHome = realpathSync.native(home); + if (isRootOrAncestor(physicalDirectory, physicalHome)) + return ok(at(fallback)); + } catch (cause: unknown) { + if (!isEnoentError(cause)) + return err({ + path: inspecting, + message: `Could not verify the Kiro profile path ${inspecting}: ${errorMessage(cause)}`, + }); + } + } + return ok(at(selected)); +}; + /** * Junie's home: `JUNIE_HOME` for Junie CLI, else `~/.junie`. add-mcp notes the * JetBrains plugin always reads `~/.junie`. Junie resolves a relative diff --git a/src/application/SupportedClients.ts b/src/application/SupportedClients.ts index a1b0624d157..5cde3cf7de9 100644 --- a/src/application/SupportedClients.ts +++ b/src/application/SupportedClients.ts @@ -57,6 +57,7 @@ import { MCPORTER_URL_FIELDS, ampLocation, fxLocation, + kiroLocation, junieLocation, gooseLocation, gooseConfigPaths, @@ -1159,8 +1160,7 @@ export const SUPPORTED_CLIENT_DEFINITIONS = [ }, // The IDE, CLI and web clients share the user file. displayName: "Kiro", - configPath: [".kiro", "settings", "mcp.json"], - markerPath: [".kiro"], + location: kiroLocation, format: "json", }), defineClient({ diff --git a/tests/fixtures/clientEnvironment.ts b/tests/fixtures/clientEnvironment.ts index 2eeafb15167..c125811879a 100644 --- a/tests/fixtures/clientEnvironment.ts +++ b/tests/fixtures/clientEnvironment.ts @@ -39,6 +39,7 @@ const CLIENT_LOCATION_ENVIRONMENT = [ "KILO_CONFIG_DIR", "KILO_DISABLE_PROJECT_CONFIG", "KIMI_CODE_HOME", + "KIRO_HOME", "MCPORTER_CONFIG", "LOCALAPPDATA", "OMP_PROFILE",