Skip to content

Commit a28b9d0

Browse files
authored
Merge pull request #3121 from hodgesds/scxtop-mcp-less-ctx
scxtop: Add query limits to get_process_timeline and get_cpu_timeline MCP server methods
2 parents 08068a7 + 8913db8 commit a28b9d0

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

tools/scxtop/src/mcp/tools.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,10 @@ impl McpTools {
464464
"end_time_ns": {
465465
"type": "integer",
466466
"description": "End of time range (optional, defaults to trace end)"
467+
},
468+
"limit": {
469+
"type": "integer",
470+
"description": "Maximum number of events to return (optional, defaults to memory-aware limit)"
467471
}
468472
},
469473
"required": ["trace_id", "pid"]
@@ -490,6 +494,10 @@ impl McpTools {
490494
"end_time_ns": {
491495
"type": "integer",
492496
"description": "End of time range (optional, defaults to trace end)"
497+
},
498+
"limit": {
499+
"type": "integer",
500+
"description": "Maximum number of events to return (optional, defaults to memory-aware limit)"
493501
}
494502
},
495503
"required": ["trace_id", "cpu"]
@@ -2590,20 +2598,30 @@ impl McpTools {
25902598

25912599
let timeline = trace.get_timeline_for_process(pid, start_ns, end_ns);
25922600

2601+
// Limit output for readability using memory-aware limit or user-provided limit
2602+
let default_limit = self.mem_limits.timeline_limit();
2603+
let timeline_limit = args
2604+
.get("limit")
2605+
.and_then(|v| v.as_u64())
2606+
.map(|v| v as usize)
2607+
.unwrap_or(default_limit);
2608+
let limited_events: Vec<_> = timeline.events.iter().take(timeline_limit).collect();
2609+
25932610
Ok(json!({
25942611
"content": [{
25952612
"type": "text",
25962613
"text": format!(
25972614
"Process Timeline for PID {} ({})\n\
25982615
Time range: {} - {} ns\n\
25992616
Total events: {}\n\n\
2600-
Timeline:\n{}",
2617+
Timeline (first {} events):\n{}",
26012618
timeline.pid,
26022619
timeline.comm,
26032620
start_ns,
26042621
end_ns,
26052622
timeline.events.len(),
2606-
serde_json::to_string_pretty(&timeline.events)
2623+
timeline_limit.min(timeline.events.len()),
2624+
serde_json::to_string_pretty(&limited_events)
26072625
.unwrap_or_else(|_| "Failed to serialize timeline".to_string())
26082626
)
26092627
}]
@@ -2645,8 +2663,13 @@ impl McpTools {
26452663

26462664
let timeline = trace.get_cpu_timeline(cpu, start_ns, end_ns);
26472665

2648-
// Limit output for readability using memory-aware limit
2649-
let timeline_limit = self.mem_limits.timeline_limit();
2666+
// Limit output for readability using memory-aware limit or user-provided limit
2667+
let default_limit = self.mem_limits.timeline_limit();
2668+
let timeline_limit = args
2669+
.get("limit")
2670+
.and_then(|v| v.as_u64())
2671+
.map(|v| v as usize)
2672+
.unwrap_or(default_limit);
26502673
let limited_events: Vec<_> = timeline.events.iter().take(timeline_limit).collect();
26512674

26522675
Ok(json!({

0 commit comments

Comments
 (0)