Type.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_TYPE_H
31 #define PT_MCP_TYPE_H
32 
33 #include <Pt/Mcp/Api.h>
34 #include <Pt/NonCopyable.h>
35 #include <string>
36 #include <vector>
37 #include <iosfwd>
38 
39 namespace Pt {
40 
41 namespace Mcp {
42 
45 class PT_MCP_API Type : private NonCopyable
46 {
47  public:
48  enum TypeId
49  {
50  Null = 0,
51  Integer = 1,
52  Number = 2,
53  String = 3,
54  Boolean = 4,
55  Object = 5,
56  Array = 6
57  };
58 
59  explicit Type(TypeId id)
60  : _id(id)
61  {}
62 
63  virtual ~Type();
64 
65  TypeId typeId() const
66  { return _id; }
67 
70  virtual void toSchema(std::ostream& os,
71  const std::string& description = "") const;
72 
73  private:
74  TypeId _id;
75 };
76 
77 
78 PT_MCP_API const Type& nullType();
79 
80 PT_MCP_API const Type& integerType();
81 
82 PT_MCP_API const Type& numberType();
83 
84 PT_MCP_API const Type& stringType();
85 
86 PT_MCP_API const Type& booleanType();
87 
88 
91 class PT_MCP_API Property
92 {
93  public:
94  Property(const std::string& name, const Type& type,
95  const std::string& description = "");
96 
97  const std::string& name() const
98  { return _name; }
99 
100  const Type& type() const
101  { return *_type; }
102 
103  const std::string& description() const
104  { return _description; }
105 
106  bool isRequired() const
107  { return _required; }
108 
109  void setOptional()
110  { _required = false; }
111 
112  private:
113  std::string _name;
114  const Type* _type;
115  std::string _description;
116  bool _required;
117 };
118 
119 
125 class PT_MCP_API ObjectType : public Type
126 {
127  public:
128  ObjectType();
129 
130  ~ObjectType();
131 
132  ObjectType& addProperty(const std::string& name, const Type& type,
133  const std::string& description = "");
134 
135  ObjectType& setOptional(const std::string& name);
136 
140 
141  const std::vector<Property>& properties() const
142  { return _properties; }
143 
144  bool isStrict() const
145  { return _strict; }
146 
147  void toSchema(std::ostream& os,
148  const std::string& description = "") const override;
149 
150  private:
151  std::vector<Property> _properties;
152  bool _strict;
153 };
154 
155 
160 class PT_MCP_API ArrayType : public Type
161 {
162  public:
163  explicit ArrayType(const Type& items);
164 
165  ~ArrayType();
166 
167  const Type& items() const
168  { return *_items; }
169 
170  void toSchema(std::ostream& os,
171  const std::string& description = "") const override;
172 
173  private:
174  const Type* _items;
175 };
176 
177 
180 class PT_MCP_API EnumType : public Type
181 {
182  public:
183  EnumType();
184 
185  ~EnumType();
186 
187  EnumType& addValue(const std::string& value);
188 
189  const std::vector<std::string>& values() const
190  { return _values; }
191 
192  void toSchema(std::ostream& os,
193  const std::string& description = "") const override;
194 
195  private:
196  std::vector<std::string> _values;
197 };
198 
199 
204 class PT_MCP_API NullableType : public Type
205 {
206  public:
207  explicit NullableType(const Type& inner);
208 
209  ~NullableType();
210 
211  const Type& inner() const
212  { return *_inner; }
213 
214  void toSchema(std::ostream& os,
215  const std::string& description = "") const override;
216 
217  private:
218  const Type* _inner;
219 };
220 
221 } // namespace Mcp
222 
223 } // namespace Pt
224 
225 #endif // PT_MCP_TYPE_H
Core module.
Definition: Allocator.h:33
virtual void toSchema(std::ostream &os, const std::string &description="") const
Writes the JSON Schema representation to the stream.
void toSchema(std::ostream &os, const std::string &description="") const override
Writes the JSON Schema representation to the stream.
Property of an ObjectType.
Definition: Type.h:92
Object type with named properties.
Definition: Type.h:126
Nullable wrapper that allows null as an alternative value.
Definition: Type.h:205
Enum type with a fixed set of allowed string values.
Definition: Type.h:181
JSON Schema type descriptor for MCP tool parameters.
Definition: Type.h:46
void toSchema(std::ostream &os, const std::string &description="") const override
Writes the JSON Schema representation to the stream.
ObjectType & setStrict()
Disallow properties not listed in the schema.
void toSchema(std::ostream &os, const std::string &description="") const override
Writes the JSON Schema representation to the stream.
void toSchema(std::ostream &os, const std::string &description="") const override
Writes the JSON Schema representation to the stream.
Unicode capable basic_string.
Definition: Api-String.h:44
Protects derived classes from being copied.
Definition: NonCopyable.h:54
Array type with element type.
Definition: Type.h:161