lark-mcp-server 0.0.3 → 0.0.4
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 +27 -116
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -268,138 +268,49 @@ async function sendRequest(request) {
|
|
|
268
268
|
}
|
|
269
269
|
}
|
|
270
270
|
|
|
271
|
-
//
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
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
|
-
}
|
|
271
|
+
// 尝试解析 JSON,支持多种格式
|
|
272
|
+
function tryParseJson(str) {
|
|
273
|
+
if (!str || typeof str !== 'string') {
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
314
276
|
|
|
315
|
-
|
|
277
|
+
const trimmed = str.trim();
|
|
278
|
+
if (!trimmed || !trimmed.startsWith('{')) {
|
|
279
|
+
return null;
|
|
316
280
|
}
|
|
317
281
|
|
|
318
|
-
|
|
282
|
+
try {
|
|
283
|
+
return JSON.parse(trimmed);
|
|
284
|
+
} catch (e) {
|
|
285
|
+
return null;
|
|
286
|
+
}
|
|
319
287
|
}
|
|
320
288
|
|
|
321
|
-
// 读取 stdin -
|
|
289
|
+
// 读取 stdin - 使用基于换行符的 JSON 解析(MCP 标准方式)
|
|
322
290
|
let buffer = '';
|
|
323
291
|
process.stdin.setEncoding('utf8');
|
|
324
292
|
process.stdin.on('data', (chunk) => {
|
|
325
293
|
buffer += chunk;
|
|
326
294
|
|
|
327
|
-
//
|
|
328
|
-
|
|
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];
|
|
295
|
+
// 按换行符分割,尝试解析每一行
|
|
296
|
+
let lines = buffer.split('\n');
|
|
348
297
|
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
continue;
|
|
352
|
-
}
|
|
298
|
+
// 保留最后一个可能不完整的行
|
|
299
|
+
buffer = lines.pop() || '';
|
|
353
300
|
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
}
|
|
301
|
+
for (const line of lines) {
|
|
302
|
+
const trimmed = line.trim();
|
|
303
|
+
if (!trimmed) continue;
|
|
358
304
|
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
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;
|
|
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);
|
|
305
|
+
// 尝试直接解析
|
|
306
|
+
const request = tryParseJson(trimmed);
|
|
307
|
+
if (request) {
|
|
391
308
|
log(`Received: ${request.method}, id=${request.id}`);
|
|
392
309
|
sendRequest(request);
|
|
393
|
-
}
|
|
394
|
-
|
|
310
|
+
} else if (trimmed.startsWith('{')) {
|
|
311
|
+
// 如果看起来像 JSON 但解析失败,记录错误
|
|
312
|
+
log('Parse error, JSON preview:', trimmed.slice(0, 200));
|
|
395
313
|
}
|
|
396
|
-
|
|
397
|
-
startIndex = jsonEnd;
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
// 清理已处理的部分
|
|
401
|
-
if (startIndex > 0 && startIndex <= buffer.length) {
|
|
402
|
-
buffer = buffer.slice(startIndex);
|
|
403
314
|
}
|
|
404
315
|
});
|
|
405
316
|
|