Entropic 2.3.8
Local-first agentic inference engine
Loading...
Searching...
No Matches
tool_registry.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
11
12#include <nlohmann/json.hpp>
13
14static auto logger = entropic::log::get("mcp.tool_registry");
15
16namespace entropic {
17
25 if (tool == nullptr) {
26 logger->warn("Attempted to register null tool");
27 return;
28 }
29 const auto& name = tool->name();
30 if (tools_.count(name) > 0) {
31 logger->warn("Tool '{}' already registered — replacing", name);
32 }
33 tools_[name] = tool;
34 logger->info("Registered tool: {}", name);
35}
36
44bool ToolRegistry::has_tool(const std::string& name) const {
45 return tools_.count(name) > 0;
46}
47
54std::string ToolRegistry::get_tools_json() const {
55 auto arr = nlohmann::json::array();
56 for (const auto& [name, tool] : tools_) {
57 nlohmann::json entry;
58 entry["name"] = tool->definition().name;
59 entry["description"] = tool->definition().description;
60 entry["inputSchema"] = nlohmann::json::parse(
61 tool->definition().input_schema);
62 arr.push_back(std::move(entry));
63 }
64 return arr.dump();
65}
66
73std::vector<const ToolDefinition*> ToolRegistry::get_definitions() const {
74 std::vector<const ToolDefinition*> defs;
75 defs.reserve(tools_.size());
76 for (const auto& [name, tool] : tools_) {
77 defs.push_back(&tool->definition());
78 }
79 return defs;
80}
81
91 const std::string& name,
92 const std::string& args_json) {
93 auto it = tools_.find(name);
94 if (it == tools_.end()) {
95 logger->warn("Unknown tool: {}", name);
96 ServerResponse resp;
97 resp.result = "Error: Unknown tool '" + name + "'";
98 return resp;
99 }
100 logger->info("Dispatch: tool='{}'", name);
101 return it->second->execute(args_json);
102}
103
111ToolBase* ToolRegistry::get_tool(const std::string& name) const {
112 auto it = tools_.find(name);
113 if (it == tools_.end()) {
114 return nullptr;
115 }
116 return it->second;
117}
118
119} // namespace entropic
Abstract base class for individual MCP tools.
Definition tool_base.h:45
const std::string & name() const
Get the tool name.
Definition tool_base.cpp:36
void register_tool(ToolBase *tool)
Register a tool instance.
ServerResponse dispatch(const std::string &name, const std::string &args_json)
Dispatch a tool call to the registered tool's execute().
std::string get_tools_json() const
Get all registered tool definitions as a JSON array string.
bool has_tool(const std::string &name) const
Check if a tool is registered by name.
ToolBase * get_tool(const std::string &name) const
Get a registered tool by name.
std::vector< const ToolDefinition * > get_definitions() const
Get all registered tool definitions.
spdlog initialization and logger access.
ENTROPIC_EXPORT std::shared_ptr< spdlog::logger > get(const std::string &name)
Get or create a named logger.
Definition logging.cpp:211
Activate model on GPU (WARM → ACTIVE).
MCPServerBase concrete base class + ServerResponse.
Structured result from tool execution.
Definition server_base.h:33
std::string result
Human-readable result.
Definition server_base.h:34
Tool registration, lookup, and dispatch.