Repository navigation
fix(cron): reuse one MCP server per name across a script cron run - #16661
iamwhatever merged 1 commit into
Conversation
Design Review (Opus 5.5, fork) — 🟡 CONCERNSDesign-level review of Design-Verdict: CONCERNS A tool-level error closes the kept server, so a cron whose calls return Watch
[DESIGN-REVIEWED] a035cbd Description read[DESCRIPTION-READ] 75303d26b345f282eebd97c96663f9f10924538fc09a54ea716d9e7d76c7f9f8 That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
First Principles Review (Opus 5.5, fork) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Before merge, check that a run killed at the job timeout still stops its kept servers. What this change shipsInventory (7 items) — 7 justifiedIntent: let a script cron call a sign-in MCP server like Slack several times per run without most calls refused with HTTP 429 (#16659). FIX.
[FIRST-PRINCIPLES-REVIEWED] a035cbd Description read[DESCRIPTION-READ] 9bc551facf7391df40d52238ed5c48a18845251ac35836e0baa0ef56a2ca5168 That sha256 names the PR description this verdict read. Recompute it with |
Opus 5.5 Review (fork) — ✅ no blocking findingsReviewed |
GPT 6.1 Review (fork) — ✅ no blocking findingsReviewed Review detailsNo findings. |
bea332e to
0b18b91
Compare
|
mechanism: new KeptMcpServers class in cron_script.py, shared by ScriptContext and the cron preview's context
|
|
self-added: yes
|
0b18b91 to
da70999
Compare
|
Rebased onto af2619f. #16280 conflicted with this PR in |
da70999 to
d79106b
Compare
|
Rebased onto 3f960a1 with the diff unchanged, to re-run CI. On da70999, |
ctx.call_tool started a new MCP server for every call and stopped it afterwards. A server that signs in to a service when it starts signed in once per call, and a Slack server doing that from a once-a-minute cron had most of its calls refused with HTTP 429. KeptMcpServers keeps a server whose call succeeds for the run's next call to the same server name. A failed call stops its server, a kept server whose process exited is replaced, and a call made while another call to the same server is running gets a server of its own. ScriptContext and the kirocrew cron preview context both use it, and both stop their kept servers once the script function returns.
d79106b to
a035cbd
Compare
|
Rebased onto 9757a35 with the diff unchanged, to re-run CI. The only red on d79106b was |
|
self-added: yes
|
iamwhatever
left a comment
There was a problem hiding this comment.
Reviewed: a script cron run now keeps one MCP server per name for the run (KeptMcpServers) instead of spawning one per call, so a server that signs in on start signs in once per run (fixes the Slack 429s in #16659). A failed call stops its server, an exited one is replaced, a concurrent call to the same name gets its own server, and the launcher calls ctx.close() in a finally however the script ends, which stops every kept server; preview uses the same lifecycle. No change to which processes are signalled beyond the ones the run itself started. Advisory: if the sandboxed script process is killed by its timeout, the finally does not run and kept servers live until their stdin closes; worth one sentence in the spec.
|
Follow-up in #16905: a call the server answers with a tool error now keeps the server, which is the Design point deferred above, and the cron spec gains the timeout sentence. One correction to the timeout note: a timeout kill does stop kept servers. Each runs in the script child's process group, which |
Problem / Motivation
Goal: A script cron's calls to the same MCP server within one run share one server process, so a server that signs in when it starts signs in once per run instead of once per call.
ctx.call_toolstarted a new MCP server for every call and stopped it after the answer. A Slack MCP server signs in to Slack each time it starts, and Slack refuses sign-ins that come close together with HTTP 429. A script cron that ran every minute and made several Slack calls per run had 71 of its 97 Slack calls refused in 25 minutes. The first call of each run nearly always worked; the calls right after it failed.Why it matters
Any script cron that calls a sign-in server more than once per run hits this, and the refusals grow with the number of calls. A refused call is lost work: in the case above, the cron missed most of the Slack messages it existed to watch during a live incident. Every call also paid a full server start and
initializehandshake.Not a goal
McpToolClient's JSON-RPC handling.What changed (motivation → approach → change)
A script run now keeps the MCP server it started and reuses it for the run's later calls to that server. A server that signs in when it starts then signs in once per run, not once per call, which is what got most of a Slack cron's calls refused.
kirocrew cron previewruns a script the same way.The cause was
ScriptContext.call_toolinsrc/kiro_crew/cron_script.pybuilding anMcpToolClientper call and closing it infinally; the cron preview's context insrc/kiro_crew/cli_commands.pydid the same. Both now hand their calls toKeptMcpServers, a small class incron_script.py. It keeps at most one client per server name, and only after a call to it succeeds, so a failed call never hands a broken server to the next one. A call made while another call to the same server is still running gets a server of its own, so a call a script has given up on never blocks a later one.McpToolClient.is_running()lets a kept client whose process exited be replaced. The launcher callsctx.close()in afinally, whatever the script raised, and the preview closes its context the same way;close()never raises. The cost is that a kept server stays up until the run ends, not for one call. The cron spec,docs/system-specs/modules/learn-cron-dashboard.md, has a new bullet on this lifetime.Backwards compatibility
Compatible: a script gets the same results from the same calls. Only when servers start and stop changes.
Removes nothing: no key, function or record is removed or renamed.
cron_script.pygainsKeptMcpServers,ScriptContextgainsclose()andMcpToolClientgainsis_running().Tests
TestScriptContextKeepsServersintest/test_cron_script.py: three calls share one server; each server name gets its own; a failed call stops its server and the next starts another; an exited server is replaced; a call made during another gets its own server and only one is kept; nothing is kept afterclose();close()stops every server even when one fails to stop.test_the_launcher_closes_the_context_however_the_script_ends: a real script child, started in its own temporary directory, returns, raisesSkipor raises an error, and the launcher closes the context each time.test_call_tool_keeps_one_server_per_name_until_the_script_returnsintest/test_cron_preview_cmd.py: two previewed calls to one server start one client, closed once after the script returned.test/test_cron_script.py,test/test_cron_script_more_coverage.py) now check that the client stays open untilclose().Each mechanism reverted on its own turns a named test red: no reuse, reusing an exited server, replacing a kept server, keeping after
close(), a failing stop abortingclose(), the launcher not closing, keeping a server after a failed call, and the preview starting a server per call.Manual verification
Ran the change against a real stdio MCP server process that reports its pid: three
ctx.call_toolcalls were answered by one pid,ctx.close()stopped that process, and a call afterclose()started a new process and stopped it again.Related Issues
Closes #16659
Pattern harvest
Not generalizable: the two script contexts that started a server per call, the cron run's and the preview's, now share
KeptMcpServers, which is the only remaining place that builds anMcpToolClient. Chat sessions keep their servers for the session, and gatewayd pools them.Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)