Skip to content

Commit a435973

Browse files
committed
Add get_component action to manage_gameobject
Adds a new 'get_component' action that retrieves a single component's serialized data instead of all components, improving efficiency and avoiding token limits when only specific component data is needed.
1 parent 1ad8c6e commit a435973

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

UnityMcpBridge/Editor/Tools/ManageGameObject.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,18 @@ public static object HandleCommand(JObject @params)
155155
);
156156
// Pass the includeNonPublicSerialized flag here
157157
return GetComponentsFromTarget(getCompTarget, searchMethod, includeNonPublicSerialized);
158+
case "get_component":
159+
string getSingleCompTarget = targetToken?.ToString();
160+
if (getSingleCompTarget == null)
161+
return Response.Error(
162+
"'target' parameter required for get_component."
163+
);
164+
string componentName = @params["componentName"]?.ToString();
165+
if (string.IsNullOrEmpty(componentName))
166+
return Response.Error(
167+
"'componentName' parameter required for get_component."
168+
);
169+
return GetSingleComponentFromTarget(getSingleCompTarget, searchMethod, componentName, includeNonPublicSerialized);
158170
case "add_component":
159171
return AddComponentToTarget(@params, targetToken, searchMethod);
160172
case "remove_component":
@@ -1008,6 +1020,70 @@ private static object GetComponentsFromTarget(string target, string searchMethod
10081020
}
10091021
}
10101022

1023+
private static object GetSingleComponentFromTarget(string target, string searchMethod, string componentName, bool includeNonPublicSerialized = true)
1024+
{
1025+
GameObject targetGo = FindObjectInternal(target, searchMethod);
1026+
if (targetGo == null)
1027+
{
1028+
return Response.Error(
1029+
$"Target GameObject ('{target}') not found using method '{searchMethod ?? "default"}'."
1030+
);
1031+
}
1032+
1033+
try
1034+
{
1035+
// Try to find the component by name
1036+
Component targetComponent = targetGo.GetComponent(componentName);
1037+
1038+
// If not found directly, try to find by type name (handle namespaces)
1039+
if (targetComponent == null)
1040+
{
1041+
Component[] allComponents = targetGo.GetComponents<Component>();
1042+
foreach (Component comp in allComponents)
1043+
{
1044+
if (comp != null)
1045+
{
1046+
string typeName = comp.GetType().Name;
1047+
string fullTypeName = comp.GetType().FullName;
1048+
1049+
if (typeName == componentName || fullTypeName == componentName)
1050+
{
1051+
targetComponent = comp;
1052+
break;
1053+
}
1054+
}
1055+
}
1056+
}
1057+
1058+
if (targetComponent == null)
1059+
{
1060+
return Response.Error(
1061+
$"Component '{componentName}' not found on GameObject '{targetGo.name}'."
1062+
);
1063+
}
1064+
1065+
var componentData = Helpers.GameObjectSerializer.GetComponentData(targetComponent, includeNonPublicSerialized);
1066+
1067+
if (componentData == null)
1068+
{
1069+
return Response.Error(
1070+
$"Failed to serialize component '{componentName}' on GameObject '{targetGo.name}'."
1071+
);
1072+
}
1073+
1074+
return Response.Success(
1075+
$"Retrieved component '{componentName}' from '{targetGo.name}'.",
1076+
componentData
1077+
);
1078+
}
1079+
catch (Exception e)
1080+
{
1081+
return Response.Error(
1082+
$"Error getting component '{componentName}' from '{targetGo.name}': {e.Message}"
1083+
);
1084+
}
1085+
}
1086+
10111087
private static object AddComponentToTarget(
10121088
JObject @params,
10131089
JToken targetToken,

UnityMcpBridge/UnityMcpServer~/src/tools/manage_gameobject.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
def register_manage_gameobject_tools(mcp: FastMCP):
1010
"""Register all GameObject management tools with the MCP server."""
1111

12-
@mcp.tool(name="manage_gameobject", description="Manage GameObjects. Note: for 'get_components', the `data` field contains a dictionary of component names and their serialized properties.")
12+
@mcp.tool(name="manage_gameobject", description="Manage GameObjects. Note: for 'get_components', the `data` field contains a dictionary of component names and their serialized properties. For 'get_component', specify 'component_name' to retrieve only that component's serialized data.")
1313
@telemetry_tool("manage_gameobject")
1414
def manage_gameobject(
1515
ctx: Context,
16-
action: Annotated[Literal["create", "modify", "delete", "find", "add_component", "remove_component", "set_component_property", "get_components"], "Perform CRUD operations on GameObjects and components."],
16+
action: Annotated[Literal["create", "modify", "delete", "find", "add_component", "remove_component", "set_component_property", "get_components", "get_component"], "Perform CRUD operations on GameObjects and components."],
1717
target: Annotated[str,
1818
"GameObject identifier by name or path for modify/delete/component actions"] | None = None,
1919
search_method: Annotated[Literal["by_id", "by_name", "by_path", "by_tag", "by_layer", "by_component"],

0 commit comments

Comments
 (0)