Skip to content

refactor: Dedup the tool-loading mode-resolution gate#1078

Draft
jirispilka wants to merge 1 commit into
masterfrom
claude/apify-mcp-quick-wins-fysrwa-dedup-mode-gate
Draft

refactor: Dedup the tool-loading mode-resolution gate#1078
jirispilka wants to merge 1 commit into
masterfrom
claude/apify-mcp-quick-wins-fysrwa-dedup-mode-gate

Conversation

@jirispilka

Copy link
Copy Markdown
Collaborator

Part of #1066 (checkbox: dedup the mode-resolution gate).

What we're solving

loadToolsByName, loadToolsFromUrl, and loadToolsFromInput each repeated the same buffer-or-compose block after getActors() — buffer the fetched actor tools when the server mode isn't resolved yet, otherwise compose the mode-specific set and upsert. The three differed only in the notify value passed to upsertTools (and loadToolsFromUrl's debug logs), and the reason those notify values differ was documented only on updateToolsAfterServerModeResolved.

How

Extract the shared block into a private registerFetchedActorTools(input, actorTools, notify) helper. Each loader keeps its own pre-work and getActors call and passes its notify value:

  • loadToolsByName = actorTools.length > 0 — its pre-mode upsert only ran under that guard, so the old literal true is equivalent.
  • loadToolsFromUrl / loadToolsFromInput = false.

The helper carries a doc comment explaining the intentional notify divergence and points to updateToolsAfterServerModeResolved for the full rationale (that comment is retained). loadToolsFromUrl's two per-branch debug logs collapse into one line emitted before the helper call.

Adds a regression test asserting the per-loader notify behavior (loadToolsByName notifies eagerly pre-init; the other two defer to the post-initialize reconcile) — previously untested.

Alternatives considered

  • Folding getActors into the helper — rejected; loadToolsByName pre-filters missing names before fetching.
  • A debugMessages param to preserve both fromUrl log lines verbatim — rejected; over-engineered for two debug breadcrumbs, a single line is clearer.

⚠️ Notification-timing note

The notify flag gates notifyToolsChangedHandler, which the hosted server (apify-mcp-server-internal) uses for Redis-backed state recovery. Behavior is byte-identical — same notify values and timing — verified against the pre-change code. The only observable change is the debug-log consolidation (two log.debug lines → one; debug-only).

Verification

  • Oracle green: type-check clean, lint 0/0, check:agents passed, test:unit 963 passed / 2 skipped (+4 new notify regression cases), format no-op.
  • pendingToolsAfterModeResolved.push now appears once (in the helper).
  • Scope: src/mcp/server.ts + tests/unit/mcp.server.capability_gating.test.ts.

🤖 Generated with Claude Code

https://claude.ai/code/session_016pXZvhEoE6Lz3DpBSL3n1Y


Generated by Claude Code

What we're solving: loadToolsByName, loadToolsFromUrl, and loadToolsFromInput
each repeated the same buffer-or-compose block after getActors() — buffer the
fetched actor tools when the server mode isn't resolved yet, otherwise compose
the mode-specific set and upsert. The three differed only in the `notify`
value passed to upsertTools (and loadToolsFromUrl's debug logs), and the reason
those notify values differ was documented only on updateToolsAfterServerModeResolved.

How: extract the shared block into a private registerFetchedActorTools(input,
actorTools, notify) helper. Each loader keeps its own pre-work and getActors
call and passes its notify value: loadToolsByName = actorTools.length > 0 (its
pre-mode upsert only ran under that guard, so the old literal `true` is
equivalent); loadToolsFromUrl and loadToolsFromInput = false. The helper carries
a doc comment explaining the intentional notify divergence and points to
updateToolsAfterServerModeResolved for the full rationale. loadToolsFromUrl's two
per-branch debug logs collapse into one line emitted before the helper call.

Adds a regression test asserting the per-loader notify behavior (loadToolsByName
notifies eagerly pre-init; the other two defer to the post-initialize reconcile),
which was previously untested.

Alternatives considered: folding getActors into the helper (rejected —
loadToolsByName pre-filters missing names before fetching); a debugMessages param
to preserve both fromUrl log lines verbatim (rejected — over-engineered for two
debug breadcrumbs; a single log line is clearer).

Note: the notify flag gates notifyToolsChangedHandler, which the hosted server
uses for Redis-backed state recovery. Behavior is byte-identical (same notify
values and timing); the only change is the debug-log consolidation.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016pXZvhEoE6Lz3DpBSL3n1Y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants