Editor/Tools/CommunityPortTools.cs
#nullable enable
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Sandbox;

namespace Editor.Mcp
{
	public static partial class SboxMcpAssistant
	{
		/// <summary>
		/// Bounded editor-dock discovery inspired by the open-source community MCPs.
		/// This is intentionally read-only: the stock editor remains the authority for
		/// opening a dock, while Hearth-MCP supplies a stable inventory for orchestration.
		/// </summary>

		/// <summary>
		/// Enables/disables the active scene's real navigation mesh and verifies the
		/// state before optionally saving through the stock scene authority.
		/// </summary>
		[McpTool("pocketknife_configure_navmesh")]
		public static string ConfigureNavMesh([Description("Whether the active scene NavMesh should be enabled.")] bool enabled = true, [Description("Save the active scene after the state change.")] bool saveScene = true)
		{
			try
			{
				var session = EditorRuntime.GetStaticProperty("Editor.SceneEditorSession", "Active");
				var scene = EditorRuntime.GetMember(session, "Scene") as Scene;
				var navMesh = scene?.NavMesh;
				if (navMesh is null)
					return McpJson.Write(ToolOutcome<object>.Failure(null, ToolStatus.Unavailable, "navmesh.not_present", "The active scene does not expose a navigation mesh instance."));

				var before = new { IsEnabled = navMesh.IsEnabled, IsGenerating = navMesh.IsGenerating, IsDirty = navMesh.IsDirty };
				if (!EditorRuntime.SetMember(navMesh, "IsEnabled", enabled))
					return McpJson.Write(ToolOutcome<object>.Failure(new { Before = before }, ToolStatus.Unavailable, "navmesh.enable_unavailable", "The loaded engine exposes a NavMesh but its enabled state is not writable through the runtime API.", remediation: "Enable the NavMesh in the scene inspector, then retry the bake."));

				EditorRuntime.InvokeInstance(navMesh, "SetDirty");
				if (saveScene)
					EditorRuntime.InvokeStatic("Editor.Mcp.SceneTools", "SaveScene");

				var after = new { IsEnabled = navMesh.IsEnabled, IsGenerating = navMesh.IsGenerating, IsDirty = navMesh.IsDirty };
				var verified = after.IsEnabled == enabled;
				if (!verified)
					return McpJson.Write(ToolOutcome<object>.Degraded(new { Before = before, After = after, RequestedEnabled = enabled, Saved = saveScene, Verified = false }, "navmesh.enable_unverified", "The NavMesh property write did not produce the requested state."));

				return McpJson.Write(ToolOutcome<object>.Ready(new { Before = before, After = after, RequestedEnabled = enabled, Saved = saveScene, Verified = true }));
			}
			catch (Exception ex)
			{
				return McpJson.Write(ToolOutcome<object>.Failure(null, ToolStatus.Failed, "navmesh.configure_failed", "NavMesh configuration failed.", ex));
			}
		}
	}
}