refactor: share the MCP session and end-user retry plumbing
Closed
GL
9d ago+102+102+012345678901234567890123456789-113-113-012345678901234567890123456789
What
fetch_mcp_toolsandcall_mcp_toolshare a new_run_mcp_session(endpoint, headers, timeout, work): it opens the transport, builds and initializes theClientSessionunder theanyio.fail_afterdeadline, runswork(session), and maps anyExceptionthrough_raise_mcp_failure. Each function now holds only its own exchange (paginatedtools/list, or onetools/call).- The "retry once with the looked-up end user" logic, written three times (per-account listing in
fetch_tools, the search/execute listing,StackOneMcpTool.execute), is nowretry_for_end_user(call, accounts, look_up).
Why
Both blocks were copied line for line, so a fix to one (deadline scope, failure mapping, retry rules) had to be made in two or three places. The next change to the MCP client (reusing a session across calls) touches exactly this code, so it goes on top of one copy instead of two.
Behaviour
None intended. Points checked:
- The deadline still covers transport entry, session entry,
initializeand the operation. - Only
Exceptionis mapped, soKeyboardInterruptandSystemExitstill propagate. - The retry fires only for a 400 naming an account that was sent without a recorded end user, at most once, and only when the lookup finds the end user. The single-account sites used to rely on
_look_up_end_userto check the refusal; the helper checks it first with the same predicate. - Headers are rebuilt on the retry (
_build_mcp_headersruns again inside the lambda), as before. - The
mcpimport guard is kept in the shared helper.
How verified
uv run ruff check .,uv run ruff format --check .,uv run ty check stackone_ai examples: clean.uv run pytest(Python 3.13, MCP mock via pnpm): 871 passed, the same count asmain; the end-user retry tests intests/test_end_user_id.pyand the mocked-session tests intests/test_fetch_tools.pyrun unchanged.
Gates (head e53d02b)
| Gate | Result |
|---|---|
| Repo skills | none in repo |
| /forge:forge | ran on 3b8576f: one duplicate comment removed (9b55941); re-run on e53d02b: +40 -107, 10 comment lines, 0 copies |
| Security scan | security-reviewer agent: no finding at confidence 70 or above. Aikido: 31 open issues on main (lockfile CVEs, workflow SAST, one old test-fixture secret), none on lines this PR changes |
| Codex security | clean on 9b55941 (job 4de47dd5) and e53d02b (job b2f55e81) |
| /security-review | ran: secrets, input validation, transport and DoS items clean; the server-side items do not apply to an SDK |
| /code-review | medium on 9b55941, 5 candidates. Fixed 1: the end-user refusal check now lives only in retry_for_end_user, so the look-up no longer repeats it. Declined 4: the unguarded mcp.types import in call_mcp_tool is unchanged from main, the session was already untyped for ty (the import carries an unresolved-import ignore), the unprefixed helper name matches is_end_user_refusal and build_request_headers, and the one-element account list keeps one helper for both paths |
| CI | green on e53d02b: 10 pass, 3 skipped |
| Unresolved review threads | 0 |
Requested by guillaume@stackone.com in Slack (repo audit thread)
š¤ Generated with Claude Code
Summary by cubic
Shares the MCP session plumbing and the end-user retry logic, previously duplicated across two and three call sites respectively. No behavior change: deadline scope, failure mapping, retry eligibility, and header rebuild on retry all work as before.
Refactor
fetch_mcp_toolsandcall_mcp_toolnow delegate to a shared_run_mcp_sessionhelper; each keeps only its own exchange (tools/listpagination or onetools/call).- The retry-once-with-looked-up-end-user logic is now
retry_for_end_user, used by the per-account listing, search/execute listing, and tool execution paths; the refusal check lives only there. - The merge from
maincarried its edits into the shared helper:tools/listpagination, the dropped ty ignores, and the shared SSL context. - The
mcpimport guard stays in the shared helper, so a missing optional dependency is still surfaced as aToolsetConfigError. - The pagination test now asserts the
list_toolsparams on each call, so a regression to the old positional cursor argument fails.