载入中...
搜索中...
未找到
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#include <vector>
20
21namespace eve::dev {
22
24inline std::string mcpStringify(const Poco::Dynamic::Var& value) {
25 std::ostringstream out;
26 Poco::JSON::Stringifier::stringify(value, out, 0, 0);
27 return out.str();
28}
29
31inline std::string mcpJsonEscape(const std::string& value) {
32 std::string out;
33 out.reserve(value.size() + 8);
34 for (char c : value) {
35 switch (c) {
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;
41 default:
42 if (static_cast<unsigned char>(c) < 0x20) {
43 char buf[8];
44 std::snprintf(buf, sizeof(buf), "\\u%04x", static_cast<unsigned>(c));
45 out += buf;
46 } else {
47 out += c;
48 }
49 break;
50 }
51 }
52 return out;
53}
54
63inline Poco::JSON::Object::Ptr mcpFailureObject(const std::string& error, const std::string& hint = {},
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);
68 out->set("error", error);
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);
75 }
76 return out;
77}
78
84inline std::string textContentResult(const std::string& text, bool isError = false) {
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);
90 content->add(item);
91 result->set("content", content);
92 if (isError) result->set("isError", true);
93 return mcpStringify(Poco::Dynamic::Var(result));
94}
95
102inline std::string imageContentResult(const std::string& base64Png, const std::string& mimeType,
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);
110 content->add(image);
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);
115 content->add(item);
116 }
117 result->set("content", content);
118 return mcpStringify(Poco::Dynamic::Var(result));
119}
120
121} // namespace eve::dev
std::string name
Definition ECS.cpp:154
vk::UniqueImage image
std::int32_t c
Poco::JSON::Object::Ptr result
std::string error
Definition Package.cpp:174
Object::Ptr value
std::string text
ScriptStructure out
std::string mcpJsonEscape(const std::string &value)
Escape a raw string for embedding inside a JSON string literal.
Definition McpJson.hpp:31
std::string mcpStringify(const Poco::Dynamic::Var &value)
Compact single-line JSON text for a newline-framed MCP payload.
Definition McpJson.hpp:24
std::string textContentResult(const std::string &text, bool isError=false)
MCP tools/call result object carrying one text content item.
Definition McpJson.hpp:84
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:102
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.
Definition McpJson.hpp:63