ContentType.h
1/* Copyright (C) 2020-2026 by Marc Boris Duerner
2 SPDX-License-Identifier: LGPL-2.1-or-later WITH mif-exception
3*/
4
5#ifndef PT_MCP_CONTENTTYPE_H
6#define PT_MCP_CONTENTTYPE_H
7
8#include <Pt/Mcp/Api.h>
9#include <Pt/Formatter.h>
10#include <string>
11
12namespace Pt {
13
14namespace Mcp {
15
35class PT_MCP_API ContentFormatter
36{
37 public:
41
46 Pt::Formatter& beginContent(std::ostream& os);
47
50 void finishContent(std::ostream& os);
51
52 protected:
56
62 virtual void onBeginContent() = 0;
63
70
73 virtual void onFinishContent() = 0;
74
77 void write(const char* s, std::size_t n);
78
81 std::ostream& output() const
82 { return *_os; }
83
84 private:
85 std::ostream* _os;
86};
87
106class PT_MCP_API ContentType
107{
108 public:
111 virtual ~ContentType();
112
119 virtual ContentFormatter* getFormatter() const = 0;
120
127 virtual void releaseFormatter(ContentFormatter* formatter) const = 0;
128};
129
130
140class PT_MCP_API TextContent : public ContentType
141{
142 public:
146
150
153 void releaseFormatter(ContentFormatter* formatter) const override;
154};
155
160PT_MCP_API const TextContent& textContent();
161
162
177class PT_MCP_API ImageContent : public ContentType
178{
179 public:
182 explicit ImageContent(const std::string& mimeType = "image/png");
183
187
190 void releaseFormatter(ContentFormatter* formatter) const override;
191
192 private:
193 std::string _mimeType;
194};
195
200PT_MCP_API const ImageContent& imageContent();
201
202} // namespace Mcp
203
204} // namespace Pt
205
206#endif // PT_MCP_CONTENTTYPE_H
Support for serialization to different formats.
Definition Formatter.h:46
Writer for one MCP content block around a Formatter.
Definition ContentType.h:36
void finishContent(std::ostream &os)
Flushes buffered output and writes the closing bytes to os.
void write(const char *s, std::size_t n)
Writes n raw bytes from s to the output stream.
Pt::Formatter & beginContent(std::ostream &os)
Writes the opening bytes of the content block to os.
virtual void onFinishContent()=0
Writes the closing bytes of the concrete content block.
virtual ~ContentFormatter()
Destroys the formatter.
virtual Pt::Formatter & onBeginFormat()=0
Returns the Formatter the decomposed result is drained into.
std::ostream & output() const
Returns the output stream passed to beginContent().
Definition ContentType.h:81
virtual void onBeginContent()=0
Writes the opening bytes of the concrete content block.
ContentFormatter()
Creates an idle content formatter.
Strategy that formats a tool result as MCP content.
Definition ContentType.h:107
virtual void releaseFormatter(ContentFormatter *formatter) const =0
Releases a formatter previously obtained from getFormatter().
virtual ContentFormatter * getFormatter() const =0
Creates a new ContentFormatter for a result.
virtual ~ContentType()
Destroys the content type.
Formats tool results as MCP image content.
Definition ContentType.h:178
ImageContent(const std::string &mimeType="image/png")
Creates an image content type with MIME type mimeType.
void releaseFormatter(ContentFormatter *formatter) const override
Releases a formatter previously obtained from getFormatter().
ContentFormatter * getFormatter() const override
Creates a new formatter for an image content block.
Formats tool results as MCP text content.
Definition ContentType.h:141
void releaseFormatter(ContentFormatter *formatter) const override
Releases a formatter previously obtained from getFormatter().
TextContent()
Creates a text content type.
ContentFormatter * getFormatter() const override
Creates a new formatter for a text content block.
PT_MCP_API const ImageContent & imageContent()
Returns the process-wide PNG image content type.
PT_MCP_API const TextContent & textContent()
Returns the process-wide text content type.
Model Context Protocol (MCP) services.
Core module.
Definition Allocator.h:33