feat: mcp configuration extraction logic optimization (#4918)

Co-authored-by: 寇佳龙 <koujialong@bonc.com.cn>
This commit is contained in:
karl 2025-04-19 10:45:22 +08:00 committed by GitHub
parent 30309c29ff
commit 32c96daf1f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -508,23 +508,20 @@ export function hasObjectKey(obj: any, key: string) {
*/ */
export function getMcpConfigSampleFromReadme(readme: string) { export function getMcpConfigSampleFromReadme(readme: string) {
if (readme) { 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]) let orgSample = JSON.parse(match[1])
orgSample = orgSample[Object.keys(orgSample)[0] ?? ''] orgSample = orgSample[Object.keys(orgSample)[0] ?? '']
if (orgSample.command === 'npx') { if (orgSample.command === 'npx') {
return orgSample return orgSample
} }
}
} catch (e) { } catch (e) {
console.log(e) console.log('getMcpConfigSampleFromReadme', e)
}
} }
} }
return null
} }
export { classNames } export { classNames }