Skip to content

Commit b9fe994

Browse files
halter73Copilot
andcommitted
Add in-memory transport section and fix SSE comment wording
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 9e85bd7 commit b9fe994

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

docs/concepts/transports/transports.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ var builder = WebApplication.CreateBuilder(args);
186186
builder.Services.AddMcpServer()
187187
.WithHttpTransport(options =>
188188
{
189-
// SSE requires stateful mode (the default). Set explicitly for clarity.
189+
// SSE requires stateful mode (the default). Set explicitly for forward compatibility.
190190
options.Stateless = false;
191191
})
192192
.WithTools<MyTools>();
@@ -216,3 +216,35 @@ No additional configuration is needed. When a client connects using the SSE prot
216216
| Best for | Local tools, IDE integrations | Remote servers, production deployments | Local HTTP debugging, server-to-client features | Legacy client compatibility |
217217

218218
For a detailed comparison of stateless vs. stateful mode — including deployment trade-offs, security considerations, and configuration — see [Sessions](xref:sessions).
219+
220+
### In-memory transport
221+
222+
The <xref:ModelContextProtocol.Server.StreamServerTransport> and <xref:ModelContextProtocol.Client.StreamClientTransport> types work with any `Stream`, including in-memory pipes. This is useful for testing, embedding an MCP server in a larger application, or running a client and server in the same process without network overhead.
223+
224+
The following example creates a client and server connected via `System.IO.Pipelines` (from the [InMemoryTransport sample](https://github.com/modelcontextprotocol/csharp-sdk/blob/51a4fde4d9cfa12ef9430deef7daeaac36625be8/samples/InMemoryTransport/Program.cs)):
225+
226+
```csharp
227+
using ModelContextProtocol.Client;
228+
using ModelContextProtocol.Server;
229+
using System.IO.Pipelines;
230+
231+
Pipe clientToServerPipe = new(), serverToClientPipe = new();
232+
233+
// Create a server using a stream-based transport over an in-memory pipe.
234+
await using McpServer server = McpServer.Create(
235+
new StreamServerTransport(clientToServerPipe.Reader.AsStream(), serverToClientPipe.Writer.AsStream()),
236+
new McpServerOptions
237+
{
238+
ToolCollection = [McpServerTool.Create((string message) => $"Echo: {message}", new() { Name = "echo" })]
239+
});
240+
_ = server.RunAsync();
241+
242+
// Connect a client using a stream-based transport over the same in-memory pipe.
243+
await using McpClient client = await McpClient.CreateAsync(
244+
new StreamClientTransport(clientToServerPipe.Writer.AsStream(), serverToClientPipe.Reader.AsStream()));
245+
246+
// List and invoke tools.
247+
var tools = await client.ListToolsAsync();
248+
var echo = tools.First(t => t.Name == "echo");
249+
Console.WriteLine(await echo.InvokeAsync(new() { ["arg"] = "Hello World" }));
250+
```

0 commit comments

Comments
 (0)