Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 20, 2025

This PR refactors the Tool_Assets.Refresh method to follow the established pattern for MCP tools that can trigger Unity compilation, bringing it in line with other tools like Script.Delete and TestRunner.Run.

Key Changes

Method Signature & Return Type:

  • Updated signature to accept [RequestID] string? requestId = null parameter
  • Changed return type from string to ResponseCallTool
  • Added proper RequestID validation matching existing patterns

Compilation Lifecycle Handling:
The method now properly handles Unity's compilation scenarios:

// Before: Always returned success immediately
public string Refresh() => MainThread.Instance.Run(() => {
    AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
    return "[Success] AssetDatabase refreshed...";
});

// After: Handles both immediate and delayed completion
public static ResponseCallTool Refresh([RequestID] string? requestId = null) {
    // ... validation ...
    
    AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);
    
    if (EditorApplication.isCompiling) {
        // Compilation triggered - return Processing and schedule notification
        ScriptUtils.SchedulePostCompilationNotification(requestId, "AssetDatabase", "Asset refresh");
        return ResponseCallTool.Processing("...waiting for completion...").SetRequestID(requestId);
    } else {
        // No compilation - return Success immediately
        return ResponseCallTool.Success("AssetDatabase refreshed...").SetRequestID(requestId);
    }
}

Improved Error Handling:

  • Validates RequestID parameter (null, empty, whitespace)
  • Checks for existing compilation errors before proceeding
  • Comprehensive try-catch with detailed error messages
  • All responses include RequestID for proper MCP server tracking

Testing:
Added comprehensive unit tests covering:

  • Valid RequestID scenarios (both immediate success and processing states)
  • Invalid RequestID validation (null, empty, whitespace)
  • Error response format validation

Benefits

  1. Consistent API: Now matches the pattern used by TestRunner.Run and Script.Delete
  2. Proper Async Handling: MCP server no longer waits indefinitely when compilation is triggered
  3. Better Error Reporting: Clear validation and exception handling with proper RequestID tracking
  4. Reliable Notifications: Uses established ScriptUtils.SchedulePostCompilationNotification system that survives domain reloads

This change ensures the MCP server receives appropriate responses for both synchronous operations (when no compilation occurs) and asynchronous operations (when Unity needs to recompile scripts after asset changes).

Fixes #230.


✨ 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] Refactor Tool_Assets.Refresh tool Refactor Tool_Assets.Refresh to use ResponseCallTool and handle compilation lifecycle Sep 20, 2025
Copilot AI requested a review from IvanMurzak September 20, 2025 10:39
Copilot finished work on behalf of IvanMurzak September 20, 2025 10:39
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.

Refactor Tool_Assets.Refresh tool

2 participants