MentorAgent Docs
Menu

How-to guidePackages: Novior.MentorAgent, Novior.MentorAgent.Server, Novior.MentorAgent.Blazor

Recipes

Give the agent procedures on your pages, approved outside your code: sources, steps, rows of a grid, limits, and how each recipe is governed.

View as Markdown
On this page

Use this page to give the agent procedures on your pages: fill a form and save it, find a row in a list and open it. A recipe is a sequence of steps on one page that the chat widget runs in the user's browser. The model sees it as one of the UI actions of that page, with a name, a description and parameters. Your code does not hold the recipes. A source that you register gives them to MentorAgent, for example from a console where someone approves them.

  • Choose it when the procedure already exists in your pages, and you want the agent to run it for the user without a change to your code.
  • Use an action instead when the work can run on the server: an action calls your services directly, with no page in between.
  • Use a UI action of the page when the procedure belongs to the code of the page: the page registers it with RegisterUIAction.

A recipe runs only on its own page, while the user has that page open. The model does not open the page to run the recipe. In 1.5.0, navigate_to opens only the pages that carry [MentorPage], so a recipe on a page that only the AutoMentor knows runs only when the user is already there. Recipes do not use the manifest of a scan or the list of pages the agent knows: a recipe has its own selectors.

A recipe clicks what the user could click, with the user's rights, in the user's browser. Three parts of your application decide about it:

WhoDecidesWhere
Your sourceWhich recipes exist, and whether each one was approved: a signature, an approval in a consoleIMentorRecipeSource, below
Your action policiesWhether the recipe is offered and whether a call runs, at every turnThe gate, as for every UI action
The userEach run of a recipe that asks for a confirmationThe confirmation box of the widget

A source gives the recipes of one session. IApprovedRecipes is an interface of your own, which reads the approved recipes where you keep them, for example in a database or a console. ApprovedRecipeSource is the source, and implements IMentorRecipeSource:

IApprovedRecipes.cs
// Where the approved recipes are kept: a console, a database, a signed file
public interface IApprovedRecipes
{
    Task<IReadOnlyList<MentorRecipeDefinition>> ForSessionAsync(
        string? sessionId, CancellationToken ct);
}
ApprovedRecipeSource.cs
public sealed class ApprovedRecipeSource(IApprovedRecipes approved)
    : IMentorRecipeSource
{
    // Asked once, when a session begins: the session keeps what it got
    public async Task<MentorRecipeSourceResult> LoadAsync(
        MentorRecipeSourceContext context, CancellationToken ct = default)
        => new(await approved.ForSessionAsync(context.SessionId, ct), [])
        {
            // A UI action of the page with the same name wins
            Precedence = MentorRecipePrecedence.Code,
        };
}

Register the source in Program.cs, next to AddMentorAgent. The second line registers an observer, which is optional: it tells you when a recipe is left out, as shown below:

Program.cs
services.AddMentorRecipeSource<ApprovedRecipeSource>();
services.AddMentorAgentSourceObserver<RecipeAlerts>();

When a session begins, it asks every source once, in the order of registration, and keeps the recipes that it got. An open conversation does not change when a source changes: the next session sees the change.

A recipe is a MentorRecipeDefinition, which your source reads from where the recipe is approved. To show its fields, this example creates one in code. It fills the form of a new invoice and saves it, for users with the role Billing, after the user confirms:

Recipes.cs
// Fills the form of a new invoice and saves it
public static MentorRecipeDefinition CreateInvoice() => new()
{
    Name = "create_invoice",
    Description = "Creates an invoice for a customer",
    Route = "/invoices/new",
    Parameters =
    [
        new() { Name = "customer", Description = "The customer's name" },
        new()
        {
            Name = "amount",
            Description = "The amount, in euros",
            Type = MentorRecipeParameterType.Number,
        },
    ],
    Steps =
    [
        new() { Kind = MentorRecipeStepKind.Set,
            Selector = "#customer", Value = "{customer}" },
        new() { Kind = MentorRecipeStepKind.Set,
            Selector = "#amount", Value = "{amount}" },
        new() { Kind = MentorRecipeStepKind.Click, Selector = "#save" },
    ],
    Roles = ["Billing"],
    RequiresConfirmation = true,
};
  • Name and route. A recipe is known by its route and its name, so two pages may each have a recipe of the same name. The route is written as the router writes it, such as /invoices/{id:int}, with no query and no fragment.
  • What the model reads. The model reads the name, the description and the parameters of the recipe, with their types and descriptions. It never reads a selector, or a value that the recipe writes itself.
  • Parameters. The value of a step is either a fixed text, or one parameter written as {name}. The parameter must be the whole value. To write a brace in a fixed text, write it twice. MentorAgent checks the value that the model gives against the type of the parameter (text, number, or true or false) before anything runs.
  • Roles and confirmation. A user who has none of the Roles is refused, and nothing runs. With RequiresConfirmation, the recipe waits for the user's yes, and the confirmation box shows the description of the recipe and the values that the model gave. MentorAgent checks the roles again after the yes.
KindWhat it does
SetWrites the value into a field, as typing would, so that @bind, React's controlled fields and Angular's forms receive it
ChooseChooses the option whose label is the value, in a select or an ARIA combobox
CheckChecks a check box or a radio button, or unchecks it with the value false
ClickClicks the element
WaitWaits for the element to appear, up to 10 seconds
RowFinds a row by the text of its key cell, then clicks inside it

Between two steps, the runner waits until the page stops changing, for 5 seconds at most. The browser answers that the recipe was done, or which step was not done and why. It never sends a value of the page.

A Row step finds the row whose key cell reads the value that the model gave. The comparison happens in the browser, so no text of a row leaves it:

Recipes.cs
// Finds an invoice in the list by its number, and opens it
public static MentorRecipeDefinition OpenInvoice() => new()
{
    Name = "open_invoice",
    Description = "Opens an invoice from the list, by its number",
    Route = "/invoices",
    Parameters =
    [
        new() { Name = "number", Description = "The invoice number" },
    ],
    Steps =
    [
        new()
        {
            Kind = MentorRecipeStepKind.Row,
            Selector = "#invoices tbody tr",   // the rows
            Cell = "td:nth-child(1)",          // the key cell of a row
            Value = "{number}",                // the key to find
            Filter = "#invoice-filter",        // the page's own filter
            Target = "a.open",                 // what to click in the row
        },
    ],
};
  • The page's filter first. With Filter, a row that is not in the page is searched with the page's own filter field. The step writes the key there, and waits up to 5 seconds for the page to answer, also when the page filters on its server.
  • A grid that renders as it scrolls. If the row is still missing, the step scrolls the rows' container from the top, a page at a time, so that a virtualized grid, such as a QuickGrid with Virtualize, renders the rows further down. Give the grid's table a class or an id of your own for the selector, for example with the Class parameter of QuickGrid.
  • Then it clicks Target inside the row. Without Target, the row is only brought into view.
  • Rows left out. Nothing inside data-mentor-exclude is read. A key that only excluded rows hold is not found.

If no row has the key after the last row, the step is not done. Its reason is no-row, and the model reads it.

MentorAgent checks each recipe of a source against MentorRecipeLimits:

LimitValue
Name of a recipe or a parameter1 to 64 letters, digits, _ and -
Steps1 to 50
Parameters10 at most, each with a name of its own
Roles20 at most, of 256 characters each
Description1,024 characters
Selector, cell, target, filter, value1,024 characters each
Route256 characters

The steps that the browser receives, with the model's values in them, are 16 KB at most. If the model's values make them larger, the model reads an error, and nothing reaches the browser.

MentorAgent leaves out a recipe that breaks a rule, and always says why, as the rule recipe.rejected-with-reason asks. It writes a warning in the log, and sends a report to every IMentorAgentSourceObserver. The report comes every time a session asks a source, also when the source gave nothing:

RecipeAlerts.cs
// Tells whoever approves the recipes which ones were left out, and why
public sealed class RecipeAlerts(ILogger<RecipeAlerts> logger)
    : IMentorAgentSourceObserver
{
    public void OnAgentsLoaded(MentorAgentSourceReport report) { }

    // Called before the session's first answer: do not wait here
    public void OnRecipesLoaded(MentorRecipeSourceReport report)
    {
        foreach (var rejected in report.Rejected)
            logger.LogWarning("Recipe {Route} {Name} left out: {Reason}",
                rejected.Route, rejected.Name, rejected.ReasonCode);
    }
}
CodeWhy
invalid-nameThe name is not 1 to 64 letters, digits, _ and -
invalid-definitionA description, a selector, a row's key cell or key, or a parameter's description is missing
invalid-routeThe route does not start with /, or has a query or a fragment
invalid-stepsNo step, more than 50, or a kind that MentorAgent does not know
invalid-parametersMore than 10 parameters, two with one name, a name that is not valid, or a type that is not known
unknown-parameterA step names, between braces, something that is not one of the recipe's parameters
invalid-valueA step's text has a single brace
too-longA field is longer than its limit, or there are too many roles
duplicateA recipe of the same route and name was given before, by this source or another: the first stays
source-failedThe whole source threw. The report has the exception's type only, and the other sources are still asked

A source can also leave out recipes itself, in MentorRecipeSourceResult.Rejected, with these codes or with codes of its own.

When a recipe has the name of a UI action that the page registers in code, the source says which one the model gets, with Precedence:

  • MentorRecipePrecedence.Code, the default: the page's UI action is offered, and the recipe is not.
  • MentorRecipePrecedence.Source: the recipe is offered, and the UI action is not.

Any other tool always has priority over a recipe: an action of your application, a tool of a Model Context Protocol (MCP) server, or one of MentorAgent's own tools. The model is never offered two tools with one name, and the log says once for the session which one lost.

A step stops the recipe when its element:

  • is not in the page;
  • is hidden or disabled;
  • is inside data-mentor-exclude;
  • is not the kind of element that the step acts on;
  • has no option with the label, for a Choose step.

The runner never looks for a similar element. Then:

  • the model reads that the recipe was not done, at which step and why;
  • the recipe is turned off, in memory until the application restarts, and is no longer offered;
  • your action observers receive Failed with element-missing, and the step and the reason in Detail;
  • the log says so once, with the recipe, the step and the reason, and no value.

The recipe stays off only in that version. A recipe with the same route and name is a new version when anything changes: a step, a selector, a parameter, a role, the confirmation or the description. A new version is on. Correct the recipe where you approve it, and the next sessions get the new version.

A recipe is governed as a UI action of its page, so your action policies decide about it like any other action:

  • they are asked when the recipe is offered and when it is called, with Kind UI, the page's name and the recipe's name, its roles and its confirmation;
  • a policy that does not offer it keeps it out of the turn, and a call to it is still refused with not-offered;
  • a policy that refuses the call stops it with not-authorized, and nothing runs;
  • a confirmation that a policy adds makes it wait for the user's yes.

A policy that refuses every action, such as one that turns the AI off, stops every recipe: none is offered and none runs.

  • Blazor Server and .NET MAUI Blazor Hybrid. The steps run through the JavaScript runtime of the circuit, or of the app's web view.
  • Blazor WebAssembly. Before each message, the client of Novior.MentorAgent.Blazor sends the path of the page shown, without its query or fragment, and the hub offers the recipes of that page. A client that sends no path is offered none.
    • The hub asks the client to run a recipe with the event RecipeRequested, and the client answers with POST /mentor/recipe-result. Only the user who owns the connection may answer.
    • A browser that does not answer within 2 minutes ends the call with an error, and the recipe stays on.
  • React, Angular and other front ends. Novior.MentorAgent.Server serves /mentor/agent.js, a module with the same runner, which a front end ties to its own hub connection: see Run recipes in your front end.

In every case, the model waits for the browser's answer before it goes on.

The rules of recipes are the recipe area of ACTION-RULES.md in the MentorAgent repository (external site). Each rule has a name that never changes, and a test in the library.

  • 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.
  • Action policies and observers — The types that let your code decide on each action, record every decision of the gate, know who confirmed a call, and list the actions.
  • Pages the agent knows — With the AutoMentor on, MentorAgent knows the pages of your Blazor app from their routes, and what a scan or your users saw of each one.
  • MentorAgent 1.5.0 release notes — What's new in MentorAgent 1.5.0: pages known from your routes, a scan of the pages, recipes, forms and grids as UI actions, and the cost per agent and user.