using Azure.Core.Pipeline;
namespace McpStudy.McpClient;
internal class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("调用MCP服务示例");
var HttpClient = new HttpClient(new SocketsHttpHandler
{
})
{
BaseAddress = new Uri("http://localhost:5000/"),
Timeout = TimeSpan.FromSeconds(10),
};
//await CallStdioAsync();
//请务必在调用前,启动SseServer服务
//await RequestSseServerAsync();
await RequestStreamableHttpServerAsync();
await Task.CompletedTask;
}
///
/// 使用Mcp客户端,直接调用Stdio类型的MCP服务
/// 真实项目或生产环境,请使用 `Microsoft.Extensions.AI`、`Microsoft.SemanticKernel`等AI库,进行调用;
///
///
static async Task CallStdioAsync()
{
Console.WriteLine($"开始调用 Studio 类型的MCP服务......{System.Environment.NewLine}");
var clientTransport = new StdioClientTransport(new StdioClientTransportOptions
{
Name = "Everything",
Command = "dotnet",
Arguments = ["run", "--project", "E:\\软件项目\\学习项目\\AI学习\\McpStudy\\McpStudy.McpServerStdio\\McpStudy.McpServerStdio.csproj"],
});
Console.WriteLine("创建客户端......");
IMcpClient client = await McpClientFactory.CreateAsync(clientTransport);
Console.WriteLine($"创建客户端完成{System.Environment.NewLine}");
Console.WriteLine("列举MCP服务中,可用的工具......");
foreach (McpClientTool tool in await client.ListToolsAsync())
{
Console.WriteLine($"工具名称:{tool.Name}, 工具描述:({tool.Description})");
}
Console.WriteLine($"列举完成{System.Environment.NewLine}");
Console.WriteLine("调用Echo工具......");
CallToolResult result = await client.CallToolAsync
(
toolName: "Echo",
arguments: new Dictionary() { ["message"] = "Hello MCP!" },
cancellationToken: CancellationToken.None
);
Console.Write("Echo工具结果:");
var content = result.Content.First(c => c.Type == "text") as TextContentBlock;
Console.WriteLine(content?.Text);
Console.WriteLine($"调用完成{System.Environment.NewLine}");
Console.WriteLine("调用 Studio 类型的MCP服务结束!");
}
//
/// 使用Mcp客户端,直接调用SSE类型的MCP服务
/// 真实项目或生产环境,请使用 `Microsoft.Extensions.AI`、`Microsoft.SemanticKernel`等AI库,进行调用;
///
///
static async Task RequestSseServerAsync()
{
Console.WriteLine($"开始调用 SSE类型的MCP服务......{System.Environment.NewLine}");
var sseClientTransportOptions = new SseClientTransportOptions()
{
Endpoint = new Uri("http://localhost:5027/sse"),
};
var sseClientTransport = new SseClientTransport(sseClientTransportOptions);
Console.WriteLine("创建客户端......");
IMcpClient client = await McpClientFactory.CreateAsync(sseClientTransport);
Console.WriteLine($"创建客户端完成{System.Environment.NewLine}");
Console.WriteLine("列举MCP服务中,可用的工具......");
foreach (McpClientTool tool in await client.ListToolsAsync())
{
Console.WriteLine($"工具名称:{tool.Name}, 工具描述:({tool.Description})");
}
Console.WriteLine($"列举完成{System.Environment.NewLine}");
Console.WriteLine("调用Echo工具......");
CallToolResult result = await client.CallToolAsync
(
toolName: "Echo",
arguments: new Dictionary() { ["message"] = "Hello MCP!" },
cancellationToken: CancellationToken.None
);
Console.Write("Echo工具结果:");
var content = result.Content.First(c => c.Type == "text") as TextContentBlock;
Console.WriteLine(content?.Text);
Console.WriteLine($"调用完成{System.Environment.NewLine}");
Console.WriteLine("调用 Studio 类型的MCP服务结束!");
}
static async Task RequestStreamableHttpServerAsync()
{
Console.WriteLine($"开始调用 StreamableHttp 类型的MCP服务......{System.Environment.NewLine}");
var clientTransport = new SseClientTransport(
new SseClientTransportOptions
{
Name = "StudyMCPServer",
Endpoint = new Uri("http://localhost:5000"),
TransportMode = HttpTransportMode.StreamableHttp,
//AdditionalHeaders = new Dictionary() { { "text/event-stream", "Content-Type" } }
}
);
var mcpClient = await McpClientFactory.CreateAsync(clientTransport);
var clientTools = await mcpClient.ListToolsAsync();
//return clientTools.Select(_ => _.AsKernelFunction());
Console.WriteLine("调用 Studio 类型的MCP服务结束!");
}
}