Class McpToolAttribute
Marks a static method or class as an MCP tool that can be invoked by MCP clients. Tools are automatically discovered at editor startup using Unity's TypeCache system.
Implements
Inherited Members
Namespace: Unity.AI.MCP.Editor.ToolRegistry
Assembly: Unity.AI.MCP.Editor.dll
Syntax
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method, AllowMultiple = false)]
public class McpToolAttribute : Attribute, _Attribute
Remarks
This attribute can be applied to:
- Static methods with 0-1 parameters (method-based tools)
- Classes implementing IUnityMcpTool or IUnityMcpTool<TParams> (class-based tools)
Method-based tools:
- Must be public static
- Can have 0 parameters, 1 typed parameter, or 1 JObject parameter
- Schemas auto-generated for typed parameters
- Use [McpSchema] attribute for custom schemas with JObject parameters
Class-based tools:
- Must have a public parameterless constructor
- Instantiated once at discovery time
- Useful when tools need state or complex initialization
Tool names must be unique across the project. Duplicate names will generate warnings.
Examples
Method-based tool with typed parameters:
public class MyParams
{
public string Name { get; set; }
}
[McpTool("my_tool", "Does something useful")]
public static object MyTool(MyParams params)
{
return new { result = $"Hello " };
}
Class-based tool:
[McpTool("stateful_tool", "Tool with state")]
public class StatefulTool : IUnityMcpTool<MyParams>
{
private int callCount = 0;
public object Execute(MyParams parameters)
{
callCount++;
return new { calls = callCount };
}
}
Constructors
McpToolAttribute(string, string, string, object)
Creates an MCP tool attribute to mark a method or class as a discoverable tool.
Declaration
public McpToolAttribute(string name, string description = null, string title = null, object annotations = null)
Parameters
| Type | Name | Description |
|---|---|---|
| string | name | The unique name of the tool as exposed to MCP clients (use snake_case) |
| string | description | Human-readable description of what the tool does. If null, defaults to the tool name |
| string | title | Optional display title. If null, defaults to the description |
| object | annotations | Optional MCP annotations object describing tool characteristics |
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | Thrown if name is null or empty |
Properties
Annotations
Gets or sets optional MCP annotations describing tool behavior. Annotations can provide hints to clients about tool characteristics (e.g., side effects, cost).
Declaration
public object Annotations { get; set; }
Property Value
| Type | Description |
|---|---|
| object |
Description
Gets the description of what the tool does. This description is sent to MCP clients and may be used by AI models to understand tool purpose.
Declaration
public string Description { get; }
Property Value
| Type | Description |
|---|---|
| string |
EnabledByDefault
Gets or sets whether this tool is enabled by default when no user override exists. Tools with this set to true are part of the curated default set shown as enabled in the settings UI. New or unvalidated tools should leave this as false.
Declaration
public bool EnabledByDefault { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
Groups
Gets or sets category tags for organizing and filtering tools. Use string identifiers from ToolCategory enum (e.g., "scripting", "scene", "assets").
Declaration
public string[] Groups { get; set; }
Property Value
| Type | Description |
|---|---|
| string[] |
Name
Gets the unique name of the tool as exposed to MCP clients. Tool names should use snake_case convention (e.g., "my_tool", "read_file").
Declaration
public string Name { get; }
Property Value
| Type | Description |
|---|---|
| string |
Title
Gets the display title of the tool (optional). If not specified, the Description is used as the title.
Declaration
public string Title { get; }
Property Value
| Type | Description |
|---|---|
| string |