Creating Custom Tools
You can extend the MCP Server with your own tools by adding a simple attribute to any C# method. No Python changes, no server restart, no tool registration — just recompile in Unity and the AI agent sees your new tool.
Basic Example
using realvirtual.MCP;
public static class MyTools
{
[McpTool("Get current time")]
public static string GetTime()
{
return $"{{\"time\":\"{System.DateTime.Now}\"}}";
}
[McpTool("Add two numbers")]
public static string Add(
[McpParam("First number")] float a,
[McpParam("Second number")] float b)
{
return $"{{\"result\":{a + b}}}";
}
}That's it. After recompilation, get_time and add are available to any connected AI agent.
Attributes
McpTool
Add [McpTool("description")] to any public static method that returns a string. The description tells the AI agent what the tool does.
McpParam
Add [McpParam("description")] to parameters to give the AI agent context about what values to provide. Optional parameters need default values.
Rules
Method must be
public staticand returnstring(JSON)Tool names auto-convert from PascalCase to snake_case (
SpawnEnemy→spawn_enemy)Methods can be in any class, in any assembly — they are found via reflection
All tool methods run on Unity's main thread
Return JSON strings — use
ToolHelpers.Ok()andToolHelpers.Error()for standard responses
Helper Utilities
The ToolHelpers class provides common patterns:
Example: Custom Sensor Tool
After recompiling, the AI agent can call get_temperatures and receive structured data about all temperature sensors in the scene.
Last updated