载入中...
搜索中...
未找到
McpJson.hpp
浏览该文件的文档.
1#pragma once
2
3// Shared MCP wire helpers.
4//
5// MCP framing here is newline-delimited JSON (see devtools/McpServer.hpp), so
6// every payload has to be a single line: stringify with indent/step 0 and escape
7// raw control characters. These helpers are inline so the embedded server and
8// its satellite tool modules (AgentDevelopmentMcp, McpRuntimeTools, ...) build
9// identical content envelopes without a second JSON codec.
10
11#include <Poco/Dynamic/Var.h>
12#include <Poco/JSON/Array.h>
13#include <Poco/JSON/Object.h>
14#include <Poco/JSON/Stringifier.h>
15
16#include <cstdio>
17#include <sstream>
18#include <string>
19
20namespace eve::dev {
21
23inline std::string mcpStringify(const Poco::Dynamic::Var& value) {
24 std::ostringstream out;
25 Poco::JSON::Stringifier::stringify(value, out, 0, 0);
26 return out.str();
27}
28
30inline std::string mcpJsonEscape(const std::string& value) {
31 std::string out;
32 out.reserve(value.size() + 8);
33 for (char c : value) {
34 switch (c) {
35 case '"': out += "\\\""; break;
36 case '\\': out += "\\\\"; break;
37 case '\n': out += "\\n"; break;
38 case '\r': out += "\\r"; break;
39 case '\t': out += "\\t"; break;
40 default:
41 if (static_cast<unsigned char>(c) < 0x20) {
42 char buf[8];
43 std::snprintf(buf, sizeof(buf), "\\u%04x", static_cast<unsigned>(c));
44 out += buf;
45 } else {
46 out += c;
47 }
48 break;
49 }
50 }
51 return out;
52}
53
59inline std::string textContentResult(const std::string& text, bool isError = false) {
60 Poco::JSON::Object::Ptr result = Poco::JSON::Object::Ptr(new Poco::JSON::Object());
61 Poco::JSON::Array::Ptr content = Poco::JSON::Array::Ptr(new Poco::JSON::Array());
62 Poco::JSON::Object::Ptr item = Poco::JSON::Object::Ptr(new Poco::JSON::Object());
63 item->set("type", "text");
64 item->set("text", text);
65 content->add(item);
66 result->set("content", content);
67 if (isError) result->set("isError", true);
68 return mcpStringify(Poco::Dynamic::Var(result));
69}
70
77inline std::string imageContentResult(const std::string& base64Png, const std::string& mimeType,
78 const std::string& caption) {
79 Poco::JSON::Object::Ptr result = Poco::JSON::Object::Ptr(new Poco::JSON::Object());
80 Poco::JSON::Array::Ptr content = Poco::JSON::Array::Ptr(new Poco::JSON::Array());
81 Poco::JSON::Object::Ptr image = Poco::JSON::Object::Ptr(new Poco::JSON::Object());
82 image->set("type", "image");
83 image->set("data", base64Png);
84 image->set("mimeType", mimeType);
85 content->add(image);
86 if (!caption.empty()) {
87 Poco::JSON::Object::Ptr item = Poco::JSON::Object::Ptr(new Poco::JSON::Object());
88 item->set("type", "text");
89 item->set("text", caption);
90 content->add(item);
91 }
92 result->set("content", content);
93 return mcpStringify(Poco::Dynamic::Var(result));
94}
95
96} // namespace eve::dev
double value
vk::UniqueImage image
std::int32_t c
std::string text
std::string mcpJsonEscape(const std::string &value)
Escape a raw string for embedding inside a JSON string literal.
Definition McpJson.hpp:30
std::string mcpStringify(const Poco::Dynamic::Var &value)
Compact single-line JSON text for a newline-framed MCP payload.
Definition McpJson.hpp:23
std::string textContentResult(const std::string &text, bool isError=false)
MCP tools/call result object carrying one text content item.
Definition McpJson.hpp:59
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.
Definition McpJson.hpp:77