/src/poco/AI/MCP/src/ToolResult.cpp
Line | Count | Source |
1 | | // |
2 | | // ToolResult.cpp |
3 | | // |
4 | | // Library: AI/MCP |
5 | | // Package: Core |
6 | | // Module: ToolResult |
7 | | // |
8 | | // Copyright (c) 2026, Aleph ONE Software Engineering LLC. |
9 | | // and Contributors. |
10 | | // |
11 | | // SPDX-License-Identifier: BSL-1.0 |
12 | | // |
13 | | |
14 | | |
15 | | #include "Poco/AI/MCP/ToolResult.h" |
16 | | #include "Poco/Exception.h" |
17 | | |
18 | | |
19 | | namespace Poco::AI::MCP { |
20 | | |
21 | | |
22 | | namespace |
23 | | { |
24 | | bool isBlock(const Poco::JSON::Object::Ptr& pBlock) |
25 | 40 | { |
26 | 40 | return pBlock != nullptr && pBlock->get("type").isString(); |
27 | 40 | } |
28 | | } |
29 | | |
30 | | |
31 | | ToolResult::ToolResult(): |
32 | 2.36k | _pContent(new Poco::JSON::Array), |
33 | 2.36k | _hasStructuredContent(false), |
34 | 2.36k | _isError(false) |
35 | 2.36k | { |
36 | 2.36k | } |
37 | | |
38 | | |
39 | | ToolResult ToolResult::error(const std::string& text) |
40 | 0 | { |
41 | 0 | ToolResult result; |
42 | 0 | result.addText(text).setError(); |
43 | 0 | return result; |
44 | 0 | } |
45 | | |
46 | | |
47 | | ToolResult& ToolResult::addText(const std::string& text) |
48 | 881 | { |
49 | 881 | Poco::JSON::Object::Ptr pBlock = new Poco::JSON::Object; |
50 | 881 | pBlock->set("type", "text"); |
51 | 881 | pBlock->set("text", text); |
52 | 881 | _pContent->add(pBlock); |
53 | 881 | return *this; |
54 | 881 | } |
55 | | |
56 | | |
57 | | ToolResult& ToolResult::addImage(const std::string& data, const std::string& mimeType) |
58 | 0 | { |
59 | 0 | Poco::JSON::Object::Ptr pBlock = new Poco::JSON::Object; |
60 | 0 | pBlock->set("type", "image"); |
61 | 0 | pBlock->set("data", data); |
62 | 0 | pBlock->set("mimeType", mimeType); |
63 | 0 | _pContent->add(pBlock); |
64 | 0 | return *this; |
65 | 0 | } |
66 | | |
67 | | |
68 | | ToolResult& ToolResult::addAudio(const std::string& data, const std::string& mimeType) |
69 | 0 | { |
70 | 0 | Poco::JSON::Object::Ptr pBlock = new Poco::JSON::Object; |
71 | 0 | pBlock->set("type", "audio"); |
72 | 0 | pBlock->set("data", data); |
73 | 0 | pBlock->set("mimeType", mimeType); |
74 | 0 | _pContent->add(pBlock); |
75 | 0 | return *this; |
76 | 0 | } |
77 | | |
78 | | |
79 | | ToolResult& ToolResult::addResourceLink(const std::string& uri, const std::string& name, const std::string& mimeType) |
80 | 0 | { |
81 | 0 | Poco::JSON::Object::Ptr pBlock = new Poco::JSON::Object; |
82 | 0 | pBlock->set("type", "resource_link"); |
83 | 0 | pBlock->set("uri", uri); |
84 | 0 | pBlock->set("name", name); |
85 | 0 | if (!mimeType.empty()) pBlock->set("mimeType", mimeType); |
86 | 0 | _pContent->add(pBlock); |
87 | 0 | return *this; |
88 | 0 | } |
89 | | |
90 | | |
91 | | ToolResult& ToolResult::addResource(const ResourceContents& contents) |
92 | 0 | { |
93 | 0 | Poco::JSON::Object::Ptr pBlock = new Poco::JSON::Object; |
94 | 0 | pBlock->set("type", "resource"); |
95 | 0 | pBlock->set("resource", contents.toObject()); |
96 | 0 | _pContent->add(pBlock); |
97 | 0 | return *this; |
98 | 0 | } |
99 | | |
100 | | |
101 | | ToolResult& ToolResult::addContent(const Poco::JSON::Object::Ptr& block) |
102 | 0 | { |
103 | 0 | if (!isBlock(block)) |
104 | 0 | { |
105 | 0 | throw Poco::InvalidArgumentException("A content block needs a string type"); |
106 | 0 | } |
107 | 0 | _pContent->add(block); |
108 | 0 | return *this; |
109 | 0 | } |
110 | | |
111 | | |
112 | | ToolResult& ToolResult::setError(bool isError) |
113 | 0 | { |
114 | 0 | _isError = isError; |
115 | 0 | return *this; |
116 | 0 | } |
117 | | |
118 | | |
119 | | ToolResult& ToolResult::setStructuredContent(const Poco::Dynamic::Var& structured) |
120 | 0 | { |
121 | 0 | _structuredContent = structured; |
122 | 0 | _hasStructuredContent = true; |
123 | 0 | return *this; |
124 | 0 | } |
125 | | |
126 | | |
127 | | ToolResult& ToolResult::removeStructuredContent() |
128 | 0 | { |
129 | 0 | _structuredContent.clear(); |
130 | 0 | _hasStructuredContent = false; |
131 | 0 | return *this; |
132 | 0 | } |
133 | | |
134 | | |
135 | | Poco::JSON::Object::Ptr ToolResult::toObject() const |
136 | 891 | { |
137 | 891 | Poco::JSON::Object::Ptr pObject = new Poco::JSON::Object; |
138 | 891 | pObject->set("content", _pContent); |
139 | 891 | if (_hasStructuredContent) |
140 | 4 | { |
141 | 4 | pObject->set("structuredContent", _structuredContent); |
142 | 4 | } |
143 | 891 | pObject->set("isError", _isError); |
144 | 891 | return pObject; |
145 | 891 | } |
146 | | |
147 | | |
148 | | ToolResult ToolResult::fromObject(const Poco::JSON::Object::Ptr& object) |
149 | 604 | { |
150 | 604 | if (object == nullptr) |
151 | 0 | { |
152 | 0 | throw Poco::DataFormatException("Tool result is not an object"); |
153 | 0 | } |
154 | 604 | if (!object->isArray("content")) |
155 | 585 | { |
156 | 585 | throw Poco::DataFormatException("Tool result without a content array"); |
157 | 585 | } |
158 | 19 | const Poco::JSON::Array::Ptr pContent = object->getArray("content"); |
159 | 56 | for (std::size_t i = 0; i < pContent->size(); ++i) |
160 | 40 | { |
161 | 40 | if (!isBlock(pContent->getObject(static_cast<unsigned>(i)))) |
162 | 3 | { |
163 | 3 | throw Poco::DataFormatException("Tool result content block without a string type"); |
164 | 3 | } |
165 | 40 | } |
166 | 16 | const Poco::Dynamic::Var isError = object->get("isError"); |
167 | 16 | if (!isError.isEmpty() && !isError.isBoolean()) |
168 | 0 | { |
169 | 0 | throw Poco::DataFormatException("Tool result with an isError that is not a boolean"); |
170 | 0 | } |
171 | | |
172 | 16 | ToolResult result; |
173 | 16 | result._pContent = pContent; |
174 | 16 | result._structuredContent = object->get("structuredContent"); |
175 | 16 | result._hasStructuredContent = object->has("structuredContent"); |
176 | 16 | result._isError = !isError.isEmpty() && isError.extract<bool>(); |
177 | 16 | return result; |
178 | 16 | } |
179 | | |
180 | | |
181 | | } // namespace Poco::AI::MCP |