Skip to content

Commit 5735987

Browse files
halter73Copilot
andcommitted
Split DI scopes guidance for stdio vs custom transports
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 083cf5b commit 5735987

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

docs/concepts/sessions/sessions.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,9 @@ This means:
333333

334334
The stdio transport creates a single server for the lifetime of the process. The server's <xref:ModelContextProtocol.McpServer.Services> is the application-level `IServiceProvider`. By default, <xref:ModelContextProtocol.Server.McpServerOptions.ScopeRequests> is `true`, so each handler invocation gets its own scope — the same behavior as stateful HTTP.
335335

336-
You can set <xref:ModelContextProtocol.Server.McpServerOptions.ScopeRequests> to `false` when using <xref:ModelContextProtocol.McpServer.Create*> with an `IServiceProvider` that is already scoped to the desired lifetime — this avoids creating redundant nested scopes. The [InMemoryTransport sample](https://github.com/modelcontextprotocol/csharp-sdk/tree/main/samples/InMemoryTransport) shows a minimal example of using `McpServer.Create` with in-memory pipes:
336+
#### McpServer.Create (custom transports)
337+
338+
When you create a server directly with <xref:ModelContextProtocol.McpServer.Create*>, you control the `IServiceProvider` and transport yourself. If you pass an already-scoped provider, you can set <xref:ModelContextProtocol.Server.McpServerOptions.ScopeRequests> to `false` to avoid creating redundant nested scopes. The [InMemoryTransport sample](https://github.com/modelcontextprotocol/csharp-sdk/tree/main/samples/InMemoryTransport) shows a minimal example of using `McpServer.Create` with in-memory pipes:
337339

338340
```csharp
339341
Pipe clientToServerPipe = new(), serverToClientPipe = new();
@@ -357,6 +359,7 @@ await using McpServer server = McpServer.Create(
357359
| **Stateful HTTP** | Application services | `true` (default) | New scope per handler invocation |
358360
| **Stateless HTTP** | `HttpContext.RequestServices` | `false` (forced) | Shared HTTP request scope |
359361
| **stdio** | Application services | `true` (default, configurable) | New scope per handler invocation |
362+
| **McpServer.Create** | Caller-provided | Caller-controlled | Depends on `ScopeRequests` and whether the provider is already scoped |
360363

361364
## Security
362365

0 commit comments

Comments
 (0)