@codua/codeoutline-win32-x64 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,291 @@
1
+ -- Small, lossless JSON/JSONC editor for MCP entries. No reserialization of
2
+ -- surrounding settings: comments, key order and unrelated bytes survive.
3
+ local M = {}
4
+ local function encode(value) return assert(xutils.json_pack(value)) end
5
+
6
+ local function parse(text)
7
+ local pos, size = text:sub(1, 3) == '\239\187\191' and 4 or 1, #text
8
+ local function skip()
9
+ while pos <= size do
10
+ if text:sub(pos, pos):match('%s') then pos = pos + 1
11
+ elseif text:sub(pos, pos + 1) == '//' then
12
+ pos = (text:find('\n', pos + 2, true) or size) + 1
13
+ elseif text:sub(pos, pos + 1) == '/*' then
14
+ pos = assert(text:find('*/', pos + 2, true), 'unterminated JSON comment') + 2
15
+ else break end
16
+ end
17
+ end
18
+ local function string_value()
19
+ local start = pos
20
+ assert(text:sub(pos, pos) == '"', 'expected JSON string')
21
+ pos = pos + 1
22
+ while pos <= size do
23
+ local char = text:sub(pos, pos)
24
+ if char == '\\' then pos = pos + 2
25
+ elseif char == '"' then
26
+ pos = pos + 1
27
+ local value = xutils.json_unpack(text:sub(start, pos - 1))
28
+ assert(type(value) == 'string', 'invalid JSON string')
29
+ return value
30
+ else pos = pos + 1 end
31
+ end
32
+ error('unterminated JSON string')
33
+ end
34
+ local value
35
+ value = function(depth)
36
+ assert(depth <= 100, 'JSON nesting exceeds 100')
37
+ skip()
38
+ local node = { start = pos, children = {}, kind = text:sub(pos, pos) }
39
+ if node.kind == '{' or node.kind == '[' then
40
+ local close = node.kind == '{' and '}' or ']'
41
+ pos = pos + 1; skip()
42
+ while text:sub(pos, pos) ~= close do
43
+ local key, property_start
44
+ if node.kind == '{' then
45
+ property_start = pos; key = string_value(); skip()
46
+ assert(text:sub(pos, pos) == ':', 'expected JSON colon'); pos = pos + 1
47
+ assert(node.children[key] == nil, 'duplicate JSON key: ' .. key)
48
+ else key = #node.children + 1 end
49
+ local child = value(depth + 1)
50
+ child.property_start = property_start or child.start
51
+ node.children[key] = child
52
+ skip()
53
+ if text:sub(pos, pos) == close then break end
54
+ assert(text:sub(pos, pos) == ',', 'expected JSON comma')
55
+ child.comma = pos; pos = pos + 1; skip()
56
+ end
57
+ node.close = pos; pos = pos + 1
58
+ elseif node.kind == '"' then node.value = string_value()
59
+ else
60
+ local token = text:sub(pos):match('^[^%s,%]%}/]+')
61
+ assert(token, 'expected JSON value')
62
+ local decoded = xutils.json_unpack(token)
63
+ assert(token == 'null' or decoded ~= nil, 'invalid JSON value')
64
+ node.value = decoded; pos = pos + #token
65
+ end
66
+ node.finish = pos - 1
67
+ return node
68
+ end
69
+ local root = value(0); skip(); assert(pos > size, 'unexpected text after JSON')
70
+ assert(root.kind == '{', 'MCP config must be a JSON object')
71
+ return root
72
+ end
73
+
74
+ local function same(node, expected)
75
+ if type(expected) ~= 'table' then return node.value == expected end
76
+ if node.kind ~= '{' and node.kind ~= '[' then return false end
77
+ for key, val in pairs(expected) do
78
+ if not node.children[key] or not same(node.children[key], val) then return false end
79
+ end
80
+ for key in pairs(node.children) do if expected[key] == nil then return false end end
81
+ return true
82
+ end
83
+
84
+ function M.json(text, path, entry, remove, force)
85
+ if text == '' then text = '{}\n' end
86
+ local root = parse(text)
87
+ local node = root
88
+ for i, key in ipairs(path) do
89
+ assert(node.kind == '{', 'MCP config parent must be an object')
90
+ local child = node.children[key]
91
+ if i == #path and child then
92
+ if remove then
93
+ local start, finish = child.property_start, child.finish
94
+ if child.comma then finish = child.comma
95
+ else
96
+ -- Only the immediately preceding comma belongs to this entry.
97
+ local nearest
98
+ for _, previous in pairs(node.children) do
99
+ if previous.comma and previous.comma < child.property_start then nearest = math.max(nearest or 0, previous.comma) end
100
+ end
101
+ if nearest then
102
+ return text:sub(1, nearest - 1) .. text:sub(nearest + 1, child.property_start - 1) .. text:sub(finish + 1), true
103
+ end
104
+ end
105
+ return text:sub(1, start - 1) .. text:sub(finish + 1), true
106
+ end
107
+ if same(child, entry) then return text, false end
108
+ assert(force, 'codeoutline entry already exists with different settings; use --force to replace it')
109
+ return text:sub(1, child.start - 1) .. encode(entry) .. text:sub(child.finish + 1), true
110
+ elseif not child then
111
+ if remove then return text, false end
112
+ local nested = entry
113
+ for j = #path, i + 1, -1 do nested = { [path[j]] = nested } end
114
+ local last
115
+ for _, previous in pairs(node.children) do
116
+ if not last or previous.finish > last.finish then last = previous end
117
+ end
118
+ local prefix = text:sub(1, node.close - 1)
119
+ if last and not last.comma then
120
+ prefix = text:sub(1, last.finish) .. ',' .. text:sub(last.finish + 1, node.close - 1)
121
+ end
122
+ local nl = text:find('\r\n', 1, true) and '\r\n' or '\n'
123
+ return prefix .. nl .. ' ' .. encode(key) .. ': ' .. encode(nested) .. nl .. text:sub(node.close), true
124
+ end
125
+ node = child
126
+ end
127
+ end
128
+
129
+ function M.has(text, path)
130
+ if text == '' then return false end
131
+ local node = parse(text)
132
+ for _, key in ipairs(path) do
133
+ if node.kind ~= '{' or not node.children[key] then return false end
134
+ node = node.children[key]
135
+ end
136
+ return true
137
+ end
138
+
139
+ -- TOML keeps all unrelated tables verbatim. Reject ambiguous inline/dotted
140
+ -- definitions rather than risk producing a duplicate table.
141
+ function M.toml(text, url, remove, force)
142
+ local headers, multiline, parent_mcp = {}, nil, false
143
+ local function classify_header(value)
144
+ local parts, i = {}, 1
145
+ while i <= #value do
146
+ while value:sub(i, i):match('%s') do i = i + 1 end
147
+ local char, part = value:sub(i, i)
148
+ if char == '"' or char == "'" then
149
+ local quote, start = char, i
150
+ i = i + 1
151
+ while i <= #value do
152
+ char = value:sub(i, i); i = i + 1
153
+ if quote == '"' and char == '\\' then i = i + 1
154
+ elseif char == quote then break end
155
+ end
156
+ assert(value:sub(i - 1, i - 1) == quote, 'invalid TOML table header')
157
+ part = quote == '"' and xutils.json_unpack(value:sub(start, i - 1)) or value:sub(start + 1, i - 2)
158
+ else
159
+ part = value:sub(i):match('^[%w_-]+')
160
+ assert(part, 'invalid TOML table header'); i = i + #part
161
+ end
162
+ parts[#parts + 1] = part
163
+ while value:sub(i, i):match('%s') do i = i + 1 end
164
+ if i <= #value then assert(value:sub(i, i) == '.', 'invalid TOML table header'); i = i + 1 end
165
+ end
166
+ return parts[1] == 'mcp_servers' and parts[2] == 'codeoutline', #parts == 2,
167
+ parts[1] == 'mcp_servers' and #parts == 1
168
+ end
169
+ local pos = 1
170
+ for line in (text .. '\n'):gmatch('(.-)\n') do
171
+ local original_size = #line
172
+ local bom = pos == 1 and line:sub(1, 3) == '\239\187\191' and 3 or 0
173
+ if bom > 0 then line = line:sub(4) end
174
+ if not multiline then
175
+ local array_header = line:match('^%s*%[%[([^%[%]]+)%]%]%s*#?.*$')
176
+ local header = array_header or line:match('^%s*%[([^%[%]]+)%]%s*#?.*$')
177
+ assert(header or not line:match('^%s*%['), 'complex TOML table header requires manual editing')
178
+ if header then
179
+ local ours, base, parent = classify_header(header)
180
+ parent_mcp = parent
181
+ assert(not (array_header and ours), 'codeoutline cannot be a TOML array table')
182
+ headers[#headers + 1] = { ours = ours, base = base, start = pos + bom }
183
+ end
184
+ local key_line = line:gsub('["\']', '')
185
+ assert(not key_line:match('^%s*mcp_servers%s*=') and not key_line:match('^%s*mcp_servers%s*%.%s*codeoutline[%s%.=]')
186
+ and not (parent_mcp and key_line:match('^%s*codeoutline[%s%.=]')),
187
+ 'inline or dotted MCP tables require manual editing; use --print codex')
188
+ end
189
+ -- Scan quoted strings so table-looking text in multiline prompts is ignored.
190
+ local i = 1
191
+ while i <= #line do
192
+ if multiline then
193
+ local close = line:find(multiline, i, true)
194
+ if not close then break end
195
+ multiline = nil; i = close + 3
196
+ else
197
+ local char, triple = line:sub(i, i), line:sub(i, i + 2)
198
+ if char == '#' then break
199
+ elseif triple == '"""' or triple == "'''" then multiline = triple; i = i + 3
200
+ elseif char == '"' or char == "'" then
201
+ local quote = char; i = i + 1
202
+ while i <= #line do
203
+ char = line:sub(i, i); i = i + 1
204
+ if quote == '"' and char == '\\' then i = i + 1
205
+ elseif char == quote then break end
206
+ end
207
+ else i = i + 1 end
208
+ end
209
+ end
210
+ pos = pos + original_size + 1
211
+ end
212
+ assert(not multiline, 'unterminated TOML multiline string')
213
+ local start, finish
214
+ for i, header in ipairs(headers) do
215
+ if header.ours and header.base then
216
+ assert(not start, 'duplicate codeoutline TOML table')
217
+ start = header.start; finish = #text + 1
218
+ for j = i + 1, #headers do
219
+ if not headers[j].ours then finish = headers[j].start; break end
220
+ end
221
+ end
222
+ end
223
+ local nl = text:find('\r\n', 1, true) and '\r\n' or '\n'
224
+ local block = '[mcp_servers.codeoutline]' .. nl .. 'url = ' .. encode(url) .. nl
225
+ if start then
226
+ local old = text:sub(start, finish - 1)
227
+ if not remove and old:gsub('%s+$', '') == block:gsub('%s+$', '') then return text, false end
228
+ assert(remove or force, 'codeoutline entry already exists with different settings; use --force to replace it')
229
+ return text:sub(1, start - 1) .. (remove and '' or block .. nl) .. text:sub(finish), true
230
+ end
231
+ for _, header in ipairs(headers) do
232
+ assert(not header.ours, 'codeoutline TOML subtables require manual editing')
233
+ end
234
+ if remove then return text, false end
235
+ return text .. (text == '' and '' or nl) .. block, true
236
+ end
237
+
238
+ -- Hermes uses a YAML block mapping. Keep other sections and servers byte for
239
+ -- byte, and decline flow/anchor/quoted-key forms that need a full YAML parser.
240
+ function M.yaml(text, url, remove, force)
241
+ local lines, offsets, pos = {}, {}, 1
242
+ for line in (text .. '\n'):gmatch('(.-)\n') do
243
+ lines[#lines + 1] = line; offsets[#offsets + 1] = pos; pos = pos + #line + 1
244
+ end
245
+ local parent, finish, indent
246
+ for i, line in ipairs(lines) do
247
+ if line:match('^mcp_servers%s*:') then
248
+ assert(not parent, 'duplicate YAML mcp_servers mapping')
249
+ local rest = line:match('^mcp_servers%s*:%s*(.-)%s*\r?$'):gsub('%s*#.*$', '')
250
+ assert(rest == '' or rest == '{}' or rest == 'null', 'inline YAML mcp_servers requires manual editing')
251
+ parent = i
252
+ elseif parent and not finish and line:match('^%S') and not line:match('^#') then finish = i end
253
+ end
254
+ local nl = text:find('\r\n', 1, true) and '\r\n' or '\n'
255
+ if not parent then
256
+ assert(not text:find('mcp_servers', 1, true), 'nonstandard YAML MCP mapping requires manual editing')
257
+ if remove then return text, false end
258
+ return text .. (text == '' and '' or nl) .. 'mcp_servers:' .. nl .. ' codeoutline:' .. nl
259
+ .. ' url: ' .. encode(url) .. nl, true
260
+ end
261
+ finish = finish or #lines + 1
262
+ local start, stop
263
+ for i = parent + 1, finish - 1 do
264
+ local spaces = lines[i]:match('^( +)%S')
265
+ if spaces and not lines[i]:match('^%s*#') then indent = math.min(indent or #spaces, #spaces) end
266
+ end
267
+ indent = indent or 2
268
+ for i = parent + 1, finish - 1 do
269
+ local spaces = lines[i]:match('^( *)codeoutline%s*:')
270
+ if spaces and #spaces == indent then
271
+ assert(not start, 'duplicate YAML codeoutline entry'); start = i
272
+ elseif start and not stop and lines[i]:match('^%s*%S') and not lines[i]:match('^%s*#') then
273
+ local leading = lines[i]:match('^( *)')
274
+ if #leading <= indent then stop = i end
275
+ end
276
+ end
277
+ local block = string.rep(' ', indent) .. 'codeoutline:' .. nl .. string.rep(' ', indent + 2) .. 'url: ' .. encode(url) .. nl
278
+ if start then
279
+ local begin_at, end_at = offsets[start], offsets[stop or finish] or #text + 1
280
+ local old = text:sub(begin_at, end_at - 1)
281
+ if not remove and old:gsub('%s+$', '') == block:gsub('%s+$', '') then return text, false end
282
+ assert(remove or force, 'codeoutline entry already exists with different settings; use --force to replace it')
283
+ return text:sub(1, begin_at - 1) .. (remove and '' or block) .. text:sub(end_at), true
284
+ end
285
+ if remove then return text, false end
286
+ local parent_end = offsets[parent] + #lines[parent]
287
+ local parent_line = lines[parent]:gsub(':%s*{}', ':'):gsub(':%s*null', ':')
288
+ return text:sub(1, offsets[parent] - 1) .. parent_line .. nl .. block .. text:sub(parent_end + 1), true
289
+ end
290
+
291
+ return M
@@ -0,0 +1,195 @@
1
+ -- Register the shared HTTP MCP service in supported agent configurations.
2
+ local config = require('codeoutline.agent_config')
3
+ local M = {}
4
+ local windows = package.config:sub(1, 1) == '\\'
5
+ local function exists(path) return path and xutils.stat(path).exists end
6
+ local function env(name, fallback)
7
+ local value = os.getenv(name)
8
+ return value and value:match('%S') and value:gsub('\\', '/') or fallback
9
+ end
10
+ local function read(path)
11
+ if not exists(path) then return '' end
12
+ local f = assert(io.open(path, 'rb'), 'cannot read config: ' .. path)
13
+ local raw = f:read(4 * 1024 * 1024 + 1); f:close()
14
+ assert(#raw <= 4 * 1024 * 1024, 'config exceeds 4 MiB: ' .. path)
15
+ return raw
16
+ end
17
+ local function write(path, raw)
18
+ local dir = assert(path:match('^(.*)/[^/]+$'))
19
+ assert(xutils.mkdir_p(dir))
20
+ local temp = assert(xutils.temp_file(dir))
21
+ local ok, err = pcall(function()
22
+ local f = assert(io.open(temp, 'wb')); assert(f:write(raw)); assert(f:close())
23
+ assert(xutils.replace_file(temp, path), 'cannot replace config')
24
+ end)
25
+ if not ok then os.remove(temp); error(err) end
26
+ end
27
+ local function on_path(name)
28
+ local separator = windows and ';' or ':'
29
+ for dir in (env('PATH', '') .. separator):gmatch('(.-)' .. separator) do
30
+ dir = dir:gsub('^"(.*)"$', '%1')
31
+ if dir ~= '' and (exists(dir .. '/' .. name) or windows and
32
+ (exists(dir .. '/' .. name .. '.exe') or exists(dir .. '/' .. name .. '.cmd'))) then return true end
33
+ end
34
+ return false
35
+ end
36
+ local function platform()
37
+ if windows then return 'windows' end
38
+ local p = assert(io.popen('uname -s')); local value = p:read('a'); p:close()
39
+ return value:match('Darwin') and 'darwin' or 'linux'
40
+ end
41
+
42
+ function M.detect(location, project)
43
+ local h = env('USERPROFILE', env('HOME', '.'))
44
+ local xdg = env('XDG_CONFIG_HOME', h .. '/.config')
45
+ local codex = env('CODEX_HOME', h .. '/.codex')
46
+ local claude = env('CLAUDE_CONFIG_DIR', h .. '/.claude')
47
+ local claude_file = env('CLAUDE_CONFIG_DIR', h) .. '/.claude.json'
48
+ local copilot = env('COPILOT_HOME', h .. '/.copilot')
49
+ local kind = platform()
50
+ local code_user = kind == 'windows' and env('APPDATA', h .. '/AppData/Roaming') .. '/Code/User'
51
+ or kind == 'darwin' and h .. '/Library/Application Support/Code/User' or xdg .. '/Code/User'
52
+ local jetbrains = kind == 'windows' and env('LOCALAPPDATA', h .. '/AppData/Local') .. '/github-copilot/intellij'
53
+ or xdg .. '/github-copilot/intellij'
54
+ local antigravity = h .. '/.gemini/config'
55
+ if not exists(antigravity .. '/mcp_config.json') and not exists(antigravity .. '/.migrated') then
56
+ antigravity = h .. '/.gemini/antigravity'
57
+ end
58
+ local opencode = xdg .. '/opencode'
59
+ local function opencode_file(dir)
60
+ if exists(dir .. '/opencode.jsonc') then return dir .. '/opencode.jsonc' end
61
+ if exists(dir .. '/opencode.json') then return dir .. '/opencode.json' end
62
+ return dir .. '/opencode.jsonc'
63
+ end
64
+ local targets = {
65
+ { id = 'claude', name = 'Claude Code', path = claude_file, local_path = project .. '/.mcp.json', marker = claude, cli = 'claude' },
66
+ { id = 'cursor', name = 'Cursor Agent', path = h .. '/.cursor/mcp.json', local_path = project .. '/.cursor/mcp.json', marker = h .. '/.cursor', cli = 'cursor' },
67
+ { id = 'codex', name = 'Codex', path = codex .. '/config.toml', local_path = project .. '/.codex/config.toml', marker = codex, cli = 'codex', format = 'toml' },
68
+ { id = 'gemini', name = 'Gemini CLI', path = h .. '/.gemini/settings.json', local_path = project .. '/.gemini/settings.json', marker = h .. '/.gemini', cli = 'gemini', entry = 'gemini' },
69
+ { id = 'opencode', name = 'OpenCode', path = opencode_file(opencode), local_path = opencode_file(project), marker = opencode, cli = 'opencode', entry = 'opencode' },
70
+ { id = 'hermes', name = 'Hermes Agent', path = env('HERMES_HOME', h .. '/.hermes') .. '/config.yaml', marker = env('HERMES_HOME', h .. '/.hermes'), cli = 'hermes', format = 'yaml' },
71
+ { id = 'kiro', name = 'Kiro', path = h .. '/.kiro/settings/mcp.json', local_path = project .. '/.kiro/settings/mcp.json', marker = h .. '/.kiro', cli = 'kiro' },
72
+ { id = 'antigravity', name = 'Antigravity IDE', path = antigravity .. '/mcp_config.json', marker = antigravity, cli = 'antigravity', entry = 'antigravity' },
73
+ { id = 'copilot-vscode', name = 'GitHub Copilot in VS Code', path = code_user .. '/mcp.json', local_path = project .. '/.vscode/mcp.json', marker = code_user, wrapper = 'servers' },
74
+ { id = 'copilot-cli', name = 'GitHub Copilot CLI', path = copilot .. '/mcp-config.json', marker = copilot, cli = 'copilot', entry = 'copilot' },
75
+ { id = 'copilot-jetbrains', name = 'GitHub Copilot in JetBrains', path = jetbrains .. '/mcp.json', marker = jetbrains, wrapper = 'servers', entry = 'jetbrains' },
76
+ }
77
+ for _, target in ipairs(targets) do
78
+ target.detected = exists(target.marker) or exists(target.path) or target.cli and on_path(target.cli) or false
79
+ if location == 'local' then target.path = target.local_path end
80
+ end
81
+ return targets
82
+ end
83
+
84
+ local function select_targets(value, targets)
85
+ local wanted, selected = {}, {}
86
+ value = value:lower():gsub('%s+', '')
87
+ if value == 'y' or value == 'yes' then value = 'all' end
88
+ if value == 'n' or value == 'no' then value = 'none' end
89
+ if value == 'none' or value == '' then return selected end
90
+ for id in value:gmatch('[^,]+') do wanted[id] = true end
91
+ assert(not value:find(',,', 1, true) and not value:match('^,') and not value:match(',$'), 'invalid target selection')
92
+ for _, target in ipairs(targets) do
93
+ if wanted[target.id] or (value == 'auto' or value == 'all') and target.detected and target.path then
94
+ assert(target.path, target.name .. ' only supports --location global')
95
+ selected[#selected + 1] = target
96
+ end
97
+ wanted[target.id] = nil
98
+ end
99
+ if value ~= 'all' and value ~= 'auto' then
100
+ for id in pairs(wanted) do error('unknown agent target: ' .. id) end
101
+ end
102
+ return selected
103
+ end
104
+
105
+ local function render(target, raw, url, remove, force)
106
+ if target.format == 'toml' then return config.toml(raw, url, remove, force) end
107
+ if target.format == 'yaml' then return config.yaml(raw, url, remove, force) end
108
+ local entry, path = { type = 'http', url = url }, { target.wrapper or 'mcpServers', 'codeoutline' }
109
+ if target.id == 'cursor' or target.id == 'kiro' then entry = { url = url }
110
+ elseif target.entry == 'gemini' then entry = { httpUrl = url }
111
+ elseif target.entry == 'antigravity' then entry = { serverUrl = url }
112
+ elseif target.entry == 'jetbrains' then entry = { url = url }
113
+ elseif target.entry == 'copilot' then entry.tools = { '*' }
114
+ elseif target.entry == 'opencode' then
115
+ entry = { type = 'remote', url = url, enabled = true }
116
+ path = { 'mcp', 'codeoutline' }
117
+ -- Preserve the native OpenCode 2 wrapper when already in use.
118
+ if config.has(raw, { 'mcp', 'servers' }) then
119
+ path = { 'mcp', 'servers', 'codeoutline' }
120
+ entry.enabled = nil; entry.disabled = false; entry.codemode = false
121
+ end
122
+ end
123
+ return config.json(raw, path, entry, remove, force)
124
+ end
125
+
126
+ local function say(message) io.write(message, '\n'); io.stdout:flush() end
127
+ function M.run(options)
128
+ options = options or {}
129
+ local location = options.location or 'global'
130
+ assert(location == 'global' or location == 'local', '--location must be global or local')
131
+ local port = tonumber(options.port or 19876)
132
+ assert(port and port % 1 == 0 and port >= 1 and port <= 65535, 'invalid --port')
133
+ local url = 'http://127.0.0.1:' .. port .. '/mcp'
134
+ local project = assert(xutils.realpath(options.project or '.')):gsub('\\', '/')
135
+ local targets = M.detect(location, project)
136
+ if options.list then
137
+ for _, target in ipairs(targets) do
138
+ say(target.id .. ': ' .. (target.detected and 'detected' or 'not detected') .. ' -> ' .. (target.path or 'global scope only'))
139
+ end
140
+ return 0
141
+ end
142
+ if options.print or options['print-config'] then
143
+ local selected = select_targets(options.print or options['print-config'], targets)
144
+ assert(#selected == 1, '--print-config requires one target id')
145
+ local text = render(selected[1], '', url, false, true)
146
+ say('# ' .. selected[1].path); say(text); return 0
147
+ end
148
+ local value = options.target or env('CODEOUTLINE_AGENTS')
149
+ if not value and options.yes then value = 'auto' end
150
+ if not value then
151
+ if os.getenv('CODEOUTLINE_INTERACTIVE') ~= '1' then
152
+ say('Optional agent MCP setup: run codeoutline agents, or codeoutline install --yes.'); return 0
153
+ end
154
+ local available = {}
155
+ for _, target in ipairs(targets) do if target.detected and target.path then available[#available + 1] = target end end
156
+ if #available == 0 then say('No supported AI tools detected; skipping MCP configuration.'); return 0 end
157
+ say('Detected AI tools: ' .. #available .. '. Confirm each tool; Enter accepts the default.')
158
+ local chosen = {}
159
+ for _, target in ipairs(available) do
160
+ local answer = require('codeoutline.install_prompt').confirm(
161
+ (options.remove and 'Remove code_explore from ' or 'Install code_explore (MCP) for ') .. target.name .. ' (' .. target.id .. ')?',
162
+ not options.remove)
163
+ if answer == nil then break end
164
+ if answer then chosen[#chosen + 1] = target.id end
165
+ end
166
+ value = #chosen > 0 and table.concat(chosen, ',') or 'none'
167
+ end
168
+ local selected = select_targets(value, targets)
169
+ -- Plan every selected edit before making any writes. Broken configs and
170
+ -- conflicts are reported without partially configuring the other agents.
171
+ local plan = {}
172
+ for _, target in ipairs(selected) do
173
+ local raw = read(target.path)
174
+ local next_raw, changed = render(target, raw, url, options.remove, options.force)
175
+ plan[#plan + 1] = { target = target, before = raw, after = next_raw, changed = changed }
176
+ end
177
+ for _, item in ipairs(plan) do
178
+ local path = item.target.path
179
+ if item.changed then
180
+ assert(read(path) == item.before, 'config changed during installation; retry: ' .. path)
181
+ if item.before ~= '' and not exists(path .. '.codeoutline.bak') then write(path .. '.codeoutline.bak', item.before) end
182
+ write(path, item.after)
183
+ end
184
+ say(item.target.name .. ': ' .. (item.changed and (options.remove and 'removed' or 'configured') or 'unchanged') .. ' ' .. path)
185
+ end
186
+ if #selected > 0 and not options.remove then
187
+ say('Restart the selected agents to load codeoutline_explore and the other MCP tools.')
188
+ say('Shared MCP endpoint: ' .. url)
189
+ if not options.installing then say('LSP editor clients can be installed with codeoutline editors.') end
190
+ if location == 'local' then say('Project configuration may require trusting the project in the agent.') end
191
+ end
192
+ return 0
193
+ end
194
+
195
+ return M