lark-mcp-server 0.0.1 → 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 +37 -15
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -268,26 +268,48 @@ async function sendRequest(request) {
|
|
|
268
268
|
}
|
|
269
269
|
}
|
|
270
270
|
|
|
271
|
-
//
|
|
271
|
+
// 尝试解析 JSON,支持多种格式
|
|
272
|
+
function tryParseJson(str) {
|
|
273
|
+
if (!str || typeof str !== 'string') {
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const trimmed = str.trim();
|
|
278
|
+
if (!trimmed || !trimmed.startsWith('{')) {
|
|
279
|
+
return null;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
try {
|
|
283
|
+
return JSON.parse(trimmed);
|
|
284
|
+
} catch (e) {
|
|
285
|
+
return null;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// 读取 stdin - 使用基于换行符的 JSON 解析(MCP 标准方式)
|
|
272
290
|
let buffer = '';
|
|
273
291
|
process.stdin.setEncoding('utf8');
|
|
274
292
|
process.stdin.on('data', (chunk) => {
|
|
275
293
|
buffer += chunk;
|
|
276
294
|
|
|
277
|
-
//
|
|
278
|
-
let
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
}
|
|
295
|
+
// 按换行符分割,尝试解析每一行
|
|
296
|
+
let lines = buffer.split('\n');
|
|
297
|
+
|
|
298
|
+
// 保留最后一个可能不完整的行
|
|
299
|
+
buffer = lines.pop() || '';
|
|
300
|
+
|
|
301
|
+
for (const line of lines) {
|
|
302
|
+
const trimmed = line.trim();
|
|
303
|
+
if (!trimmed) continue;
|
|
304
|
+
|
|
305
|
+
// 尝试直接解析
|
|
306
|
+
const request = tryParseJson(trimmed);
|
|
307
|
+
if (request) {
|
|
308
|
+
log(`Received: ${request.method}, id=${request.id}`);
|
|
309
|
+
sendRequest(request);
|
|
310
|
+
} else if (trimmed.startsWith('{')) {
|
|
311
|
+
// 如果看起来像 JSON 但解析失败,记录错误
|
|
312
|
+
log('Parse error, JSON preview:', trimmed.slice(0, 200));
|
|
291
313
|
}
|
|
292
314
|
}
|
|
293
315
|
});
|