cpp-mcp/examples/client_example.cpp

91 lines
3.2 KiB
C++
Raw Normal View History

2025-03-08 01:50:39 +08:00
/**
* @file client_example.cpp
2025-03-08 22:49:19 +08:00
* @brief Example of an MCP client implementation
*
* This file demonstrates how to create an MCP client that connects to a server.
* Follows the 2024-11-05 basic protocol specification.
2025-03-08 01:50:39 +08:00
*/
#include "mcp_client.h"
#include <iostream>
#include <string>
int main() {
2025-03-08 22:49:19 +08:00
// Create a client
2025-03-12 19:43:34 +08:00
mcp::client client("localhost", 8888);
2025-03-08 22:49:19 +08:00
// Set capabilites
mcp::json capabilities = {
{"roots", {{"listChanged", true}}}
};
client.set_capabilities(capabilities);
// Set timeout
client.set_timeout(10);
2025-03-08 01:50:39 +08:00
try {
2025-03-08 22:49:19 +08:00
// Initialize the connection
std::cout << "Initializing connection to MCP server..." << std::endl;
bool initialized = client.initialize("ExampleClient", mcp::MCP_VERSION);
if (!initialized) {
std::cerr << "Failed to initialize connection to server" << std::endl;
return 1;
}
2025-03-09 23:17:36 +08:00
// Ping the server
std::cout << "Pinging server..." << std::endl;
if (!client.ping()) {
std::cerr << "Failed to ping server" << std::endl;
return 1;
}
2025-03-08 22:49:19 +08:00
// Get server capabilities
std::cout << "Getting server capabilities..." << std::endl;
mcp::json capabilities = client.get_server_capabilities();
std::cout << "Server capabilities: " << capabilities.dump(4) << std::endl;
2025-03-08 01:50:39 +08:00
// Get available tools
2025-03-08 22:49:19 +08:00
std::cout << "\nGetting available tools..." << std::endl;
auto tools = client.get_tools();
std::cout << "Available tools:" << std::endl;
2025-03-08 01:50:39 +08:00
for (const auto& tool : tools) {
2025-03-08 22:49:19 +08:00
std::cout << "- " << tool.name << ": " << tool.description << std::endl;
2025-03-08 01:50:39 +08:00
}
2025-03-12 22:45:17 +08:00
// Get available resources
2025-03-08 01:50:39 +08:00
// Call the get_time tool
2025-03-08 22:49:19 +08:00
std::cout << "\nCalling get_time tool..." << std::endl;
2025-03-08 01:50:39 +08:00
mcp::json time_result = client.call_tool("get_time");
2025-03-09 23:17:36 +08:00
std::cout << "Current time: " << time_result["content"][0]["text"].get<std::string>() << std::endl;
2025-03-08 22:49:19 +08:00
// Call the echo tool
std::cout << "\nCalling echo tool..." << std::endl;
mcp::json echo_params = {
{"text", "Hello, MCP!"},
{"uppercase", true}
};
mcp::json echo_result = client.call_tool("echo", echo_params);
2025-03-09 23:17:36 +08:00
std::cout << "Echo result: " << echo_result["content"][0]["text"].get<std::string>() << std::endl;
2025-03-08 01:50:39 +08:00
// Call the calculator tool
2025-03-08 22:49:19 +08:00
std::cout << "\nCalling calculator tool..." << std::endl;
2025-03-08 01:50:39 +08:00
mcp::json calc_params = {
{"operation", "add"},
2025-03-08 22:49:19 +08:00
{"a", 10},
{"b", 5}
2025-03-08 01:50:39 +08:00
};
mcp::json calc_result = client.call_tool("calculator", calc_params);
2025-03-09 23:17:36 +08:00
std::cout << "10 + 5 = " << calc_result["content"][0]["text"].get<std::string>() << std::endl;
2025-03-08 22:49:19 +08:00
} catch (const mcp::mcp_exception& e) {
std::cerr << "MCP error: " << e.what() << " (code: " << static_cast<int>(e.code()) << ")" << std::endl;
return 1;
2025-03-08 01:50:39 +08:00
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
2025-03-08 22:49:19 +08:00
std::cout << "\nClient example completed successfully" << std::endl;
2025-03-08 01:50:39 +08:00
return 0;
}