Repository navigation
Expand file tree
/
Copy pathLogToConsoleTool.cs
More file actions
53 lines (45 loc) · 2.05 KB
/
Copy pathLogToConsoleTool.cs
File metadata and controls
53 lines (45 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using UnityEngine;
using Unity.AI.MCP.Editor.Helpers;
using Unity.AI.MCP.Editor.ToolRegistry;
// A simple MCP tool that logs a message to the Unity Console.
// Demonstrates: parameter record, [McpTool] attribute, Response helper.
public static class LogToConsoleTool
{
// Tool name uses a dotted prefix for namespacing.
// Unity's SanitizeToolName() converts dots to underscores
// so the client sees "McpToolkit_LogToConsole".
const string ToolName = "McpToolkit.LogToConsole";
// What AI agents read to decide when to use this tool.
const string Description =
"Logs a message to the Unity Console with a specified log type (Log, Warning, or Error).";
// Human-readable label
const string Title = "Log To Console";
// [McpTool] registers this method with the MCP tool registry.
// - EnabledByDefault = true: tool is active for external clients without manual opt-in.
// - Groups: controls which category this tool appears under in Project Settings.
[McpTool(ToolName, Description, Title, EnabledByDefault = true, Groups = new[] { "Demo Example" })]
public static object HandleCommand(LogToConsoleParams parameters)
{
// The method must be public static with 0 or 1 parameter.
// If the signature doesn't match, TypeCache won't discover it.
string message = parameters.Message;
string logType = parameters.LogType ?? "Log";
// Response.Error() returns a structured object.
if (string.IsNullOrEmpty(message))
return Response.Error("MESSAGE_REQUIRED: The message parameter cannot be empty.");
switch (logType)
{
case "Warning":
Debug.LogWarning(message);
break;
case "Error":
Debug.LogError(message);
break;
default:
Debug.Log(message);
break;
}
// Response.Success() returns a structured object with success, message, and data fields.
return Response.Success($"Logged {logType}: {message}");
}
}