Interface IUnityMcpTool<TParams>
Generic interface for class-based MCP tools with strongly-typed parameters. Recommended for most tools as it provides automatic schema generation and type safety.
Namespace: Unity.AI.MCP.Editor.ToolRegistry
Assembly: Unity.AI.MCP.Editor.dll
Syntax
public interface IUnityMcpTool<TParams> where TParams : class
Type Parameters
| Name | Description |
|---|---|
| TParams | The strongly-typed parameter class or record. Must be a reference type with a parameterless constructor |
Remarks
This interface automatically generates input schemas from the TParams type,
eliminating the need for manual schema definition and parameter casting.
The parameter type should be a class or record with public properties decorated with McpDescriptionAttribute for better schema documentation.
Schema generation supports:
- Primitive types (int, string, bool, etc.)
- Enums (converted to string constraints)
- Collections (arrays and lists)
- Nested objects
- Nullable types
- Default values from property initializers
The class must have a parameterless constructor and be decorated with McpToolAttribute.
Examples
public class GreetParams
{
[McpDescription("Name of person to greet", Required = true)]
public string Name { get; set; }
[McpDescription("Greeting style")]
public string Style { get; set; } = "formal";
}
[McpTool("greet_person", "Greets a person with a message")]
public class GreetTool : IUnityMcpTool<GreetParams>
{
public Task<object> ExecuteAsync(GreetParams parameters)
{
var greeting = parameters.Style == "formal"
? $"Good day, {parameters.Name}"
: $"Hey {parameters.Name}!";
return Task.FromResult<object>(new { message = greeting });
}
}
Methods
ExecuteAsync(TParams)
Executes the tool asynchronously with strongly-typed parameters deserialized from the MCP client request.
Declaration
Task<object> ExecuteAsync(TParams parameters)
Parameters
| Type | Name | Description |
|---|---|---|
| TParams | parameters | Strongly-typed parameters deserialized from the MCP client request |
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. The parameters are automatically deserialized from JSON using Newtonsoft.Json with enum string conversion enabled. 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 |