You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.4 KiB
C#
48 lines
1.4 KiB
C#
using System.Diagnostics;
|
|
using System.Threading.Tasks;
|
|
|
|
using ModelContextProtocol;
|
|
using ModelContextProtocol.Client;
|
|
|
|
using ModelContextProtocol.AspNetCore;
|
|
|
|
|
|
|
|
namespace McpStudy.McpClient;
|
|
internal class Program
|
|
{
|
|
static async Task Main(string[] args)
|
|
{
|
|
Console.WriteLine("调用MCP服务器示例");
|
|
|
|
await Task.CompletedTask;
|
|
}
|
|
|
|
static async Task CallStdio()
|
|
{
|
|
var clientTransport = new StdioClientTransport(new StdioClientTransportOptions
|
|
{
|
|
Name = "Everything",
|
|
Command = "npx",
|
|
Arguments = ["-y", "@modelcontextprotocol/server-everything"],
|
|
});
|
|
|
|
var client = await McpClientFactory.CreateAsync(clientTransport);
|
|
|
|
// Print the list of tools available from the server.
|
|
foreach (var tool in await client.ListToolsAsync())
|
|
{
|
|
Console.WriteLine($"{tool.Name} ({tool.Description})");
|
|
}
|
|
|
|
// Execute a tool (this would normally be driven by LLM tool invocations).
|
|
var result = await client.CallToolAsync(
|
|
"echo",
|
|
new Dictionary<string, object?>() { ["message"] = "Hello MCP!" },
|
|
cancellationToken: CancellationToken.None);
|
|
|
|
// echo always returns one and only one text content object
|
|
Console.WriteLine(result.Content.First(c => c.Type == "text").ToString());
|
|
}
|
|
}
|