Questa pagina non è ancora tradotta in italiano: qui la trovi in inglese.
Register actions in code
Make the methods of a class actions from Program.cs, without attributes: options.Actions, the builder, roles and confirmation, the mistakes caught at startup.
In questa pagina
Use this page when you cannot put MentorAgent's attributes on the class that holds your methods: for example, the class comes from a package, or the team that owns it does not want MentorAgent in it. You register each method in Program.cs, with what the [MentorAction] attribute would declare. The other ways are compared in Ways to declare actions.
The example is the "Cases" app of the tutorial, with the roles and confirmations of its second step, written without attributes.
The class
Section titled “The class”The class has no attribute and no reference to MentorAgent. It must be a public class, with public instance methods:
// The tutorial's actions, with no MentorAgent attribute on the class
public class CaseOperations(CaseStore store)
{
public SupportCase? GetCase(string caseNumber) => store.Find(caseNumber);
public IReadOnlyList<SupportCase> ListCases(string? status = null)
=> store.List(status);
public string AddNote(string caseNumber, string text)
{
store.AddNote(caseNumber, text);
return $"Note added to case {caseNumber}.";
}
public string CloseCase(string caseNumber, string resolution)
{
store.Close(caseNumber, resolution);
return $"Case {caseNumber} closed.";
}
}Register the methods
Section titled “Register the methods”Register the class in the services, then each method in options.Actions, with its description and its rules:
builder.Services.AddSingleton<CaseStore>();
builder.Services.AddScoped<CaseOperations>();
builder.Services.AddMentorAgent(options =>
{
options.ScanAssemblies = [typeof(Program).Assembly];
options.Actions
.Add<CaseOperations>(nameof(CaseOperations.GetCase),
action => action
.Describe("Gets a support case: its status, "
+ "priority and notes")
.ReadOnly())
.Add<CaseOperations>(nameof(CaseOperations.ListCases),
action => action
.Describe("Lists support cases, optionally by status")
.ReadOnly())
.Add<CaseOperations>(nameof(CaseOperations.AddNote),
action => action
.Describe("Adds a note to a support case")
.RequireRoles("Agents", "Supervisors")
.RequireConfirmation())
.Add<CaseOperations>(nameof(CaseOperations.CloseCase),
action => action
.Describe("Closes a support case with a resolution")
.RequireRoles("Supervisors")
.RequireConfirmation());
});- The name is the name of the method in snake case, here
get_caseandclose_case, as for an attribute.Name(…)sets another name, with letters, digits,_and-, up to 64 characters. ScanAssembliesis still required. SetScanAssembliesas in any application: the registrations do not replace it. MentorAgent reads the registrations once, when the application starts.- A registered action is like a
[MentorAction]method. The same rules apply to it as to any other action: its roles, its confirmation, the default confirmation for writes, the governor, your action policies, the Model Context Protocol (MCP) server and the Agent-to-Agent (A2A) card.
The builder
Section titled “The builder”Each method of the builder declares what a property of [MentorAction] would:
| Builder | [MentorAction] | Note |
|---|---|---|
Describe(text) | Description | Required |
Category(name) | Category | |
ReadOnly() | ReadOnly = true | |
RequireConfirmation(bool = true) | RequiresConfirmation | When you do not call it, RequireConfirmationForWrites decides, as for an attribute that does not declare RequiresConfirmation |
RequireRoles(roles…) | RequiredRoles | Any one of the roles is enough. A second call replaces the first |
Off() | Enabled = false | |
NavigateTo(url) | NavigateTo | |
Hint(text) | ProactiveHint | |
Name(name) | — | The name that the model calls the action by |
Add(Type, string, …) registers a method in the same way when the type is known only at run time, for example in generated code.
What the user sees
Section titled “What the user sees”The rules and the behavior are the same as in the tutorial:
get_caseandlist_casesread, and run with no confirmation box.add_noteis offered to theAgentsand theSupervisors, andclose_caseto theSupervisorsonly. A user without one of the roles is not offered the action, and a call to it is refused before any confirmation.- Both ask the user to confirm before they run.
In IMentorActionCatalog, these actions have the origin fluent.
Mistakes caught at startup
Section titled “Mistakes caught at startup”A mistake stops the application when it starts, with a message that names the method:
- a method that does not exist, or more than one public method with that name;
- a static or generic method, or an abstract class;
- a registration without
Describe; - the same method registered twice;
- a method that is already an action through
[MentorAction]or[Description], in an assembly ofScanAssemblies.
Keep the attribute or the registration, not both. An attribute in an assembly that ScanAssemblies does not list does not count.
The rules are the fluent area of ACTION-RULES.md: see Action rules.
Vedi anche
Sezione “Vedi anche”- Ways to declare actions — Attributes, registration in code, commands and queries, endpoints: the ways to make your code an action, when to choose each, and what they all share.
- 2. Require roles and confirmation — Tutorial step 2: restrict actions to roles and ask the user to confirm before the agent changes data, then try both cases.
- Ruoli, conferma e governance — Ruoli e approvazione human-in-the-loop per i tool di un agente AI in .NET: controlli nel codice, non nel prompt, su ogni canale, e cosa resta a te.
- Configuration options — Every MentorOptions property with its type, default and effect, grouped by area, plus the options of the WebAssembly client and the YAML loader.
