MentorAgent Docs
Menu

How-to guidePackage: Novior.MentorAgent

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.

View as Markdown
On this page

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 has no attribute and no reference to MentorAgent. It must be a public class, with public instance methods:

CaseOperations.cs
// 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 class in the services, then each method in options.Actions, with its description and its rules:

Program.cs
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_case and close_case, as for an attribute. Name(…) sets another name, with letters, digits, _ and -, up to 64 characters.
  • ScanAssemblies is still required. Set ScanAssemblies as 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.

Each method of the builder declares what a property of [MentorAction] would:

Builder[MentorAction]Note
Describe(text)DescriptionRequired
Category(name)Category
ReadOnly()ReadOnly = true
RequireConfirmation(bool = true)RequiresConfirmationWhen you do not call it, RequireConfirmationForWrites decides, as for an attribute that does not declare RequiresConfirmation
RequireRoles(roles…)RequiredRolesAny 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.

The rules and the behavior are the same as in the tutorial:

  • get_case and list_cases read, and run with no confirmation box.
  • add_note is offered to the Agents and the Supervisors, and close_case to the Supervisors only. 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.

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 of ScanAssemblies.

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.

  • 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.
  • Roles, confirmation and governance — Role checks and human-in-the-loop approval for AI agent tools in .NET, enforced in code on every channel and not in the prompt, and what stays in your code.
  • 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.