Entropic 2.14.3
Local-first agentic inference engine
Loading...
Searching...
No Matches
serve.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
18#include <entropic/entropic.h>
19
20#include <csignal>
21#include <cstdio>
22#include <cstdlib>
23#include <cstring>
24#include <filesystem>
25#include <string>
26
27#include <pthread.h>
28
29namespace entropic::cli {
30
31namespace {
32
37struct ServeArgs {
38 std::string config;
39 std::string port;
40};
41
51bool parse_args(int argc, char* argv[], ServeArgs& out) {
52 bool ok = true;
53 for (int i = 1; i < argc && ok; ++i) {
54 const std::string flag = argv[i];
55 const bool has_value = i + 1 < argc;
56 if (flag == "--config" && has_value) {
57 out.config = argv[++i];
58 } else if (flag == "--port" && has_value) {
59 out.port = argv[++i];
60 } else {
61 ok = false;
62 }
63 }
64 return ok && !out.config.empty() && !out.port.empty();
65}
66
79 pthread_sigmask(SIG_BLOCK, &set, nullptr);
80}
81
96 int sig = 0;
97 while (sigwait(&set, &sig) == 0 && sig == SIGUSR1) {
98 const auto rc = entropic_release_model(handle, nullptr);
99 std::fprintf(stderr, "entropic serve: SIGUSR1, models released (%d)%s%s\n",
100 static_cast<int>(rc), rc == ENTROPIC_OK ? "" : ": ",
101 rc == ENTROPIC_OK ? "" : entropic_last_error(handle));
102 }
103 return sig;
104}
105
114entropic_error_t configure_host(const ServeArgs& args,
115 entropic_handle_t* handle) {
116 // The env overrides apply on every configure path, so the port set here
117 // wins over whatever the file says without a second config format.
118 setenv("ENTROPIC_MCP__EXTERNAL__ENABLED", "true", 1);
119 setenv("ENTROPIC_MCP__EXTERNAL__HTTP_PORT", args.port.c_str(), 1);
121 if (rc == ENTROPIC_OK) {
122 rc = entropic_configure_from_file(*handle, args.config.c_str());
123 }
124 return rc;
125}
126
132void print_usage() {
133 std::fprintf(stderr,
134 "usage: entropic serve --config FILE --port N\n"
135 "\n"
136 "Serve the engine to every MCP client at http://127.0.0.1:N/mcp.\n"
137 "Clients send 'Authorization: Bearer <token>', the token being the\n"
138 "contents of ~/.entropic/serve/N.token (regenerated each start).\n"
139 "Loopback only; for remote use, tunnel: ssh -L N:127.0.0.1:N host.\n"
140 "Runs until SIGINT or SIGTERM. SIGUSR1 releases the loaded models\n"
141 "(the next request reloads) and keeps serving.\n");
142}
143
144} // namespace
145
154int run_serve(int argc, char* argv[]) {
155 ServeArgs args;
156 if (!parse_args(argc, argv, args)) {
157 print_usage();
158 return 1;
159 }
162 entropic_handle_t handle = nullptr;
163 const entropic_error_t rc = configure_host(args, &handle);
164 if (rc != ENTROPIC_OK) {
165 const char* why = handle != nullptr ? entropic_last_error(handle) : nullptr;
166 std::fprintf(stderr, "entropic serve: cannot start (%d): %s\n",
167 static_cast<int>(rc), why != nullptr ? why : "unknown");
168 } else {
169 std::fprintf(stderr,
170 "entropic serve: http://127.0.0.1:%s/mcp (token: %s/.entropic/serve/%s.token)\n",
171 args.port.c_str(),
172 std::getenv("HOME") != nullptr ? std::getenv("HOME") : "~",
173 args.port.c_str());
174 const int sig = serve_until_stopped(handle, set);
175 std::fprintf(stderr, "entropic serve: %s, shutting down\n",
176 sig == SIGTERM ? "SIGTERM" : "SIGINT");
177 }
178 entropic_destroy(handle); // stops the listener, removes the token
179 return rc == ENTROPIC_OK ? 0 : 1;
180}
181
182} // namespace entropic::cli
Public C API for the Entropic inference engine.
ENTROPIC_EXPORT void entropic_destroy(entropic_handle_t handle)
Destroy an engine instance and free all resources.
ENTROPIC_EXPORT entropic_error_t entropic_create(entropic_handle_t *handle)
Create a new engine instance.
Definition entropic.cpp:261
ENTROPIC_EXPORT entropic_error_t entropic_configure_from_file(entropic_handle_t handle, const char *config_path)
Configure engine from a YAML or JSON config file.
ENTROPIC_EXPORT entropic_error_t entropic_release_model(entropic_handle_t handle, const char *tier_name)
Release a resident model, keeping everything else on the handle.
entropic_error_t
Error codes returned by all C API functions.
Definition error.h:37
@ ENTROPIC_OK
Success.
Definition error.h:38
ENTROPIC_EXPORT const char * entropic_last_error(entropic_handle_t handle)
Get the last error message for a handle.
Definition entropic.cpp:156
static void print_usage()
Print top-level usage to stderr.
Definition main.cpp:43
int run_serve(int argc, char *argv[])
entropic serve entry point.
Definition serve.cpp:154
@ ok
Tool dispatched, returned non-empty content.
@ optional
optional: every escape asks the host's path approver
std::string port
–port N
Definition serve.cpp:39
std::string config
–config FILE
Definition serve.cpp:38
Engine handle struct — owns all subsystems.