From faabc4a2e7ed361eca0995dcd6272aebb18ced64 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Thu, 31 Jul 2025 17:07:40 -0400 Subject: [PATCH 1/2] Add an in-memory transport sample --- samples/IntraProcess/IntraProcess.csproj | 15 +++++++++ samples/IntraProcess/Program.cs | 40 ++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 samples/IntraProcess/IntraProcess.csproj create mode 100644 samples/IntraProcess/Program.cs diff --git a/samples/IntraProcess/IntraProcess.csproj b/samples/IntraProcess/IntraProcess.csproj new file mode 100644 index 000000000..7c1161ce9 --- /dev/null +++ b/samples/IntraProcess/IntraProcess.csproj @@ -0,0 +1,15 @@ + + + + Exe + net8.0 + enable + enable + true + + + + + + + diff --git a/samples/IntraProcess/Program.cs b/samples/IntraProcess/Program.cs new file mode 100644 index 000000000..67e2d320c --- /dev/null +++ b/samples/IntraProcess/Program.cs @@ -0,0 +1,40 @@ +using ModelContextProtocol.Client; +using ModelContextProtocol.Protocol; +using ModelContextProtocol.Server; +using System.IO.Pipelines; + +Pipe clientToServerPipe = new(), serverToClientPipe = new(); + +// Create a server using a stream-based transport over an in-memory pipe. +await using IMcpServer server = McpServerFactory.Create( + new StreamServerTransport(clientToServerPipe.Reader.AsStream(), serverToClientPipe.Writer.AsStream()), + new McpServerOptions() + { + Capabilities = new() + { + Tools = new() + { + ToolCollection = [McpServerTool.Create((string arg) => $"Echo: {arg}", new() { Name = "Echo" })] + } + } + }); +_ = server.RunAsync(); + +// Connect a client using a stream-based transport over the same in-memory pipe. +await using IMcpClient client = await McpClientFactory.CreateAsync( + new StreamClientTransport(clientToServerPipe.Writer.AsStream(), serverToClientPipe.Reader.AsStream())); + +// List all tools. +var tools = await client.ListToolsAsync(); +foreach (var tool in tools) +{ + Console.WriteLine($"Tool Name: {tool.Name}"); +} +Console.WriteLine(); + +// Invoke a tool. +var echo = tools.First(t => t.Name == "Echo"); +Console.WriteLine(await echo.InvokeAsync(new() +{ + ["arg"] = "Hello World" +})); \ No newline at end of file From 8e6bd15c123792de99c186f62df0e6b280d2cf34 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Thu, 31 Jul 2025 20:19:57 -0400 Subject: [PATCH 2/2] Rename sample --- ModelContextProtocol.slnx | 1 + .../InMemoryTransport.csproj} | 0 samples/{IntraProcess => InMemoryTransport}/Program.cs | 0 3 files changed, 1 insertion(+) rename samples/{IntraProcess/IntraProcess.csproj => InMemoryTransport/InMemoryTransport.csproj} (100%) rename samples/{IntraProcess => InMemoryTransport}/Program.cs (100%) diff --git a/ModelContextProtocol.slnx b/ModelContextProtocol.slnx index 5ed8ba0d6..d4fed7ea9 100644 --- a/ModelContextProtocol.slnx +++ b/ModelContextProtocol.slnx @@ -12,6 +12,7 @@ + diff --git a/samples/IntraProcess/IntraProcess.csproj b/samples/InMemoryTransport/InMemoryTransport.csproj similarity index 100% rename from samples/IntraProcess/IntraProcess.csproj rename to samples/InMemoryTransport/InMemoryTransport.csproj diff --git a/samples/IntraProcess/Program.cs b/samples/InMemoryTransport/Program.cs similarity index 100% rename from samples/IntraProcess/Program.cs rename to samples/InMemoryTransport/Program.cs