Add Python scripts and plugin tools to the MCP server

The MCP tools are listed in core, API and UI lanes. With Python scripts turned on in Preferences, agents
and people can run Python against the open app; plugins holding the tools permission can call the tools,
and plugins can offer operations of their own to agents. Calls can pin object and volume ids so a stale
index is refused, and run_script takes a script as JSON text as well as an object.
This commit is contained in:
SoftFever committed 2026-11-02 16:05:13 +08:00
1 parent f31843dcd1
commit 99018203de
68 files changed
+4844 -82

No files matched your search

+291
View File
@@ -39,9 +39,13 @@
#include "python_test_support.hpp"
#include <pybind11/embed.h>
#include <pybind11/eval.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <set>
#include <string>
#include <vector>
#include <wx/string.h>
#include <wx/uri.h>
@@ -628,3 +632,290 @@ TEST_CASE("Plugin host API exposes TriangleMesh geometry to Python", "[PluginHos
REQUIRE_THAT(instance_matrix.attr("__getitem__")(py::make_tuple(0, 3)).cast<double>(), WithinAbs(5.0, 1e-6));
REQUIRE_THAT(instance_matrix.attr("__getitem__")(py::make_tuple(1, 3)).cast<double>(), WithinAbs(6.0, 1e-6));
}
namespace {
// Every function and public method reachable under orca.host, as "<submodule>.<name>" or
// "<submodule>.<Class>.<name>" ("" for orca.host itself), sorted.
std::vector<std::string> host_callables(const py::module_& host)
{
py::dict scope;
scope["host"] = host;
py::exec(R"PY(
import inspect, types
names = []
def walk(module, prefix, seen):
if id(module) in seen:
return
seen.add(id(module))
for name, value in vars(module).items():
if name.startswith("_"):
continue
if isinstance(value, types.ModuleType):
if value.__name__.startswith(host.__name__):
walk(value, prefix + name + ".", seen)
elif inspect.isclass(value):
if getattr(value, "__module__", "").startswith(host.__name__) or value.__module__ == "orca":
for member, attr in vars(value).items():
if not member.startswith("_") and callable(attr) and not isinstance(attr, property):
names.append(prefix + name + "." + member)
elif callable(value):
names.append(prefix + name)
walk(host, "", set())
names = sorted(set(names))
)PY", scope);
return scope["names"].cast<std::vector<std::string>>();
}
} // namespace
TEST_CASE("every orca.host function is classified as callable by scripts or by plugins only", "[PluginHost][Python]")
{
py::module_ orca = import_orca_module();
// Functions that change state outside the MCP tools, which call require_plugin_caller() first.
const std::set<std::string> plugin_only{
"Plater.export_3mf_copy",
"plugin.storage",
"preset.PresetEditor.begin_update",
"preset.PresetEditor.update",
"preset.bundles.reload_local",
"ui.create_dock_panel",
"ui.create_progress_dialog",
"ui.create_window",
"ui.message",
"ui.push_notification",
// Reached only through a function above (a transaction from begin_update, a handle from a window or
// progress dialog), or only inside a slicing hook, which a script never runs in.
"Layer.make_slices",
"SurfaceCollection.append",
"SurfaceCollection.clear",
"SurfaceCollection.set",
"SurfaceCollection.set_type",
"preset.PresetTransaction.commit",
"preset.PresetTransaction.set",
"ui.ProgressDialog.close",
"ui.ProgressDialog.pulse",
"ui.ProgressDialog.start_pulse",
"ui.ProgressDialog.stop_pulse",
"ui.ProgressDialog.update",
"ui.UiDockPanel.close",
"ui.UiDockPanel.hide",
"ui.UiDockPanel.post",
"ui.UiDockPanel.show",
"ui.UiWindow.close",
"ui.UiWindow.post",
};
// Functions a person's or an agent's script may call: reads, and orca.host.tools.
const std::set<std::string> script_callable{
"ui.ProgressDialog.is_open",
"ui.UiDockPanel.is_open",
"ui.UiWindow.is_open",
"ExPolygon.area",
"ExPolygon.contains",
"ExPolygon.diff_ex",
"ExPolygon.douglas_peucker",
"ExPolygon.intersection_ex",
"ExPolygon.is_valid",
"ExPolygon.num_contours",
"ExPolygon.offset",
"ExPolygon.rotate",
"ExPolygon.scale",
"ExPolygon.set_holes",
"ExPolygon.simplify",
"ExPolygon.translate",
"ExPolygon.union_ex",
"ExtrusionEntityCollection.flatten_paths",
"ExtrusionEntityCollection.size",
"ExtrusionPath.points",
"Layer.lslices",
"Layer.regions",
"LayerRegion.config_value",
"LayerRegion.layer",
"LayerRegion.region",
"Model.bounding_box",
"Model.bounding_box_approx",
"Model.current_plate_index",
"Model.design_id",
"Model.designer",
"Model.id",
"Model.is_fdm_support_painted",
"Model.is_fuzzy_skin_painted",
"Model.is_mm_painted",
"Model.is_seam_painted",
"Model.material_count",
"Model.max_z",
"Model.object",
"Model.object_count",
"Model.objects",
"ModelInstance.assemble_matrix",
"ModelInstance.assemble_offset",
"ModelInstance.assemble_rotation",
"ModelInstance.bounding_box",
"ModelInstance.id",
"ModelInstance.is_assemble_initialized",
"ModelInstance.is_left_handed",
"ModelInstance.is_printable",
"ModelInstance.matrix",
"ModelInstance.mirror",
"ModelInstance.offset",
"ModelInstance.offset_to_assembly",
"ModelInstance.rotation",
"ModelInstance.scaling_factor",
"ModelObject.bounding_box",
"ModelObject.config_keys",
"ModelObject.config_value",
"ModelObject.facets_count",
"ModelObject.has_custom_layering",
"ModelObject.id",
"ModelObject.instance",
"ModelObject.instance_count",
"ModelObject.instances",
"ModelObject.is_cut",
"ModelObject.is_fdm_support_painted",
"ModelObject.is_fuzzy_skin_painted",
"ModelObject.is_mm_painted",
"ModelObject.is_multiparts",
"ModelObject.is_seam_painted",
"ModelObject.materials_count",
"ModelObject.max_z",
"ModelObject.mesh_errors_count",
"ModelObject.min_z",
"ModelObject.parts_count",
"ModelObject.raw_mesh_bounding_box",
"ModelObject.volume",
"ModelObject.volume_count",
"ModelObject.volumes",
"ModelVolume.bounding_box",
"ModelVolume.config_keys",
"ModelVolume.config_value",
"ModelVolume.extruder_id",
"ModelVolume.facets_count",
"ModelVolume.id",
"ModelVolume.is_fdm_support_painted",
"ModelVolume.is_fuzzy_skin_painted",
"ModelVolume.is_manifold",
"ModelVolume.is_mm_painted",
"ModelVolume.is_model_part",
"ModelVolume.is_modifier",
"ModelVolume.is_negative_volume",
"ModelVolume.is_precise_seam",
"ModelVolume.is_precise_seam_strong",
"ModelVolume.is_precise_seam_weak",
"ModelVolume.is_seam_painted",
"ModelVolume.is_support_blocker",
"ModelVolume.is_support_enforcer",
"ModelVolume.is_support_modifier",
"ModelVolume.matrix",
"ModelVolume.mesh",
"ModelVolume.mesh_errors_count",
"ModelVolume.mirror",
"ModelVolume.offset",
"ModelVolume.rotation",
"ModelVolume.scaling_factor",
"ModelVolume.type",
"ModelVolume.volume",
"Plater.inside_snapshot_capture",
"Plater.is_presets_dirty",
"Plater.is_project_dirty",
"Plater.model",
"Plater.project_path",
"Polygon.append",
"Polygon.area",
"Polygon.as_array",
"Polygon.centroid",
"Polygon.contains",
"Polygon.douglas_peucker",
"Polygon.is_clockwise",
"Polygon.is_counter_clockwise",
"Polygon.is_valid",
"Polygon.make_clockwise",
"Polygon.make_counter_clockwise",
"Polygon.offset",
"Polygon.rotate",
"Polygon.set_points",
"Polygon.simplify",
"Polygon.size",
"Polygon.translate",
"Preset.config_keys",
"Preset.config_value",
"Preset.is_from_bundle",
"Preset.is_user",
"Preset.label",
"PresetBundle.current_filament_preset_names",
"PresetBundle.current_filament_presets",
"PresetBundle.current_print_preset",
"PresetBundle.current_printer_preset",
"PresetBundle.current_process_preset",
"PresetBundle.full_config_keys",
"PresetBundle.full_config_value",
"PresetCollection.edited_preset",
"PresetCollection.find_preset",
"PresetCollection.get_edited_preset",
"PresetCollection.get_selected_preset",
"PresetCollection.get_selected_preset_name",
"PresetCollection.preset",
"PresetCollection.preset_names",
"PresetCollection.selected_preset",
"PresetCollection.selected_preset_name",
"PresetCollection.size",
"Print.canceled",
"Print.config_keys",
"Print.config_value",
"Print.model",
"Print.objects",
"PrintObject.bounding_box",
"PrintObject.config_keys",
"PrintObject.config_value",
"PrintObject.id",
"PrintObject.layers",
"PrintObject.model_object",
"PrintObject.support_layers",
"PrintObject.trafo",
"PrintRegion.config_keys",
"PrintRegion.config_value",
"Surface.area",
"Surface.is_bottom",
"Surface.is_bridge",
"Surface.is_external",
"Surface.is_internal",
"Surface.is_solid",
"Surface.is_top",
"SurfaceCollection.empty",
"SurfaceCollection.filter_by_type",
"SurfaceCollection.has",
"SurfaceCollection.size",
"TriangleMesh.bounding_box",
"TriangleMesh.face_normals",
"TriangleMesh.facets_count",
"TriangleMesh.is_empty",
"TriangleMesh.is_manifold",
"TriangleMesh.triangle",
"TriangleMesh.triangle_count",
"TriangleMesh.triangles",
"TriangleMesh.vertex",
"TriangleMesh.vertex_count",
"TriangleMesh.vertices",
"TriangleMesh.volume",
"app_info",
"gl_info",
"model",
"plater",
"preset.bundles.list_ids",
"preset_bundle",
"selected_printer",
"tools.call",
"tools.run_script",
};
std::vector<std::string> unclassified;
for (const std::string& name : host_callables(orca.attr("host").cast<py::module_>()))
if (plugin_only.count(name) == 0 && script_callable.count(name) == 0)
unclassified.push_back(name);
// A new binding is plugin-only until listed here: reads go in script_callable, anything that changes
// state outside the tools calls require_plugin_caller() and goes in plugin_only.
std::string listed;
for (const std::string& name : unclassified)
listed += " \"" + name + "\",\n";
INFO("Unclassified orca.host functions:\n" << listed);
CHECK(unclassified.empty());
}