ToolDeclaration.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_TOOLDECLARATION_H
6#define PT_MCP_TOOLDECLARATION_H
7
8#include <Pt/Mcp/Api.h>
9#include <Pt/Mcp/Type.h>
10#include <Pt/Mcp/ContentType.h>
11#include <Pt/NonCopyable.h>
12#include <string>
13#include <vector>
14
15namespace Pt {
16
17namespace Mcp {
18
38class PT_MCP_API Tool : private NonCopyable
39{
40 public:
43 Tool(const std::string& name, const std::string& description);
44
48
53 Tool& addParam(const std::string& name, const Type& type,
54 const std::string& description = "");
55
58 Tool& setOptional(const std::string& paramName);
59
63
66 const std::string& name() const
67 { return _name; }
68
71 const std::string& description() const
72 { return _description; }
73
76 const std::vector<Property>& params() const
77 { return _params; }
78
81 std::size_t paramCount() const
82 { return _params.size(); }
83
86 int getParamIndex(const std::string& name) const;
87
90 const ContentType& content() const;
91
92 private:
93 std::string _name;
94 std::string _description;
95 std::vector<Property> _params;
96 const ContentType* _content;
97};
98
99
125class PT_MCP_API ToolDeclaration : private NonCopyable
126{
127 public:
132 ToolDeclaration(const std::string& serverName,
133 const std::string& serverVersion);
134
138
143 Tool& addTool(const std::string& name, const std::string& description);
144
147 const Tool* getTool(const std::string& name) const;
148
151 const std::string& serverName() const
152 { return _serverName; }
153
156 const std::string& serverVersion() const
157 { return _serverVersion; }
158
164 static std::string preferredVersion(const std::string& requested);
165
168 static bool isSupportedVersion(const std::string& version);
169
172 void toToolsList(std::ostream& os) const;
173
179 void toInitializeResult(std::ostream& os,
180 const char* protocolVersion = 0) const;
181
182 private:
183 std::string _serverName;
184 std::string _serverVersion;
185 std::vector<Tool*> _tools;
186};
187
188} // namespace Mcp
189
190} // namespace Pt
191
192#endif // PT_MCP_TOOLDECLARATION_H
Strategy that formats a tool result as MCP content.
Definition ContentType.h:107
ToolDeclaration(const std::string &serverName, const std::string &serverVersion)
Creates a declaration for a server named serverName.
void toToolsList(std::ostream &os) const
Writes the tools/list result JSON to os.
const std::string & serverName() const
Returns the server name.
Definition ToolDeclaration.h:151
~ToolDeclaration()
Destroys the declaration and its tools.
const std::string & serverVersion() const
Returns the server version string.
Definition ToolDeclaration.h:156
static std::string preferredVersion(const std::string &requested)
Returns the protocol version to use for requested.
void toInitializeResult(std::ostream &os, const char *protocolVersion=0) const
Writes the initialize result JSON to os.
static bool isSupportedVersion(const std::string &version)
Returns true if version is a supported protocol version.
const Tool * getTool(const std::string &name) const
Returns the tool named name, or a null pointer.
Tool & addTool(const std::string &name, const std::string &description)
Creates a tool named name and returns it.
One MCP tool in a server declaration.
Definition ToolDeclaration.h:39
Tool(const std::string &name, const std::string &description)
Creates a tool named name with description.
const std::string & name() const
Returns the tool name.
Definition ToolDeclaration.h:66
~Tool()
Destroys the tool.
Tool & setOptional(const std::string &paramName)
Marks the parameter named paramName as optional.
const std::vector< Property > & params() const
Returns the parameters of this tool.
Definition ToolDeclaration.h:76
Tool & setContent(const ContentType &content)
Sets the content type used to format the tool result.
std::size_t paramCount() const
Returns the number of parameters.
Definition ToolDeclaration.h:81
Tool & addParam(const std::string &name, const Type &type, const std::string &description="")
Adds a required parameter named name of type.
const std::string & description() const
Returns the tool description.
Definition ToolDeclaration.h:71
const ContentType & content() const
Returns the content type of the tool result.
int getParamIndex(const std::string &name) const
Returns the index of the parameter named name, or -1.
JSON Schema type for an MCP tool parameter.
Definition Type.h:34
NonCopyable()
Default constructor.
Definition NonCopyable.h:63
Model Context Protocol (MCP) services.
Core module.
Definition Allocator.h:33