-
Notifications
You must be signed in to change notification settings - Fork 682
Expand file tree
/
Copy pathProgram.cs
More file actions
34 lines (29 loc) · 1.13 KB
/
Program.cs
File metadata and controls
34 lines (29 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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 McpServer server = McpServer.Create(
new StreamServerTransport(clientToServerPipe.Reader.AsStream(), serverToClientPipe.Writer.AsStream()),
new McpServerOptions()
{
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 McpClient client = await McpClient.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"
}));