Skip to content

Commit 083cf5b

Browse files
halter73Copilot
andcommitted
Replace ScopeRequests sample with McpServer.Create example
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent d13e0f3 commit 083cf5b

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

docs/concepts/sessions/sessions.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -333,16 +333,21 @@ 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` if the `IServiceProvider` passed to the server is already scoped to the desired lifetime. For example, when using <xref:ModelContextProtocol.McpServer.Create*> with a pre-scoped service provider, disabling `ScopeRequests` avoids creating redundant nested scopes. However, be cautious setting this globally via `AddMcpServer` for stdio servers — with the default hosting, the server receives the root application `IServiceProvider`, so scoped services would behave like singletons for the lifetime of the process.
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:
337337

338338
```csharp
339-
builder.Services.AddMcpServer(options =>
340-
{
341-
// Disable per-handler scoping — only appropriate when the server's IServiceProvider is already scoped.
342-
options.ScopeRequests = false;
343-
})
344-
.WithStdioServerTransport()
345-
.WithTools<MyTools>();
339+
Pipe clientToServerPipe = new(), serverToClientPipe = new();
340+
341+
await using var scope = serviceProvider.CreateAsyncScope();
342+
343+
await using McpServer server = McpServer.Create(
344+
new StreamServerTransport(clientToServerPipe.Reader.AsStream(), serverToClientPipe.Writer.AsStream()),
345+
new McpServerOptions
346+
{
347+
ScopeRequests = false, // The scope is already managed externally.
348+
ToolCollection = [McpServerTool.Create((string arg) => $"Echo: {arg}", new() { Name = "Echo" })]
349+
},
350+
serviceProvider: scope.ServiceProvider);
346351
```
347352

348353
#### DI scope summary

0 commit comments

Comments
 (0)