universal-plugin-hub 1.0.5 → 1.0.12

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/src/routes.js CHANGED
@@ -15,9 +15,11 @@ import { getOrFetchIcon, readLocalIcon } from './icons.js'
15
15
  import {
16
16
  installPlugin, uninstallPlugin, togglePlugin, updatePlugin, installedDetails, isScanDirRegistered,
17
17
  toggleConnector, isConnectorActive, verifyConnectorAuth, saveConnectorAuth,
18
- fetchConnectorTools, setToolDisabled, isLspActive, readCordisPatch, unregisterConnector, getConnectorEntry,
18
+ fetchConnectorTools, setToolDisabled, isLspActive, readCordisPatch, unregisterConnector, getConnectorEntry, readMcpToolsState,
19
19
  } from './install.js'
20
20
  import { inspectLocalPlugin, importLocalPlugin, saveUploadedZip } from './local.js'
21
+ import { getOAuthVerification, cancelOAuthVerification, readSavedOAuthClient } from './oauth.js'
22
+ import { remoteConfigFromEntry } from './oauth-config.js'
21
23
 
22
24
  const API_PREFIX = '/universal-plugin-hub/api'
23
25
 
@@ -46,6 +48,9 @@ function userMcpConfig(name, body) {
46
48
  env: body.env && typeof body.env === 'object' ? body.env : {},
47
49
  headers: body.headers && typeof body.headers === 'object' ? body.headers : {},
48
50
  oauth: body.oauth || 'none',
51
+ oauthClientId: body.oauthClientId || '',
52
+ oauthCallbackPort: body.oauthCallbackPort || null,
53
+ oauthScope: body.oauthScope || '',
49
54
  }
50
55
  }
51
56
 
@@ -285,9 +290,31 @@ async function handle(req, res, prefix = API_PREFIX) {
285
290
  if (changed) writeState(state)
286
291
  const mcps = state.userMcps
287
292
  .filter((x) => x && x.name)
288
- .map((x) => ({ ...x, enabled: enabledIds.has('mcp-user-mcp-' + x.name) }))
293
+ .map((x) => {
294
+ const entry = getConnectorEntry('user-mcp', x.name)
295
+ const remote = remoteConfigFromEntry(entry?.config) || readMcpToolsState()[`user-mcp::${x.name}`]?.savedOAuthRemoteConfig
296
+ const client = remote?.oauth === 'custom' ? readSavedOAuthClient('user-mcp', x.name, remote.url, remote.oauth) : null
297
+ return {
298
+ ...x,
299
+ ...(remote ? {
300
+ transport: 'http', url: remote.url, headers: remote.headers,
301
+ oauth: remote.oauth, oauthClientId: client?.clientId || remote.oauthClientId,
302
+ oauthHasClientSecret: client?.hasClientSecret || false,
303
+ oauthCallbackPort: remote.oauthCallbackPort || null, oauthScope: remote.oauthScope || '',
304
+ } : {}),
305
+ enabled: enabledIds.has('mcp-user-mcp-' + x.name),
306
+ }
307
+ })
289
308
  return sendJson(res, 200, { ok: true, mcps })
290
309
  }
310
+ if (path === '/mcps/oauth-status') {
311
+ requireSameOrigin(req)
312
+ if (method !== 'GET' && method !== 'POST') return sendJson(res, 405, { error: 'method not allowed' })
313
+ const sessionId = String(method === 'GET' ? url.searchParams.get('sessionId') : (await readJsonBody(req)).sessionId || '')
314
+ const result = getOAuthVerification(sessionId, { pluginName: 'user-mcp' })
315
+ if (method === 'POST') return sendJson(res, 200, { ok: true, cancelled: result.found ? cancelOAuthVerification(sessionId, '已取消授权', { pluginName: 'user-mcp' }) : false })
316
+ return sendJson(res, 200, result)
317
+ }
291
318
  if (method === 'POST' && path === '/mcps/verify') {
292
319
  requireSameOrigin(req)
293
320
  const body = await readJsonBody(req)
@@ -363,11 +390,22 @@ async function handle(req, res, prefix = API_PREFIX) {
363
390
  }
364
391
 
365
392
  // ── connector routes (must precede catch-all /plugins/ routes) ────────
393
+ if ((method === 'GET' || method === 'POST') && path.startsWith('/plugins/') && path.includes('/connectors/') && path.endsWith('/oauth-status')) {
394
+ requireSameOrigin(req)
395
+ const parts = path.slice('/plugins/'.length).split('/')
396
+ const pluginName = safeSegment(parts[0], 'plugin name')
397
+ const connectorName = safeSegment(parts[2], 'connector name')
398
+ const sessionId = String(method === 'GET' ? url.searchParams.get('sessionId') : (await readJsonBody(req)).sessionId || '')
399
+ const result = getOAuthVerification(sessionId, { pluginName, connectorName })
400
+ if (method === 'POST') return sendJson(res, 200, { ok: true, cancelled: result.found ? cancelOAuthVerification(sessionId, '已取消授权', { pluginName, connectorName }) : false })
401
+ return sendJson(res, 200, result)
402
+ }
403
+
366
404
  if (method === 'GET' && path.startsWith('/plugins/') && path.includes('/connectors/') && path.endsWith('/tools')) {
367
405
  const parts = path.slice('/plugins/'.length).split('/')
368
406
  const pluginName = safeSegment(parts[0], 'plugin name')
369
407
  const connectorName = safeSegment(parts[2], 'connector name')
370
- const result = await fetchConnectorTools(pluginName, connectorName)
408
+ const result = await fetchConnectorTools(pluginName, connectorName, url.searchParams.get('fresh') === '1')
371
409
  return sendJson(res, 200, result)
372
410
  }
373
411
 
@@ -432,16 +470,32 @@ async function handle(req, res, prefix = API_PREFIX) {
432
470
  row = readMarketplace(sourceId).find((r) => r.name === pluginName) || row
433
471
  detail = pluginDetail(sourceId, pluginName, row.source, row)
434
472
  }
473
+ // One pass over the installed tree per request: `installedDetails()` re-reads
474
+ // every connector, skill and LSP entry for every plugin, so it must not run
475
+ // twice in the same handler.
476
+ let installed = null
477
+ const findInstalled = () => {
478
+ if (!installed) installed = installedDetails().find((p) => p.name === pluginName)
479
+ return installed
480
+ }
435
481
  if (!detail) {
436
- const inst = installedDetails().find((p) => p.name === pluginName)
482
+ const inst = findInstalled()
437
483
  if (inst) {
438
484
  detail = inst
439
485
  if (!row) row = inst
440
486
  }
441
487
  }
442
488
  if (detail && Array.isArray(detail.connectors)) {
489
+ // `pluginDetail` reads the marketplace copy, which knows nothing about
490
+ // what the user saved here (OAuth mode, client id, callback port, the
491
+ // URL they edited). Take the installed row's entry whenever one exists,
492
+ // so opening a connector's settings reflects the saved configuration
493
+ // instead of silently reverting it to `None` and the manifest URL.
494
+ const installedConnectors = new Map(((findInstalled()?.connectors) || []).map((c) => [c.name, c]))
443
495
  detail.connectors = detail.connectors.map((c) => ({
444
496
  ...c,
497
+ ...(installedConnectors.get(c.name) || {}),
498
+ name: c.name,
445
499
  connected: isConnectorActive(pluginName, c.name),
446
500
  }))
447
501
  }