Entropic 2.11.1
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
52
66std::string MCPServerBase::list_tools() const {
68}
69
89 const std::string& tool_name,
90 const std::string& args_json) {
91 logger->info("[EXECUTE] {}.{}", name_, tool_name);
92
93 auto response = registry_.dispatch(tool_name, args_json);
94
95 auto* tool = registry_.get_tool(tool_name);
96 if (tool != nullptr) {
97 inject_anchor_if_needed(*tool, args_json, response);
98 }
99
100 return serialize_response(response);
101}
102
119 const std::string& tool_name,
120 const std::string& /*args_json*/) const {
121 return tool_name;
122}
123
140 const std::string& /*tool_name*/) const {
141 return false;
142}
143
155bool MCPServerBase::configure(const std::string& /*config_json*/) {
156 return true;
157}
158
170bool MCPServerBase::set_working_dir(const std::string& /*path*/) {
171 return true;
172}
173
193static const char* const DIRECTIVE_NAMES[] = {
194 "stop_processing", // 0
195 "tier_change", // 1
196 "delegate", // 2
197 "pipeline", // 3
198 "complete", // 4
199 "clear_self_todos", // 5
200 "inject_context", // 6
201 "prune_messages", // 7
202 "context_anchor", // 8
203 "phase_change", // 9
204 "notify_presenter", // 10
205};
206
218static const char* directive_type_name(
220 auto idx = static_cast<int>(type);
221 constexpr int count = sizeof(DIRECTIVE_NAMES)
222 / sizeof(DIRECTIVE_NAMES[0]);
223 if (idx < 0 || idx >= count) {
224 return "unknown";
225 }
226 return DIRECTIVE_NAMES[idx];
227}
228
242std::string MCPServerBase::serialize_response(
243 const ServerResponse& response) {
244 nlohmann::json j;
245 j["result"] = response.result;
246
247 auto directives = nlohmann::json::array();
248 for (const auto& d : response.directives) {
249 nlohmann::json dj;
250 dj["type"] = directive_type_name(d.type);
251 directives.push_back(std::move(dj));
252 }
253 j["directives"] = std::move(directives);
254
255 return j.dump();
256}
257
271void MCPServerBase::inject_anchor_if_needed(
272 const ToolBase& tool,
273 const std::string& args_json,
274 ServerResponse& response) {
275 auto key = tool.anchor_key(args_json);
276 if (key.empty()) {
277 return;
278 }
279 Directive anchor;
281 response.directives.push_back(std::move(anchor));
282 logger->info("Auto-injected ContextAnchor: {}", key);
283}
284
285} // 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:58
@ ENTROPIC_DIRECTIVE_CONTEXT_ANCHOR
Replace context anchor.
Definition enums.h:67
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.
@ count
Sentinel — MUST remain last.
static const char *const DIRECTIVE_NAMES[]
Serialize ServerResponse to JSON envelope.
MCPServerBase concrete base class + ServerResponse.