using System.Collections.Generic;
using System.Linq;
using MCPForUnity.Editor.Helpers;
using Newtonsoft.Json.Linq;
using UnityEngine;
namespace MCPForUnity.Editor.Tools
{
/// <summary>
/// Tool for searching GameObjects in the scene.
/// Returns only instance IDs with pagination support.
///
/// This is a focused search tool that returns lightweight results (IDs only).
/// For detailed GameObject data, use the unity://scene/gameobject/{id} resource.
/// </summary>
[McpForUnityTool("find_gameobjects")]
public static class FindGameObjects
{
/// <summary>
/// Handles the find_gameobjects command.
/// </summary>
/// <param name="params">Command parameters</param>
/// <returns>Paginated list of instance IDs</returns>
public static object HandleCommand(JObject @params)
{
if (@params == null)
{
return new ErrorResponse("Parameters cannot be null.");
}
// Parse search parameters
string searchMethod = ParamCoercion.CoerceString(@params["searchMethod"] ?? @params["search_method"], "by_name");
string searchTerm = ParamCoercion.CoerceString(@params["searchTerm"] ?? @params["search_term"] ?? @params["target"], null);
if (string.IsNullOrEmpty(searchTerm))
{
return new ErrorResponse("'searchTerm' or 'target' parameter is required.");
}
// Pagination parameters using standard PaginationRequest
var pagination = PaginationRequest.FromParams(@params, defaultPageSize: 50);
pagination.PageSize = Mathf.Clamp(pagination.PageSize, 1, 500);
// Search options
bool includeInactive = ParamCoercion.CoerceBool(@params["includeInactive"] ?? @params["searchInactive"] ?? @params["include_inactive"], false);
try
{
// Get all matching instance IDs
var allIds = GameObjectLookup.SearchGameObjects(searchMethod, searchTerm, includeInactive, 0);
// Use standard pagination response
var paginatedResult = PaginationResponse<int>.Create(allIds, pagination);
return new SuccessResponse("Found GameObjects", new
{
instanceIDs = paginatedResult.Items,
pageSize = paginatedResult.PageSize,
cursor = paginatedResult.Cursor,
nextCursor = paginatedResult.NextCursor,
totalCount = paginatedResult.TotalCount,
hasMore = paginatedResult.HasMore
});
}
catch (System.Exception ex)
{
McpLog.Error($"[FindGameObjects] Error searching GameObjects: {ex.Message}");
return new ErrorResponse($"Error searching GameObjects: {ex.Message}");
}
}
}
}