cpp-mcp/examples/client_example.cpp

201 lines
7.9 KiB
C++

/**
* @file client_example.cpp
* @brief Example MCP client implementation
*/
#include "mcp_client.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
// Helper function to print JSON
void print_json(const mcp::json& json, int indent = 0) {
std::string indent_str(indent, ' ');
if (json.is_object()) {
std::cout << indent_str << "{" << std::endl;
for (auto it = json.begin(); it != json.end(); ++it) {
std::cout << indent_str << " \"" << it.key() << "\": ";
if (it.value().is_object() || it.value().is_array()) {
std::cout << std::endl;
print_json(it.value(), indent + 2);
} else if (it.value().is_string()) {
std::cout << "\"" << it.value().get<std::string>() << "\"";
} else {
std::cout << it.value().dump();
}
if (std::next(it) != json.end()) {
std::cout << ",";
}
std::cout << std::endl;
}
std::cout << indent_str << "}";
} else if (json.is_array()) {
std::cout << indent_str << "[" << std::endl;
for (size_t i = 0; i < json.size(); ++i) {
print_json(json[i], indent + 2);
if (i < json.size() - 1) {
std::cout << ",";
}
std::cout << std::endl;
}
std::cout << indent_str << "]";
} else {
std::cout << json.dump();
}
if (indent == 0) {
std::cout << std::endl;
}
}
int main() {
// Create a client to connect to the server
mcp::client client("localhost", 8080);
try {
// Get server information
std::cout << "Getting server information..." << std::endl;
mcp::json server_info = client.get_server_info();
std::cout << "Server name: " << server_info["name"].get<std::string>() << std::endl;
std::cout << "Server version: " << server_info["version"].get<std::string>() << std::endl;
std::cout << std::endl;
// Get available tools
std::cout << "Getting available tools..." << std::endl;
mcp::json tools = client.get_tools();
std::cout << "Available tools: " << tools.size() << std::endl;
for (const auto& tool : tools) {
std::cout << "- " << tool["name"].get<std::string>() << ": "
<< tool["description"].get<std::string>() << std::endl;
}
std::cout << std::endl;
// Call the get_time tool
std::cout << "Calling get_time tool..." << std::endl;
mcp::json time_result = client.call_tool("get_time");
std::cout << "Current time: " << time_result["formatted"].get<std::string>() << std::endl;
std::cout << std::endl;
// Call the calculator tool
std::cout << "Calling calculator tool..." << std::endl;
mcp::json calc_params = {
{"operation", "add"},
{"a", 42},
{"b", 58}
};
mcp::json calc_result = client.call_tool("calculator", calc_params);
std::cout << "42 + 58 = " << calc_result["result"].get<int>() << std::endl;
std::cout << std::endl;
// Call the text_processor tool
std::cout << "Calling text_processor tool..." << std::endl;
mcp::json text_params = {
{"text", "Hello, MCP Protocol!"},
{"to_uppercase", true},
{"word_count", true}
};
mcp::json text_result = client.call_tool("text_processor", text_params);
std::cout << "Uppercase: " << text_result["uppercase"].get<std::string>() << std::endl;
std::cout << "Word count: " << text_result["word_count"].get<int>() << std::endl;
std::cout << std::endl;
// List workflows
std::cout << "Listing workflows..." << std::endl;
mcp::response workflows_res = client.get("/workflows");
if (workflows_res.status_code == 200) {
mcp::json workflows = mcp::json::parse(workflows_res.body);
std::cout << "Available workflows: " << workflows.size() << std::endl;
for (const auto& wf : workflows) {
std::cout << "- " << wf["name"].get<std::string>();
if (wf.contains("description") && !wf["description"].get<std::string>().empty()) {
std::cout << ": " << wf["description"].get<std::string>();
}
std::cout << std::endl;
}
} else {
std::cout << "Error listing workflows: " << workflows_res.body << std::endl;
}
std::cout << std::endl;
// Execute a workflow
if (workflows_res.status_code == 200) {
mcp::json workflows = mcp::json::parse(workflows_res.body);
for (size_t i = 0; i < workflows.size(); ++i) {
std::string workflow_name = workflows[i]["name"].get<std::string>();
std::cout << "Executing workflow: " << workflow_name << std::endl;
mcp::json context = {
{"custom_param", "Custom value"}
};
mcp::response wf_exec_res = client.post_json(
"/workflows/" + workflow_name + "/execute",
{{"context", context}}
);
if (wf_exec_res.status_code == 200) {
mcp::json result = mcp::json::parse(wf_exec_res.body);
std::cout << "Workflow execution result:" << std::endl;
print_json(result);
} else {
std::cout << "Error executing workflow: " << wf_exec_res.body << std::endl;
}
}
}
std::cout << std::endl;
// List agents
std::cout << "Listing agents..." << std::endl;
mcp::response agents_res = client.get("/agents");
if (agents_res.status_code == 200) {
mcp::json agents = mcp::json::parse(agents_res.body);
std::cout << "Available agents: " << agents.size() << std::endl;
for (const auto& agent : agents) {
std::cout << "- " << agent["name"].get<std::string>() << std::endl;
}
} else {
std::cout << "Error listing agents: " << agents_res.body << std::endl;
}
std::cout << std::endl;
// Process request with the echo agent
std::cout << "Processing request with echo agent..." << std::endl;
mcp::json echo_input = {
{"text", "Hello from client!"}
};
mcp::response echo_res = client.post_json("/agents/echo/process", echo_input);
if (echo_res.status_code == 200) {
mcp::json result = mcp::json::parse(echo_res.body);
std::cout << "Echo agent response:" << std::endl;
print_json(result);
} else {
std::cout << "Error processing with echo agent: " << echo_res.body << std::endl;
}
std::cout << std::endl;
// Process request with the workflow runner agent
std::cout << "Processing request with workflow runner agent..." << std::endl;
mcp::json wf_runner_input = {
{"workflow", "time_text_calc"}
};
mcp::response wf_runner_res = client.post_json("/agents/workflow_runner/process", wf_runner_input);
if (wf_runner_res.status_code == 200) {
mcp::json result = mcp::json::parse(wf_runner_res.body);
std::cout << "Workflow runner agent response:" << std::endl;
print_json(result);
} else {
std::cout << "Error processing with workflow runner agent: " << wf_runner_res.body << std::endl;
}
std::cout << std::endl;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return 0;
}