lark-mcp-server 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +124 -13
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -268,27 +268,138 @@ async function sendRequest(request) {
|
|
|
268
268
|
}
|
|
269
269
|
}
|
|
270
270
|
|
|
271
|
-
//
|
|
271
|
+
// 修复 JSON 字符串中的控制字符
|
|
272
|
+
// JSON 规范不允许字符串内有实际的换行符,必须转义为 \n
|
|
273
|
+
function fixJsonControlChars(str) {
|
|
274
|
+
let result = '';
|
|
275
|
+
let inString = false;
|
|
276
|
+
let escapeNext = false;
|
|
277
|
+
|
|
278
|
+
for (let i = 0; i < str.length; i++) {
|
|
279
|
+
const char = str[i];
|
|
280
|
+
const code = char.charCodeAt(0);
|
|
281
|
+
|
|
282
|
+
if (escapeNext) {
|
|
283
|
+
result += char;
|
|
284
|
+
escapeNext = false;
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if (char === '\\' && inString) {
|
|
289
|
+
result += char;
|
|
290
|
+
escapeNext = true;
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if (char === '"') {
|
|
295
|
+
inString = !inString;
|
|
296
|
+
result += char;
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// 如果在字符串内且遇到控制字符,转义它们
|
|
301
|
+
if (inString && code < 32) {
|
|
302
|
+
if (code === 10) {
|
|
303
|
+
result += '\\n';
|
|
304
|
+
} else if (code === 13) {
|
|
305
|
+
result += '\\r';
|
|
306
|
+
} else if (code === 9) {
|
|
307
|
+
result += '\\t';
|
|
308
|
+
} else {
|
|
309
|
+
// 其他控制字符用 Unicode 转义
|
|
310
|
+
result += '\\u' + code.toString(16).padStart(4, '0');
|
|
311
|
+
}
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
result += char;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
return result;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// 读取 stdin - 使用更智能的 JSON 解析
|
|
272
322
|
let buffer = '';
|
|
273
323
|
process.stdin.setEncoding('utf8');
|
|
274
324
|
process.stdin.on('data', (chunk) => {
|
|
275
325
|
buffer += chunk;
|
|
276
326
|
|
|
277
327
|
// 尝试解析完整的 JSON 消息
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
328
|
+
// 使用大括号配对来检测完整的 JSON 对象
|
|
329
|
+
let startIndex = 0;
|
|
330
|
+
|
|
331
|
+
while (startIndex < buffer.length) {
|
|
332
|
+
// 找到第一个 {
|
|
333
|
+
const jsonStart = buffer.indexOf('{', startIndex);
|
|
334
|
+
if (jsonStart === -1) {
|
|
335
|
+
// 没有找到 JSON 开始,清理 buffer 中 startIndex 之前的内容
|
|
336
|
+
buffer = buffer.slice(startIndex);
|
|
337
|
+
break;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// 尝试从这个位置解析 JSON
|
|
341
|
+
let braceCount = 0;
|
|
342
|
+
let inString = false;
|
|
343
|
+
let escapeNext = false;
|
|
344
|
+
let jsonEnd = -1;
|
|
345
|
+
|
|
346
|
+
for (let i = jsonStart; i < buffer.length; i++) {
|
|
347
|
+
const char = buffer[i];
|
|
348
|
+
|
|
349
|
+
if (escapeNext) {
|
|
350
|
+
escapeNext = false;
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (char === '\\' && inString) {
|
|
355
|
+
escapeNext = true;
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
if (char === '"' && !escapeNext) {
|
|
360
|
+
inString = !inString;
|
|
361
|
+
continue;
|
|
290
362
|
}
|
|
363
|
+
|
|
364
|
+
if (!inString) {
|
|
365
|
+
if (char === '{') {
|
|
366
|
+
braceCount++;
|
|
367
|
+
} else if (char === '}') {
|
|
368
|
+
braceCount--;
|
|
369
|
+
if (braceCount === 0) {
|
|
370
|
+
jsonEnd = i + 1;
|
|
371
|
+
break;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
if (jsonEnd === -1) {
|
|
378
|
+
// JSON 不完整,等待更多数据
|
|
379
|
+
buffer = buffer.slice(startIndex);
|
|
380
|
+
break;
|
|
291
381
|
}
|
|
382
|
+
|
|
383
|
+
// 提取完整的 JSON
|
|
384
|
+
let jsonStr = buffer.slice(jsonStart, jsonEnd);
|
|
385
|
+
|
|
386
|
+
// 修复 JSON 字符串内的控制字符
|
|
387
|
+
jsonStr = fixJsonControlChars(jsonStr);
|
|
388
|
+
|
|
389
|
+
try {
|
|
390
|
+
const request = JSON.parse(jsonStr);
|
|
391
|
+
log(`Received: ${request.method}, id=${request.id}`);
|
|
392
|
+
sendRequest(request);
|
|
393
|
+
} catch (e) {
|
|
394
|
+
log('Parse error:', e.message, 'JSON preview:', jsonStr.slice(0, 100));
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
startIndex = jsonEnd;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// 清理已处理的部分
|
|
401
|
+
if (startIndex > 0 && startIndex <= buffer.length) {
|
|
402
|
+
buffer = buffer.slice(startIndex);
|
|
292
403
|
}
|
|
293
404
|
});
|
|
294
405
|
|