Skip to content

Commit 44a7818

Browse files
committed
fix: review issue
1 parent 9a32e0c commit 44a7818

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

packages/canvas/DesignCanvas/src/mcp/resources/utils.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,44 @@
33
// - 标题大小写不敏感(转换为小写后匹配)
44
// - 仅在两个相邻的二级标题之间切片
55
export function pickSectionByHeading(markdown: string, title: string): string {
6-
if (typeof markdown !== 'string') return ''
7-
if (!title) return markdown
8-
const lines = markdown.split('\n')
9-
const startIdx = lines.findIndex((l) => l.trim().toLowerCase() === `## ${title}`.toLowerCase())
10-
if (startIdx === -1) return ''
6+
if (typeof markdown !== 'string' || !markdown) {
7+
return ''
8+
}
9+
10+
if (typeof title !== 'string' || !title.trim()) {
11+
return markdown
12+
}
13+
14+
const normalize = (s: string) =>
15+
s
16+
.trim()
17+
.replace(/\u2019/g, "'")
18+
.replace(/\s+/g, ' ')
19+
.toLowerCase()
20+
const target = normalize(title)
21+
const lines = markdown.split(/\r?\n/)
22+
const isH2 = (line: string) => line.trim().startsWith('## ')
23+
const headingText = (line: string) => line.trim().replace(/^##\s+/, '')
24+
let startIdx = -1
25+
for (let i = 0; i < lines.length; i += 1) {
26+
if (isH2(lines[i]) && normalize(headingText(lines[i])) === target) {
27+
startIdx = i
28+
break
29+
}
30+
}
31+
32+
if (startIdx === -1) {
33+
return ''
34+
}
35+
1136
let endIdx = lines.length
37+
1238
for (let i = startIdx + 1; i < lines.length; i += 1) {
13-
const line = lines[i]
14-
if (line.startsWith('## ')) {
39+
if (isH2(lines[i])) {
1540
endIdx = i
1641
break
1742
}
1843
}
44+
1945
return lines.slice(startIdx, endIdx).join('\n')
2046
}

packages/common/composable/mcp/resources.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { toRaw } from 'vue'
22
import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
33
import type { ResourceItem, ResourceTemplateItem, IState } from './type'
4-
import type { RegisteredResource, ReadResourceCallback } from '@modelcontextprotocol/sdk/server/mcp.d.ts'
4+
import type { RegisteredResource, ReadResourceCallback } from '@modelcontextprotocol/sdk/server/mcp.js'
55
import { ResourceTemplate } from '@modelcontextprotocol/sdk/server/mcp.js'
66

77
const logger = console
@@ -130,6 +130,12 @@ export const removeResource = (state: IState, uri: string) => {
130130
logger.error('error when remove resource', uri, e)
131131
} finally {
132132
map.delete(uri)
133+
134+
try {
135+
state.server?.sendResourceListChanged()
136+
} catch (e) {
137+
logger.error('error when sendResourceListChanged after removeResource', e)
138+
}
133139
}
134140
}
135141

@@ -151,15 +157,21 @@ export const updateResource = (state: IState, uri: string, updates?: UpdateResou
151157

152158
if (Object.prototype.hasOwnProperty.call(updates, 'uri')) {
153159
const newUri = updates.uri as string | null | undefined
154-
try {
155-
map.delete(uri)
156-
if (typeof newUri === 'string' && newUri) {
160+
if (typeof newUri === 'string' && newUri && newUri !== uri) {
161+
try {
162+
map.delete(uri)
157163
map.set(newUri, inst)
164+
} catch (e) {
165+
logger.error('error when migrate resourceInstanceMap key', uri, '->', newUri, e)
158166
}
159-
} catch (e) {
160-
logger.error('error when migrate resourceInstanceMap key', uri, '->', newUri, e)
161167
}
162168
}
169+
170+
try {
171+
state.server?.sendResourceListChanged()
172+
} catch (e) {
173+
logger.error('error when sendResourceListChanged after updateResource', e)
174+
}
163175
}
164176

165177
// 注册资源与资源模板

0 commit comments

Comments
 (0)