11#include <Poco/Dynamic/Var.h>
12#include <Poco/JSON/Array.h>
13#include <Poco/JSON/Object.h>
14#include <Poco/JSON/Stringifier.h>
25 std::ostringstream
out;
26 Poco::JSON::Stringifier::stringify(
value,
out, 0, 0);
36 case '"':
out +=
"\\\"";
break;
37 case '\\':
out +=
"\\\\";
break;
38 case '\n':
out +=
"\\n";
break;
39 case '\r':
out +=
"\\r";
break;
40 case '\t':
out +=
"\\t";
break;
42 if (
static_cast<unsigned char>(
c) < 0x20) {
44 std::snprintf(buf,
sizeof(buf),
"\\u%04x",
static_cast<unsigned>(
c));
64 bool retryable =
false,
65 const std::vector<std::string>& nextTools = {}) {
66 Poco::JSON::Object::Ptr
out = Poco::JSON::Object::Ptr(
new Poco::JSON::Object());
67 out->set(
"ok",
false);
69 if (retryable)
out->set(
"retryable",
true);
70 if (!hint.empty())
out->set(
"hint", hint);
71 if (!nextTools.empty()) {
72 Poco::JSON::Array::Ptr tools = Poco::JSON::Array::Ptr(
new Poco::JSON::Array());
73 for (
const auto&
name : nextTools) tools->add(
name);
74 out->set(
"nextTools", tools);
85 Poco::JSON::Object::Ptr
result = Poco::JSON::Object::Ptr(
new Poco::JSON::Object());
86 Poco::JSON::Array::Ptr content = Poco::JSON::Array::Ptr(
new Poco::JSON::Array());
87 Poco::JSON::Object::Ptr item = Poco::JSON::Object::Ptr(
new Poco::JSON::Object());
88 item->set(
"type",
"text");
89 item->set(
"text",
text);
91 result->set(
"content", content);
92 if (isError)
result->set(
"isError",
true);
103 const std::string& caption) {
104 Poco::JSON::Object::Ptr
result = Poco::JSON::Object::Ptr(
new Poco::JSON::Object());
105 Poco::JSON::Array::Ptr content = Poco::JSON::Array::Ptr(
new Poco::JSON::Array());
106 Poco::JSON::Object::Ptr
image = Poco::JSON::Object::Ptr(
new Poco::JSON::Object());
107 image->set(
"type",
"image");
108 image->set(
"data", base64Png);
109 image->set(
"mimeType", mimeType);
111 if (!caption.empty()) {
112 Poco::JSON::Object::Ptr item = Poco::JSON::Object::Ptr(
new Poco::JSON::Object());
113 item->set(
"type",
"text");
114 item->set(
"text", caption);
117 result->set(
"content", content);
std::string mcpJsonEscape(const std::string &value)
Escape a raw string for embedding inside a JSON string literal.
std::string mcpStringify(const Poco::Dynamic::Var &value)
Compact single-line JSON text for a newline-framed MCP payload.
std::string textContentResult(const std::string &text, bool isError=false)
MCP tools/call result object carrying one text content item.
std::string imageContentResult(const std::string &base64Png, const std::string &mimeType, const std::string &caption)
MCP tools/call result object carrying one image content item.
Poco::JSON::Object::Ptr mcpFailureObject(const std::string &error, const std::string &hint={}, bool retryable=false, const std::vector< std::string > &nextTools={})
Structured tool failure object for agent self-correction.