Skip to content

Commit 2bacb1e

Browse files
authored
server : validate --tools CLI argument against known tool names (#22538)
Previously, unknown tool names passed via --tools were silently ignored. Now the server validates each tool name at startup and exits with an error if an unrecognized tool is specified, listing the available tools. Assisted-by: llama.cpp:local pi
1 parent d6e7b03 commit 2bacb1e

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

tools/server/server-tools.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <atomic>
1111
#include <cstring>
1212
#include <climits>
13+
#include <algorithm>
1314

1415
namespace fs = std::filesystem;
1516

@@ -744,6 +745,24 @@ void server_tools::setup(const std::vector<std::string> & enabled_tools) {
744745
std::unordered_set<std::string> enabled_set(enabled_tools.begin(), enabled_tools.end());
745746
auto all_tools = build_tools();
746747

748+
// collect all known tool names for validation
749+
std::vector<std::string> known_names;
750+
known_names.reserve(all_tools.size());
751+
for (const auto & t : all_tools) {
752+
known_names.push_back(t->name);
753+
}
754+
755+
// validate that every requested tool is known
756+
for (const auto & name : enabled_tools) {
757+
if (name == "all") continue;
758+
if (std::find(known_names.begin(), known_names.end(), name) == known_names.end()) {
759+
throw std::runtime_error(string_format(
760+
"unknown tool \"%s\". available tools: %s",
761+
name.c_str(),
762+
string_join(known_names, ", ").c_str()));
763+
}
764+
}
765+
747766
tools.clear();
748767
for (auto & t : all_tools) {
749768
if (enabled_set.count(t->name) > 0 || enabled_set.count("all") > 0) {

tools/server/server.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,12 @@ int main(int argc, char ** argv) {
215215
}
216216
// EXPERIMENTAL built-in tools
217217
if (!params.server_tools.empty()) {
218-
tools.setup(params.server_tools);
218+
try {
219+
tools.setup(params.server_tools);
220+
} catch (const std::exception & e) {
221+
LOG_ERR("%s: tools setup failed: %s\n", __func__, e.what());
222+
return 1;
223+
}
219224
SRV_WRN("%s", "-----------------\n");
220225
SRV_WRN("%s", "Built-in tools are enabled, do not expose server to untrusted environments\n");
221226
SRV_WRN("%s", "This feature is EXPERIMENTAL and may be changed in the future\n");

0 commit comments

Comments
 (0)