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

- URL: https://docs.mentoragent.net/configure/several-tenants
- Verified with: MentorAgent 1.4.0
- Last updated: 2026-11-18
- Packages: Novior.MentorAgent, Novior.MentorAgent.Server

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

## When to choose 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.

## Name the tenant of each request

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

```csharp title="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!);
}
```

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

## Never without a tenant

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.

## What stays apart

| What | With a resolver |
|---|---|
| Memory and the rate limit | The key of the user's memory and of [`RateLimitPerUser`](https://docs.mentoragent.net/reference/options#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 conversations | A 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 dashboard | Every 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 search | A source receives the tenant in `MentorRagRequest.TenantId`: see [Search your documents](https://docs.mentoragent.net/configure/search-your-documents). |
| Logs | Every 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. |

## What stays with your application

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

## Roles, policies and the governor

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](https://docs.mentoragent.net/concepts/governance#your-own-rules-action-policies) or to the [governor](https://docs.mentoragent.net/concepts/governance#turn-actions-on-and-off-from-outside-the-code), which can read `IMentorTenant` from the request's services.

## Common errors

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

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.

### Restoring a conversation answers 400

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

---

- Previous: [Search your documents](https://docs.mentoragent.net/configure/search-your-documents.md)
- Next: [Test without a model](https://docs.mentoragent.net/configure/test-without-model.md)
