From 32c96daf1fe99e9824ef54fb13f1d79980eb4318 Mon Sep 17 00:00:00 2001 From: karl Date: Sat, 19 Apr 2025 10:45:22 +0800 Subject: [PATCH] feat: mcp configuration extraction logic optimization (#4918) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 寇佳龙 --- src/renderer/src/utils/index.ts | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/renderer/src/utils/index.ts b/src/renderer/src/utils/index.ts index 79805aa2..ec4f38b2 100644 --- a/src/renderer/src/utils/index.ts +++ b/src/renderer/src/utils/index.ts @@ -508,23 +508,20 @@ export function hasObjectKey(obj: any, key: string) { */ export function getMcpConfigSampleFromReadme(readme: string) { if (readme) { - // 使用正则表达式匹配 mcpServers 对象内容 - const regex = /"mcpServers"\s*:\s*({(?:[^{}]*|{(?:[^{}]*|{[^{}]*})*})*})/ - const match = readme.match(regex) - console.log('match', match) - if (match && match[1]) { - // 添加缺失的闭合括号检测 - try { + try { + const regex = /"mcpServers"\s*:\s*({(?:[^{}]*|{(?:[^{}]*|{[^{}]*})*})*})/g + for (const match of readme.matchAll(regex)) { let orgSample = JSON.parse(match[1]) orgSample = orgSample[Object.keys(orgSample)[0] ?? ''] if (orgSample.command === 'npx') { return orgSample } - } catch (e) { - console.log(e) } + } catch (e) { + console.log('getMcpConfigSampleFromReadme', e) } } + return null } export { classNames }