gemini.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * Gemini CLI target (also covers the rebranded "Antigravity CLI" —
  3. * Google is in the middle of unifying its CLI tools under
  4. * Antigravity, and the new CLI continues to read `~/.gemini/settings.json`
  5. * + project-local `.gemini/settings.json`). Writes:
  6. *
  7. * - MCP server entry to `~/.gemini/settings.json` (global) or
  8. * `./.gemini/settings.json` (local) under the standard
  9. * `mcpServers.codegraph` key. Same shape as Claude / Cursor.
  10. * - Instructions to `~/.gemini/GEMINI.md` (global) or `./GEMINI.md`
  11. * (local — Gemini reads the project root file directly, not
  12. * under `.gemini/`).
  13. *
  14. * No permissions concept — Gemini CLI gates tool invocations through
  15. * the `trust` field per server, not an external allowlist. We leave
  16. * `trust` unset so the user controls confirmation prompts.
  17. *
  18. * The Antigravity IDE shares `~/.gemini/GEMINI.md` for instructions
  19. * but uses a separate MCP config file (`~/.gemini/antigravity/mcp_config.json`)
  20. * — see `./antigravity.ts`. Both targets writing to GEMINI.md is
  21. * safe: the marker-based section replacement makes the second write
  22. * a byte-identical no-op.
  23. */
  24. import * as fs from 'fs';
  25. import * as path from 'path';
  26. import * as os from 'os';
  27. import {
  28. AgentTarget,
  29. DetectionResult,
  30. InstallOptions,
  31. Location,
  32. WriteResult,
  33. } from './types';
  34. import {
  35. getMcpServerConfig,
  36. jsonDeepEqual,
  37. readJsonFile,
  38. removeMarkedSection,
  39. replaceOrAppendMarkedSection,
  40. writeJsonFile,
  41. } from './shared';
  42. import {
  43. CODEGRAPH_SECTION_END,
  44. CODEGRAPH_SECTION_START,
  45. INSTRUCTIONS_TEMPLATE,
  46. } from '../instructions-template';
  47. function configDir(loc: Location): string {
  48. return loc === 'global'
  49. ? path.join(os.homedir(), '.gemini')
  50. : path.join(process.cwd(), '.gemini');
  51. }
  52. function settingsJsonPath(loc: Location): string {
  53. return path.join(configDir(loc), 'settings.json');
  54. }
  55. function instructionsPath(loc: Location): string {
  56. // Global GEMINI.md lives under ~/.gemini/; project-local GEMINI.md
  57. // lives at the project root (NOT under .gemini/), matching how
  58. // Gemini CLI's hierarchical context loader searches.
  59. return loc === 'global'
  60. ? path.join(configDir('global'), 'GEMINI.md')
  61. : path.join(process.cwd(), 'GEMINI.md');
  62. }
  63. class GeminiTarget implements AgentTarget {
  64. readonly id = 'gemini' as const;
  65. readonly displayName = 'Gemini CLI';
  66. readonly docsUrl = 'https://geminicli.com/docs/tools/mcp-server/';
  67. supportsLocation(_loc: Location): boolean {
  68. return true;
  69. }
  70. detect(loc: Location): DetectionResult {
  71. const file = settingsJsonPath(loc);
  72. const config = readJsonFile(file);
  73. const alreadyConfigured = !!config.mcpServers?.codegraph;
  74. const installed = loc === 'global'
  75. ? fs.existsSync(configDir('global')) || fs.existsSync(file)
  76. : fs.existsSync(file) || fs.existsSync(configDir('local'));
  77. return { installed, alreadyConfigured, configPath: file };
  78. }
  79. install(loc: Location, _opts: InstallOptions): WriteResult {
  80. const files: WriteResult['files'] = [];
  81. files.push(writeMcpEntry(loc));
  82. files.push(writeInstructionsEntry(loc));
  83. return { files };
  84. }
  85. uninstall(loc: Location): WriteResult {
  86. const files: WriteResult['files'] = [];
  87. const file = settingsJsonPath(loc);
  88. const config = readJsonFile(file);
  89. if (config.mcpServers?.codegraph) {
  90. delete config.mcpServers.codegraph;
  91. if (Object.keys(config.mcpServers).length === 0) {
  92. delete config.mcpServers;
  93. }
  94. // If the file is now an empty `{}` we still leave it — other
  95. // (top-level) Gemini settings the user might add later can
  96. // share the file; deleting it would be surprising.
  97. writeJsonFile(file, config);
  98. files.push({ path: file, action: 'removed' });
  99. } else {
  100. files.push({ path: file, action: 'not-found' });
  101. }
  102. const instr = instructionsPath(loc);
  103. const action = removeMarkedSection(instr, CODEGRAPH_SECTION_START, CODEGRAPH_SECTION_END);
  104. files.push({ path: instr, action });
  105. return { files };
  106. }
  107. printConfig(loc: Location): string {
  108. const target = settingsJsonPath(loc);
  109. const snippet = JSON.stringify({ mcpServers: { codegraph: getMcpServerConfig() } }, null, 2);
  110. return `# Add to ${target}\n\n${snippet}\n`;
  111. }
  112. describePaths(loc: Location): string[] {
  113. return [settingsJsonPath(loc), instructionsPath(loc)];
  114. }
  115. }
  116. function writeMcpEntry(loc: Location): WriteResult['files'][number] {
  117. const file = settingsJsonPath(loc);
  118. const dir = path.dirname(file);
  119. if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
  120. const existing = readJsonFile(file);
  121. const before = existing.mcpServers?.codegraph;
  122. const after = getMcpServerConfig();
  123. if (jsonDeepEqual(before, after)) {
  124. return { path: file, action: 'unchanged' };
  125. }
  126. const action: 'created' | 'updated' =
  127. before ? 'updated' : (fs.existsSync(file) ? 'updated' : 'created');
  128. if (!existing.mcpServers) existing.mcpServers = {};
  129. existing.mcpServers.codegraph = after;
  130. writeJsonFile(file, existing);
  131. return { path: file, action };
  132. }
  133. function writeInstructionsEntry(loc: Location): WriteResult['files'][number] {
  134. const file = instructionsPath(loc);
  135. const dir = path.dirname(file);
  136. if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
  137. const action = replaceOrAppendMarkedSection(
  138. file,
  139. INSTRUCTIONS_TEMPLATE,
  140. CODEGRAPH_SECTION_START,
  141. CODEGRAPH_SECTION_END,
  142. );
  143. const mapped: 'created' | 'updated' | 'unchanged' =
  144. action === 'created' ? 'created'
  145. : action === 'unchanged' ? 'unchanged'
  146. : 'updated';
  147. return { path: file, action: mapped };
  148. }
  149. export const geminiTarget: AgentTarget = new GeminiTarget();