Entropic 2.3.8
Local-first agentic inference engine
Loading...
Searching...
No Matches
server_base.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
10
11#include <nlohmann/json.hpp>
12
13static auto logger = entropic::log::get("mcp.server_base");
14
15namespace entropic {
16
24 : name_(std::move(name)) {}
25
32const std::string& MCPServerBase::name() const {
33 return name_;
34}
35
45
52std::string MCPServerBase::list_tools() const {
54}
55
65 const std::string& tool_name,
66 const std::string& args_json) {
67 logger->info("[EXECUTE] {}.{}", name_, tool_name);
68
69 auto response = registry_.dispatch(tool_name, args_json);
70
71 auto* tool = registry_.get_tool(tool_name);
72 if (tool != nullptr) {
73 inject_anchor_if_needed(*tool, args_json, response);
74 }
75
76 return serialize_response(response);
77}
78
88 const std::string& tool_name,
89 const std::string& /*args_json*/) const {
90 return tool_name;
91}
92
101 const std::string& /*tool_name*/) const {
102 return false;
103}
104
112bool MCPServerBase::configure(const std::string& /*config_json*/) {
113 return true;
114}
115
123bool MCPServerBase::set_working_dir(const std::string& /*path*/) {
124 return true;
125}
126
146static const char* const DIRECTIVE_NAMES[] = {
147 "stop_processing", // 0
148 "tier_change", // 1
149 "delegate", // 2
150 "pipeline", // 3
151 "complete", // 4
152 "clear_self_todos", // 5
153 "inject_context", // 6
154 "prune_messages", // 7
155 "context_anchor", // 8
156 "phase_change", // 9
157 "notify_presenter", // 10
158};
159
167static const char* directive_type_name(
169 auto idx = static_cast<int>(type);
170 constexpr int count = sizeof(DIRECTIVE_NAMES)
171 / sizeof(DIRECTIVE_NAMES[0]);
172 if (idx < 0 || idx >= count) {
173 return "unknown";
174 }
175 return DIRECTIVE_NAMES[idx];
176}
177
185std::string MCPServerBase::serialize_response(
186 const ServerResponse& response) {
187 nlohmann::json j;
188 j["result"] = response.result;
189
190 auto directives = nlohmann::json::array();
191 for (const auto& d : response.directives) {
192 nlohmann::json dj;
193 dj["type"] = directive_type_name(d.type);
194 directives.push_back(std::move(dj));
195 }
196 j["directives"] = std::move(directives);
197
198 return j.dump();
199}
200
209void MCPServerBase::inject_anchor_if_needed(
210 const ToolBase& tool,
211 const std::string& args_json,
212 ServerResponse& response) {
213 auto key = tool.anchor_key(args_json);
214 if (key.empty()) {
215 return;
216 }
217 Directive anchor;
219 response.directives.push_back(std::move(anchor));
220 logger->info("Auto-injected ContextAnchor: {}", key);
221}
222
223} // namespace entropic
std::string list_tools() const
List all registered tools as a JSON array string.
std::string execute(const std::string &tool_name, const std::string &args_json)
Execute a tool and wrap result in ServerResponse JSON.
const std::string & name() const
Get the server name.
std::string name_
Server name.
virtual bool skip_duplicate_check(const std::string &tool_name) const
Check if a tool should skip duplicate detection.
void register_tool(ToolBase *tool)
Register a tool with this server.
MCPServerBase(std::string name)
Construct with server name.
virtual std::string get_permission_pattern(const std::string &tool_name, const std::string &args_json) const
Generate permission pattern for 'Always Allow/Deny'.
ToolRegistry registry_
Tool registry.
virtual bool set_working_dir(const std::string &path)
Set the working directory.
virtual bool configure(const std::string &config_json)
Configure the server after creation.
Abstract base class for individual MCP tools.
Definition tool_base.h:45
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.
ToolBase * get_tool(const std::string &name) const
Get a registered tool by name.
entropic_directive_type_t
Directive types emitted by MCP tool results.
Definition enums.h:53
@ ENTROPIC_DIRECTIVE_CONTEXT_ANCHOR
Replace context anchor.
Definition enums.h:62
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).
static const char * directive_type_name(entropic_directive_type_t type)
Map directive type enum to wire-format string.
static const char *const DIRECTIVE_NAMES[]
Serialize ServerResponse to JSON envelope.
MCPServerBase concrete base class + ServerResponse.