kiro.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * Kiro CLI / IDE target. Writes:
  3. *
  4. * - MCP server entry to `~/.kiro/settings/mcp.json` (global) or
  5. * `./.kiro/settings/mcp.json` (local). Standard `mcpServers.codegraph`
  6. * shape, same as Claude / Cursor / Gemini.
  7. * - Instructions to `~/.kiro/steering/codegraph.md` (global) or
  8. * `./.kiro/steering/codegraph.md` (local). Kiro's "steering" system
  9. * loads every `*.md` file in the steering dir as agent context, so
  10. * a dedicated `codegraph.md` is the natural surface — we own the
  11. * whole file outright (no marker-based merging needed) and delete
  12. * it on uninstall.
  13. *
  14. * No permissions concept — Kiro gates tool invocations through its own
  15. * UI prompts rather than an external allowlist. `autoAllow` is silently
  16. * ignored.
  17. *
  18. * Paths are identical on macOS / Linux / Windows because Kiro resolves
  19. * its config root from `os.homedir()` on all three (Windows `~` →
  20. * `%USERPROFILE%\.kiro`).
  21. *
  22. * Docs: https://kiro.dev/docs/cli/mcp/
  23. * https://kiro.dev/docs/cli/steering/
  24. */
  25. import * as fs from 'fs';
  26. import * as path from 'path';
  27. import * as os from 'os';
  28. import {
  29. AgentTarget,
  30. DetectionResult,
  31. InstallOptions,
  32. Location,
  33. WriteResult,
  34. } from './types';
  35. import {
  36. atomicWriteFileSync,
  37. getMcpServerConfig,
  38. jsonDeepEqual,
  39. readJsonFile,
  40. writeJsonFile,
  41. } from './shared';
  42. import { INSTRUCTIONS_TEMPLATE } from '../instructions-template';
  43. function configDir(loc: Location): string {
  44. return loc === 'global'
  45. ? path.join(os.homedir(), '.kiro')
  46. : path.join(process.cwd(), '.kiro');
  47. }
  48. function mcpJsonPath(loc: Location): string {
  49. return path.join(configDir(loc), 'settings', 'mcp.json');
  50. }
  51. function steeringPath(loc: Location): string {
  52. return path.join(configDir(loc), 'steering', 'codegraph.md');
  53. }
  54. class KiroTarget implements AgentTarget {
  55. readonly id = 'kiro' as const;
  56. readonly displayName = 'Kiro';
  57. readonly docsUrl = 'https://kiro.dev/docs/cli/mcp/';
  58. supportsLocation(_loc: Location): boolean {
  59. return true;
  60. }
  61. detect(loc: Location): DetectionResult {
  62. const file = mcpJsonPath(loc);
  63. const config = readJsonFile(file);
  64. const alreadyConfigured = !!config.mcpServers?.codegraph;
  65. const installed = loc === 'global'
  66. ? fs.existsSync(configDir('global')) || fs.existsSync(file)
  67. : fs.existsSync(file) || fs.existsSync(configDir('local'));
  68. return { installed, alreadyConfigured, configPath: file };
  69. }
  70. install(loc: Location, _opts: InstallOptions): WriteResult {
  71. const files: WriteResult['files'] = [];
  72. files.push(writeMcpEntry(loc));
  73. files.push(writeSteeringEntry(loc));
  74. return {
  75. files,
  76. // The IDE-only enable-MCP step is load-bearing: Kiro IDE ships
  77. // with MCP support disabled by default, so even a valid
  78. // `~/.kiro/settings/mcp.json` at the documented path is ignored
  79. // until the user flips the toggle. Kiro CLI reads the same file
  80. // without a gate, so we call out which audience this applies to.
  81. notes: [
  82. 'Restart Kiro for MCP changes to take effect.',
  83. 'Kiro IDE: also enable MCP in Settings (search "MCP" → "Enabled"). Kiro CLI users can skip this step.',
  84. ],
  85. };
  86. }
  87. uninstall(loc: Location): WriteResult {
  88. const files: WriteResult['files'] = [];
  89. const file = mcpJsonPath(loc);
  90. const config = readJsonFile(file);
  91. if (config.mcpServers?.codegraph) {
  92. delete config.mcpServers.codegraph;
  93. if (Object.keys(config.mcpServers).length === 0) {
  94. delete config.mcpServers;
  95. }
  96. writeJsonFile(file, config);
  97. files.push({ path: file, action: 'removed' });
  98. } else {
  99. files.push({ path: file, action: 'not-found' });
  100. }
  101. files.push(removeSteeringEntry(loc));
  102. return { files };
  103. }
  104. printConfig(loc: Location): string {
  105. const target = mcpJsonPath(loc);
  106. const snippet = JSON.stringify({ mcpServers: { codegraph: getMcpServerConfig() } }, null, 2);
  107. return `# Add to ${target}\n\n${snippet}\n`;
  108. }
  109. describePaths(loc: Location): string[] {
  110. return [mcpJsonPath(loc), steeringPath(loc)];
  111. }
  112. }
  113. function writeMcpEntry(loc: Location): WriteResult['files'][number] {
  114. const file = mcpJsonPath(loc);
  115. const dir = path.dirname(file);
  116. if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
  117. const existing = readJsonFile(file);
  118. const before = existing.mcpServers?.codegraph;
  119. const after = getMcpServerConfig();
  120. if (jsonDeepEqual(before, after)) {
  121. return { path: file, action: 'unchanged' };
  122. }
  123. const action: 'created' | 'updated' =
  124. before ? 'updated' : (fs.existsSync(file) ? 'updated' : 'created');
  125. if (!existing.mcpServers) existing.mcpServers = {};
  126. existing.mcpServers.codegraph = after;
  127. writeJsonFile(file, existing);
  128. return { path: file, action };
  129. }
  130. /**
  131. * Write the dedicated steering file. Unlike CLAUDE.md / GEMINI.md
  132. * (shared files where codegraph owns a marker-delimited section),
  133. * Kiro's steering dir loads every `*.md` as a discrete document — so
  134. * `codegraph.md` is ours outright. Byte-equality short-circuits
  135. * idempotent re-runs; mismatched content gets a clean rewrite.
  136. */
  137. function writeSteeringEntry(loc: Location): WriteResult['files'][number] {
  138. const file = steeringPath(loc);
  139. const dir = path.dirname(file);
  140. if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
  141. const body = INSTRUCTIONS_TEMPLATE + '\n';
  142. if (!fs.existsSync(file)) {
  143. atomicWriteFileSync(file, body);
  144. return { path: file, action: 'created' };
  145. }
  146. const existing = fs.readFileSync(file, 'utf-8');
  147. if (existing === body) {
  148. return { path: file, action: 'unchanged' };
  149. }
  150. atomicWriteFileSync(file, body);
  151. return { path: file, action: 'updated' };
  152. }
  153. /**
  154. * Delete the steering file we own. If a user has hand-edited the file
  155. * out of recognition we still remove it — codegraph.md is a name we
  156. * claim, and a partial install leaving the file behind is worse than
  157. * a clean delete.
  158. */
  159. function removeSteeringEntry(loc: Location): WriteResult['files'][number] {
  160. const file = steeringPath(loc);
  161. if (!fs.existsSync(file)) return { path: file, action: 'not-found' };
  162. try { fs.unlinkSync(file); } catch { /* ignore */ }
  163. return { path: file, action: 'removed' };
  164. }
  165. export const kiroTarget: AgentTarget = new KiroTarget();