ToolDeclaration.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_TOOLDECLARATION_H
31 #define PT_MCP_TOOLDECLARATION_H
32 
33 #include <Pt/Mcp/Api.h>
34 #include <Pt/Mcp/Type.h>
35 #include <Pt/NonCopyable.h>
36 #include <string>
37 #include <vector>
38 
39 namespace Pt {
40 
41 class Decomposer;
42 
43 namespace Mcp {
44 
50 class PT_MCP_API ContentType
51 {
52  public:
53  virtual ~ContentType();
54 
60  virtual void format(Decomposer* result, std::ostream& os) const = 0;
61 };
62 
63 
69 class PT_MCP_API TextContent : public ContentType
70 {
71  public:
72  TextContent();
73 
74  void format(Decomposer* result, std::ostream& os) const override;
75 };
76 
77 PT_MCP_API const TextContent& textContent();
78 
79 
85 class PT_MCP_API Tool : private NonCopyable
86 {
87  public:
88  Tool(const std::string& name, const std::string& description);
89 
90  ~Tool();
91 
92  Tool& addParam(const std::string& name, const Type& type,
93  const std::string& description = "");
94 
95  Tool& setOptional(const std::string& paramName);
96 
97  Tool& setContent(const ContentType& content);
98 
99  const std::string& name() const
100  { return _name; }
101 
102  const std::string& description() const
103  { return _description; }
104 
105  const std::vector<Property>& params() const
106  { return _params; }
107 
108  std::size_t paramCount() const
109  { return _params.size(); }
110 
111  int getParamIndex(const std::string& name) const;
112 
113  const ContentType& content() const;
114 
115  private:
116  std::string _name;
117  std::string _description;
118  std::vector<Property> _params;
119  const ContentType* _content;
120 };
121 
122 
128 class PT_MCP_API ToolDeclaration : private NonCopyable
129 {
130  public:
131  ToolDeclaration(const std::string& serverName,
132  const std::string& serverVersion);
133 
134  ~ToolDeclaration();
135 
136  Tool& addTool(const std::string& name, const std::string& description);
137 
138  const Tool* getTool(const std::string& name) const;
139 
140  const std::string& serverName() const
141  { return _serverName; }
142 
143  const std::string& serverVersion() const
144  { return _serverVersion; }
145 
151  static std::string preferredVersion(const std::string& requested);
152 
155  static bool isSupportedVersion(const std::string& version);
156 
159  void toToolsList(std::ostream& os) const;
160 
166  void toInitializeResult(std::ostream& os,
167  const char* protocolVersion = 0) const;
168 
169  private:
170  std::string _serverName;
171  std::string _serverVersion;
172  std::vector<Tool*> _tools;
173 };
174 
175 } // namespace Mcp
176 
177 } // namespace Pt
178 
179 #endif // PT_MCP_TOOLDECLARATION_H
Core module.
Definition: Allocator.h:33
Manages the decomposition of types during serialization.
Definition: Decomposer.h:44
static bool isSupportedVersion(const std::string &version)
Returns true if version is a supported protocol version.
virtual void format(Decomposer *result, std::ostream &os) const =0
Formats the decomposed result into an MCP content block.
void format(Decomposer *result, std::ostream &os) const override
Formats the decomposed result into an MCP content block.
JSON Schema type descriptor for MCP tool parameters.
Definition: Type.h:46
MCP tool descriptor.
Definition: ToolDeclaration.h:86
MCP server and tool registry.
Definition: ToolDeclaration.h:129
void toInitializeResult(std::ostream &os, const char *protocolVersion=0) const
Writes the initialize result JSON to the stream.
void toToolsList(std::ostream &os) const
Writes the tools/list result JSON to the stream.
static std::string preferredVersion(const std::string &requested)
Returns the best supported protocol version for the given request.
Formats a tool result as MCP content.
Definition: ToolDeclaration.h:51
Protects derived classes from being copied.
Definition: NonCopyable.h:54
Formats tool results as MCP text content.
Definition: ToolDeclaration.h:70