Interface IUnityMcpTool
Interface for implementing class-based MCP tools with flexible parameter handling. Use this when your tool needs state management, complex initialization, or custom schema generation. For simpler tools with strongly-typed parameters, consider using IUnityMcpTool<TParams> instead.
Namespace: Unity.AI.MCP.Editor.ToolRegistry
Assembly: Unity.AI.MCP.Editor.dll
Syntax
public interface IUnityMcpTool
Remarks
Class-based tools are instantiated once at discovery time and reused for all executions. The class must have a parameterless constructor and be decorated with the McpToolAttribute.
Schema generation:
- If GetInputSchema() returns null, a default flexible schema is provided
- If GetOutputSchema() returns null, the system attempts to auto-generate from the return type
Thread safety: Execute may be called from Unity's main thread. Avoid blocking operations.
Examples
[McpTool("custom_tool", "Performs custom operations")]
public class CustomTool : IUnityMcpTool
{
public Task<object> ExecuteAsync(object parameters)
{
var jobject = parameters as JObject;
var action = jobject?["action"]?.Value<string>();
return Task.FromResult<object>(new { success = true, result = $"Executed {action}" });
}
public object GetInputSchema()
{
return new
{
type = "object",
properties = new
{
action = new { type = "string", description = "Action to perform" }
},
required = new[] { "action" }
};
}
}
Methods
ExecuteAsync(object)
Executes the tool asynchronously with the provided parameters from the MCP client.
Declaration
Task<object> ExecuteAsync(object parameters)
Parameters
| Type | Name | Description |
|---|---|---|
| object | parameters | Parameters as JObject from MCP client, or null if no parameters provided |
Returns
| Type | Description |
|---|---|
| Task<object> | A task containing the tool execution result, which will be serialized to JSON. Can be an anonymous object, class instance, or primitive type |
Remarks
Called on Unity's main thread when an MCP client invokes this tool. Parameters are provided as a Newtonsoft.Json.Linq.JObject for flexible access. Return values are automatically serialized to JSON for the MCP response. For synchronous tools, return Task.FromResult(result).
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown exceptions are caught and returned as error responses to the MCP client |
GetInputSchema()
Provides a custom JSON schema for this tool's input parameters.
Declaration
object GetInputSchema()
Returns
| Type | Description |
|---|---|
| object | JSON schema object (typically an anonymous object with type/properties/required fields), or null to use default schema |
Remarks
Optional: Return null to use a default flexible schema that accepts any object properties. The schema should follow JSON Schema specification and describe the expected parameter structure.
GetOutputSchema()
Provides a custom JSON schema for this tool's output structure.
Declaration
object GetOutputSchema()
Returns
| Type | Description |
|---|---|
| object | JSON schema object describing the return value structure, or null for no output schema |
Remarks
Optional: Return null for no output schema. The system will attempt to auto-generate a schema by inspecting the Execute method's return type, but this may not work for all types. Providing an explicit schema improves documentation and client-side typing.