Files
OrcaSlicer/tests/slic3rutils/test_mcp_protocol.cpp
T
SoftFever fece418ae0 Make the automation runtime its own subsystem, with MCP as one front end
Everything that drives OrcaSlicer by command (the dispatch of the command
catalog, jobs, the journal, replays, Python scripts, recordings) lived
inside the MCP server's service class. So Action Replay, the Run Script
panel, a plugin's tool call, a person's recording and a run from the
command line each had to start "the MCP service", with all of its GUI
hooks, although none of them needs a server or an agent.

The automation runtime is now a subsystem of its own. It starts the first
time a front end asks for it and knows none of them. The MCP endpoint
(HTTP, the protocol, the token, the discovery file, the takeover of a port
another window holds) is one front end beside the others, opened and
closed without stopping what the others run. The widget inspector, menu
capture and ImGui item recording are turned on only by a front end that
drives the GUI, so a person's recording or long command does not carry
them.

The part of the runtime that needs no GUI is the library
libslic3r_automation, built without wxWidgets and tested by its own suite,
automation_tests. A wx dependency in it fails to build, and its tests no
longer link the GUI library or the embedded Python.

The command line has one option set for this: --run FILE runs a replay or
a Python script once the GUI is ready, with --outputdir for its files, and
--unrestricted-scripts and --cad turn their settings on for one run
without saving them. --mcp-run, --mcp-run-out and --mcp-token are gone;
the scripts setting is python_scripts_unrestricted and a session's output
folder is <datadir>/automation/out. get_app_state reports ready and
version at the top level, and its mcp block describes the endpoint only.

No change to slicing output.
2026-11-14 23:41:01 +08:00

84 lines
3.1 KiB
C++

#include <catch2/catch_test_macros.hpp>
#include "automation/automation_test_utils.hpp"
#include "slic3r/GUI/CommandCatalog.hpp"
#include "slic3r/GUI/Mcp/McpProtocol.hpp"
#include "slic3r/GUI/NativeCommands.hpp"
#include "libslic3r_version.h"
#include <nlohmann/json.hpp>
#include <algorithm>
#include <set>
#include <string>
#include <vector>
// The protocol's cases that read the catalog NativeCommands builds. The others are in
// tests/automation/test_mcp_protocol.cpp.
using namespace Slic3r::GUI;
using namespace Slic3r::GUI::Mcp;
using nlohmann::json;
using Slic3r::GUI::CommandCatalog;
using Slic3r::GUI::NativeCommand;
using Slic3r::GUI::Mcp::Test::DirectCaller;
namespace NativeCommands = Slic3r::GUI::NativeCommands;
namespace {
json body(const PostReply& r) { return json::parse(r.body); }
} // namespace
TEST_CASE("tools/list lists the listed commands in the order they were added", "[Mcp]")
{
const CommandCatalog& catalog = NativeCommands::instance();
DirectCaller caller;
McpProtocol protocol{catalog, caller, ServerInfo{}};
const json tools = body(protocol.handle_post(PostRequest{json{{"jsonrpc", "2.0"}, {"id", 2}, {"method", "tools/list"}}.dump(), {}, {}, {}}))
["result"]["tools"];
std::vector<std::string> names;
for (const json& tool : tools)
names.push_back(tool["name"].get<std::string>());
std::vector<std::string> added;
for (const NativeCommand& def : catalog.commands())
if (def.listed)
added.push_back(def.key);
CHECK(names == added);
// The families in the order register_all_commands() adds them: the Speed Dial's commands before them are
// not listed.
REQUIRE_FALSE(names.empty());
CHECK(names.front() == "get_app_state");
const auto position = [&names](const std::string& name) {
const auto it = std::find(names.begin(), names.end(), name);
REQUIRE(it != names.end());
return it - names.begin();
};
CHECK(position("search_tools") < position("open_files"));
CHECK(position("slice") < position("click"));
}
TEST_CASE("the catalog the stdio bridge lists without the app holds every tool family", "[Mcp]")
{
const CommandCatalog& catalog = NativeCommands::instance();
std::set<std::string> names;
for (const NativeCommand& def : catalog.commands()) {
if (!def.listed)
continue;
CHECK(names.insert(def.key).second);
}
for (const char* name : {"get_app_state", "run_action", "open_files", "set_settings", "slice", "set_height_ranges", "set_view",
"list_widgets", "run_replay", "start_recording", "run_script", "call_tool", "set_variable_layer_height",
"set_custom_gcode_at_layer"})
CHECK(names.count(name) == 1);
// Built once, for the life of the process.
CHECK(&NativeCommands::instance() == &catalog);
CHECK(server_info().version == SLIC3R_VERSION);
CHECK_FALSE(server_info().instructions.empty());
// Clients cut the instructions off after 4096 characters.
CHECK(server_info().instructions.size() <= 4096);
}