You can extend UnityNaturalMCP by defining your own tools using the MCP C# SDK. This requires three steps:
1. Define the Tool Class
Create an Editor assembly definition (asmdef) and implement your tool class using the [McpServerToolType] and [McpServerTool] attributes.
using System.ComponentModel;
using ModelContextProtocol.Server;
[McpServerToolType, Description("Description of custom MCP tool")]
public class MyCustomMCPTool
{
[McpServerTool, Description("Method description")]
public string MyMethod()
{
return "Hello from Unity!";
}
}
2. Create a Tool Builder
Create a class inheriting from McpBuilderScriptableObject to register your tool class with the server.
using Microsoft.Extensions.DependencyInjection;
using UnityEngine;
using UnityNaturalMCP.Editor;
[CreateAssetMenu(fileName = "MyCustomMCPToolBuilder",
menuName = "UnityNaturalMCP/My Custom Tool Builder")]
public class MyCustomMCPToolBuilder : McpBuilderScriptableObject
{
public override void Build(IMcpServerBuilder builder)
{
builder.WithTools<MyCustomMCPTool>();
}
}
3. Register the Tool in Unity
- In the Unity Project window, right-click and select
Create > UnityNaturalMCP > My Custom Tool Builder. - Go to
Edit > Project Settings > Unity Natural MCP and click Refresh to restart the server and load the new tools.
Best Practices
- Error Handling: Wrap tool logic in
try-catch blocks and use Debug.LogError to ensure errors are visible in Unity logs, as MCP clients may not automatically surface internal exceptions. - Asynchronous Processing: If using Unity APIs, ensure you switch to the main thread using
UniTask.SwitchToMainThread() and use ValueTask<T> as the return type.
[McpServerTool, Description("Example of asynchronous processing")]
public async ValueTask<string> AsyncMethod()
{
await UniTask.SwitchToMainThread();
await UniTask.Delay(1000);
return "Finished async processing";
}