Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 2, 2025

Description

Optimizes aspire mcp init to reduce redundant git operations and recursive directory traversal. Previously, each environment scanner (VsCode, ClaudeCode, OpenCode) independently called IGitRepository.GetRootAsync() and performed recursive searches up the directory tree. This PR centralizes repository detection to a single upfront call and provides scanners with the workspace root via context.

Changes

Core optimization:

  • McpInitCommand now calls IGitRepository.GetRootAsync() once before invoking scanners
  • Added user prompt to confirm/specify workspace root (defaults to git root or working directory)
  • Updated DetectAsync to accept repositoryRoot parameter passed through AgentEnvironmentScanContext

Scanner improvements:

  • Removed IGitRepository dependency from all scanners (VsCode, ClaudeCode, OpenCode)
  • Scanners now use context.RepositoryRoot as search boundary instead of recursively walking directory tree
  • Simplified scanner logic without git-related complexity
  • Updated terminology to use "workspace root" instead of "repository root" in log messages and comments for better clarity (workspace won't always be a git repository)

Bug fixes and code quality improvements:

  • Fixed critical bug in ClaudeCodeAgentEnvironmentScanner where the else block was unreachable due to always-true condition
  • Simplified logic by removing unnecessary intermediate variables
  • Updated variable names in McpInitCommand for consistency (defaultWorkspaceRoot, workspaceRootPath, workspaceRoot)
  • Improved code comments to accurately describe behavior
  • Updated test names to remove outdated terminology

Result: Git command invocations reduced from 3 to 1, recursive searches eliminated.

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
    • No. Follow-up changes expected.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
    • No
  • Did you add public API?
    • Yes
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
    • No
  • Does the change require an update in our Aspire docs?
    • Yes
    • No
Original prompt

The agent environment scanners all take a dependency on the IGitRepository service and individually result in invoking Git. At the same time where is potential for the environment scanners to spend more time scanning folders than they need to.

Modify the aspire mcp init command so that it uses the IGitRepository API once to try and discover the git working folder and if not default to the current working directory as the root of the repository - and then ask the user up front before calling DetectAsync what the path to the root of the workspace is.

Then modify the DetectAsync method to take a context object which contains the DirectoryInfo for the supplied root of the workspace and change the scanners to use this folder as the root of the repo when searching for files.

This should remove the need to do recursive searches up the source tree and improve efficience for each scanner.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Modify aspire mcp init command for improved efficiency Optimize MCP init by consolidating git lookups and eliminating recursive searches Dec 2, 2025
Copilot AI requested a review from mitchdenny December 2, 2025 03:15
Copilot finished work on behalf of mitchdenny December 2, 2025 03:15
@mitchdenny
Copy link
Member

@copilot src/Aspire.Cli/Agents/ClaudeCode/ClaudeCodeAgentEnvironmentScanner.cs:40 use the term "workspace root" since that is more generic. It won't always be a git repo.

Copy link
Contributor Author

Copilot AI commented Dec 2, 2025

@copilot src/Aspire.Cli/Agents/ClaudeCode/ClaudeCodeAgentEnvironmentScanner.cs:40 use the term "workspace root" since that is more generic. It won't always be a git repo.

Updated to use "workspace root" terminology throughout all scanners (VsCode, ClaudeCode, OpenCode) in log messages and comments. Commit 5f3220e.

Copilot finished work on behalf of mitchdenny December 2, 2025 03:51
@github-actions
Copy link
Contributor

github-actions bot commented Dec 2, 2025

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 13278

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 13278"

@mitchdenny mitchdenny marked this pull request as ready for review December 2, 2025 22:27
Copilot AI review requested due to automatic review settings December 2, 2025 22:27
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes the aspire mcp init command by centralizing git repository detection and eliminating redundant operations. Previously, each environment scanner independently called IGitRepository.GetRootAsync() and performed recursive directory searches. The refactoring moves repository detection to a single upfront call in McpInitCommand, with the workspace root passed to scanners via context.

Key Changes

  • Centralized git repository lookup in McpInitCommand with user confirmation prompt
  • Updated DetectAsync API to accept repositoryRoot parameter via AgentEnvironmentScanContext
  • Removed IGitRepository dependency from all scanners (VsCode, ClaudeCode, OpenCode)

Reviewed changes

Copilot reviewed 26 out of 27 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
src/Aspire.Cli/Commands/McpInitCommand.cs Added git repository lookup, user prompt for workspace root, and validation logic
src/Aspire.Cli/Commands/McpCommand.cs Added IGitRepository dependency injection to McpCommand constructor
src/Aspire.Cli/Agents/IAgentEnvironmentDetector.cs Added repositoryRoot parameter to DetectAsync interface
src/Aspire.Cli/Agents/AgentEnvironmentDetector.cs Updated to pass repositoryRoot through context
src/Aspire.Cli/Agents/AgentEnvironmentScanContext.cs Added RepositoryRoot property for scanners to use as search boundary
src/Aspire.Cli/Agents/VsCode/VsCodeAgentEnvironmentScanner.cs Removed IGitRepository dependency, updated to use context.RepositoryRoot
src/Aspire.Cli/Agents/ClaudeCode/ClaudeCodeAgentEnvironmentScanner.cs Removed IGitRepository dependency, updated to use context.RepositoryRoot
src/Aspire.Cli/Agents/OpenCode/OpenCodeAgentEnvironmentScanner.cs Removed IGitRepository dependency, updated to use context.RepositoryRoot
src/Aspire.Cli/Resources/McpCommandStrings.resx Added three new resource strings for workspace root prompts
src/Aspire.Cli/Resources/McpCommandStrings.Designer.cs Auto-generated resource accessor properties for new strings
src/Aspire.Cli/Resources/xlf/*.xlf Localization files updated with state="new" for untranslated strings
tests/Aspire.Cli.Tests/Utils/CliTestHelper.cs Added GitRepositoryFactory for test dependency injection
tests/Aspire.Cli.Tests/Agents/VsCodeAgentEnvironmentScannerTests.cs Removed FakeGitRepository, updated tests to pass RepositoryRoot via context
tests/Aspire.Cli.Tests/Agents/CopilotCliAgentEnvironmentScannerTests.cs Updated test contexts to include RepositoryRoot property
tests/Aspire.Cli.Tests/Agents/AgentEnvironmentDetectorTests.cs Updated DetectAsync calls with repositoryRoot parameter
Files not reviewed (1)
  • src/Aspire.Cli/Resources/McpCommandStrings.Designer.cs: Language not supported

@mitchdenny
Copy link
Member

@copilot apply changes based on the comments in this thread

Copilot AI requested a review from mitchdenny December 2, 2025 23:11
Copilot finished work on behalf of mitchdenny December 2, 2025 23:11
Copy link
Contributor Author

Copilot AI commented Dec 2, 2025

@copilot apply changes based on the comments in this thread

All changes from that review thread have already been applied in commit 868a8ef. The code now includes all the requested improvements: simplified logic, fixed bug making else block reachable, consistent variable naming, and improved comments.

Copilot finished work on behalf of mitchdenny December 2, 2025 23:16
Copilot finished work on behalf of mitchdenny December 2, 2025 23:58
@mitchdenny mitchdenny enabled auto-merge (squash) December 4, 2025 09:28
@davidfowl
Copy link
Member

davidfowl commented Dec 4, 2025

@mitchdenny do we handle relative paths properly? If I run mcp init and I don't enter an absolute path it does not do the expected thing.

@davidfowl
Copy link
Member

Might not be related to relative paths

image

@mitchdenny mitchdenny merged commit 2619635 into main Dec 4, 2025
568 of 573 checks passed
@mitchdenny mitchdenny deleted the copilot/modify-aspire-mcp-init-command branch December 4, 2025 10:26
@dotnet-policy-service dotnet-policy-service bot added this to the 13.1 milestone Dec 4, 2025
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