kiro.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. notes: ['Restart Kiro for MCP changes to take effect.'],
  77. };
  78. }
  79. uninstall(loc: Location): WriteResult {
  80. const files: WriteResult['files'] = [];
  81. const file = mcpJsonPath(loc);
  82. const config = readJsonFile(file);
  83. if (config.mcpServers?.codegraph) {
  84. delete config.mcpServers.codegraph;
  85. if (Object.keys(config.mcpServers).length === 0) {
  86. delete config.mcpServers;
  87. }
  88. writeJsonFile(file, config);
  89. files.push({ path: file, action: 'removed' });
  90. } else {
  91. files.push({ path: file, action: 'not-found' });
  92. }
  93. files.push(removeSteeringEntry(loc));
  94. return { files };
  95. }
  96. printConfig(loc: Location): string {
  97. const target = mcpJsonPath(loc);
  98. const snippet = JSON.stringify({ mcpServers: { codegraph: getMcpServerConfig() } }, null, 2);
  99. return `# Add to ${target}\n\n${snippet}\n`;
  100. }
  101. describePaths(loc: Location): string[] {
  102. return [mcpJsonPath(loc), steeringPath(loc)];
  103. }
  104. }
  105. function writeMcpEntry(loc: Location): WriteResult['files'][number] {
  106. const file = mcpJsonPath(loc);
  107. const dir = path.dirname(file);
  108. if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
  109. const existing = readJsonFile(file);
  110. const before = existing.mcpServers?.codegraph;
  111. const after = getMcpServerConfig();
  112. if (jsonDeepEqual(before, after)) {
  113. return { path: file, action: 'unchanged' };
  114. }
  115. const action: 'created' | 'updated' =
  116. before ? 'updated' : (fs.existsSync(file) ? 'updated' : 'created');
  117. if (!existing.mcpServers) existing.mcpServers = {};
  118. existing.mcpServers.codegraph = after;
  119. writeJsonFile(file, existing);
  120. return { path: file, action };
  121. }
  122. /**
  123. * Write the dedicated steering file. Unlike CLAUDE.md / GEMINI.md
  124. * (shared files where codegraph owns a marker-delimited section),
  125. * Kiro's steering dir loads every `*.md` as a discrete document — so
  126. * `codegraph.md` is ours outright. Byte-equality short-circuits
  127. * idempotent re-runs; mismatched content gets a clean rewrite.
  128. */
  129. function writeSteeringEntry(loc: Location): WriteResult['files'][number] {
  130. const file = steeringPath(loc);
  131. const dir = path.dirname(file);
  132. if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
  133. const body = INSTRUCTIONS_TEMPLATE + '\n';
  134. if (!fs.existsSync(file)) {
  135. atomicWriteFileSync(file, body);
  136. return { path: file, action: 'created' };
  137. }
  138. const existing = fs.readFileSync(file, 'utf-8');
  139. if (existing === body) {
  140. return { path: file, action: 'unchanged' };
  141. }
  142. atomicWriteFileSync(file, body);
  143. return { path: file, action: 'updated' };
  144. }
  145. /**
  146. * Delete the steering file we own. If a user has hand-edited the file
  147. * out of recognition we still remove it — codegraph.md is a name we
  148. * claim, and a partial install leaving the file behind is worse than
  149. * a clean delete.
  150. */
  151. function removeSteeringEntry(loc: Location): WriteResult['files'][number] {
  152. const file = steeringPath(loc);
  153. if (!fs.existsSync(file)) return { path: file, action: 'not-found' };
  154. try { fs.unlinkSync(file); } catch { /* ignore */ }
  155. return { path: file, action: 'removed' };
  156. }
  157. export const kiroTarget: AgentTarget = new KiroTarget();