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() { ["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()); } }