Multi-Tenancy

Serving several customers from one process, with limits that hold and data that does not leak between them. Three parts, and they are independent: who the caller is, what they may consume, and where their tasks are stored.

Getting the first one wrong makes the other two decorative, so it comes first.

Tenant identity has to come from somewhere trustworthy

Every limit and every partition is keyed on the tenant id the handler resolved. A caller who can choose their own tenant id can choose their own limits and read another tenant's partition. That is not a subtle failure — it is the whole of multi-tenancy, decided by one function.

Three resolvers ship:

ResolverReadsTrustworthy when
BearerTokenTenantResolver::with_mapperwhat your closure returns for the bearer tokenthe closure verifies the token (signature, expiry) and returns its tenant claim — this is the one to reach for
HeaderTenantResolvera request headera proxy you control sets it and strips any inbound copy
PathSegmentTenantResolvera URL path segmentthe same, and never on its own

BearerTokenTenantResolver::default() (and the deprecated new()) names the unverified token by a digest, tenant_id_for_token(token), so the caller still chooses its own tenant, but the token itself is no longer stored, exported or audited as the tenant id; it is for tests. Until 0.16 the tenant was the raw token (upgrading).

A resolver does nothing until you install it with RequestHandlerBuilder::with_tenant_resolver. Without one the handler uses the tenant the client names, verbatim, so any caller can name any tenant. With one, the resolved tenant is authoritative: a client that names a different tenant is refused with InvalidParams. A request the resolver cannot place (None) falls into the shared default partition unless you also call require_resolved_tenant(), which refuses it; turn it on whenever every request should belong to a tenant.

PathSegmentTenantResolver reads the request URL on the JSON-RPC, HTTP+JSON and WebSocket dispatchers. The gRPC dispatcher and the Axum adapter give it no URL, so there it resolves no tenant. Before 0.14.1 it fell back to a path request header, which any caller could set (GHSA-hr9h-6jvf-wvg6); upgrade.

A header or a path segment is caller-supplied input. It is a legitimate design when something in front of the agent authenticates the caller and rewrites the header — and a vulnerability the moment that is not true, because curl -H 'X-Tenant-Id: someone-else' is the entire attack (with the default header name).

Write your own TenantResolver when identity comes from somewhere else; the trait exists for exactly that.

What a tenant may consume

PerTenantConfig is a default TenantLimits plus per-tenant overrides. Hand it to RequestHandlerBuilder::with_tenant_config:

#![allow(unused)]
fn main() {
use std::time::Duration;
use a2a_protocol_server::tenant_config::{PerTenantConfig, TenantLimits};

let config = PerTenantConfig::builder()
    .default_limits(TenantLimits::builder()
        .with_max_concurrent_tasks(100)
        .with_rate_limit_rps(50)
        .build())
    .with_override("premium-corp", TenantLimits::builder()
        .with_max_concurrent_tasks(1000)
        .with_executor_timeout(Duration::from_secs(120))
        .with_rate_limit_rps(500)
        .build())
    .build();

assert_eq!(config.get("premium-corp").max_concurrent_tasks, Some(1000));
assert_eq!(config.get("unknown").max_concurrent_tasks, Some(100));
}
LimitEnforcedOn exceeding
max_concurrent_tasksper-tenant semaphore in each replica; the permit is taken before any side effectServerError::Overloaded
executor_timeoutresolved before the executor is spawnedthe task fails, as with the handler-wide timeout
event_queue_capacityat queue creationthe stream buffer is that deep
rate_limit_rpsRateLimitInterceptor — opt-inthe request is refused

The one that can be set and do nothing

Three of the four the handler applies by itself. rate_limit_rps is counted in an interceptor, so it needs wiring:

#![allow(unused)]
fn main() {
use a2a_protocol_server::{PerTenantConfig, RateLimitInterceptor};
use a2a_protocol_server::rate_limit::RateLimitConfig;
fn wire(config: PerTenantConfig) -> Result<RateLimitInterceptor, Box<dyn std::error::Error>> {
let limiter = RateLimitInterceptor::new(RateLimitConfig::default())?
    .with_tenant_config(config);
Ok(limiter)
}
}

Without that call the other three still apply and rate_limit_rps silently does nothing. This is the only field in the design that can be set and not take effect, which is why it is named here rather than left to be discovered in production.

Refused, not queued

A tenant at max_concurrent_tasks is refused. Queueing would convert a declared bound into unbounded latency and unbounded memory — the limit would stop meaning anything under exactly the load it exists for. A queue in front of the shared capacity, bounded in length and in wait, is the admission gate below; a tenant's own max_concurrent_tasks stays refuse-only.

max_concurrent_tasks is per replica

The semaphore is in the handler, so it is one process's. Behind a load balancer, N replicas admit up to N times the limit for a tenant, and one replica can refuse a tenant while another has room. The limit protects each replica; it does not protect the fleet or a downstream quota such as a model's rate limit. Either set it to the fleet limit divided by the replica count (and set it again when the autoscaler changes the count), or enforce the fleet limit in the proxy or gateway in front, where every request passes. Counting across replicas, in the database next to the turn claims, is not built.

A send takes its slot before it waits for the task's turn claim, which another replica may hold for up to executor_drain_timeout (5 s by default), so a tenant whose sends wait on claims can have its slots held by sends that are waiting and not yet running.

Limits bound use; they do not reserve capacity

Process-wide resources — the event-queue manager's max_concurrent_queues, handler sweep thresholds, the tenant-partition cap on the store wrappers — are shared pools. A tenant running inside its own limit can still exhaust one and cause another tenant's requests to be refused as overloaded.

Size per-tenant max_concurrent_tasks so the sum across active tenants stays within the process-wide caps, or put an admission gate in front of them. Data isolation does not depend on any of this, and is not affected by it.

Reserving capacity, queueing, and priority: the admission gate

RequestHandlerBuilder::with_admission(Admission) puts one gate in front of the send path (audit H9, ADR 0045). It is off by default, and everything it decides comes from the server's configuration, never from the request:

Admission::new(256)                       // turns running at once through the gate
    .with_max_queued(1024)                // sends that may wait, across tenants
    .with_max_wait(Duration::from_millis(500))
    .with_tenant("premium",
        TenantShare::new().with_reserved(32).with_priority(1).with_weight(4))
  • Capacity. At most capacity turns run through the gate. The build refuses 0, and a capacity above with_max_concurrent_streams, which would refuse what the gate admitted.
  • Reservations. with_reserved(n) keeps n slots for that tenant alone; the rest are shared. The build refuses reservations past the capacity, and a default share that reserves anything.
  • A bounded wait. A send with no slot waits if the queue has room, for at most max_wait or what remains of its budget, whichever is shorter. A full queue or a wait that reaches max_wait is refused Overloaded (503 with Retry-After, as before); a wait the budget cut short is refused as a spent budget is (invalid params), since retrying cannot help that send. The defaults wait for nothing.
  • Priority and weights. A freed slot goes to the highest priority with a send waiting that may take it; within a priority, tenants take turns, weight sends each per round, oldest first within a tenant. The tenant is the one the TenantResolver resolved, so priority follows what the server decided about the caller. A higher class can starve a lower one under sustained load; that is what a class means.

A queued send holds its tenant's max_concurrent_tasks slot while it waits, so a tenant's waiters are bounded by its own limit. The gate is per replica, like every limit here. RequestHandler::admission_load() returns what it holds, and the load signal reports it.

Where their tasks are stored

Tenant-aware wrappers partition by tenant id, for both task and push-config stores:

tasks          TenantAwareInMemoryTaskStore
               TenantAwareSqliteTaskStore          (feature: sqlite)
               TenantAwarePostgresTaskStore        (feature: postgres)

push configs   TenantAwareInMemoryPushConfigStore
               TenantAwareSqlitePushConfigStore    (feature: sqlite)
               TenantAwarePostgresPushConfigStore  (feature: postgres)

A task written under one tenant is not readable, listable or cancellable under another. That holds regardless of the limits above being set.

A store that cannot isolate refuses tenants

The default stores (InMemoryTaskStore, SqliteTaskStore, PostgresTaskStore and their push-config counterparts) do not partition by tenant. A request that resolves to a non-empty tenant — named by the client or derived by a resolver — is therefore refused with UnsupportedOperation (-32004 on JSON-RPC, 400 on HTTP+JSON) unless both the task store and the push-config store are tenant-aware. Requests without a tenant are unaffected.

Before this, such a request was served from the shared records, so tenant B could get, list, subscribe to and cancel tenant A's tasks. Configuring a tenant-aware task store is enough: an unset push-config store then defaults to TenantAwareInMemoryPushConfigStore. A store you write yourself declares isolation by returning true from TaskStore::isolates_tenants (and PushConfigStore::isolates_tenants).

Where tenants key only limits over records every caller is meant to share, RequestHandlerBuilder::accept_unisolated_tenants() serves them anyway. That is an explicit statement that tenants share data, not a default.

The fifth limit, and why it is not on TenantLimits

A per-tenant cap on stored tasks lives on the store — TenantAwareInMemoryTaskStore::with_tenant_override, which gives a named tenant its own TaskStoreConfig and so its own max_capacity, task_ttl, eviction_interval and max_page_size.

It has to live there for a structural reason: a store is constructed independently and handed to the builder, so it never sees a PerTenantConfig. TenantLimits::max_stored_tasks was the field that tried to be this limit from the config side and that nothing could ever read. It was deprecated in 0.10.0 and removed in 0.15.0.

A total across every tenant

Each partition's max_capacity bounds that tenant, so the default store's bound overall is max_tenants × max_capacity: ten million tasks at the defaults. TenantStoreConfig::with_max_total_tasks(n) (since 0.15) caps the sum. A write that would add a new task at the total first evicts the writer's own partition's oldest finished task; when that partition holds none, the write is refused as temporary (HTTP 503, gRPC UNAVAILABLE). A write to a task already held is never refused, another tenant's tasks are never evicted to make room, and a running task never is. (With TaskStoreConfig::with_parked_task_ttl set, a task parked longer than that counts as finished for this too.) TenantAwareInMemoryTaskStore::total_tasks() reports the count. The SQL tenant stores have no total; size their tables with the database.

#![allow(unused)]
fn main() {
use a2a_protocol_server::{TenantAwareInMemoryTaskStore, TenantStoreConfig};

let store = TenantAwareInMemoryTaskStore::with_config(
    TenantStoreConfig::default().with_max_total_tasks(100_000),
);
assert_eq!(store.total_tasks(), Some(0));
}

Callers within one tenant

A tenant is a boundary between organisations, not between the people in one. Spec §13.1 requires results scoped to the caller's authorization boundaries, so a handler with an authenticating interceptor gives each caller it identifies a partition of its own by default (since 0.15; before it, every caller in a tenant saw the same tasks, and Bob could read, list, cancel and subscribe to Alice's, and register a webhook on them):

  • The partition is chosen by the identity the authenticating interceptor sets (BearerTokenAuthInterceptor::with_labelled_tokens as bearer:<label>, ApiKeyAuthInterceptor::with_labelled_keys as api-key:<label>, the JWT interceptor as jwt:<issuer>:<sub>, since 0.15, so one sub from two issuers is two callers; MutualTlsAuthInterceptor as mtls:<name>, by default the certificate's SHA-256 fingerprint, mtls:sha256:<hex>; a custom interceptor calls CallContext::set_caller_identity). Another caller's task is TaskNotFound, the same answer as an id that does not exist, so its existence is not revealed, and ListTasks returns only the caller's own.
  • A request the interceptor lets through without naming anyone (an unlabelled token or key) keeps the tenant's shared records, as before. A JWT without sub is refused with PermissionDenied instead, since 0.15. Those records are never an identified caller's, and an identified caller's are never theirs.
  • The halt switch, the tenant limits above, audit chains and metrics still see one tenant. Each caller's partition counts against the store's tenant limit: the in-memory store refuses new partitions past max_tenants (1,000 by default) until old ones empty; the SQL tenant stores have no such limit. Each partition also gets the whole per-tenant capacity (10,000 tasks by default, or what with_task_store_config sets), so the default in-memory store holds up to 1,000 times that. To bound it otherwise, supply a TenantAwareInMemoryTaskStore with your own TenantStoreConfig, or use a SQL store.
  • Isolation needs stores that partition by tenant. Left unset they default to the tenant-aware in-memory stores; build() refuses a store you supplied that does not partition, naming the two ways out.
  • isolate_tasks_by_caller() says it explicitly and requires every caller to be identified: a request with no identity is refused, and build() refuses a chain with no authenticating interceptor.
  • An approval gate that wants an approver other than the requester cannot work while tasks are isolated, since the approver could never reach the requester's task; build() refuses the pair and names the two ways out (share tasks, or allow self-approval).
  • Callers who are meant to share tasks call share_tasks_across_callers(). Where the sharing group is known (a team, a project), make the group the tenant instead, with a resolver that reads it from the verified identity, so other groups stay out.
  • A handler with no authenticating interceptor has no identities to keep apart and shares, as before.

Erasing a caller

Spec §13.4 says agents SHOULD let users ask for their data to be deleted. With callers isolated, a caller's data is exactly its partitions, so every tenant-aware store has erase_caller(identity): it removes that caller's partitions in every tenant, with their tasks, logged events and idempotency keys (task stores) or webhooks (push-config stores), in one transaction on the SQL stores, and reports how many partitions and rows went. The identity is the one the interceptor set, such as bearer:alice or jwt:<issuer>:<sub> (auth::caller_id builds them). Keep a handle to each store (with_task_store_arc, with_push_config_store_arc) to call it.

#![allow(unused)]
fn main() {
async fn run() {
use std::sync::Arc;
use a2a_protocol_server::push::TenantAwareInMemoryPushConfigStore;
use a2a_protocol_server::store::TenantAwareInMemoryTaskStore;

let tasks = Arc::new(TenantAwareInMemoryTaskStore::new());
let webhooks = Arc::new(TenantAwareInMemoryPushConfigStore::new());
// ... builder.with_task_store_arc(tasks.clone()).with_push_config_store_arc(webhooks.clone())

let caller = a2a_protocol_server::auth::caller_id::bearer("alice");
let removed = tasks.erase_caller(&caller).await;
let _ = webhooks.erase_caller(&caller).await;
println!("{} partitions, {} rows", removed.partitions, removed.rows);
}
}

It does not reach records the caller left in the tenant's shared partition (an unlabelled credential, or share_tasks_across_callers), which name no one; audit records, which AuditLog::erase_subject handles; or copies in backups and logs.

The strict profile

Profile::MultiTenant turns the choices above into refusals at build(), so a handler that serves several tenants cannot be assembled with one of them left at its permissive default:

#![allow(unused)]
fn main() {
use a2a_protocol_server::profile::Profile;
use a2a_protocol_server::tenant_config::{PerTenantConfig, TenantLimits};
use a2a_protocol_server::tenant_resolver::HeaderTenantResolver;
use a2a_protocol_server::{BearerTokenAuthInterceptor, RequestHandlerBuilder, agent_executor};

struct Agent;
agent_executor!(Agent, |_ctx, _queue| async { Ok(()) });

fn handler() -> Result<a2a_protocol_server::RequestHandler, a2a_protocol_server::error::ServerError> {
    RequestHandlerBuilder::new(Agent)
        .without_agent_card()
        .with_profile(Profile::MultiTenant)
        .with_interceptor(BearerTokenAuthInterceptor::with_labelled_tokens([("token", "alice")]))
        // A gateway sets this header from the authenticated identity and strips
        // it from client traffic. Saying so here is the assertion the profile asks for.
        .with_authorized_tenant_resolver(HeaderTenantResolver::default())
        .with_tenant_config(
            PerTenantConfig::default()
                .with_default(TenantLimits::default().with_max_concurrent_tasks(Some(8))),
        )
        .build()
}
}

It applies two things: every caller is named (isolate_tasks_by_caller(): a credential that names nobody is refused, not kept in the tenant's shared records), unless you called share_tasks_across_callers(), which it leaves as your choice; and a request no tenant can be found for is refused (require_resolved_tenant()), not served from the shared default partition.

build() then refuses, naming the fix, unless:

  • an interceptor authenticates;
  • the resolver was given with with_authorized_tenant_resolver. A resolver given with with_tenant_resolver alone is refused: a tenant the client chose isolates nothing. The assertion is yours. Nothing in the SDK can check that a gateway strips a header or that a token's signature was verified before its tenant claim was read; the profile makes you state it;
  • the task and push-config stores partition by tenant, and accept_unisolated_tenants() was not called;
  • if you chose to share tasks across callers, you supplied a tenant-aware task store (the default for a sharing handler keeps one set of records for every tenant);
  • every tenant, the default and each override, has a concurrency limit (with_max_concurrent_tasks). The limit is per replica; see above.

What it leaves to you: whether the resolver is in fact authorized, retention and erasure of what the stores hold, a tool-using agent's own strictness (ToolLoop::strict, ArgumentCheck, AuditedPolicy), and, for an approver who is not the requester, ApprovalGate::with_review_grants() (Human oversight).

What is not isolated

  • CPU and memory are shared. A tenant inside its task limit can still run expensive executors. executor_timeout bounds duration, not cost.
  • The process is shared. A panic in one tenant's executor is caught, but memory exhaustion is not something the limits above prevent.
  • There is no per-tenant metric dimension. A tenant hitting its ceiling shows up as Overloaded in the aggregate — see Observability.
  • Agent cards are not per-tenant. One process serves one card.

See also Security for the identity half of this, and Running More Than One Replica for the store choices when partitions must be shared across processes.