ContentType.h
1 /*
2  * Copyright (C) 2020-2026 by Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
27  * MA 02110-1301 USA
28  */
29 
30 #ifndef PT_MCP_CONTENTTYPE_H
31 #define PT_MCP_CONTENTTYPE_H
32 
33 #include <Pt/Mcp/Api.h>
34 #include <Pt/Formatter.h>
35 #include <string>
36 
37 namespace Pt {
38 
39 namespace Mcp {
40 
54 class PT_MCP_API ContentFormatter
55 {
56  public:
57  virtual ~ContentFormatter();
58 
62  Pt::Formatter& beginContent(std::ostream& os);
63 
67  void finishContent(std::ostream& os);
68 
69  protected:
71 
77  virtual void onBeginContent() = 0;
78 
83  virtual Pt::Formatter& onBeginFormat() = 0;
84 
87  virtual void onFinishContent() = 0;
88 
91  void write(const char* s, std::size_t n);
92 
95  std::ostream& output() const
96  { return *_os; }
97 
98  private:
99  std::ostream* _os;
100 };
101 
119 class PT_MCP_API ContentType
120 {
121  public:
122  virtual ~ContentType();
123 
131  virtual ContentFormatter* getFormatter() const = 0;
132 
139  virtual void releaseFormatter(ContentFormatter* formatter) const = 0;
140 };
141 
142 
148 class PT_MCP_API TextContent : public ContentType
149 {
150  public:
151  TextContent();
152 
153  ContentFormatter* getFormatter() const override;
154 
155  void releaseFormatter(ContentFormatter* formatter) const override;
156 };
157 
158 PT_MCP_API const TextContent& textContent();
159 
160 
168 class PT_MCP_API ImageContent : public ContentType
169 {
170  public:
171  explicit ImageContent(const std::string& mimeType = "image/png");
172 
173  ContentFormatter* getFormatter() const override;
174 
175  void releaseFormatter(ContentFormatter* formatter) const override;
176 
177  private:
178  std::string _mimeType;
179 };
180 
181 PT_MCP_API const ImageContent& imageContent();
182 
183 } // namespace Mcp
184 
185 } // namespace Pt
186 
187 #endif // PT_MCP_CONTENTTYPE_H
Core module.
Definition: Allocator.h:33
virtual void onBeginContent()=0
Writes the opening bytes of the concrete content block.
ContentFormatter * getFormatter() const override
Creates a new ContentFormatter for a result.
virtual Pt::Formatter & onBeginFormat()=0
Returns the Formatter the decomposed result is drained into.
virtual ContentFormatter * getFormatter() const =0
Creates a new ContentFormatter for a result.
std::ostream & output() const
Returns the output stream passed to beginContent().
Definition: ContentType.h:95
Formats tool results as MCP image content.
Definition: ContentType.h:169
Writes the wrapper bytes of an MCP content block around a Formatter.
Definition: ContentType.h:55
ContentFormatter * getFormatter() const override
Creates a new ContentFormatter for a result.
void releaseFormatter(ContentFormatter *formatter) const override
Releases a ContentFormatter previously obtained from getFormatter().
void releaseFormatter(ContentFormatter *formatter) const override
Releases a ContentFormatter previously obtained from getFormatter().
virtual void releaseFormatter(ContentFormatter *formatter) const =0
Releases a ContentFormatter previously obtained from getFormatter().
Formats a tool result as MCP content.
Definition: ContentType.h:120
virtual void onFinishContent()=0
Writes the closing bytes of the concrete content block.
Support for serialization to different formats.
Definition: Formatter.h:46
void finishContent(std::ostream &os)
Flushes buffered output and writes the closing bytes of the content array to os.
Formats tool results as MCP text content.
Definition: ContentType.h:149
void write(const char *s, std::size_t n)
Writes raw bytes to the output stream.
Pt::Formatter & beginContent(std::ostream &os)
Writes the opening bytes of the content block to os and returns the Formatter the result is to be dec...