Skip to content

Commit c1e53ca

Browse files
committed
fix(test): MCP 测试脚本切到 NDJSON framing,修复 0/11 全挂
server 在 fdff270 已切换到 newline-delimited JSON,但测试脚本仍按 Content-Length framing 收发,导致 initialize 永远等不到响应。 现在 11/11 全通过。
1 parent 1e559d2 commit c1e53ca

1 file changed

Lines changed: 7 additions & 14 deletions

File tree

test-mcp.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -131,28 +131,21 @@ async function runTests() {
131131
buf = Buffer.concat([buf, chunk])
132132

133133
while (true) {
134-
const hdrEnd = buf.indexOf('\r\n\r\n')
135-
if (hdrEnd === -1) break
136-
const hdr = buf.slice(0, hdrEnd).toString('ascii')
137-
const m = hdr.match(/Content-Length:\s*(\d+)/i)
138-
if (!m) { buf = buf.slice(hdrEnd + 4); continue }
139-
const len = parseInt(m[1], 10)
140-
const bodyStart = hdrEnd + 4
141-
if (buf.length < bodyStart + len) break
142-
const body = buf.slice(bodyStart, bodyStart + len).toString('utf8')
143-
buf = buf.slice(bodyStart + len)
134+
const nl = buf.indexOf(0x0a)
135+
if (nl === -1) break
136+
const line = buf.slice(0, nl).toString('utf8').trim()
137+
buf = buf.slice(nl + 1)
138+
if (!line) continue
144139
try {
145-
const obj = JSON.parse(body)
140+
const obj = JSON.parse(line)
146141
if (obj.id != null) responses.set(obj.id, obj)
147142
} catch { /* skip */ }
148143
}
149144
})
150145

151146
function sendMsg(id: number, method: string, params?: Record<string, unknown>) {
152147
const obj = { jsonrpc: '2.0', id, method, params: params || {} }
153-
const body = Buffer.from(JSON.stringify(obj), 'utf8')
154-
const header = Buffer.from(`Content-Length: ${body.length}\r\n\r\n`, 'ascii')
155-
child.stdin.write(Buffer.concat([header, body]))
148+
child.stdin.write(JSON.stringify(obj) + '\n')
156149
}
157150

158151
// Wait for server to start

0 commit comments

Comments
 (0)