MCPcopy
hub / github.com/ZToolsCenter/ZTools / readBody

Method readBody

src/main/core/mcpServer.ts:258–289  ·  view source on GitHub ↗

* 读取并解析请求体,仅接受 JSON 对象。

(req: IncomingMessage)

Source from the content-addressed store, hash-verified

256 * 读取并解析请求体,仅接受 JSON 对象。
257 */
258 private readBody(req: IncomingMessage): Promise<Record<string, unknown>> {
259 return new Promise((resolve, reject) => {
260 const chunks: Buffer[] = []
261 let size = 0
262 const maxBodySize = 1024 * 1024
263
264 req.on('data', (chunk: Buffer) => {
265 size += chunk.length
266 if (size > maxBodySize) {
267 reject(new Error('请求体过大'))
268 req.destroy()
269 return
270 }
271 chunks.push(chunk)
272 })
273
274 req.on('end', () => {
275 const raw = Buffer.concat(chunks).toString('utf-8')
276 if (!raw) {
277 resolve({})
278 return
279 }
280 try {
281 resolve(JSON.parse(raw))
282 } catch {
283 reject(new Error('无效的 JSON 格式'))
284 }
285 })
286
287 req.on('error', reject)
288 })
289 }
290
291 /**
292 * 校验 JSON-RPC 基础结构,并将请求转发到 MCP 路由层。

Callers 1

handleRequestMethod · 0.95

Calls 1

onMethod · 0.80

Tested by

no test coverage detected