mcp-log-query-server 3.3.0 → 3.4.0

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.
Files changed (2) hide show
  1. package/index.js +23 -5
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -670,25 +670,43 @@ async function handleToolCall(name, args) {
670
670
 
671
671
  console.error(`[MCP] 追踪日志: traceId=${traceId}, namespace=${targetNamespace || 'default'}, 服务数=${servicesToSearch.length}`);
672
672
 
673
+ const TRACE_TOTAL_TIMEOUT = 50000; // 总耗时上限 50s
674
+ const TRACE_PER_SERVICE = 10000; // 单服务超时 10s
675
+ const traceStart = Date.now();
673
676
  const results = [];
677
+ let searched = 0;
678
+ let skipped = 0;
679
+
674
680
  for (const serviceName of servicesToSearch) {
681
+ // 总耗时检查
682
+ if (Date.now() - traceStart > TRACE_TOTAL_TIMEOUT) {
683
+ skipped = servicesToSearch.length - searched;
684
+ console.error(`[MCP] trace_log 总耗时超过 ${TRACE_TOTAL_TIMEOUT}ms,跳过剩余 ${skipped} 个服务`);
685
+ break;
686
+ }
687
+
675
688
  const service = findService(serviceName, targetNamespace);
676
- if (!service) continue;
689
+ if (!service) { searched++; continue; }
677
690
 
678
691
  try {
679
692
  const command = `grep -i -C ${contextLines} "${traceId}"`;
680
- const result = await queryLog(service, command);
693
+ const result = await queryLog(service, command, { timeout: TRACE_PER_SERVICE });
681
694
 
682
695
  if (result && result.trim() && !result.includes('未找到')) {
683
696
  results.push({ service: serviceName, namespace: service.namespace, logs: result });
684
697
  }
685
698
  } catch (err) {
686
- console.error(`[MCP] 搜索 ${serviceName} 失败: ${err.message}`);
699
+ // 快速跳过失败/超时的服务
700
+ console.error(`[MCP] ${serviceName} 跳过: ${err.message.substring(0, 80)}`);
687
701
  }
702
+ searched++;
688
703
  }
689
704
 
705
+ const elapsed = Date.now() - traceStart;
706
+ const timeNote = skipped > 0 ? `\n**注意**: 已搜索 ${searched}/${servicesToSearch.length} 个服务(耗时 ${elapsed}ms,跳过 ${skipped} 个)` : '';
707
+
690
708
  if (results.length === 0) {
691
- return { content: [{ type: 'text', text: `## TraceId 追踪结果\n\n**traceId**: ${traceId}\n**namespace**: ${targetNamespace || '默认'}\n\n❌ 未在任何服务中找到匹配的日志` }] };
709
+ return { content: [{ type: 'text', text: `## TraceId 追踪结果\n\n**traceId**: ${traceId}\n**namespace**: ${targetNamespace || '默认'}\n\n❌ 未在已搜索的 ${searched} 个服务中找到匹配的日志${timeNote}` }] };
692
710
  }
693
711
 
694
712
  const output = results.map(r => `### ${r.service} (${r.namespace})\n\`\`\`\n${r.logs}\n\`\`\``).join('\n\n');
@@ -696,7 +714,7 @@ async function handleToolCall(name, args) {
696
714
  return {
697
715
  content: [{
698
716
  type: 'text',
699
- text: `## TraceId 追踪结果\n\n**traceId**: ${traceId}\n**namespace**: ${targetNamespace || '默认'}\n**匹配服务数**: ${results.length}\n\n${output}`
717
+ text: `## TraceId 追踪结果\n\n**traceId**: ${traceId}\n**namespace**: ${targetNamespace || '默认'}\n**匹配服务数**: ${results.length}${timeNote}\n\n${output}`
700
718
  }]
701
719
  };
702
720
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mcp-log-query-server",
3
- "version": "3.3.0",
3
+ "version": "3.4.0",
4
4
  "description": "MCP Server for querying server logs via SSH jump host and Grafana Loki API",
5
5
  "main": "index.js",
6
6
  "type": "module",