picocode-core 0.9.209 → 0.9.210
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.
- package/package.json +1 -1
- package/src/mcp.js +119 -47
package/package.json
CHANGED
package/src/mcp.js
CHANGED
|
@@ -79,8 +79,11 @@ async function readJson(file, fallback) {
|
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
|
|
83
|
-
|
|
82
|
+
// the global file: every project's servers, and the ones switched off
|
|
83
|
+
// everywhere unless a project switches them back on
|
|
84
|
+
export async function readRegistry() {
|
|
85
|
+
const registry = await readJson(globalMcpFile(), {})
|
|
86
|
+
return { servers: registry.servers || {}, disabled: registry.disabled || {} }
|
|
84
87
|
}
|
|
85
88
|
|
|
86
89
|
export async function writeRegistry(registry) {
|
|
@@ -88,6 +91,8 @@ export async function writeRegistry(registry) {
|
|
|
88
91
|
await writeFile(globalMcpFile(), JSON.stringify(registry, null, 2) + '\n')
|
|
89
92
|
}
|
|
90
93
|
|
|
94
|
+
// a project's file: its own servers, and a switch per server where the
|
|
95
|
+
// project differs from the global setting (true off, false on)
|
|
91
96
|
export async function readProjectConfig(root) {
|
|
92
97
|
const config = await readJson(projectMcpFile(root), {})
|
|
93
98
|
return { disabled: config.disabled || {}, servers: config.servers || {} }
|
|
@@ -120,21 +125,20 @@ export async function createMcpTransport(command) {
|
|
|
120
125
|
}
|
|
121
126
|
|
|
122
127
|
export async function createMcpRuntime({ root, onChange = () => {} }) {
|
|
123
|
-
|
|
124
|
-
|
|
128
|
+
let registry = await readRegistry()
|
|
129
|
+
let projectConfig = await readProjectConfig(root)
|
|
125
130
|
const servers = new Map()
|
|
131
|
+
// switches for this session alone, never written anywhere
|
|
132
|
+
const sessionSettings = new Map()
|
|
133
|
+
|
|
134
|
+
const projectSetting = (name) => (Object.hasOwn(projectConfig.disabled, name) ? !projectConfig.disabled[name] : null)
|
|
135
|
+
const globalEnabled = (name) => !registry.disabled[name]
|
|
136
|
+
// the session's switch wins, then the project's, then the global one
|
|
137
|
+
const effective = (name) => sessionSettings.has(name) ? sessionSettings.get(name) : projectSetting(name) ?? globalEnabled(name)
|
|
126
138
|
|
|
127
139
|
const register = (name, command, scope) => {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
command,
|
|
131
|
-
scope,
|
|
132
|
-
enabled: !projectConfig.disabled[name],
|
|
133
|
-
status: projectConfig.disabled[name] ? 'disabled' : 'idle',
|
|
134
|
-
error: null,
|
|
135
|
-
connection: null,
|
|
136
|
-
transport: null,
|
|
137
|
-
})
|
|
140
|
+
const enabled = effective(name)
|
|
141
|
+
servers.set(name, { name, command, scope, enabled, status: enabled ? 'idle' : 'disabled', error: null, connection: null, transport: null })
|
|
138
142
|
}
|
|
139
143
|
for (const [name, command] of Object.entries(registry.servers)) register(name, command, 'global')
|
|
140
144
|
for (const [name, command] of Object.entries(projectConfig.servers)) register(name, command, 'project')
|
|
@@ -146,13 +150,21 @@ export async function createMcpRuntime({ root, onChange = () => {} }) {
|
|
|
146
150
|
server.error = null
|
|
147
151
|
onChange()
|
|
148
152
|
try {
|
|
149
|
-
|
|
150
|
-
server.
|
|
153
|
+
const transport = await createMcpTransport(server.command)
|
|
154
|
+
server.transport = transport
|
|
155
|
+
const connection = await connectMCP({
|
|
151
156
|
name,
|
|
152
|
-
transport: () =>
|
|
157
|
+
transport: () => transport,
|
|
153
158
|
})
|
|
159
|
+
// switched off or replaced while it was connecting
|
|
160
|
+
if (servers.get(name) !== server || !server.enabled || server.transport !== transport) {
|
|
161
|
+
await connection.close().catch(() => {})
|
|
162
|
+
return
|
|
163
|
+
}
|
|
164
|
+
server.connection = connection
|
|
154
165
|
server.status = 'connected'
|
|
155
166
|
} catch (err) {
|
|
167
|
+
if (servers.get(name) !== server || !server.enabled) return
|
|
156
168
|
server.status = 'error'
|
|
157
169
|
server.error = String(err.message || err).slice(0, 300)
|
|
158
170
|
server.connection = null
|
|
@@ -163,10 +175,28 @@ export async function createMcpRuntime({ root, onChange = () => {} }) {
|
|
|
163
175
|
|
|
164
176
|
async function disconnect(name) {
|
|
165
177
|
const server = servers.get(name)
|
|
166
|
-
if (server
|
|
167
|
-
|
|
168
|
-
|
|
178
|
+
if (!server) return
|
|
179
|
+
const connection = server.connection
|
|
180
|
+
server.connection = null
|
|
181
|
+
server.transport = null
|
|
182
|
+
if (connection) await connection.close().catch(() => {})
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// brings one server in line with its switches; connecting and closing
|
|
186
|
+
// run in the background, so nothing waits on a slow server
|
|
187
|
+
function apply(name) {
|
|
188
|
+
const server = servers.get(name)
|
|
189
|
+
if (!server) return
|
|
190
|
+
const enabled = effective(name)
|
|
191
|
+
if (enabled === server.enabled && (enabled ? server.status !== 'disabled' : true)) return
|
|
192
|
+
server.enabled = enabled
|
|
193
|
+
if (enabled) connect(name).catch(() => {})
|
|
194
|
+
else {
|
|
195
|
+
server.status = 'disabled'
|
|
196
|
+
server.error = null
|
|
197
|
+
disconnect(name).catch(() => {})
|
|
169
198
|
}
|
|
199
|
+
onChange()
|
|
170
200
|
}
|
|
171
201
|
|
|
172
202
|
return {
|
|
@@ -177,18 +207,59 @@ export async function createMcpRuntime({ root, onChange = () => {} }) {
|
|
|
177
207
|
}
|
|
178
208
|
return Promise.allSettled(pending)
|
|
179
209
|
},
|
|
210
|
+
// reads both files again and follows them: servers added, removed or
|
|
211
|
+
// changed elsewhere, and switches flipped in another session
|
|
212
|
+
async sync() {
|
|
213
|
+
registry = await readRegistry()
|
|
214
|
+
projectConfig = await readProjectConfig(root)
|
|
215
|
+
const wanted = new Map([
|
|
216
|
+
...Object.entries(registry.servers).map(([name, command]) => [name, { command, scope: 'global' }]),
|
|
217
|
+
...Object.entries(projectConfig.servers).map(([name, command]) => [name, { command, scope: 'project' }]),
|
|
218
|
+
])
|
|
219
|
+
for (const name of [...servers.keys()]) {
|
|
220
|
+
if (wanted.has(name)) continue
|
|
221
|
+
disconnect(name).catch(() => {})
|
|
222
|
+
servers.delete(name)
|
|
223
|
+
}
|
|
224
|
+
for (const [name, { command, scope }] of wanted) {
|
|
225
|
+
const server = servers.get(name)
|
|
226
|
+
if (!server) {
|
|
227
|
+
register(name, command, scope)
|
|
228
|
+
if (servers.get(name).enabled) connect(name).catch(() => {})
|
|
229
|
+
continue
|
|
230
|
+
}
|
|
231
|
+
server.scope = scope
|
|
232
|
+
if (server.command !== command) {
|
|
233
|
+
server.command = command
|
|
234
|
+
disconnect(name).catch(() => {})
|
|
235
|
+
server.status = server.enabled ? 'idle' : 'disabled'
|
|
236
|
+
if (server.enabled) connect(name).catch(() => {})
|
|
237
|
+
}
|
|
238
|
+
apply(name)
|
|
239
|
+
}
|
|
240
|
+
onChange()
|
|
241
|
+
},
|
|
242
|
+
// on or off for this session only; null follows the project and global
|
|
243
|
+
setSessionEnabled(name, enabled) {
|
|
244
|
+
if (enabled === null || enabled === undefined) sessionSettings.delete(name)
|
|
245
|
+
else sessionSettings.set(name, !!enabled)
|
|
246
|
+
apply(name)
|
|
247
|
+
},
|
|
180
248
|
async add(name, command, scope = 'global') {
|
|
181
249
|
if (scope === 'project') {
|
|
182
250
|
const config = await readProjectConfig(root)
|
|
183
251
|
config.servers[name] = command
|
|
184
252
|
await writeProjectConfig(root, config)
|
|
253
|
+
projectConfig = config
|
|
185
254
|
} else {
|
|
186
|
-
const
|
|
187
|
-
|
|
188
|
-
await writeRegistry(
|
|
255
|
+
const next = await readRegistry()
|
|
256
|
+
next.servers[name] = command
|
|
257
|
+
await writeRegistry(next)
|
|
258
|
+
registry = next
|
|
189
259
|
}
|
|
190
|
-
|
|
191
|
-
await connect(name)
|
|
260
|
+
register(name, command, scope)
|
|
261
|
+
if (servers.get(name).enabled) await connect(name)
|
|
262
|
+
else onChange()
|
|
192
263
|
},
|
|
193
264
|
async update(name, command) {
|
|
194
265
|
const server = servers.get(name)
|
|
@@ -197,10 +268,12 @@ export async function createMcpRuntime({ root, onChange = () => {} }) {
|
|
|
197
268
|
const config = await readProjectConfig(root)
|
|
198
269
|
config.servers[name] = command
|
|
199
270
|
await writeProjectConfig(root, config)
|
|
271
|
+
projectConfig = config
|
|
200
272
|
} else {
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
await writeRegistry(
|
|
273
|
+
const next = await readRegistry()
|
|
274
|
+
next.servers[name] = command
|
|
275
|
+
await writeRegistry(next)
|
|
276
|
+
registry = next
|
|
204
277
|
}
|
|
205
278
|
await disconnect(name)
|
|
206
279
|
server.command = command
|
|
@@ -215,34 +288,30 @@ export async function createMcpRuntime({ root, onChange = () => {} }) {
|
|
|
215
288
|
const config = await readProjectConfig(root)
|
|
216
289
|
delete config.servers[name]
|
|
217
290
|
await writeProjectConfig(root, config)
|
|
291
|
+
projectConfig = config
|
|
218
292
|
} else {
|
|
219
|
-
const
|
|
220
|
-
delete
|
|
221
|
-
|
|
293
|
+
const next = await readRegistry()
|
|
294
|
+
delete next.servers[name]
|
|
295
|
+
delete next.disabled[name]
|
|
296
|
+
await writeRegistry(next)
|
|
297
|
+
registry = next
|
|
222
298
|
}
|
|
223
299
|
await disconnect(name)
|
|
224
300
|
servers.delete(name)
|
|
225
301
|
onChange()
|
|
226
302
|
},
|
|
303
|
+
// the project's switch: off here, or back to on (overriding a global off)
|
|
227
304
|
async toggle(name) {
|
|
228
305
|
const server = servers.get(name)
|
|
229
306
|
if (!server) return
|
|
230
307
|
const config = await readProjectConfig(root)
|
|
231
|
-
if (server.enabled)
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
await writeProjectConfig(root, config)
|
|
239
|
-
disconnect(name).catch(() => {})
|
|
240
|
-
} else {
|
|
241
|
-
server.enabled = true
|
|
242
|
-
delete config.disabled[name]
|
|
243
|
-
await writeProjectConfig(root, config)
|
|
244
|
-
await connect(name)
|
|
245
|
-
}
|
|
308
|
+
if (server.enabled) config.disabled[name] = true
|
|
309
|
+
else if (globalEnabled(name)) delete config.disabled[name]
|
|
310
|
+
else config.disabled[name] = false
|
|
311
|
+
await writeProjectConfig(root, config)
|
|
312
|
+
projectConfig = config
|
|
313
|
+
sessionSettings.delete(name)
|
|
314
|
+
apply(name)
|
|
246
315
|
},
|
|
247
316
|
reconnect(name) {
|
|
248
317
|
return disconnect(name).then(() => connect(name))
|
|
@@ -253,6 +322,9 @@ export async function createMcpRuntime({ root, onChange = () => {} }) {
|
|
|
253
322
|
command: s.command,
|
|
254
323
|
scope: s.scope,
|
|
255
324
|
enabled: s.enabled,
|
|
325
|
+
globalEnabled: globalEnabled(s.name),
|
|
326
|
+
projectEnabled: projectSetting(s.name),
|
|
327
|
+
sessionEnabled: sessionSettings.has(s.name) ? sessionSettings.get(s.name) : null,
|
|
256
328
|
status: s.status,
|
|
257
329
|
error: s.error,
|
|
258
330
|
toolCount: s.connection?.tools.length || 0,
|
|
@@ -261,7 +333,7 @@ export async function createMcpRuntime({ root, onChange = () => {} }) {
|
|
|
261
333
|
},
|
|
262
334
|
tools() {
|
|
263
335
|
return [...servers.values()]
|
|
264
|
-
.filter((s) => s.connection)
|
|
336
|
+
.filter((s) => s.connection && s.enabled)
|
|
265
337
|
.flatMap((s) => s.connection.tools)
|
|
266
338
|
},
|
|
267
339
|
closeAll() {
|