Skip to content

Commit 85349a8

Browse files
authored
Merge pull request DeusData#826 from DeusData/fix/smoke-invariants-triage
fix(mcp): exit promptly on stdin EOF over a FIFO; smoke tools-list follows pagination
2 parents 541ff61 + f2626f0 commit 85349a8

2 files changed

Lines changed: 49 additions & 21 deletions

File tree

‎scripts/smoke-invariants.sh‎

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -454,28 +454,48 @@ inv_tools_list() {
454454
fail "tools-list" "server not alive"
455455
return
456456
fi
457-
local req='{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}'
458-
if ! mcp_send_recv "$req" 15; then
459-
fail "tools-list" "no response within 15s (hang)"
460-
return
461-
fi
462-
if ! printf '%s' "$MCP_RESP" | is_json; then
463-
fail "tools-list" "response not valid JSON"
464-
return
465-
fi
466-
# Extract tool names from result.tools[].name.
467-
local got_names got_count
468-
got_names="$(printf '%s' "$MCP_RESP" | "$PY" -c '
457+
# tools/list is cursor-paginated (MCP spec): the server returns a page of
458+
# tools plus result.nextCursor. Follow the cursor across pages and accumulate
459+
# the advertised tools — exactly what a compliant client does — then assert the
460+
# union covers all expected tools.
461+
local got_names="" cursor="" page=0
462+
while :; do
463+
page=$((page + 1))
464+
if [ "$page" -gt 20 ]; then
465+
fail "tools-list" "pagination did not terminate after 20 pages"
466+
return
467+
fi
468+
local params='{}'
469+
[ -n "$cursor" ] && params="{\"cursor\":\"$cursor\"}"
470+
local req="{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"tools/list\",\"params\":$params}"
471+
if ! mcp_send_recv "$req" 15; then
472+
fail "tools-list" "no response within 15s (page $page, hang)"
473+
return
474+
fi
475+
if ! printf '%s' "$MCP_RESP" | is_json; then
476+
fail "tools-list" "response not valid JSON (page $page)"
477+
return
478+
fi
479+
# Line 1 = space-joined names on this page; line 2 = nextCursor ("" if last).
480+
local parsed
481+
parsed="$(printf '%s' "$MCP_RESP" | "$PY" -c '
469482
import sys,json
470483
try:
471484
d=json.load(sys.stdin)
472485
except Exception:
473-
sys.exit(0)
474-
tools=(d.get("result") or {}).get("tools") or []
475-
print(" ".join(sorted(t.get("name","") for t in tools)))' 2>/dev/null)"
476-
got_count="$(printf '%s' "$got_names" | tr ' ' '\n' | grep -c . )"
486+
print(""); print(""); sys.exit(0)
487+
r=d.get("result") or {}
488+
print(" ".join(t.get("name","") for t in (r.get("tools") or [])))
489+
print(r.get("nextCursor") or "")' 2>/dev/null)"
490+
got_names="$got_names $(printf '%s' "$parsed" | sed -n '1p')"
491+
local next; next="$(printf '%s' "$parsed" | sed -n '2p')"
492+
[ -z "$next" ] && break
493+
cursor="$next"
494+
done
495+
local got_count
496+
got_count="$(printf '%s' "$got_names" | tr ' ' '\n' | grep -c .)"
477497
if [ "${got_count:-0}" -ne "$EXPECTED_TOOL_COUNT" ]; then
478-
fail "tools-list" "got $got_count tools, expected $EXPECTED_TOOL_COUNT; names=[$got_names]"
498+
fail "tools-list" "got $got_count tools across $page page(s), expected $EXPECTED_TOOL_COUNT; names=[$got_names]"
479499
return
480500
fi
481501
local missing=""

‎src/mcp/mcp.c‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5684,8 +5684,9 @@ static int poll_for_input_unix(cbm_mcp_server_t *srv, int fd, FILE *in) {
56845684
/* Phase 2: peek FILE* buffer */
56855685
int saved_flags = fcntl(fd, F_GETFL);
56865686
if (saved_flags < 0) {
5687-
/* fcntl failed — fall through to blocking poll */
5688-
pr = poll(&pfd, SKIP_ONE, STORE_IDLE_TIMEOUT_S * MCP_TIMEOUT_MS);
5687+
/* fcntl failed — fall through to a short blocking poll (see the Phase-3
5688+
* note below on why the interval is bounded, not the full idle timeout) */
5689+
pr = poll(&pfd, SKIP_ONE, MCP_TIMEOUT_MS);
56895690
if (pr < 0) {
56905691
return CBM_NOT_FOUND;
56915692
}
@@ -5705,8 +5706,15 @@ static int poll_for_input_unix(cbm_mcp_server_t *srv, int fd, FILE *in) {
57055706
return CBM_NOT_FOUND; /* true EOF */
57065707
}
57075708
clearerr(in);
5708-
/* Phase 3: blocking poll */
5709-
pr = poll(&pfd, SKIP_ONE, STORE_IDLE_TIMEOUT_S * MCP_TIMEOUT_MS);
5709+
/* Phase 3: blocking poll, bounded to a SHORT interval (not the full idle
5710+
* timeout). macOS poll()/select() do NOT report POLLIN/POLLHUP when a
5711+
* FIFO's last writer closes — only read() returns 0 there (verified). A
5712+
* 60s poll would therefore leave the server blocked up to a full idle
5713+
* timeout after stdin EOF (a client that closes the pipe would appear to
5714+
* hang). Waking every MCP_TIMEOUT_MS lets the Phase-2 read() above detect
5715+
* the EOF within ~1s. Idle-store eviction (threshold STORE_IDLE_TIMEOUT_S)
5716+
* is idempotent, so checking it on each short tick is harmless. */
5717+
pr = poll(&pfd, SKIP_ONE, MCP_TIMEOUT_MS);
57105718
if (pr < 0) {
57115719
return CBM_NOT_FOUND;
57125720
}

0 commit comments

Comments
 (0)