-
Notifications
You must be signed in to change notification settings - Fork 743
Display resources #658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Display resources #658
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a2a73df
Add resource discovery service and UI for managing MCP resources
msanatan 6f97582
Consolidate duplicate IsBuiltIn logic into StringCaseUtility.IsBuiltI…
msanatan 26eae44
Add resource enable/disable enforcement and improve error response ha…
msanatan ce59030
Block execution of disabled built-in tools in TransportCommandDispatc…
msanatan 0bda8c4
Fire warning in the rare chance there are duplicate names
msanatan 349e214
Handle rare case a resource name is null
msanatan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| using System.Collections.Generic; | ||
|
|
||
| namespace MCPForUnity.Editor.Services | ||
| { | ||
| /// <summary> | ||
| /// Metadata for a discovered resource | ||
| /// </summary> | ||
| public class ResourceMetadata | ||
| { | ||
| public string Name { get; set; } | ||
| public string Description { get; set; } | ||
| public string ClassName { get; set; } | ||
| public string Namespace { get; set; } | ||
| public string AssemblyName { get; set; } | ||
| public bool IsBuiltIn { get; set; } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Service for discovering MCP resources via reflection | ||
| /// </summary> | ||
| public interface IResourceDiscoveryService | ||
| { | ||
| /// <summary> | ||
| /// Discovers all resources marked with [McpForUnityResource] | ||
| /// </summary> | ||
| List<ResourceMetadata> DiscoverAllResources(); | ||
|
|
||
| /// <summary> | ||
| /// Gets metadata for a specific resource | ||
| /// </summary> | ||
| ResourceMetadata GetResourceMetadata(string resourceName); | ||
|
|
||
| /// <summary> | ||
| /// Returns only the resources currently enabled | ||
| /// </summary> | ||
| List<ResourceMetadata> GetEnabledResources(); | ||
|
|
||
| /// <summary> | ||
| /// Checks whether a resource is currently enabled | ||
| /// </summary> | ||
| bool IsResourceEnabled(string resourceName); | ||
|
|
||
| /// <summary> | ||
| /// Updates the enabled state for a resource | ||
| /// </summary> | ||
| void SetResourceEnabled(string resourceName, bool enabled); | ||
|
|
||
| /// <summary> | ||
| /// Invalidates the resource discovery cache | ||
| /// </summary> | ||
| void InvalidateCache(); | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
MCPForUnity/Editor/Services/IResourceDiscoveryService.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
MCPForUnity/Editor/Services/ResourceDiscoveryService.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Reflection; | ||
| using MCPForUnity.Editor.Constants; | ||
| using MCPForUnity.Editor.Helpers; | ||
| using MCPForUnity.Editor.Resources; | ||
| using UnityEditor; | ||
|
|
||
| namespace MCPForUnity.Editor.Services | ||
| { | ||
| public class ResourceDiscoveryService : IResourceDiscoveryService | ||
| { | ||
| private Dictionary<string, ResourceMetadata> _cachedResources; | ||
|
|
||
| public List<ResourceMetadata> DiscoverAllResources() | ||
| { | ||
| if (_cachedResources != null) | ||
| { | ||
| return _cachedResources.Values.ToList(); | ||
| } | ||
|
|
||
| _cachedResources = new Dictionary<string, ResourceMetadata>(); | ||
|
|
||
| var resourceTypes = TypeCache.GetTypesWithAttribute<McpForUnityResourceAttribute>(); | ||
| foreach (var type in resourceTypes) | ||
| { | ||
| McpForUnityResourceAttribute resourceAttr; | ||
| try | ||
| { | ||
| resourceAttr = type.GetCustomAttribute<McpForUnityResourceAttribute>(); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| McpLog.Warn($"Failed to read [McpForUnityResource] for {type.FullName}: {ex.Message}"); | ||
| continue; | ||
| } | ||
|
|
||
| if (resourceAttr == null) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| var metadata = ExtractResourceMetadata(type, resourceAttr); | ||
| if (metadata != null) | ||
| { | ||
| if (_cachedResources.ContainsKey(metadata.Name)) | ||
| { | ||
| McpLog.Warn($"Duplicate resource name '{metadata.Name}' from {type.FullName}; overwriting previous registration."); | ||
| } | ||
| _cachedResources[metadata.Name] = metadata; | ||
| EnsurePreferenceInitialized(metadata); | ||
| } | ||
| } | ||
|
|
||
| McpLog.Info($"Discovered {_cachedResources.Count} MCP resources via reflection", false); | ||
| return _cachedResources.Values.ToList(); | ||
| } | ||
|
|
||
| public ResourceMetadata GetResourceMetadata(string resourceName) | ||
| { | ||
| if (string.IsNullOrEmpty(resourceName)) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| if (_cachedResources == null) | ||
| { | ||
| DiscoverAllResources(); | ||
| } | ||
|
|
||
| return _cachedResources.TryGetValue(resourceName, out var metadata) ? metadata : null; | ||
| } | ||
|
|
||
| public List<ResourceMetadata> GetEnabledResources() | ||
| { | ||
| return DiscoverAllResources() | ||
| .Where(r => IsResourceEnabled(r.Name)) | ||
| .ToList(); | ||
| } | ||
|
|
||
| public bool IsResourceEnabled(string resourceName) | ||
| { | ||
| if (string.IsNullOrEmpty(resourceName)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| string key = GetResourcePreferenceKey(resourceName); | ||
| if (EditorPrefs.HasKey(key)) | ||
| { | ||
| return EditorPrefs.GetBool(key, true); | ||
| } | ||
|
|
||
| // Default: all resources enabled | ||
| return true; | ||
| } | ||
|
|
||
| public void SetResourceEnabled(string resourceName, bool enabled) | ||
| { | ||
| if (string.IsNullOrEmpty(resourceName)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| string key = GetResourcePreferenceKey(resourceName); | ||
| EditorPrefs.SetBool(key, enabled); | ||
| } | ||
|
|
||
| public void InvalidateCache() | ||
| { | ||
| _cachedResources = null; | ||
| } | ||
|
|
||
| private ResourceMetadata ExtractResourceMetadata(Type type, McpForUnityResourceAttribute resourceAttr) | ||
| { | ||
| try | ||
| { | ||
| string resourceName = resourceAttr.ResourceName; | ||
| if (string.IsNullOrEmpty(resourceName)) | ||
| { | ||
| resourceName = StringCaseUtility.ToSnakeCase(type.Name); | ||
| } | ||
|
|
||
| string description = resourceAttr.Description ?? $"Resource: {resourceName}"; | ||
|
|
||
| var metadata = new ResourceMetadata | ||
| { | ||
| Name = resourceName, | ||
| Description = description, | ||
| ClassName = type.Name, | ||
| Namespace = type.Namespace ?? "", | ||
| AssemblyName = type.Assembly.GetName().Name | ||
| }; | ||
|
|
||
| metadata.IsBuiltIn = StringCaseUtility.IsBuiltInMcpType( | ||
| type, metadata.AssemblyName, "MCPForUnity.Editor.Resources"); | ||
|
|
||
| return metadata; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| McpLog.Error($"Failed to extract metadata for resource {type.Name}: {ex.Message}"); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private void EnsurePreferenceInitialized(ResourceMetadata metadata) | ||
| { | ||
| if (metadata == null || string.IsNullOrEmpty(metadata.Name)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| string key = GetResourcePreferenceKey(metadata.Name); | ||
| if (!EditorPrefs.HasKey(key)) | ||
| { | ||
| EditorPrefs.SetBool(key, true); | ||
| } | ||
| } | ||
|
|
||
| private static string GetResourcePreferenceKey(string resourceName) | ||
| { | ||
| return EditorPrefKeys.ResourceEnabledPrefix + resourceName; | ||
| } | ||
| } | ||
| } | ||
11 changes: 11 additions & 0 deletions
11
MCPForUnity/Editor/Services/ResourceDiscoveryService.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.