@fengguanghuai/dsh-capability-hub 1.0.1 → 1.0.2

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.
Files changed (3) hide show
  1. package/client.js +352 -20
  2. package/index.js +109 -6
  3. package/package.json +1 -1
package/client.js CHANGED
@@ -1111,12 +1111,22 @@ window.__ModuleLoader__.load({
1111
1111
  const [bfSkillFilter, setBfSkillFilter] = useState('');
1112
1112
 
1113
1113
  // Custom MCP form state
1114
+ const [cmEditing, setCmEditing] = useState(false);
1114
1115
  const [cmName, setCmName] = useState('');
1115
1116
  const [cmTransport, setCmTransport] = useState('streamable-http');
1116
1117
  const [cmUrl, setCmUrl] = useState('');
1118
+ const [cmAuthType, setCmAuthType] = useState('none'); // 'none' | 'bearer' | 'api-key' | 'custom'
1119
+ const [cmBearerToken, setCmBearerToken] = useState('');
1120
+ const [cmApiKeyHeader, setCmApiKeyHeader] = useState('X-API-Key');
1121
+ const [cmApiKeyValue, setCmApiKeyValue] = useState('');
1122
+ const [cmHeadersText, setCmHeadersText] = useState('');
1123
+ const [cmKeepHeaders, setCmKeepHeaders] = useState({}); // { key: maskedValue }
1117
1124
  const [cmCommand, setCmCommand] = useState('npx');
1118
1125
  const [cmArgs, setCmArgs] = useState('');
1126
+ const [cmEnvText, setCmEnvText] = useState('');
1127
+ const [cmKeepEnv, setCmKeepEnv] = useState({}); // { key: maskedValue }
1119
1128
  const [cmCwd, setCmCwd] = useState('');
1129
+ const [cmShowSecret, setCmShowSecret] = useState(false);
1120
1130
 
1121
1131
  // Skill add / edit / import / delete state
1122
1132
  const [skModeTab, setSkModeTab] = useState('create'); // 'create' | 'import'
@@ -1214,13 +1224,56 @@ window.__ModuleLoader__.load({
1214
1224
  setDrawer({ kind: 'bundle-form' });
1215
1225
  }, []);
1216
1226
 
1217
- const openConnectMcp = useCallback(() => {
1218
- setCmName('');
1219
- setCmTransport('streamable-http');
1220
- setCmUrl('');
1221
- setCmCommand('npx');
1222
- setCmArgs('');
1223
- setCmCwd('');
1227
+ const openConnectMcp = useCallback((existing = null) => {
1228
+ setCmShowSecret(false);
1229
+ if (existing && typeof existing === 'object' && typeof existing.name === 'string' && existing.name) {
1230
+ setCmEditing(true);
1231
+ setCmName(existing.name || '');
1232
+ const tr = existing.transport === 'stdio' ? 'stdio' : 'streamable-http';
1233
+ setCmTransport(tr);
1234
+ setCmUrl(existing.url || '');
1235
+ setCmCommand(existing.command || 'npx');
1236
+ setCmArgs(Array.isArray(existing.args) ? existing.args.join(' ') : '');
1237
+ setCmCwd(existing.cwd || '');
1238
+ const existingHeaders = existing.headers && typeof existing.headers === 'object' ? { ...existing.headers } : {};
1239
+ const existingEnv = existing.env && typeof existing.env === 'object' ? { ...existing.env } : {};
1240
+ setCmKeepHeaders(existingHeaders);
1241
+ setCmKeepEnv(existingEnv);
1242
+ setCmBearerToken('');
1243
+ setCmApiKeyValue('');
1244
+ setCmHeadersText('');
1245
+ setCmEnvText('');
1246
+ const hKeys = Object.keys(existingHeaders);
1247
+ if (hKeys.length === 0) {
1248
+ setCmAuthType('none');
1249
+ setCmApiKeyHeader('X-API-Key');
1250
+ } else if (hKeys.length === 1 && /^authorization$/i.test(hKeys[0]) && /^bearer\s+/i.test(existingHeaders[hKeys[0]] || '')) {
1251
+ setCmAuthType('bearer');
1252
+ setCmApiKeyHeader('X-API-Key');
1253
+ } else if (hKeys.length === 1) {
1254
+ setCmAuthType('api-key');
1255
+ setCmApiKeyHeader(hKeys[0]);
1256
+ } else {
1257
+ setCmAuthType('custom');
1258
+ setCmApiKeyHeader('X-API-Key');
1259
+ }
1260
+ } else {
1261
+ setCmEditing(false);
1262
+ setCmName('');
1263
+ setCmTransport('streamable-http');
1264
+ setCmUrl('');
1265
+ setCmAuthType('none');
1266
+ setCmBearerToken('');
1267
+ setCmApiKeyHeader('X-API-Key');
1268
+ setCmApiKeyValue('');
1269
+ setCmHeadersText('');
1270
+ setCmKeepHeaders({});
1271
+ setCmCommand('npx');
1272
+ setCmArgs('');
1273
+ setCmEnvText('');
1274
+ setCmKeepEnv({});
1275
+ setCmCwd('');
1276
+ }
1224
1277
  setDrawer({ kind: 'connect-mcp' });
1225
1278
  }, []);
1226
1279
 
@@ -1622,7 +1675,7 @@ window.__ModuleLoader__.load({
1622
1675
  activeTab === 'skills'
1623
1676
  ? h('button', { type: 'button', className: 'ch-btn ch-btn-primary', onClick: () => openAddSkill(null) }, t('btn.addSkill'))
1624
1677
  : activeTab === 'mcps'
1625
- ? h('button', { type: 'button', className: 'ch-btn ch-btn-primary', onClick: openConnectMcp }, t('btn.connectMcp'))
1678
+ ? h('button', { type: 'button', className: 'ch-btn ch-btn-primary', onClick: () => openConnectMcp() }, t('btn.connectMcp'))
1626
1679
  : h('button', { type: 'button', className: 'ch-btn ch-btn-primary', onClick: () => openBundleForm(null) }, t('btn.newBundle')),
1627
1680
  ),
1628
1681
  ),
@@ -2111,6 +2164,7 @@ window.__ModuleLoader__.load({
2111
2164
  { style: { display: 'flex', alignItems: 'center', gap: 8, flexWrap: 'wrap' } },
2112
2165
  h('span', { className: m.enabled ? 'ch-pill ok' : 'ch-pill' }, m.enabled ? t('status.connected') : t('status.muted')),
2113
2166
  h('span', { className: 'ch-pill ch-mono' }, m.transport),
2167
+ m.hasAuth ? h('span', { className: 'ch-pill ok' }, '已配置鉴权') : null,
2114
2168
  h('span', { className: 'ch-pill' }, `${m.activeTools}/${m.totalTools} tools`),
2115
2169
  ),
2116
2170
  h(
@@ -2119,6 +2173,17 @@ window.__ModuleLoader__.load({
2119
2173
  m.canRestart
2120
2174
  ? h('button', { type: 'button', className: 'ch-btn ch-btn-sm', onClick: () => runMutation({ action: 'restart_mcp', serverName: m.name }) }, t('btn.restart'))
2121
2175
  : null,
2176
+ m.isCustom
2177
+ ? h(
2178
+ 'button',
2179
+ {
2180
+ type: 'button',
2181
+ className: 'ch-btn ch-btn-sm',
2182
+ onClick: () => openConnectMcp(m),
2183
+ },
2184
+ '编辑配置',
2185
+ )
2186
+ : null,
2122
2187
  m.isCustom
2123
2188
  ? h(
2124
2189
  'button',
@@ -2419,6 +2484,83 @@ window.__ModuleLoader__.load({
2419
2484
  }
2420
2485
 
2421
2486
  if (drawer.kind === 'connect-mcp') {
2487
+ const credRe = /\$\{credential:[^}]+\}/;
2488
+ const maskPreview = (val) => {
2489
+ const str = String(val || '');
2490
+ if (!str) return '';
2491
+ if (credRe.test(str)) return str;
2492
+ const bm = /^(Bearer\s+)(.+)$/i.exec(str);
2493
+ if (bm) return `${bm[1]}${maskPreview(bm[2])}`;
2494
+ if (str.length <= 8) return '****';
2495
+ return `${str.slice(0, 4)}****${str.slice(-4)}`;
2496
+ };
2497
+ const parseKvLines = (text, sep) => {
2498
+ const map = {};
2499
+ for (const rawLine of String(text || '').split(/\r?\n/)) {
2500
+ const line = rawLine.trim();
2501
+ if (!line || line.startsWith('#')) continue;
2502
+ const idx = line.indexOf(sep);
2503
+ if (idx <= 0) continue;
2504
+ const k = line.slice(0, idx).trim();
2505
+ const v = line.slice(idx + 1).trim();
2506
+ if (k && v) map[k] = v;
2507
+ }
2508
+ return map;
2509
+ };
2510
+ const buildHttpHeaders = (forPreview) => {
2511
+ const out = {};
2512
+ if (cmAuthType === 'bearer') {
2513
+ const tok = cmBearerToken.trim();
2514
+ if (tok) {
2515
+ const rawVal = /^bearer\s+/i.test(tok) ? tok : `Bearer ${tok}`;
2516
+ out.Authorization = forPreview ? maskPreview(rawVal) : rawVal;
2517
+ } else {
2518
+ const existingAuthKey = Object.keys(cmKeepHeaders).find((k) => /^authorization$/i.test(k));
2519
+ if (existingAuthKey) {
2520
+ out[existingAuthKey] = forPreview ? cmKeepHeaders[existingAuthKey] : '__KEEP__';
2521
+ }
2522
+ }
2523
+ } else if (cmAuthType === 'api-key') {
2524
+ const hk = cmApiKeyHeader.trim() || 'X-API-Key';
2525
+ const hv = cmApiKeyValue.trim();
2526
+ if (hv) {
2527
+ out[hk] = forPreview ? maskPreview(hv) : hv;
2528
+ } else if (cmKeepHeaders[hk]) {
2529
+ out[hk] = forPreview ? cmKeepHeaders[hk] : '__KEEP__';
2530
+ }
2531
+ } else if (cmAuthType === 'custom') {
2532
+ for (const [k, masked] of Object.entries(cmKeepHeaders)) {
2533
+ out[k] = forPreview ? masked : '__KEEP__';
2534
+ }
2535
+ const parsed = parseKvLines(cmHeadersText, ':');
2536
+ for (const [k, v] of Object.entries(parsed)) {
2537
+ out[k] = forPreview ? maskPreview(v) : v;
2538
+ }
2539
+ }
2540
+ return out;
2541
+ };
2542
+ const buildStdioEnv = (forPreview) => {
2543
+ const out = {};
2544
+ for (const [k, masked] of Object.entries(cmKeepEnv)) {
2545
+ out[k] = forPreview ? masked : '__KEEP__';
2546
+ }
2547
+ const parsed = parseKvLines(cmEnvText, '=');
2548
+ for (const [k, v] of Object.entries(parsed)) {
2549
+ out[k] = forPreview ? maskPreview(v) : v;
2550
+ }
2551
+ return out;
2552
+ };
2553
+
2554
+ const previewHeaders = buildHttpHeaders(true);
2555
+ const previewEnv = buildStdioEnv(true);
2556
+ const hasCredTpl =
2557
+ credRe.test(cmUrl) ||
2558
+ credRe.test(cmCommand) ||
2559
+ credRe.test(cmArgs) ||
2560
+ credRe.test(cmCwd) ||
2561
+ Object.values(previewHeaders).some((v) => credRe.test(String(v))) ||
2562
+ Object.values(previewEnv).some((v) => credRe.test(String(v)));
2563
+
2422
2564
  const previewJson = JSON.stringify(
2423
2565
  {
2424
2566
  mcpServers: {
@@ -2428,24 +2570,31 @@ window.__ModuleLoader__.load({
2428
2570
  transport: 'stdio',
2429
2571
  command: cmCommand.trim() || 'npx',
2430
2572
  ...(cmArgs.trim() ? { args: cmArgs.trim().split(/\s+/) } : {}),
2573
+ ...(Object.keys(previewEnv).length ? { env: previewEnv } : {}),
2431
2574
  ...(cmCwd.trim() ? { cwd: cmCwd.trim() } : {}),
2575
+ ...(hasCredTpl ? { expandTemplates: true } : {}),
2432
2576
  }
2433
2577
  : {
2434
2578
  transport: 'streamable-http',
2435
2579
  url: cmUrl.trim() || 'https://example.com/mcp',
2580
+ ...(Object.keys(previewHeaders).length ? { headers: previewHeaders } : {}),
2581
+ ...(hasCredTpl ? { expandTemplates: true } : {}),
2436
2582
  },
2437
2583
  },
2438
2584
  },
2439
2585
  null,
2440
2586
  2,
2441
2587
  );
2588
+ const existingBearerKey = Object.keys(cmKeepHeaders).find((k) => /^authorization$/i.test(k));
2589
+ const existingApiKeyVal = cmKeepHeaders[cmApiKeyHeader.trim() || 'X-API-Key'];
2590
+
2442
2591
  return h(
2443
2592
  React.Fragment,
2444
2593
  null,
2445
2594
  h(
2446
2595
  'div',
2447
2596
  { className: 'ch-drawer-head' },
2448
- h('div', { className: 'ch-drawer-title' }, '连接自定义 MCP 服务'),
2597
+ h('div', { className: 'ch-drawer-title' }, cmEditing ? `编辑自定义 MCP · ${cmName}` : '连接自定义 MCP 服务'),
2449
2598
  h('button', { type: 'button', className: 'ch-btn ch-btn-sm', onClick: () => setDrawer(null) }, '✕'),
2450
2599
  ),
2451
2600
  h(
@@ -2459,6 +2608,7 @@ window.__ModuleLoader__.load({
2459
2608
  className: 'ch-input ch-field-input ch-mono',
2460
2609
  placeholder: '例如: github_mcp 或 local_sqlite',
2461
2610
  value: cmName,
2611
+ disabled: cmEditing,
2462
2612
  onChange: (e) => setCmName(e.target.value),
2463
2613
  }),
2464
2614
  ),
@@ -2491,15 +2641,153 @@ window.__ModuleLoader__.load({
2491
2641
  ),
2492
2642
  cmTransport === 'streamable-http'
2493
2643
  ? h(
2494
- 'div',
2495
- { className: 'ch-field' },
2496
- h('label', { className: 'ch-label' }, 'MCP Endpoint URL'),
2497
- h('input', {
2498
- className: 'ch-input ch-field-input ch-mono',
2499
- placeholder: 'https://mcp.example.com/mcp',
2500
- value: cmUrl,
2501
- onChange: (e) => setCmUrl(e.target.value),
2502
- }),
2644
+ React.Fragment,
2645
+ null,
2646
+ h(
2647
+ 'div',
2648
+ { className: 'ch-field' },
2649
+ h('label', { className: 'ch-label' }, 'MCP Endpoint URL'),
2650
+ h('input', {
2651
+ className: 'ch-input ch-field-input ch-mono',
2652
+ placeholder: 'https://mcp.example.com/mcp',
2653
+ value: cmUrl,
2654
+ onChange: (e) => setCmUrl(e.target.value),
2655
+ }),
2656
+ ),
2657
+ h(
2658
+ 'div',
2659
+ { className: 'ch-field' },
2660
+ h('span', { className: 'ch-label' }, '鉴权方式 (Authentication)'),
2661
+ h(
2662
+ 'div',
2663
+ { className: 'ch-seg', style: { gridTemplateColumns: 'repeat(4, 1fr)' } },
2664
+ [
2665
+ ['none', '无鉴权'],
2666
+ ['bearer', 'Bearer'],
2667
+ ['api-key', 'API Key'],
2668
+ ['custom', '自定义'],
2669
+ ].map(([k, label]) =>
2670
+ h(
2671
+ 'button',
2672
+ {
2673
+ key: k,
2674
+ type: 'button',
2675
+ className: cmAuthType === k ? 'ch-seg-btn active' : 'ch-seg-btn',
2676
+ onClick: () => setCmAuthType(k),
2677
+ },
2678
+ label,
2679
+ ),
2680
+ ),
2681
+ ),
2682
+ ),
2683
+ cmAuthType === 'bearer'
2684
+ ? h(
2685
+ 'div',
2686
+ { className: 'ch-field' },
2687
+ h(
2688
+ 'div',
2689
+ { style: { display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 8 } },
2690
+ h('label', { className: 'ch-label' }, 'Bearer Token'),
2691
+ h(
2692
+ 'button',
2693
+ { type: 'button', className: 'ch-btn ch-btn-sm', onClick: () => setCmShowSecret((v) => !v) },
2694
+ cmShowSecret ? '隐藏' : '显示',
2695
+ ),
2696
+ ),
2697
+ h('input', {
2698
+ type: cmShowSecret ? 'text' : 'password',
2699
+ className: 'ch-input ch-field-input ch-mono',
2700
+ placeholder: existingBearerKey
2701
+ ? `已保存 (${cmKeepHeaders[existingBearerKey]}),留空保持不变`
2702
+ : 'sk-... 或 ${credential:MCP_TOKEN}',
2703
+ value: cmBearerToken,
2704
+ onChange: (e) => setCmBearerToken(e.target.value),
2705
+ }),
2706
+ h('div', { className: 'ch-muted', style: { fontSize: 12 } }, '将自动以 Authorization: Bearer <token> 发送,支持 DSH 凭据模板 ${credential:REF}。'),
2707
+ )
2708
+ : null,
2709
+ cmAuthType === 'api-key'
2710
+ ? h(
2711
+ React.Fragment,
2712
+ null,
2713
+ h(
2714
+ 'div',
2715
+ { className: 'ch-field' },
2716
+ h('label', { className: 'ch-label' }, 'Header 名称'),
2717
+ h('input', {
2718
+ className: 'ch-input ch-field-input ch-mono',
2719
+ placeholder: 'X-API-Key',
2720
+ value: cmApiKeyHeader,
2721
+ onChange: (e) => setCmApiKeyHeader(e.target.value),
2722
+ }),
2723
+ ),
2724
+ h(
2725
+ 'div',
2726
+ { className: 'ch-field' },
2727
+ h(
2728
+ 'div',
2729
+ { style: { display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 8 } },
2730
+ h('label', { className: 'ch-label' }, 'API Key 值'),
2731
+ h(
2732
+ 'button',
2733
+ { type: 'button', className: 'ch-btn ch-btn-sm', onClick: () => setCmShowSecret((v) => !v) },
2734
+ cmShowSecret ? '隐藏' : '显示',
2735
+ ),
2736
+ ),
2737
+ h('input', {
2738
+ type: cmShowSecret ? 'text' : 'password',
2739
+ className: 'ch-input ch-field-input ch-mono',
2740
+ placeholder: existingApiKeyVal
2741
+ ? `已保存 (${existingApiKeyVal}),留空保持不变`
2742
+ : '输入 Key 或 ${credential:API_KEY}',
2743
+ value: cmApiKeyValue,
2744
+ onChange: (e) => setCmApiKeyValue(e.target.value),
2745
+ }),
2746
+ ),
2747
+ )
2748
+ : null,
2749
+ cmAuthType === 'custom'
2750
+ ? h(
2751
+ 'div',
2752
+ { className: 'ch-field' },
2753
+ h('label', { className: 'ch-label' }, '自定义 Headers(每行一条 Key: Value)'),
2754
+ Object.keys(cmKeepHeaders).length > 0
2755
+ ? h(
2756
+ 'div',
2757
+ { style: { display: 'flex', flexWrap: 'wrap', gap: 6, marginBottom: 6 } },
2758
+ Object.entries(cmKeepHeaders).map(([k, masked]) =>
2759
+ h(
2760
+ 'span',
2761
+ { key: k, className: 'ch-pill ch-mono', style: { display: 'inline-flex', alignItems: 'center', gap: 6 } },
2762
+ `${k}: ${masked}`,
2763
+ h(
2764
+ 'button',
2765
+ {
2766
+ type: 'button',
2767
+ style: { border: 'none', background: 'transparent', cursor: 'pointer', padding: 0, color: 'inherit' },
2768
+ title: '移除此 Header',
2769
+ onClick: () =>
2770
+ setCmKeepHeaders((prev) => {
2771
+ const next = { ...prev };
2772
+ delete next[k];
2773
+ return next;
2774
+ }),
2775
+ },
2776
+ '✕',
2777
+ ),
2778
+ ),
2779
+ ),
2780
+ )
2781
+ : null,
2782
+ h('textarea', {
2783
+ className: 'ch-input ch-field-input ch-mono',
2784
+ rows: 3,
2785
+ placeholder: 'Authorization: Bearer ${credential:MCP_TOKEN}\nX-Tenant-Id: prod',
2786
+ value: cmHeadersText,
2787
+ onChange: (e) => setCmHeadersText(e.target.value),
2788
+ }),
2789
+ )
2790
+ : null,
2503
2791
  )
2504
2792
  : h(
2505
2793
  React.Fragment,
@@ -2526,6 +2814,46 @@ window.__ModuleLoader__.load({
2526
2814
  onChange: (e) => setCmArgs(e.target.value),
2527
2815
  }),
2528
2816
  ),
2817
+ h(
2818
+ 'div',
2819
+ { className: 'ch-field' },
2820
+ h('label', { className: 'ch-label' }, '环境变量 / 鉴权凭据 (Env,每行 KEY=VALUE,可选)'),
2821
+ Object.keys(cmKeepEnv).length > 0
2822
+ ? h(
2823
+ 'div',
2824
+ { style: { display: 'flex', flexWrap: 'wrap', gap: 6, marginBottom: 6 } },
2825
+ Object.entries(cmKeepEnv).map(([k, masked]) =>
2826
+ h(
2827
+ 'span',
2828
+ { key: k, className: 'ch-pill ch-mono', style: { display: 'inline-flex', alignItems: 'center', gap: 6 } },
2829
+ `${k}=${masked}`,
2830
+ h(
2831
+ 'button',
2832
+ {
2833
+ type: 'button',
2834
+ style: { border: 'none', background: 'transparent', cursor: 'pointer', padding: 0, color: 'inherit' },
2835
+ title: '移除此环境变量',
2836
+ onClick: () =>
2837
+ setCmKeepEnv((prev) => {
2838
+ const next = { ...prev };
2839
+ delete next[k];
2840
+ return next;
2841
+ }),
2842
+ },
2843
+ '✕',
2844
+ ),
2845
+ ),
2846
+ ),
2847
+ )
2848
+ : null,
2849
+ h('textarea', {
2850
+ className: 'ch-input ch-field-input ch-mono',
2851
+ rows: 3,
2852
+ placeholder: 'GITHUB_TOKEN=${credential:GITHUB_TOKEN}\nAPI_KEY=your-key',
2853
+ value: cmEnvText,
2854
+ onChange: (e) => setCmEnvText(e.target.value),
2855
+ }),
2856
+ ),
2529
2857
  h(
2530
2858
  'div',
2531
2859
  { className: 'ch-field' },
@@ -2541,7 +2869,7 @@ window.__ModuleLoader__.load({
2541
2869
  h(
2542
2870
  'div',
2543
2871
  { className: 'ch-code-box' },
2544
- h('div', { className: 'ch-code-head' }, h('span', null, '底层配置实时预览')),
2872
+ h('div', { className: 'ch-code-head' }, h('span', null, '底层配置实时预览(密钥已脱敏)')),
2545
2873
  h('pre', { className: 'ch-pre' }, previewJson),
2546
2874
  ),
2547
2875
  ),
@@ -2556,19 +2884,23 @@ window.__ModuleLoader__.load({
2556
2884
  className: 'ch-btn ch-btn-primary',
2557
2885
  disabled: !cmName.trim(),
2558
2886
  onClick: async () => {
2887
+ const headers = cmTransport === 'streamable-http' ? buildHttpHeaders(false) : {};
2888
+ const env = cmTransport === 'stdio' ? buildStdioEnv(false) : {};
2559
2889
  const res = await runMutation({
2560
2890
  action: 'save_custom_mcp',
2561
2891
  serverName: cmName.trim(),
2562
2892
  transport: cmTransport,
2563
2893
  url: cmUrl.trim(),
2894
+ headers,
2564
2895
  command: cmCommand.trim(),
2565
2896
  argsText: cmArgs.trim(),
2897
+ env,
2566
2898
  serverCwd: cmCwd.trim(),
2567
2899
  });
2568
2900
  if (res) setDrawer(null);
2569
2901
  },
2570
2902
  },
2571
- '保存并立即挂载',
2903
+ cmEditing ? '保存并重新挂载' : '保存并立即挂载',
2572
2904
  ),
2573
2905
  ),
2574
2906
  );
package/index.js CHANGED
@@ -447,17 +447,73 @@ function parseRepoInstallArgs(input) {
447
447
  return normalized.split(/\s+/).filter(Boolean);
448
448
  }
449
449
 
450
- function formatMcpJsonSnippet(srv) {
450
+ const CREDENTIAL_TEMPLATE_RE = /\$\{credential:[^}]+\}/;
451
+
452
+ export function sanitizeKeyValueMap(raw) {
453
+ const out = {};
454
+ if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return out;
455
+ for (const [k, v] of Object.entries(raw)) {
456
+ const key = String(k || '').trim();
457
+ if (!key || key === '__proto__' || key === 'constructor' || key === 'prototype') continue;
458
+ const val = v == null ? '' : String(v);
459
+ if (!val.trim()) continue;
460
+ out[key] = val;
461
+ }
462
+ return out;
463
+ }
464
+
465
+ export function maskSecretValue(raw) {
466
+ const str = String(raw || '');
467
+ if (!str) return '';
468
+ if (CREDENTIAL_TEMPLATE_RE.test(str)) return str;
469
+ const bearerMatch = /^(Bearer\s+)(.+)$/i.exec(str);
470
+ if (bearerMatch) {
471
+ return `${bearerMatch[1]}${maskSecretValue(bearerMatch[2])}`;
472
+ }
473
+ if (str.length <= 8) return '****';
474
+ return `${str.slice(0, 4)}****${str.slice(-4)}`;
475
+ }
476
+
477
+ export function maskKeyValueMap(raw) {
478
+ const clean = sanitizeKeyValueMap(raw);
479
+ const out = {};
480
+ for (const [k, v] of Object.entries(clean)) {
481
+ out[k] = maskSecretValue(v);
482
+ }
483
+ return out;
484
+ }
485
+
486
+ export function hasCredentialTemplate(cfg) {
487
+ if (!cfg || typeof cfg !== 'object') return false;
488
+ const values = [
489
+ cfg.url,
490
+ cfg.command,
491
+ cfg.cwd,
492
+ ...(Array.isArray(cfg.args) ? cfg.args : []),
493
+ ...Object.values(cfg.headers && typeof cfg.headers === 'object' ? cfg.headers : {}),
494
+ ...Object.values(cfg.env && typeof cfg.env === 'object' ? cfg.env : {}),
495
+ ];
496
+ return values.some(v => typeof v === 'string' && CREDENTIAL_TEMPLATE_RE.test(v));
497
+ }
498
+
499
+ export function formatMcpJsonSnippet(srv) {
500
+ const maskedHeaders = maskKeyValueMap(srv.headers);
501
+ const maskedEnv = maskKeyValueMap(srv.env);
502
+ const expand = Boolean(srv.expandTemplates) || hasCredentialTemplate(srv);
451
503
  const entry = srv.transport === 'stdio'
452
504
  ? {
453
505
  transport: 'stdio',
454
506
  command: srv.command || 'npx',
455
507
  ...(srv.args?.length ? { args: srv.args } : {}),
508
+ ...(Object.keys(maskedEnv).length ? { env: maskedEnv } : {}),
456
509
  ...(srv.cwd ? { cwd: srv.cwd } : {}),
510
+ ...(expand ? { expandTemplates: true } : {}),
457
511
  }
458
512
  : {
459
513
  transport: 'streamable-http',
460
514
  url: srv.url || 'https://example.com/mcp',
515
+ ...(Object.keys(maskedHeaders).length ? { headers: maskedHeaders } : {}),
516
+ ...(expand ? { expandTemplates: true } : {}),
461
517
  };
462
518
  return JSON.stringify({ mcpServers: { [srv.name]: entry } }, null, 2);
463
519
  }
@@ -733,18 +789,21 @@ export function apply(ctx) {
733
789
  if (!cfg || cfg.enabled === false || state.disabledServers?.[srv] === true) continue;
734
790
  if (customMcpDisposers.has(srv)) continue;
735
791
  try {
792
+ const expandTemplates = Boolean(cfg.expandTemplates) || hasCredentialTemplate(cfg);
736
793
  const entry = cfg.transport === 'stdio'
737
794
  ? {
738
795
  transport: 'stdio',
739
796
  command: cfg.command || '',
740
797
  args: Array.isArray(cfg.args) ? cfg.args : [],
741
- env: cfg.env && typeof cfg.env === 'object' ? cfg.env : {},
798
+ env: sanitizeKeyValueMap(cfg.env),
742
799
  cwd: cfg.cwd || '',
800
+ ...(expandTemplates ? { expandTemplates: true } : {}),
743
801
  }
744
802
  : {
745
803
  transport: 'streamable-http',
746
804
  url: cfg.url || '',
747
- headers: cfg.headers && typeof cfg.headers === 'object' ? cfg.headers : {},
805
+ headers: sanitizeKeyValueMap(cfg.headers),
806
+ ...(expandTemplates ? { expandTemplates: true } : {}),
748
807
  };
749
808
  const off = mgr.register(srv, entry);
750
809
  customMcpDisposers.set(srv, off);
@@ -875,6 +934,19 @@ export function apply(ctx) {
875
934
  const command = customCfg?.command || managedCfg?.command || hint?.command || '';
876
935
  const args = customCfg?.args || managedCfg?.args || [];
877
936
  const srvCwd = customCfg?.cwd || managedCfg?.cwd || '';
937
+ const rawHeaders = customCfg?.headers || managedCfg?.headers || {};
938
+ const rawEnv = customCfg?.env || managedCfg?.env || {};
939
+ const maskedHeaders = maskKeyValueMap(rawHeaders);
940
+ const maskedEnv = maskKeyValueMap(rawEnv);
941
+ const hasAuth = Object.keys(maskedHeaders).length > 0 || Object.keys(maskedEnv).length > 0;
942
+ const expandTemplates = Boolean(customCfg?.expandTemplates || managedCfg?.expandTemplates) || hasCredentialTemplate({
943
+ url,
944
+ command,
945
+ args,
946
+ cwd: srvCwd,
947
+ headers: rawHeaders,
948
+ env: rawEnv,
949
+ });
878
950
  const source = customCfg ? 'custom' : managedView?.configured ? 'settings' : 'bundle';
879
951
 
880
952
  const srvRecord = {
@@ -885,6 +957,10 @@ export function apply(ctx) {
885
957
  command,
886
958
  args,
887
959
  cwd: srvCwd,
960
+ headers: maskedHeaders,
961
+ env: maskedEnv,
962
+ hasAuth,
963
+ expandTemplates,
888
964
  patchPath: hint?.patchPath || '',
889
965
  enabled: eff.enabled,
890
966
  disableReason: eff.reason,
@@ -1387,16 +1463,31 @@ export function apply(ctx) {
1387
1463
  if (!SERVER_NAME_RE.test(serverName)) {
1388
1464
  return { ok: false, error: 'MCP 服务名称仅支持字母、数字、下划线或连字符(1-32位)' };
1389
1465
  }
1466
+ const prevCfg = state.customServers[serverName] || {};
1467
+ const resolveMapWithKeep = (incoming, prevMap) => {
1468
+ const clean = sanitizeKeyValueMap(incoming);
1469
+ const merged = {};
1470
+ for (const [k, v] of Object.entries(clean)) {
1471
+ if (v === '__KEEP__' && prevMap && typeof prevMap[k] === 'string') {
1472
+ merged[k] = prevMap[k];
1473
+ } else if (v !== '__KEEP__') {
1474
+ merged[k] = v;
1475
+ }
1476
+ }
1477
+ return merged;
1478
+ };
1390
1479
  const transport = payload.transport === 'stdio' ? 'stdio' : 'streamable-http';
1391
1480
  if (transport === 'streamable-http') {
1392
1481
  const url = String(payload.url || '').trim();
1393
1482
  if (!/^https?:\/\//i.test(url)) {
1394
1483
  return { ok: false, error: 'Streamable HTTP 需要有效的 http(s):// 地址' };
1395
1484
  }
1485
+ const headers = resolveMapWithKeep(payload.headers, prevCfg.headers);
1396
1486
  state.customServers[serverName] = {
1397
1487
  transport: 'streamable-http',
1398
1488
  url,
1399
- headers: payload.headers && typeof payload.headers === 'object' ? payload.headers : {},
1489
+ headers,
1490
+ expandTemplates: Boolean(payload.expandTemplates) || hasCredentialTemplate({ url, headers }),
1400
1491
  enabled: payload.enabled !== false,
1401
1492
  };
1402
1493
  } else {
@@ -1407,12 +1498,15 @@ export function apply(ctx) {
1407
1498
  : String(payload.argsText || '').trim()
1408
1499
  ? String(payload.argsText).trim().split(/\s+/)
1409
1500
  : [];
1501
+ const env = resolveMapWithKeep(payload.env, prevCfg.env);
1502
+ const serverCwd = String(payload.serverCwd || '').trim();
1410
1503
  state.customServers[serverName] = {
1411
1504
  transport: 'stdio',
1412
1505
  command,
1413
1506
  args,
1414
- env: payload.env && typeof payload.env === 'object' ? payload.env : {},
1415
- cwd: String(payload.serverCwd || '').trim(),
1507
+ env,
1508
+ cwd: serverCwd,
1509
+ expandTemplates: Boolean(payload.expandTemplates) || hasCredentialTemplate({ command, args, env, cwd: serverCwd }),
1416
1510
  enabled: payload.enabled !== false,
1417
1511
  };
1418
1512
  }
@@ -1587,8 +1681,17 @@ export function apply(ctx) {
1587
1681
  toolName: { type: 'string', description: 'Full MCP tool name (mcp__<server>__<tool>) for toggle_mcp_tool.' },
1588
1682
  transport: { type: 'string', enum: ['streamable-http', 'stdio'], description: 'Transport for save_custom_mcp.' },
1589
1683
  url: { type: 'string', description: 'Endpoint URL for streamable-http save_custom_mcp.' },
1684
+ headers: {
1685
+ type: 'object',
1686
+ description: 'Optional HTTP request headers for streamable-http authentication (e.g. {"Authorization": "Bearer ${credential:MCP_TOKEN}"}).',
1687
+ },
1590
1688
  command: { type: 'string', description: 'Executable command for stdio save_custom_mcp.' },
1591
1689
  args: { type: 'array', items: { type: 'string' }, description: 'Command arguments for stdio save_custom_mcp.' },
1690
+ env: {
1691
+ type: 'object',
1692
+ description: 'Optional environment variables for stdio authentication (e.g. {"GITHUB_TOKEN": "${credential:GITHUB_TOKEN}"}).',
1693
+ },
1694
+ serverCwd: { type: 'string', description: 'Optional working directory for stdio save_custom_mcp.' },
1592
1695
  },
1593
1696
  },
1594
1697
  output: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fengguanghuai/dsh-capability-hub",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Codex-style unified capability hub for managing MCP servers, tools, skills, and switchable capability bundles in the top-left sidebar.",
5
5
  "keywords": [
6
6
  "dsh-plugin",