# 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.

- URL: https://docs.mentoragent.net/configure/register-actions-in-code
- Verified with: MentorAgent 1.5.0
- Last updated: 2026-11-27
- Packages: Novior.MentorAgent

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]`](https://docs.mentoragent.net/reference/attributes#mentoraction) attribute would declare. The other ways are compared in [Ways to declare actions](https://docs.mentoragent.net/concepts/ways-to-declare-actions).

The example is the "Cases" app of the [tutorial](https://docs.mentoragent.net/get-started/tutorial/actions), with the roles and confirmations of [its second step](https://docs.mentoragent.net/get-started/tutorial/roles-and-confirmation), written without attributes.

## The class

The class has no attribute and no reference to MentorAgent. It must be a public class, with public instance methods:

```csharp title="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 methods

Register the class in the services, then each method in `options.Actions`, with its description and its rules:

```csharp title="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`](https://docs.mentoragent.net/reference/options#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](https://docs.mentoragent.net/reference/glossary#governor), your [action policies](https://docs.mentoragent.net/reference/action-policies), the Model Context Protocol (MCP) server and the Agent-to-Agent (A2A) card.

## 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`](https://docs.mentoragent.net/reference/options#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

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`](https://docs.mentoragent.net/reference/action-policies#imentoractioncatalog), these actions have the origin `fluent`.

## 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 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](https://docs.mentoragent.net/concepts/action-rules).

---

- Previous: [A2A server](https://docs.mentoragent.net/configure/a2a-server.md)
- Next: [Commands and queries as actions](https://docs.mentoragent.net/configure/commands-and-queries.md)
