Skip to content

Commit 03d1ce9

Browse files
dsarnoClaude
and
Claude
committed
fix: Add resource lookup by URI to support MCP client requests
Added support for finding resources by URI in addition to resource name. This fixes compatibility with Claude Code MCP client, which sends requests using the URI ('unity://hierarchy') instead of the resource name ('get_hierarchy'). 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent c64652e commit 03d1ce9

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

Diff for: Editor/UnityBridge/McpUnityServer.cs

+5
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public static McpUnityServer Instance
6666
/// </summary>
6767
public Dictionary<string, string> Clients => _clients;
6868

69+
/// <summary>
70+
/// Dictionary of registered resources
71+
/// </summary>
72+
public Dictionary<string, McpResourceBase> Resources => _resources;
73+
6974
/// <summary>
7075
/// Private constructor to enforce singleton pattern
7176
/// </summary>

Diff for: Editor/UnityBridge/McpUnitySocketHandler.cs

+33-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,16 @@ protected override async void OnMessage(MessageEventArgs e)
7777
}
7878
else
7979
{
80-
tcs.SetResult(CreateErrorResponse($"Unknown method: {method}", "unknown_method"));
80+
// Check if we're trying to access a resource by URI
81+
var resourceByUri = FindResourceByUri(method);
82+
if (resourceByUri != null)
83+
{
84+
EditorCoroutineUtility.StartCoroutineOwnerless(FetchResourceCoroutine(resourceByUri, parameters, tcs));
85+
}
86+
else
87+
{
88+
tcs.SetResult(CreateErrorResponse($"Unknown method: {method}", "unknown_method"));
89+
}
8190
}
8291

8392
// Wait for the task to complete
@@ -191,6 +200,29 @@ private IEnumerator FetchResourceCoroutine(McpResourceBase resource, JObject par
191200
yield return null;
192201
}
193202

203+
/// <summary>
204+
/// Find a resource by its URI
205+
/// </summary>
206+
/// <param name="uri">The URI to search for</param>
207+
/// <returns>The resource if found, null otherwise</returns>
208+
private McpResourceBase FindResourceByUri(string uri)
209+
{
210+
// Get resources from the server
211+
var resources = _server.Resources;
212+
213+
// Look for a resource with a matching URI
214+
foreach (var resource in resources.Values)
215+
{
216+
if (resource.Uri == uri)
217+
{
218+
Debug.Log($"[MCP Unity] Found resource {resource.Name} by URI {uri}");
219+
return resource;
220+
}
221+
}
222+
223+
return null;
224+
}
225+
194226
/// <summary>
195227
/// Create a JSON-RPC 2.0 response
196228
/// </summary>

0 commit comments

Comments
 (0)