Sonde v1.1.0
Embeddable Model Context Protocol server for Qt applications
Loading...
Searching...
No Matches
types.h
Go to the documentation of this file.
1#pragma once
2
6
7#include <sonde/global.h>
8
9#include <QtCore/QByteArray>
10#include <QtCore/QDateTime>
11#include <QtCore/QJsonArray>
12#include <QtCore/QJsonObject>
13#include <QtCore/QJsonValue>
14#include <QtCore/QSharedDataPointer>
15#include <QtCore/QString>
16
17#include <functional>
18#include <memory>
19#include <optional>
20
21QT_BEGIN_NAMESPACE
22class QObject;
23QT_END_NAMESPACE
24
26namespace Sonde {
27
28class Server;
29
41
43SONDE_CORE_EXPORT QString logLevelName(LogLevel level);
46SONDE_CORE_EXPORT std::optional<LogLevel> logLevelFromName(QStringView name);
48SONDE_CORE_EXPORT QStringList logLevelNames();
49
50// ---------------------------------------------------------------------------
51// Tools
52// ---------------------------------------------------------------------------
53
55struct SONDE_CORE_EXPORT ToolDefinition
56{
57 QString name;
58 QString title;
59 QString description;
60 QJsonObject inputSchema;
61 QJsonObject outputSchema;
62 QJsonObject annotations;
63
65 QJsonObject toJson() const;
66};
67
68class ToolResultData;
69
77class SONDE_CORE_EXPORT ToolResult
78{
79public:
81 ToolResult();
85 ToolResult(const ToolResult &other);
90
92 static ToolResult text(const QString &text);
94 static ToolResult error(const QString &message);
98 static ToolResult image(const QByteArray &data, const QString &mimeType);
101 static ToolResult structured(const QJsonObject &object);
102
104 ToolResult &addText(const QString &text);
106 ToolResult &addImage(const QByteArray &data, const QString &mimeType);
108 ToolResult &addContent(const QJsonObject &contentBlock);
110 ToolResult &setStructuredContent(const QJsonObject &object);
112 ToolResult &setError(bool isError = true);
113
115 bool isError() const;
117 QJsonArray content() const;
119 QJsonObject toJson() const;
120
121private:
122 QSharedDataPointer<ToolResultData> d;
123};
124
125class ToolCallPrivate;
126
141class SONDE_CORE_EXPORT ToolCall
142{
143public:
145 ToolCall() = default;
146
148 bool isValid() const { return d != nullptr; }
150 QString toolName() const;
152 QJsonObject arguments() const;
154 QString sessionId() const;
155
157 bool isFinished() const;
159 bool isCancelled() const;
160
162 void finish(const ToolResult &result) const;
164 void fail(const QString &message) const { finish(ToolResult::error(message)); }
165
170 void reportProgress(double progress, double total = -1, const QString &message = { }) const;
171
174 void onCancelled(std::function<void()> callback) const;
175
180 QObject *context() const;
181
182private:
183 friend class ServerPrivate;
185 explicit ToolCall(std::shared_ptr<ToolCallPrivate> state) : d(std::move(state)) { }
186 std::shared_ptr<ToolCallPrivate> d;
187};
188
190using ToolHandler = std::function<ToolResult(const QJsonObject &arguments)>;
192using AsyncToolHandler = std::function<void(const ToolCall &call)>;
193
194// ---------------------------------------------------------------------------
195// Resources
196// ---------------------------------------------------------------------------
197
199struct SONDE_CORE_EXPORT ResourceDefinition
200{
201 QString uri;
202 QString name;
203 QString title;
204 QString description;
205 QString mimeType;
206
208 QJsonObject toJson() const;
209};
210
212struct SONDE_CORE_EXPORT ResourceContents
213{
214 QString mimeType;
215 QString text;
216 QByteArray blob;
217 bool isBinary = false;
218
220 static ResourceContents fromText(const QString &text,
221 const QString &mimeType = QStringLiteral("text/plain"));
223 static ResourceContents fromBlob(const QByteArray &data, const QString &mimeType);
224};
225
227using ResourceReader = std::function<ResourceContents(const QString &uri)>;
228
229// ---------------------------------------------------------------------------
230// Events
231// ---------------------------------------------------------------------------
232
234struct SONDE_CORE_EXPORT Event
235{
236 quint64 sequence = 0;
237 QDateTime timestamp;
238 QString source;
240 QString category;
241 QString message;
242 QJsonObject data;
243
245 QJsonObject toJson() const;
249 QString toLine() const;
250};
251
252} // namespace Sonde
An embeddable Model Context Protocol server.
Definition server.h:92
The shared state of one tool call.
Definition server_p.h:243
Handle for an in-flight tools/call, used by asynchronous tools.
Definition types.h:142
bool isValid() const
Whether the handle refers to a call.
Definition types.h:148
std::shared_ptr< ToolCallPrivate > d
Shared call state.
Definition types.h:186
void finish(const ToolResult &result) const
Answers the call. Later calls are ignored.
Definition toolcall.cpp:117
ToolCall(std::shared_ptr< ToolCallPrivate > state)
Wraps the shared call state (created by the server).
Definition types.h:185
void fail(const QString &message) const
Answers the call with ToolResult::error(message).
Definition types.h:164
ToolCall()=default
An invalid handle; every operation on it is a no-op.
ToolResult's implicitly shared contents.
Definition types.cpp:63
The result of a tools/call: content blocks, optional structured content, and an error flag.
Definition types.h:78
static ToolResult text(const QString &text)
A result with a single text block.
Definition types.cpp:85
static ToolResult structured(const QJsonObject &object)
A result whose structuredContent is object, plus the same object serialized as text for clients that ...
Definition types.cpp:100
ToolResult(const ToolResult &other)
Shares other's contents (copied when either changes).
QJsonArray content() const
The content blocks.
Definition types.cpp:80
static ToolResult image(const QByteArray &data, const QString &mimeType)
A result with a single image block.
Definition types.cpp:95
ToolResult & addImage(const QByteArray &data, const QString &mimeType)
Appends an image block from raw bytes (encoded to base64 here).
Definition types.cpp:114
static ToolResult error(const QString &message)
A failed result (isError) with an explanatory text block.
Definition types.cpp:90
QJsonObject toJson() const
The result as a tools/call result object.
Definition types.cpp:140
bool isError() const
Whether the result is marked as an error.
Definition types.cpp:75
ToolResult & addContent(const QJsonObject &contentBlock)
Appends an arbitrary MCP content block (e.g. an embedded resource).
Definition types.cpp:122
ToolResult & operator=(const ToolResult &other)
Shares other's contents (copied when either changes).
QSharedDataPointer< ToolResultData > d
Implicitly shared contents.
Definition types.h:122
~ToolResult()
Destroys the result.
ToolResult & addText(const QString &text)
Appends a text block.
Definition types.cpp:107
ToolResult()
An empty, successful result.
Definition types.cpp:70
ToolResult & setError(bool isError=true)
Marks the result as a tool error (or clears the mark).
Definition types.cpp:134
ToolResult & setStructuredContent(const QJsonObject &object)
Sets the structuredContent object.
Definition types.cpp:128
What every Sonde header includes: the version, and the deprecation macros (design 0031).
Embeddable Model Context Protocol server for Qt applications.
Definition dispatch.cpp:10
std::function< ResourceContents(const QString &uri)> ResourceReader
Produces a resource's contents when a client reads it.
Definition types.h:227
std::optional< LogLevel > logLevelFromName(QStringView name)
Parses an MCP level name (case-insensitive).
Definition types.cpp:22
QString logLevelName(LogLevel level)
The MCP wire name of level, e.g. "warning".
Definition types.cpp:17
std::function< ToolResult(const QJsonObject &arguments)> ToolHandler
Handler of a synchronous tool: arguments in, result out.
Definition types.h:190
LogLevel
MCP (RFC 5424) log levels, from lowest to highest severity.
Definition types.h:31
@ Warning
Warning conditions.
Definition types.h:35
@ Notice
Normal but significant events.
Definition types.h:34
@ Critical
Critical conditions.
Definition types.h:37
@ Info
General informational messages.
Definition types.h:33
@ Emergency
The system is unusable.
Definition types.h:39
@ Debug
Detailed debugging information.
Definition types.h:32
@ Alert
Action must be taken immediately.
Definition types.h:38
std::function< void(const ToolCall &call)> AsyncToolHandler
Handler of an asynchronous tool; answer via ToolCall::finish().
Definition types.h:192
QStringList logLevelNames()
All level names, lowest severity first.
Definition types.cpp:33
Why something failed.
Definition expected.h:42
One entry of an event stream (see EventSource).
Definition types.h:235
QJsonObject toJson() const
The event as JSON (used for log notifications).
Definition types.cpp:184
QString toLine() const
The event as text: "#seq time level source[category]: message {data}".
Definition types.cpp:200
QJsonObject data
Extra source-specific fields.
Definition types.h:242
QString source
EventSource::name() of the producer.
Definition types.h:238
QDateTime timestamp
When the event happened.
Definition types.h:237
QString category
Optional sub-category, e.g. a Qt logging category.
Definition types.h:240
LogLevel level
Severity.
Definition types.h:239
quint64 sequence
Assigned by the server; increases monotonically.
Definition types.h:236
QString message
Human-readable message.
Definition types.h:241
Contents returned by a ResourceReader: text or binary data.
Definition types.h:213
QString text
Text contents, used when !isBinary.
Definition types.h:215
bool isBinary
Whether blob rather than text holds the contents.
Definition types.h:217
static ResourceContents fromText(const QString &text, const QString &mimeType=QStringLiteral("text/plain"))
Text contents.
Definition types.cpp:165
QByteArray blob
Raw bytes, used when isBinary (base64-encoded on the wire).
Definition types.h:216
QString mimeType
MIME type; empty falls back to the definition's.
Definition types.h:214
static ResourceContents fromBlob(const QByteArray &data, const QString &mimeType)
Binary contents.
Definition types.cpp:173
Describes a resource as advertised in resources/list.
Definition types.h:200
QJsonObject toJson() const
The definition as a resources/list entry.
Definition types.cpp:152
QString uri
Unique URI, e.g. "app://config".
Definition types.h:201
QString description
Optional description.
Definition types.h:204
QString name
Short name; defaults to the URI.
Definition types.h:202
QString mimeType
Default MIME type of the contents.
Definition types.h:205
QString title
Optional human-readable title.
Definition types.h:203
Describes a tool as advertised in tools/list.
Definition types.h:56
QJsonObject inputSchema
JSON Schema of the arguments; empty means {"type":"object"}.
Definition types.h:60
QString title
Optional human-readable title.
Definition types.h:58
QJsonObject annotations
Optional hints (readOnlyHint, destructiveHint, ...).
Definition types.h:62
QJsonObject outputSchema
Optional JSON Schema of structuredContent.
Definition types.h:61
QJsonObject toJson() const
The definition as a tools/list entry.
Definition types.cpp:43
QString description
What the model reads to decide when to use the tool.
Definition types.h:59
QString name
Unique name, characters [A-Za-z0-9_.-].
Definition types.h:57