MentorAgent Docs
Menu

Questa pagina non è ancora tradotta in italiano: qui la trovi in inglese.

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

Serve several tenants

Serve several companies from one instance: tell MentorAgent the tenant of each request, and keep memory, limits, conversations, metrics and documents apart.

On this page

Use this page when one instance of your application serves more than one company, for example in a software-as-a-service product. Each company is a tenant. MentorAgent keeps each user apart, but two users of two companies could share the memory of anonymous users, a document search or the dashboard. Tell MentorAgent the tenant of each request, and it keeps each company apart. Your data stays with your application: MentorAgent passes the tenant on, and your services filter on it.

  • Choose it when one instance serves several companies, and a company must never see another's memory, conversations, figures or documents.
  • Leave it out when each company has its own instance, or its own database behind its own deployment. Without a resolver there is one tenant, as before.

Write an IMentorTenantResolver that names the tenant from the request, and register it once. Your actions read the tenant from IMentorTenant:

ClaimTenantResolver.cs
// The tenant is a claim of the signed-in user
public class ClaimTenantResolver : IMentorTenantResolver
{
    // null: the request is refused
    public ValueTask<string?> ResolveAsync(
        MentorTenantRequest request, CancellationToken cancellationToken)
        => ValueTask.FromResult(
            request.User?.FindFirst("tenant_id")?.Value);
}

// An action reads the tenant of the request
public class OrderActions(IMentorTenant tenant, IOrderStore store)
{
    [MentorAction(Description = "Counts the open orders",
        ReadOnly = true)]
    public Task<int> CountOpenOrders() =>
        store.CountOpenAsync(tenant.Id!);
}
Program.cs
builder.Services.AddMentorAgent(options => { /* … */ });
builder.Services.AddMentorTenantResolver<ClaimTenantResolver>();
  • You choose where the tenant comes from. The resolver receives the signed-in user, the channel and the request's services. You can read the tenant from a claim of the user, as in the example, from a host name, or from a header that your gateway sets.
  • One per application. MentorAgent asks the resolver once for every unit of work, before it does anything else for it. A unit of work is a turn that starts in a Blazor circuit, on the hub, on /mentor/chat or in an Agent-to-Agent (A2A) task, and every tools/list and tools/call of the Model Context Protocol (MCP) server.
  • IMentorTenant.Id is the tenant of the current turn, or of the current call to the MCP server. Before the first turn, GetIdAsync() asks the resolver first. Reading Id where no tenant was resolved throws.

MentorAgent refuses the request when the resolver throws, or answers null, an empty text or white space. The turn then ends before the model is called, a tools/call gets an error, a tools/list lists nothing, and an A2A task fails. MentorAgent does not use a shared tenant instead, because that would put the request among the data that every tenant shares. The user reads, in the language of the widget, that the organization could not be identified.

A conversation stays with its tenant. In a circuit or a hub connection, MentorAgent refuses a turn whose tenant differs from the tenant that the conversation began with, and it tells the user to reload the page.

WhatWith a resolver
Memory and the rate limitThe key of the user's memory and of RateLimitPerUser starts with the tenant. The same user in two tenants has two memories and two allowances, and the anonymous bucket is one per tenant. The MCP server's allowance per caller is per tenant too.
Saved conversationsA session snapshot names its tenant, and is restored for that tenant only. A snapshot of another tenant, or with no tenant where a resolver is registered, is refused with 400 on /mentor/session.
Metrics and the dashboardEvery counter and the mentor.turn span carry the mentoragent.tenant tag. The dashboard and GET /mentor/admin/metrics show the reader's tenant only, and answer 403 to a reader without a tenant.
Document searchA source receives the tenant in MentorRagRequest.TenantId: see Search your documents.
LogsEvery log line of a turn, your code's included, is written in a log scope with mentoragent.tenant. A provider that includes scopes shows it.
  • Your data. An action reads IMentorTenant.Id and asks your services for that tenant's data. MentorAgent does not filter what your methods return.
  • The documents of each tenant. MentorAgent filters no document. A source that holds several tenants' documents returns those of TenantId. With the vector store source, set Filter. A source that implements only the older SearchAsync(query, maxResults) never sees the tenant, and a warning says so at its first search with one.
  • The conversation history that you store. The history that MentorAgent keeps is already separate for each conversation. Your ChatHistoryProviderFactory runs after the tenant is known, so read IMentorTenant there and store the history by tenant. A single ChatHistoryProvider instance serves every tenant, and a warning at startup says so.
  • A metrics store. The figures of a tenant are kept in memory since the process started. An IMentorMetricsStore saves the instance's totals; to serve a tenant's figures from your backend, it implements QueryAsync(tenant, …).
  • Budgets. A limit that stops a tenant when it has spent its budget belongs to the service in front of your application.

The tenant is not a role. RequiredRoles still reads the user's roles. A rule per tenant, such as an action that only some companies have, belongs to an action policy or to the governor, which can read IMentorTenant from the request's services.

Every request is refused: the organization could not be identified

Section titled “Every request is refused: the organization could not be identified”

The resolver answered nothing. Check that the claim, the header or the host name that it reads is there on every channel: the hub, /mentor/chat, the MCP server and A2A read the user of the request, after ASP.NET Core authentication.

A conversation is refused: it belongs to another organization

Section titled “A conversation is refused: it belongs to another organization”

The tenant of the turn is not the one that the conversation began with, for example after the user switched company in the same page. Reload the page to start a new conversation.

The snapshot belongs to another tenant, or it was saved without a resolver. A snapshot is restored only for the tenant that saved it.

  • Search your documents — Let the agent answer from your own documents: a source of your own or any vector store, the relevance threshold, citations, failures, and what it costs.
  • Roles from your sign-in — Make the roles of your sign-in reach RequiredRoles: JWT bearer tokens, Microsoft Entra ID app roles and groups, Windows groups, and roles from your database.
  • Production checklist — Settings to review before production: governance, safety check, rate limits, cost and routing, history, several instances, observability and outgoing data.
  • 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.