# MentorHubScopes class

> The MentorHubScopes class of Novior.MentorAgent.Server.Hubs: Keeps ONE DI scope per SignalR connection, so the MentorAgent coordinator (MCP connections…

- URL: https://docs.mentoragent.net/api/novior-mentoragent-server-hubs/mentorhubscopes
- Verified with: MentorAgent 1.2.0
- Last updated: 2026-10-27
- Packages: Novior.MentorAgent.Server

Keeps ONE DI scope per SignalR connection, so the MentorAgent coordinator (MCP connections, skills, discovery, agent) is built once per connection and reused for every message — mirroring Blazor Server's "one scope per circuit" model. Without this, SignalR's default "one scope per hub method call" would, on every message: - rebuild the coordinator and reconnect the external MCP servers (spawning a process each time), - and start a brand-new `AgentSession`, so the conversation history never persisted. Singleton. Each connection's scope is disposed when the connection ends.

## Definition

Namespace: `Novior.MentorAgent.Server.Hubs`. Package: `Novior.MentorAgent.Server`.

`public sealed class MentorHubScopes : IAsyncDisposable`

```shell
dotnet add package Novior.MentorAgent.Server
```

## Constructors

`public MentorHubScopes(IServiceScopeFactory scopeFactory)`

## Methods

### `DisposeAsync`

`public ValueTask DisposeAsync()`

### `GetOrCreate`

`public IServiceProvider GetOrCreate(string connectionId)`

Returns the service provider of this connection's scope, creating it on first use.

### `RemoveAsync`

`public ValueTask RemoveAsync(string connectionId)`

Disposes this connection's scope (closes MCP connections, disposes the orchestrator).

### `TryGet`

`public IServiceProvider? TryGet(string connectionId)`

Returns an existing connection scope without creating one, or `null`. Used by out-of-band HTTP endpoints (e.g. `POST /mentor/cancel`) that must reach the live orchestrator of a SignalR connection without going through the hub — a hub method cannot run while `SendMessage` is awaiting on the same connection.

---

- Previous: [MentorHub class](https://docs.mentoragent.net/api/novior-mentoragent-server-hubs/mentorhub.md)
- Next: [ClientServiceExtensions class](https://docs.mentoragent.net/api/novior-mentoragent-blazor-extensions/clientserviceextensions.md)
