Type.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_TYPE_H
6#define PT_MCP_TYPE_H
7
8#include <Pt/Mcp/Api.h>
9#include <Pt/NonCopyable.h>
10#include <string>
11#include <vector>
12#include <iosfwd>
13
14namespace Pt {
15
16namespace Mcp {
17
33class PT_MCP_API Type : private NonCopyable
34{
35 public:
38 enum TypeId
39 {
40 Null = 0,
41 Integer = 1,
42 Number = 2,
43 String = 3,
44 Boolean = 4,
45 Object = 5,
46 Array = 6
47 };
48
51 explicit Type(TypeId id)
52 : _id(id)
53 {}
54
57 virtual ~Type();
58
61 TypeId typeId() const
62 { return _id; }
63
69 virtual void toSchema(std::ostream& os,
70 const std::string& description = "") const;
71
72 private:
73 TypeId _id;
74};
75
76
81PT_MCP_API const Type& nullType();
82
87PT_MCP_API const Type& integerType();
88
93PT_MCP_API const Type& numberType();
94
99PT_MCP_API const Type& stringType();
100
105PT_MCP_API const Type& booleanType();
106
107
117class PT_MCP_API Property
118{
119 public:
124 Property(const std::string& name, const Type& type,
125 const std::string& description = "");
126
129 const std::string& name() const
130 { return _name; }
131
134 const Type& type() const
135 { return *_type; }
136
139 const std::string& description() const
140 { return _description; }
141
144 bool isRequired() const
145 { return _required; }
146
150 { _required = false; }
151
152 private:
153 std::string _name;
154 const Type* _type;
155 std::string _description;
156 bool _required;
157};
158
159
174class PT_MCP_API ObjectType : public Type
175{
176 public:
180
184
189 ObjectType& addProperty(const std::string& name, const Type& type,
190 const std::string& description = "");
191
194 ObjectType& setOptional(const std::string& name);
195
199
202 const std::vector<Property>& properties() const
203 { return _properties; }
204
207 bool isStrict() const
208 { return _strict; }
209
212 void toSchema(std::ostream& os,
213 const std::string& description = "") const override;
214
215 private:
216 std::vector<Property> _properties;
217 bool _strict;
218};
219
220
229class PT_MCP_API ArrayType : public Type
230{
231 public:
234 explicit ArrayType(const Type& items);
235
239
242 const Type& items() const
243 { return *_items; }
244
247 void toSchema(std::ostream& os,
248 const std::string& description = "") const override;
249
250 private:
251 const Type* _items;
252};
253
254
264class PT_MCP_API EnumType : public Type
265{
266 public:
270
274
277 EnumType& addValue(const std::string& value);
278
281 const std::vector<std::string>& values() const
282 { return _values; }
283
286 void toSchema(std::ostream& os,
287 const std::string& description = "") const override;
288
289 private:
290 std::vector<std::string> _values;
291};
292
293
304class PT_MCP_API NullableType : public Type
305{
306 public:
309 explicit NullableType(const Type& inner);
310
314
317 const Type& inner() const
318 { return *_inner; }
319
322 void toSchema(std::ostream& os,
323 const std::string& description = "") const override;
324
325 private:
326 const Type* _inner;
327};
328
329} // namespace Mcp
330
331} // namespace Pt
332
333#endif // PT_MCP_TYPE_H
void toSchema(std::ostream &os, const std::string &description="") const override
Writes this array as a JSON Schema object to os.
~ArrayType()
Destroys the array schema.
const Type & items() const
Returns the schema type of the array elements.
Definition Type.h:242
ArrayType(const Type &items)
Creates an array schema whose items have type items.
const std::vector< std::string > & values() const
Returns the allowed string values.
Definition Type.h:281
EnumType & addValue(const std::string &value)
Adds value to the set of allowed strings.
void toSchema(std::ostream &os, const std::string &description="") const override
Writes this enum as a JSON Schema object to os.
~EnumType()
Destroys the enum schema.
EnumType()
Creates an enum schema with no values.
const Type & inner() const
Returns the wrapped schema type.
Definition Type.h:317
void toSchema(std::ostream &os, const std::string &description="") const override
Writes this nullable type as a JSON Schema object to os.
NullableType(const Type &inner)
Creates a nullable wrapper around inner.
~NullableType()
Destroys the nullable wrapper.
ObjectType()
Creates an empty object schema.
ObjectType & setOptional(const std::string &name)
Marks the property named name as optional.
bool isStrict() const
Returns true when additional properties are forbidden.
Definition Type.h:207
void toSchema(std::ostream &os, const std::string &description="") const override
Writes this object as a JSON Schema object to os.
const std::vector< Property > & properties() const
Returns the properties of this object schema.
Definition Type.h:202
ObjectType & setStrict()
Forbids properties that are not listed in the schema.
~ObjectType()
Destroys the object schema.
ObjectType & addProperty(const std::string &name, const Type &type, const std::string &description="")
Adds a required property named name of type.
Property(const std::string &name, const Type &type, const std::string &description="")
Creates a required property named name of type.
const Type & type() const
Returns the schema type of this property.
Definition Type.h:134
const std::string & name() const
Returns the property name.
Definition Type.h:129
bool isRequired() const
Returns true when the property is required.
Definition Type.h:144
const std::string & description() const
Returns the schema description of this property.
Definition Type.h:139
void setOptional()
Marks the property as optional.
Definition Type.h:149
JSON Schema type for an MCP tool parameter.
Definition Type.h:34
virtual void toSchema(std::ostream &os, const std::string &description="") const
Writes this type as a JSON Schema object to os.
TypeId typeId() const
Returns the JSON Schema kind of this type.
Definition Type.h:61
Type(TypeId id)
Creates a type with schema kind id.
Definition Type.h:51
TypeId
JSON Schema kind of a Type.
Definition Type.h:39
@ Integer
JSON integer.
Definition Type.h:41
@ Boolean
JSON boolean.
Definition Type.h:44
@ Object
JSON object.
Definition Type.h:45
@ Null
JSON null.
Definition Type.h:40
@ String
JSON string.
Definition Type.h:43
@ Array
JSON array.
Definition Type.h:46
@ Number
JSON number.
Definition Type.h:42
virtual ~Type()
Destroys the type.
NonCopyable()
Default constructor.
Definition NonCopyable.h:63
PT_MCP_API const Type & nullType()
Returns the process-wide JSON null schema type.
PT_MCP_API const Type & stringType()
Returns the process-wide JSON string schema type.
PT_MCP_API const Type & integerType()
Returns the process-wide JSON integer schema type.
PT_MCP_API const Type & numberType()
Returns the process-wide JSON number schema type.
PT_MCP_API const Type & booleanType()
Returns the process-wide JSON boolean schema type.
Model Context Protocol (MCP) services.
Core module.
Definition Allocator.h:33