Skip to content

Scope network interception state to MCP sessions #752

@shaun0927

Description

@shaun0927

Why OpenSafari should reflect this

network_intercept currently keeps intercept rules in a module-global activeRules array and applies/clears them without using the canonical src/network-interceptor.ts implementation. In long-lived MCP sessions this can leak request blocking/mocking rules across independent sessions, and the clear path restores fetch only while leaving XHR hooks outside the canonical restore contract.

This is directionally aligned with OpenSafari because network interception is a user-facing QA feature: stale or cross-session rules can make Safari/webview tests fail in ways that look like app or network bugs rather than automation state leakage.

Risk / user impact

  • Severity: high reliability risk, medium user impact.
  • A stale rule can block login, API calls, or asset loads in later tests.
  • Incomplete restore can leave a page in a modified network state after clear or network_offline=false.
  • The issue is scoped to existing OpenSafari network tooling and does not require new dependencies.

How to implement

  • Route network_intercept and network_offline through src/network-interceptor.ts.
  • Scope interceptor instances by MCP session id, with an explicit default only for legacy callers.
  • Add optional device_id support so tools can target the intended WebKit connection.
  • Preserve existing action names: block stays block; modify maps to the canonical mock response path.
  • Ensure clear/disable restores fetch and XHR hooks through the canonical disable path.
  • Add unit tests proving session isolation and action mapping.

Decisions needed before implementation

  1. Whether a future PR should expose rule ids for individual removal; first PR can keep clear-all behavior.
  2. Whether interceptor state should be additionally keyed by device id; first PR can use session scope plus device_id routing.
  3. Whether response headers should be configurable; first PR can preserve text/plain default.

Success criteria

  • Rules added in one MCP session do not affect another session's interceptor state.
  • clear=true disables and clears only that session's interceptor.
  • network_offline uses the same per-session interceptor as network_intercept.
  • Existing block/modify tool responses remain backward compatible.
  • Targeted tests, lint, build, and CI pass.

Post-merge OpenSafari live validation

  • Start two MCP sessions against Safari; add an intercept rule in session A and confirm session B has no rule count/state leakage.
  • Add a modify rule and confirm fetch returns mocked response.
  • Call clear=true, then confirm normal fetch/XHR behavior is restored in that session.
  • Toggle network_offline on/off and confirm the same page can fetch again after restore.

Direction/necessity review

  • Aligned: yes, this protects existing Safari QA/network tooling from cross-session state leakage.
  • Necessary: yes, long-running AI-agent MCP sessions routinely reuse the same process and can otherwise inherit stale rules.
  • Minimal first PR: per-session canonical interceptor routing and tests only; no new network interception feature surface beyond optional device_id.

Metadata

Metadata

Assignees

No one assigned

    Labels

    automation-roadmapOpenSafari automation roadmap work itemsenhancementNew feature or requestreliabilityReliability and stability

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions