cpp-mcp/test/test_client.cpp

61 lines
1.8 KiB
C++
Raw Normal View History

2025-03-12 19:43:34 +08:00
/**
* @file test_client.cpp
* @brief MCP
*/
#include "mcp_client.h"
#include <iostream>
#include <thread>
#include <chrono>
int main() {
// 创建客户端
mcp::client client("localhost", 8080);
// 设置超时
client.set_timeout(30);
// 初始化客户端
std::cout << "正在初始化客户端..." << std::endl;
bool success = client.initialize("TestClient", "1.0.0");
if (!success) {
std::cerr << "初始化失败" << std::endl;
return 1;
}
std::cout << "初始化成功" << std::endl;
// 获取服务器能力
std::cout << "服务器能力: " << client.get_server_capabilities().dump(2) << std::endl;
// 获取可用工具
std::cout << "正在获取可用工具..." << std::endl;
auto tools = client.get_tools();
std::cout << "可用工具数量: " << tools.size() << std::endl;
for (const auto& tool : tools) {
std::cout << "工具: " << tool.name << " - " << tool.description << std::endl;
}
// 发送ping请求
std::cout << "正在发送ping请求..." << std::endl;
bool ping_result = client.ping();
std::cout << "Ping结果: " << (ping_result ? "成功" : "失败") << std::endl;
// 列出资源
std::cout << "正在列出资源..." << std::endl;
auto resources = client.list_resources();
std::cout << "资源: " << resources.dump(2) << std::endl;
// 测试多个并发请求
std::cout << "测试并发请求..." << std::endl;
for (int i = 0; i < 5; i++) {
std::cout << "请求 " << i << "..." << std::endl;
auto response = client.send_request("ping");
std::cout << "响应 " << i << ": " << response.result.dump() << std::endl;
}
std::cout << "测试完成" << std::endl;
return 0;
}