Repository navigation
fix(mcp): honor per-dependency targets for dependencies.mcp entries - #3951
Daniel Meppiel (danielmeppiel) wants to merge 8 commits into
Conversation
`targets:` on a dependencies.mcp entry was unknown to MCPDependency, so it was preserved in `extra`, rendered into every native MCP config, and every server reached every target. Stale pruning was also not per target, so `install -g --only mcp --target codex` retired servers still declared for claude. - `targets` is a modeled MCPDependency field parsed by parse_target_subset; it is never written into native config. - mcp_dependency_runtimes (the MCP target-selection owner) narrows the run's runtimes per dependency for writes, claim bookkeeping, pruning and --frozen claims. - Pruning removes a server only from targets it no longer selects; an unselected target keeps servers whose targets: still name it. - Extend the mutation_writes.mcp_target_selection guard and add mutation cases plus an installed-CLI regression for project and global scopes. Fixes #2606 apm-spec-waiver: implements accepted issue #2606 scope; OpenAPM manifest schema already admits additional MCP keys, no normative requirement changes Co-authored-by: Copilot App <[email protected]>
A target-scoped install keeps servers whose entry targets: still name a target outside the run. When the manifest's own targets: is what dropped that target, no entry reaches it, so its claims retire as before. Extend the lifecycle test with the manifest-drop and undeclare cases. apm-spec-waiver: implements accepted issue #2606 scope; no normative spec requirement changes Co-authored-by: Copilot App <[email protected]>
Co-authored-by: Copilot App <[email protected]>
The skip path assumed every dependency had a targets list and dropped untargeted deps from registry and self-defined groups whenever the run's runtime list was empty, so registry operations were never built. Skip and warn only when the entry declares targets: with no overlap. Co-authored-by: Copilot App <[email protected]>
There was a problem hiding this comment.
🟡 Changes recommended
Critical ownership and declaration-tracking bugs can delete still-requested MCP servers when targets change or are excluded, and prevent skipped targets from receiving updated arguments on later installs.
3 open issues
What changed in this PR
This PR addresses #2606 by adding per-dependency MCP target selection to APM’s install flow, separating target metadata from native server configuration.
Changes:
- Models and validates MCP dependency
targetsinstead of treating it as passthrough configuration. - Applies dependency target selection to writes, ownership claims, pruning, and frozen-install checks.
- Adds regression coverage, architecture guards, and user documentation.
| File | Description |
|---|---|
tests/unit/test_mcp_overlays.py |
Tests target parsing and exclusion from passthrough fields. |
tests/integration/test_mcp_dependency_targets_lifecycle.py |
Covers project scoping, retargeting, and global pruning. |
tests/integration/test_architecture_owner_rule_mutations.py |
Adds mutations checking target-selection delegation. |
src/apm_cli/models/dependency/mcp.py |
Models, validates, and serializes dependency targets. |
src/apm_cli/integration/mcp_integrator_install.py |
Centralizes dependency target filtering for writes and claims. |
src/apm_cli/install/service.py |
Supports target-specific frozen MCP claims. |
src/apm_cli/install/service_integration.py |
Supplies scoped claims to frozen preflight. |
src/apm_cli/install/mcp/integration.py |
Adds per-target retirement and retained-claim handling. |
scripts/architecture_linter/checks/mutation_mcp_target_and_scope.py |
Guards use of the target-selection owner. |
packages/apm-guide/.apm/skills/apm-usage/dependencies.md |
Adds a scoped MCP dependency example. |
docs/src/content/docs/reference/manifest-schema.md |
Documents MCP targets and reserved-key behavior. |
CHANGELOG.md |
Records the user-visible scoping fix. |
.apm/architecture/owners/hooks-integrations.json |
Expands the target-selection owner description. |
Review effort: Balanced
APM Review Panel:
|
| Persona | B | R | N | Takeaway |
|---|---|---|---|---|
| Python Architect | 0 | 3 | 3 | Sound single-owner predicate. Type the apm.yml provenance check and unify the forked retire loops. |
| Test Coverage Expert | 0 | 3 | 0 | Leak, scoping and global keep/retire are covered end to end. No-overlap skip, registry grouping and owned-path pruning are not. |
| Doc Writer | 0 | 2 | 1 | Accurate main contract. Omits the skip warning and the manifest-drop prune. CHANGELOG line is compliant. |
| Devx Ux Expert | 0 | 2 | 1 | Semantics match package targets:. Docs miss the shared .mcp.json caveat and the consumer page. |
| Supply Chain Security Expert | 0 | 2 | 1 | Only narrows, reserved from extra, hashes kept. Document the shared .mcp.json reach. Name-only prune hardening is a follow-up. |
B = blocking-severity findings, R = recommended, N = nits.
Counts are signal strength, not gates. The maintainer ships.
Top 5 follow-ups
- [Test Coverage Expert] Add the three missing tests: the no-overlap skip (warning, server absent, no claim, untargeted servers unaffected), registry grouping by (runtimes, url), and owned-path deselection. -- These are destructive and skip branches on a surface that holds credential-bearing config.
- [Python Architect] Replace
source == "apm.yml"with a typed predicate pinned in the guard, and unify the retention set algebra. -- Today a renamed label would silently change what gets deleted. - [Python Architect] Decide and document the named_only behavior for untargeted servers on a skipped target, and add an assertion. -- Whether a filtered install deletes servers is a user-visible promise.
- [Supply Chain Security Expert] In the docs, cover the shared claude/copilot
.mcp.jsonreach, the manifest-drop retirement rule and the skip warning. Move the skip warning to DiagnosticCollector. --targetscannot isolate credentials between harnesses that share one file. - [Supply Chain Security Expert] Track separately: hash-verified
remove_stalefor every adapter. Here, assert that a retained claim keeps its native-hash receipt. -- The gap predates this PR and fixing it changes removal semantics in every adapter, which is out of scope.
Architecture
flowchart TD
A["install / install -g --only mcp --target X"] --> C["MCPDependency.from_dict: parse_target_subset, targets reserved from extra"]
C --> D["run_service_integrations"]
D --> F["--frozen preflight: claims per target = mcp_servers_for_runtime"]
D --> G["run_mcp_integration"]
G --> I["run_mcp_install: dep_runtimes = mcp_dependency_runtimes(dep, target_runtimes)"]
I --> N["registry groups by (runtimes, url); self-defined groups by runtimes"]
N --> R{"retire loop per owned target"}
R -- active --> S["retire (owned and declared) - selected"]
R -- inactive --> T{"manifest dropped the target?"}
T -- yes --> U["retire all"]
T -- no --> V["keep servers whose targets name it"]
S --> W["names_owned_by_active_peers (shared .mcp.json)"]
U --> W
V --> W
W --> X["remove_stale per target"]
X --> Y["update_lockfile: mcp_target_servers + hashes"]
Recommendation
The fix is correct, the acceptance tests pass, and reach only narrows. Fold follow-ups 1-4 into this PR before it merges for 0.37.0. Track the hash-verified removal hardening for all adapters separately, citing this PR as a new trigger.
Full per-persona findings
Python Architect
- [recommended] Destructive pruning is keyed on the free-form label
target_decision.source == "apm.yml", atsrc/apm_cli/install/mcp/integration.py:577. Suggested: add a typed predicate on EffectiveTargetDecision, backed by a resolver constant, and pin it in the guard. - [recommended] The per-target retention algebra is forked between run_mcp_integration and run_owned_mcp_integration. Suggested: use one pure planner.
- [recommended] named_only retires untargeted servers from a skipped target. Suggested: decide explicitly, document it, and add an assertion.
- [nit] The skip warning uses logger.warning, while the package twin uses DiagnosticCollector.
- [nit] The linter fragments are line-matched, so a reformat could false-positive.
- [nit] claude and copilot share the project
.mcp.json. Document it.
Test Coverage Expert
- [recommended] The no-overlap skip path is untested. Proof (missing at):
tests/integration/test_mcp_dependency_targets_lifecycle.py - [recommended] Registry grouping by (runtimes, url) is untested. Proof (missing at):
tests/unit/integration/test_mcp_per_dep_registry.py - [recommended] The run_owned_mcp_integration deselection branch is untested. Proof (missing at):
tests/unit/install/test_owned_mcp_integration.py
Doc Writer
- [recommended] The schema row and the apm-usage comment miss the manifest-drop retirement rule.
- [recommended] The schema row omits the no-overlap skip warning.
- [nit] consumer/install-mcp-servers.md has no pointer to per-entry
targets:.
Devx Ux Expert
- [recommended] Document that claude and copilot share the project
.mcp.json. - [recommended] The consumer page section "How targets: gates which configs get written" should cover per-server
targets:. - [nit] The warning vocabulary differs from the package path.
Supply Chain Security Expert
- [recommended] In project scope,
targets: [claude]still exposes a server to copilot through the shared.mcp.json. Document it. - [recommended] Per-target retirement on name-only adapters ignores the native hash. This gap predates the PR. Suggested: track it as a follow-up.
- [nit] Assert that the retained claude claim keeps its native-hash receipt.
This panel is advisory. It does not block merge. Re-apply the
panel-review label after addressing feedback to re-run.
Generated by autopilot-pr-review-worker. This comment is AI-generated and may contain errors.
Addresses the #3951 panel (CEO follow-ups 1-4): - typed EffectiveTargetDecision.from_manifest replaces the free-form source == "apm.yml" check that decided manifest-drop retirement; - deselected_mcp_claims in ownership.py is the one retire algebra for the install and owned (bundle) paths; the guard pins both plus the typed predicate, with two new mutation cases; - tests for the no-overlap skip warning, untargeted servers following --target, the retained claim's native-hash receipt, registry grouping by narrowed runtimes, and owned-path deselection; - docs: skip warning, manifest-drop rule, untargeted rule and the shared claude/copilot .mcp.json reach. apm-spec-waiver: implements accepted issue #2606 scope; no normative spec requirement changes Co-authored-by: Copilot App <[email protected]>
…606-mcp-targets-scoping
Addresses Copilot inline review on #3951: - src/apm_cli/install/mcp/integration.py:621: --exclude no longer clears claims on a target the manifest still lists; only targets missing from the manifest's own runtimes retire. - integration.py:623: a skipped target keeps the old declaration baseline for its retained servers, so its next install sees the drift and applies the new declaration. - src/apm_cli/integration/mcp_integrator_install.py:1934: retargeting between claude and copilot hands the shared .mcp.json claim and receipt to the newly selected peer before narrowing, so the entry is not read as foreign or deleted. Lifecycle regression covers all three; guard and mutation case pin the shared-claim transfer. apm-spec-waiver: implements accepted issue #2606 scope; no normative spec requirement changes Co-authored-by: Copilot App <[email protected]>
…606-mcp-targets-scoping
…icrosoft#2606) Harvested from microsoft#3951 (eecc79d). Co-authored-by: Copilot App <[email protected]>

Description
targets:on adependencies.mcpentry was not a modeled key.MCPDependency.from_dictrouted it intoextra, so it was written into every native MCP config, and every server reached every target. Stale pruning also did not work per target:apm install -g --only mcp --target codexremovedsrv-afrom~/.claude.jsoneven thoughsrv-awas still declared for claude.What changed:
targetsis a modeledMCPDependencyfield. It is parsed byparse_target_subset(unknown names are rejected) and is never rendered into a native config.mcp_dependency_runtimesinintegration/mcp_integrator_install.py, the MCP target-selection owner, narrows the run's runtimes for each dependency. Writes, claim bookkeeping, pruning, owned (uninstall) cleanup and--frozenclaim preflight all go through it.targets:still name it. The exception is a target that the manifest's owntargets:dropped: that target is outside every entry's reach, so its claims retire as before.mutation_writes.mcp_target_selectionguard is extended, with mutation cases intests/integration/test_architecture_owner_rule_mutations.py.Issue and approved scope
Issue: Fixes #2606
Human scope-approval comment: #2606 (comment)
This PR completes the approved scope:
targetsno longer leaks into native config, and pruning is per target. No new MCP schema fields and no other passthrough changes.Type of change
Testing
Validation evidence (local, on main b50c403 plus this branch):
pytest tests/unit/test_mcp_overlays.py::TestMCPDependencyTargetsScoping tests/integration/test_mcp_dependency_targets_lifecycle.py(APM_BINARY_PATH=.venv/bin/apm): passed. Withsrc/reverted to main, all three original cases fail: thetargetsleak, project scoping, and the global--target codexprune.[claude, cursor]scoping, retarget,--frozen, and a manifest-dropped target. Globalinstall -g --only mcp --target codexkeeps the claude-scopedsrv-a, and undeclaringsrv-astill retires it.pytest tests/integration/test_architecture_owner_rule_mutations.py tests/unit/scripts/test_architecture_runner.py -n 8: 894 passed, 2 failed under parallel load. Both failures (credential_report...[no-once-per-key]andcopilot_project_mcp_destination...[bundle-reads-retired-claims-as-live]) are in guards this PR does not touch, and both pass when rerun serially.ruff check/ruff format --check(ruff 0.15.12, CI paths): clean.pylint R0801: 10.00/10.scripts/lint-auth-signals.sh: clean.lint_architecture_boundaries.py --root .: clean. File-length guard: clean.Changelog
## [Unreleased]inCHANGELOG.md:user-visible effect first, 25 words or fewer, ending with
(#PR);breaking changes only under upgrade notes; no Highlights; see
.github/instructions/changelog.instructions.md.Spec conformance (OpenAPM v0.1)
No
req-XXXchanges. The commits carry anapm-spec-waiver:trailer because the OpenAPM manifest already admits additional MCP keys.CONFORMANCE.{md,json}regenerated