|
| 1 | +# In-Memory Transport Sample |
| 2 | + |
| 3 | +Demonstrates connecting an MCP client and server in the same process using stream-based |
| 4 | +transports over in-memory pipes (`System.IO.Pipelines`) — no child process, no network. |
| 5 | + |
| 6 | +The server is created directly with `McpServer.Create`, without a host or dependency |
| 7 | +injection container, and exposes a single `Echo` tool defined from a delegate with |
| 8 | +`McpServerTool.Create`. The client connects over the same pipe pair with |
| 9 | +`StreamClientTransport`, lists the server's tools, and invokes the tool. |
| 10 | + |
| 11 | +This pattern is useful for testing MCP servers, embedding a server inside a larger |
| 12 | +application, or running a client and server in the same process without transport overhead. |
| 13 | +See [Transports: In-memory transport](../../docs/concepts/transports/transports.md) for more |
| 14 | +background. |
| 15 | + |
| 16 | +## Run |
| 17 | + |
| 18 | +```bash |
| 19 | +dotnet run --project samples/InMemoryTransport/InMemoryTransport.csproj |
| 20 | +``` |
| 21 | + |
| 22 | +Expected output: |
| 23 | + |
| 24 | +``` |
| 25 | +Tool Name: Echo |
| 26 | +
|
| 27 | +Echo: Hello World |
| 28 | +``` |
| 29 | + |
| 30 | +## Key files |
| 31 | + |
| 32 | +- [`Program.cs`](Program.cs) — the entire sample: pipe setup, server creation, client |
| 33 | + connection, tool listing, and tool invocation. |
| 34 | + |
| 35 | +## Notes |
| 36 | + |
| 37 | +- `StreamServerTransport` and `StreamClientTransport` work with any `Stream`. This sample |
| 38 | + wires them to a pair of `Pipe` instances, one per direction; the client's output stream is |
| 39 | + the server's input stream and vice versa. |
| 40 | +- The server is started with a fire-and-forget `server.RunAsync()` because both endpoints |
| 41 | + live in the same process. `await using` on the server and client ensures both are disposed |
| 42 | + when the program exits. |
0 commit comments