bambu-printer-mcp 1.1.63 → 1.1.65
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/counterpart/remote.d.ts +47 -0
- package/dist/counterpart/remote.js +141 -0
- package/dist/counterpart/sliced-file.d.ts +31 -0
- package/dist/counterpart/sliced-file.js +110 -0
- package/dist/index.js +54 -11
- package/dist/slicer/slice-cache.d.ts +28 -0
- package/dist/slicer/slice-cache.js +178 -0
- package/dist/stl/stl-manipulator.js +15 -1
- package/package.json +1 -1
- package/src/counterpart/remote.ts +160 -0
- package/src/counterpart/sliced-file.ts +115 -0
- package/src/index.ts +57 -13
- package/src/slicer/slice-cache.ts +171 -0
- package/src/stl/stl-manipulator.ts +13 -1
package/README.md
CHANGED
|
@@ -1139,7 +1139,7 @@ Inspect the slicer settings embedded in a saved 3MF template or in an extracted
|
|
|
1139
1139
|
}
|
|
1140
1140
|
```
|
|
1141
1141
|
|
|
1142
|
-
This returns a compact summary of the high-signal settings such as printer preset, default print profile, filament profiles, layer height, infill density, shell counts, support mode, and bed type. It accepts either `source_path` or `template_name`. For 3MF inputs it also writes the extracted settings blob to a temp path so the result can be reused directly, and returns `own_process
|
|
1142
|
+
This returns a compact summary of the high-signal settings such as printer preset, default print profile, filament profiles, layer height, infill density, shell counts, support mode, and bed type. It accepts either `source_path` or `template_name`. For 3MF inputs it also writes the extracted settings blob to a temp path so the result can be reused directly, and returns `own_process`. With `source_path`, pass the slice's other arguments too (`slicer_profile`, `template_name`, `template_3mf_path`, `layer_height`, `wall_loops`, `sparse_infill_density`, `quality`, `strength`). `own_process` is then true when that `slice_stl` call prints the process the 3MF carries (a designer's project) unchanged.
|
|
1143
1143
|
|
|
1144
1144
|
#### slice_with_template
|
|
1145
1145
|
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How Counterpart's remote endpoint (`/v1/mcp`) offers each tool to a paired device, declared by this server so a
|
|
3
|
+
* new tool or argument needs no Counterpart release. Sent as each listed tool's `_meta["counterpart/remote"]`:
|
|
4
|
+
*
|
|
5
|
+
* - `scope`: the least device scope that may call the tool: read < design < control < print < raw. The table keeps
|
|
6
|
+
* the classes Counterpart gave these tools by hand before this version, so no paired device gained or lost a tool;
|
|
7
|
+
* `tests/tool-annotations.test.mjs` holds the name floors (a print start is at least print, a raw call raw).
|
|
8
|
+
* - `raisedBy`: boolean arguments that, when true, need a higher scope (`launch` starts Fusion).
|
|
9
|
+
* - `arguments`: argument names that are not plain values, matched at any depth of a call's arguments:
|
|
10
|
+
* `config`: this server's configuration chooses it (the printer it targets, its account, hardware and programs),
|
|
11
|
+
* so a device may not set it; `config-top`: the same, as a top-level argument only (`type`, the printer type);
|
|
12
|
+
* `path`: a local file or folder the tool reads; `output`: one it writes. Any other argument is a plain value.
|
|
13
|
+
* A tool that forwards an open object (an arbitrary RPC payload or another server's tool arguments) declares every
|
|
14
|
+
* configuration name, since any of them may appear inside it.
|
|
15
|
+
*/
|
|
16
|
+
export type RemoteScope = "read" | "design" | "control" | "print" | "raw";
|
|
17
|
+
export type ArgumentKind = "config" | "config-top" | "path" | "output";
|
|
18
|
+
export interface RemoteDeclaration {
|
|
19
|
+
scope: RemoteScope;
|
|
20
|
+
raisedBy?: Record<string, RemoteScope>;
|
|
21
|
+
arguments?: Record<string, ArgumentKind>;
|
|
22
|
+
}
|
|
23
|
+
export declare const REMOTE_META_KEY = "counterpart/remote";
|
|
24
|
+
export declare const REMOTE_SCOPES: Readonly<Record<string, RemoteScope>>;
|
|
25
|
+
/**
|
|
26
|
+
* Overrides of a server's own configuration (both printer MCPs use the same names): the printer it targets, its
|
|
27
|
+
* account and credentials, its hardware, and the programs it runs.
|
|
28
|
+
*/
|
|
29
|
+
export declare const CONFIG_ARGUMENTS: ReadonlySet<string>;
|
|
30
|
+
/** The argument kinds of one tool, read from its own input schema. */
|
|
31
|
+
export declare function remoteArguments(tool: {
|
|
32
|
+
name: string;
|
|
33
|
+
inputSchema?: unknown;
|
|
34
|
+
}): Record<string, ArgumentKind>;
|
|
35
|
+
/** The declaration for one tool; a tool missing from REMOTE_SCOPES (a bug the tests catch) is raw. */
|
|
36
|
+
export declare function remoteDeclaration(tool: {
|
|
37
|
+
name: string;
|
|
38
|
+
inputSchema?: unknown;
|
|
39
|
+
}): RemoteDeclaration;
|
|
40
|
+
/** The tool with its declaration in `_meta`. */
|
|
41
|
+
export declare function withRemote<T extends {
|
|
42
|
+
name: string;
|
|
43
|
+
inputSchema?: unknown;
|
|
44
|
+
_meta?: Record<string, unknown>;
|
|
45
|
+
}>(tool: T): T & {
|
|
46
|
+
_meta: Record<string, unknown>;
|
|
47
|
+
};
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
export const REMOTE_META_KEY = "counterpart/remote";
|
|
2
|
+
export const REMOTE_SCOPES = {
|
|
3
|
+
// read
|
|
4
|
+
counterpart_discover_printers: "read",
|
|
5
|
+
counterpart_setup_fields: "read",
|
|
6
|
+
counterpart_propose_filaments: "read",
|
|
7
|
+
get_printer_status: "read",
|
|
8
|
+
get_printer_filaments: "read",
|
|
9
|
+
resolve_3mf_ams_slots: "read",
|
|
10
|
+
list_3mf_plate_objects: "read",
|
|
11
|
+
get_stl_info: "read",
|
|
12
|
+
list_templates: "read",
|
|
13
|
+
list_printer_files: "read",
|
|
14
|
+
camera_frame: "read",
|
|
15
|
+
camera_snapshot: "read",
|
|
16
|
+
inspect_print_file: "read",
|
|
17
|
+
blender_mcp_status: "read",
|
|
18
|
+
counterpart_printer_info: "read",
|
|
19
|
+
counterpart_print_progress: "read",
|
|
20
|
+
fusion_mcp_status: "read",
|
|
21
|
+
// design
|
|
22
|
+
slice_stl: "design",
|
|
23
|
+
lay_flat: "design",
|
|
24
|
+
counterpart_make_3mf: "design",
|
|
25
|
+
// control
|
|
26
|
+
bambu_connect_import_file: "control",
|
|
27
|
+
camera_stream_start: "control",
|
|
28
|
+
camera_stream_stop: "control",
|
|
29
|
+
cancel_print: "control",
|
|
30
|
+
pause_print: "control",
|
|
31
|
+
resume_print: "control",
|
|
32
|
+
clear_hms_errors: "control",
|
|
33
|
+
set_print_speed: "control",
|
|
34
|
+
set_airduct_mode: "control",
|
|
35
|
+
reread_ams_rfid: "control",
|
|
36
|
+
set_temperature: "control",
|
|
37
|
+
set_fan_speed: "control",
|
|
38
|
+
set_light: "control",
|
|
39
|
+
set_ams_drying: "control",
|
|
40
|
+
counterpart_print_route: "control",
|
|
41
|
+
fusion_mcp_new_design: "control",
|
|
42
|
+
fusion_mcp_export: "control",
|
|
43
|
+
// print
|
|
44
|
+
extend_stl_base: "print",
|
|
45
|
+
scale_stl: "print",
|
|
46
|
+
rotate_stl: "print",
|
|
47
|
+
get_slice_settings: "print",
|
|
48
|
+
slice_with_template: "print",
|
|
49
|
+
bambu_network_bridge_status: "print",
|
|
50
|
+
print_3mf_bambu_network: "print",
|
|
51
|
+
delete_printer_file: "print",
|
|
52
|
+
upload_file: "print",
|
|
53
|
+
start_print: "print",
|
|
54
|
+
start_print_job: "print",
|
|
55
|
+
skip_objects: "print",
|
|
56
|
+
print_3mf: "print",
|
|
57
|
+
print_collar_charm: "print",
|
|
58
|
+
merge_vertices: "print",
|
|
59
|
+
center_model: "print",
|
|
60
|
+
// raw
|
|
61
|
+
save_template: "raw",
|
|
62
|
+
bambu_network_call: "raw",
|
|
63
|
+
upload_gcode: "raw",
|
|
64
|
+
x2d_native_control: "raw",
|
|
65
|
+
blender_mcp_call: "raw",
|
|
66
|
+
blender_mcp_edit_model: "raw",
|
|
67
|
+
fusion_mcp_call: "raw",
|
|
68
|
+
};
|
|
69
|
+
const RAISED_BY = {
|
|
70
|
+
fusion_mcp_status: { launch: "design" }, // starts Fusion on this computer
|
|
71
|
+
blender_mcp_status: { connect: "design" }, // connects to Blender
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Overrides of a server's own configuration (both printer MCPs use the same names): the printer it targets, its
|
|
75
|
+
* account and credentials, its hardware, and the programs it runs.
|
|
76
|
+
*/
|
|
77
|
+
export const CONFIG_ARGUMENTS = new Set([
|
|
78
|
+
"host", "port", "api_key", "username", "password", "dev_ip", "dev_id", "bambu_serial", "bambu_token", "user_info",
|
|
79
|
+
"country_code", "connection_type", "connection_mode", "route", "bambu_model", "nozzle_diameter", "nozzle_diameters",
|
|
80
|
+
"bridge_command", "slicer_path", "slicer_type", "ffmpeg_path", "plugin_dir", "runtime_dir", "template_dir",
|
|
81
|
+
"config_filename", "bambu_network_method", "use_ssl_for_mqtt", "use_ssl_for_ftp", "extra_options",
|
|
82
|
+
"allow_mutating_method",
|
|
83
|
+
]);
|
|
84
|
+
/** Top-level only: the printer type (`type`); a nested `type` is a plain value. */
|
|
85
|
+
const TOP_LEVEL_CONFIG = new Set(["type"]);
|
|
86
|
+
/** Files read whose names don't end in path, dir or file. */
|
|
87
|
+
const FILE_ARGUMENTS = new Set(["filament_profile", "slicer_profile", "load_filaments", "source_f3d"]);
|
|
88
|
+
const isPath = (key) => /(?:path|dir|directory|_file)$/.test(key) || FILE_ARGUMENTS.has(key);
|
|
89
|
+
function kindOf(tool, key, depth) {
|
|
90
|
+
// counterpart_print_route's own `route` is the connection the caller chooses.
|
|
91
|
+
if ((CONFIG_ARGUMENTS.has(key) && !(key === "route" && tool === "counterpart_print_route")) || /config_dir$/.test(key))
|
|
92
|
+
return "config";
|
|
93
|
+
if (depth === 0 && TOP_LEVEL_CONFIG.has(key))
|
|
94
|
+
return "config-top";
|
|
95
|
+
if (!isPath(key))
|
|
96
|
+
return undefined;
|
|
97
|
+
return /^output_|^out_path$|^save_path$/.test(key) ? "output" : "path";
|
|
98
|
+
}
|
|
99
|
+
/** The argument kinds of one tool, read from its own input schema. */
|
|
100
|
+
export function remoteArguments(tool) {
|
|
101
|
+
const kinds = {};
|
|
102
|
+
let open = false;
|
|
103
|
+
const visit = (schema, depth) => {
|
|
104
|
+
if (!schema || typeof schema !== "object")
|
|
105
|
+
return;
|
|
106
|
+
const node = schema;
|
|
107
|
+
// An object whose keys the schema doesn't name (beyond the top level) may carry any argument.
|
|
108
|
+
if (depth > 0 && node.type === "object" && (node.additionalProperties === true || node.additionalProperties === undefined && !node.properties))
|
|
109
|
+
open = true;
|
|
110
|
+
if (depth > 0 && node.additionalProperties && typeof node.additionalProperties === "object")
|
|
111
|
+
open = true;
|
|
112
|
+
for (const [key, child] of Object.entries(node.properties ?? {})) {
|
|
113
|
+
const kind = kindOf(tool.name, key, depth);
|
|
114
|
+
if (kind && !(kinds[key] === "config" && kind === "config-top"))
|
|
115
|
+
kinds[key] = kind;
|
|
116
|
+
visit(child, depth + 1);
|
|
117
|
+
}
|
|
118
|
+
visit(node.items, depth);
|
|
119
|
+
for (const option of [...(node.anyOf ?? []), ...(node.oneOf ?? [])])
|
|
120
|
+
visit(option, depth);
|
|
121
|
+
};
|
|
122
|
+
visit(tool.inputSchema, 0);
|
|
123
|
+
if (open)
|
|
124
|
+
for (const key of CONFIG_ARGUMENTS)
|
|
125
|
+
if (!(key === "route" && tool.name === "counterpart_print_route"))
|
|
126
|
+
kinds[key] ?? (kinds[key] = "config");
|
|
127
|
+
return kinds;
|
|
128
|
+
}
|
|
129
|
+
/** The declaration for one tool; a tool missing from REMOTE_SCOPES (a bug the tests catch) is raw. */
|
|
130
|
+
export function remoteDeclaration(tool) {
|
|
131
|
+
const args = remoteArguments(tool);
|
|
132
|
+
return {
|
|
133
|
+
scope: REMOTE_SCOPES[tool.name] ?? "raw",
|
|
134
|
+
...(RAISED_BY[tool.name] ? { raisedBy: RAISED_BY[tool.name] } : {}),
|
|
135
|
+
...(Object.keys(args).length ? { arguments: args } : {}),
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
/** The tool with its declaration in `_meta`. */
|
|
139
|
+
export function withRemote(tool) {
|
|
140
|
+
return { ...tool, _meta: { ...(tool._meta ?? {}), [REMOTE_META_KEY]: remoteDeclaration(tool) } };
|
|
141
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a client shows about a sliced file, read here so it never parses slicer formats itself (Counterpart PRD
|
|
3
|
+
* §8.0 item 9): the selected plate's estimate and slice settings, and how many printable plates the file has.
|
|
4
|
+
* Bambu Studio and OrcaSlicer 3MFs carry them in Metadata/slice_info.config, project_settings.config and the
|
|
5
|
+
* plate's G-code header; a G-code file in its own header and footer comments. Anything not found is left out.
|
|
6
|
+
*/
|
|
7
|
+
export interface SlicedFileFacts {
|
|
8
|
+
plate_count?: number;
|
|
9
|
+
estimate?: {
|
|
10
|
+
seconds?: number;
|
|
11
|
+
grams?: number;
|
|
12
|
+
};
|
|
13
|
+
settings: {
|
|
14
|
+
layer_height_mm?: number;
|
|
15
|
+
sparse_infill_percent?: number;
|
|
16
|
+
wall_loops?: number;
|
|
17
|
+
layer_count?: number;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/** The facts of a sliced 3MF or G-code file for one zero-based plate; what can't be read is left out, never an error. */
|
|
21
|
+
export declare function slicedFileFacts(filePath: string, plateIndex?: number): Promise<SlicedFileFacts>;
|
|
22
|
+
/**
|
|
23
|
+
* Bambu Studio's (or OrcaSlicer's) verdict on a slice, from its result.json: whether it failed, its own words
|
|
24
|
+
* (the error and every plate's warning, verbatim), and how many plates it sliced.
|
|
25
|
+
*/
|
|
26
|
+
export interface SlicerReport {
|
|
27
|
+
failed: boolean;
|
|
28
|
+
message?: string;
|
|
29
|
+
plate_count?: number;
|
|
30
|
+
}
|
|
31
|
+
export declare function slicerReport(result: unknown): SlicerReport | undefined;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { loadSafe3mfArchive, readBoundedPrintFile } from "../safety/archive.js";
|
|
3
|
+
const number = (value) => {
|
|
4
|
+
const parsed = typeof value === "number" ? value : typeof value === "string" ? Number.parseFloat(value) : NaN;
|
|
5
|
+
return Number.isFinite(parsed) && parsed >= 0 ? parsed : undefined;
|
|
6
|
+
};
|
|
7
|
+
const first = (value) => Array.isArray(value) ? value[0] : value;
|
|
8
|
+
const defined = (value) => Object.fromEntries(Object.entries(value).filter(([, item]) => item !== undefined));
|
|
9
|
+
function headerSettings(text) {
|
|
10
|
+
const read = (pattern) => number(text.match(pattern)?.[1]);
|
|
11
|
+
const layers = read(/^;\s*(?:total layer number|total layers count|LAYER_COUNT)\s*[:=]\s*(\d+)/im);
|
|
12
|
+
const walls = read(/^;\s*(?:wall_loops|perimeters)\s*=\s*(\d+)/im);
|
|
13
|
+
return defined({
|
|
14
|
+
layer_height_mm: read(/^;\s*layer_height\s*=\s*([\d.]+)/im),
|
|
15
|
+
sparse_infill_percent: read(/^;\s*(?:sparse_infill_density|fill_density)\s*=\s*([\d.]+)%/im),
|
|
16
|
+
wall_loops: walls === undefined ? undefined : Math.trunc(walls),
|
|
17
|
+
layer_count: layers ? Math.trunc(layers) : undefined,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
/** The facts of a sliced 3MF or G-code file for one zero-based plate; what can't be read is left out, never an error. */
|
|
21
|
+
export async function slicedFileFacts(filePath, plateIndex = 0) {
|
|
22
|
+
try {
|
|
23
|
+
return await readFacts(filePath, plateIndex);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return { settings: {} };
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async function readFacts(filePath, plateIndex) {
|
|
30
|
+
const head = Buffer.alloc(4);
|
|
31
|
+
const handle = await fs.promises.open(filePath, "r");
|
|
32
|
+
try {
|
|
33
|
+
await handle.read(head, 0, 4, 0);
|
|
34
|
+
}
|
|
35
|
+
finally {
|
|
36
|
+
await handle.close();
|
|
37
|
+
}
|
|
38
|
+
if (head[0] !== 0x50 || head[1] !== 0x4b) {
|
|
39
|
+
// G-code: PrusaSlicer and Orca write their settings in a footer, others in a header; read both ends.
|
|
40
|
+
const stat = await fs.promises.stat(filePath);
|
|
41
|
+
const size = 256 * 1024;
|
|
42
|
+
const read = async (start) => {
|
|
43
|
+
const buffer = Buffer.alloc(Math.min(size, stat.size));
|
|
44
|
+
const file = await fs.promises.open(filePath, "r");
|
|
45
|
+
try {
|
|
46
|
+
await file.read(buffer, 0, buffer.length, start);
|
|
47
|
+
}
|
|
48
|
+
finally {
|
|
49
|
+
await file.close();
|
|
50
|
+
}
|
|
51
|
+
return buffer.toString("latin1");
|
|
52
|
+
};
|
|
53
|
+
return { plate_count: 1, settings: headerSettings(`${await read(0)}\n${await read(Math.max(0, stat.size - size))}`) };
|
|
54
|
+
}
|
|
55
|
+
const zip = await loadSafe3mfArchive(await readBoundedPrintFile(filePath));
|
|
56
|
+
const plates = new Set(Object.keys(zip.files).flatMap(name => /^Metadata\/plate_([1-9]\d*)\.gcode$/.exec(name)?.[1] ?? []));
|
|
57
|
+
const text = async (name) => zip.file(name)?.async("string");
|
|
58
|
+
let settings = {};
|
|
59
|
+
const project = await text("Metadata/project_settings.config");
|
|
60
|
+
if (project) {
|
|
61
|
+
try {
|
|
62
|
+
const values = JSON.parse(project);
|
|
63
|
+
const infill = String(first(values.sparse_infill_density) ?? "");
|
|
64
|
+
const walls = number(first(values.wall_loops));
|
|
65
|
+
settings = defined({
|
|
66
|
+
layer_height_mm: number(first(values.layer_height)),
|
|
67
|
+
sparse_infill_percent: infill === "" ? undefined : number(infill.replace("%", "")),
|
|
68
|
+
wall_loops: walls === undefined ? undefined : Math.trunc(walls),
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
catch { /* the settings stay unknown */ }
|
|
72
|
+
}
|
|
73
|
+
// Only the plate G-code's header, never the whole (often hundreds of MB) as a string.
|
|
74
|
+
const gcode = await zip.file(`Metadata/plate_${plateIndex + 1}.gcode`)?.async("nodebuffer");
|
|
75
|
+
// The project settings are authoritative; the plate's G-code header adds the layer count.
|
|
76
|
+
if (gcode)
|
|
77
|
+
settings = { ...headerSettings(gcode.subarray(0, 64 * 1024).toString("latin1")), ...settings };
|
|
78
|
+
let estimate;
|
|
79
|
+
const info = await text("Metadata/slice_info.config");
|
|
80
|
+
const plate = info?.match(/<plate(?:\s[^>]*)?>[\s\S]*?<\/plate>/g)
|
|
81
|
+
?.find(block => new RegExp(`<metadata\\s+key=["']index["']\\s+value=["']${plateIndex + 1}["']`).test(block));
|
|
82
|
+
if (plate) {
|
|
83
|
+
const value = (key) => {
|
|
84
|
+
const found = plate.match(new RegExp(`<metadata\\s+key=["']${key}["']\\s+value=["']([^"']+)["']`))?.[1];
|
|
85
|
+
const parsed = found && /^\d+(?:\.\d+)?$/.test(found) ? Number(found) : undefined;
|
|
86
|
+
return parsed !== undefined && parsed > 0 ? parsed : undefined;
|
|
87
|
+
};
|
|
88
|
+
const seconds = value("prediction"), grams = value("weight");
|
|
89
|
+
if (seconds !== undefined || grams !== undefined)
|
|
90
|
+
estimate = defined({ seconds, grams });
|
|
91
|
+
}
|
|
92
|
+
return { ...(plates.size ? { plate_count: plates.size } : {}), ...(estimate ? { estimate } : {}), settings };
|
|
93
|
+
}
|
|
94
|
+
const clean = (text) => text.replace(/\r\n?/g, "\n").replace(/[\x00-\x09\x0b-\x1f\x7f]/g, " ").trim();
|
|
95
|
+
export function slicerReport(result) {
|
|
96
|
+
if (!result || typeof result !== "object" || Array.isArray(result))
|
|
97
|
+
return undefined;
|
|
98
|
+
const value = result;
|
|
99
|
+
const plates = Array.isArray(value.sliced_plates) ? value.sliced_plates : [];
|
|
100
|
+
const ids = plates.map(plate => plate && typeof plate === "object" ? plate.id : undefined);
|
|
101
|
+
const plateCount = ids.length && ids.every(id => typeof id === "number" && Number.isSafeInteger(id) && id > 0) ? new Set(ids).size : undefined;
|
|
102
|
+
const warnings = plates.map(plate => plate && typeof plate.warning_message === "string" ? clean(plate.warning_message) : "").filter(Boolean);
|
|
103
|
+
// Anything but the slicer's success code fails closed, a missing one included.
|
|
104
|
+
const failed = value.return_code !== 0;
|
|
105
|
+
const error = typeof value.error_string === "string" ? clean(value.error_string) : "";
|
|
106
|
+
const words = [...new Set([...(failed && error ? [error] : []), ...warnings])].join("\n").slice(0, 2000);
|
|
107
|
+
if (failed)
|
|
108
|
+
return { failed, message: words || `The slicer could not slice this model (return code ${String(value.return_code)}).` };
|
|
109
|
+
return { failed, ...(words ? { message: words } : {}), ...(plateCount ? { plate_count: plateCount } : {}) };
|
|
110
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { CAMERA_STREAM_TOOLS } from "./printers/camera-tools.js";
|
|
3
3
|
import { withAnnotations } from "./tool-annotations.js";
|
|
4
|
+
import { withRemote } from "./counterpart/remote.js";
|
|
5
|
+
import { slicedFileFacts, slicerReport } from "./counterpart/sliced-file.js";
|
|
6
|
+
/** The slicer's verdict beside its result.json (`slicer_report`). */
|
|
7
|
+
function reportOf(result) {
|
|
8
|
+
const report = slicerReport(result);
|
|
9
|
+
return report ? { slicer_report: report } : {};
|
|
10
|
+
}
|
|
11
|
+
function withSlicerReport(sliced) {
|
|
12
|
+
return { ...sliced, ...reportOf(sliced.slicer_result) };
|
|
13
|
+
}
|
|
4
14
|
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
5
15
|
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
6
16
|
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
|
@@ -795,6 +805,17 @@ async function previewOfSnapshot(snapshot, plateIndex, sha256) {
|
|
|
795
805
|
const preview = await previewPrintFile({ gcode, ...(projectColours ? { projectColours } : {}) });
|
|
796
806
|
return preview ? { ...preview, artifact_sha256: sha256, plate_index: plateIndex } : undefined;
|
|
797
807
|
}
|
|
808
|
+
/**
|
|
809
|
+
* The slice's arguments change a resolved process: an explicit value (layer height, walls, infill, supports), or a
|
|
810
|
+
* quality or strength other than standard. Invalid values throw, as slice_stl's own parsing does.
|
|
811
|
+
*/
|
|
812
|
+
function changesProcess(args) {
|
|
813
|
+
// slice_stl's own validation of these values (quality, strength, layer height, walls, infill, supports).
|
|
814
|
+
const nozzle = Number(args.nozzle_diameter || DEFAULT_NOZZLE_DIAMETER);
|
|
815
|
+
parseProcessOverrides(withPrintQuality(args, nozzle), nozzle);
|
|
816
|
+
return ["layer_height", "wall_loops", "sparse_infill_density", "enable_support", "support_type", "support_threshold_angle"].some(key => args[key] !== undefined)
|
|
817
|
+
|| (args.quality !== undefined && args.quality !== "standard") || (args.strength !== undefined && args.strength !== "standard");
|
|
818
|
+
}
|
|
798
819
|
function hasExplicitSlicerProfile(args) {
|
|
799
820
|
return typeof args?.slicer_profile === "string" && args.slicer_profile.trim().length > 0;
|
|
800
821
|
}
|
|
@@ -1673,7 +1694,12 @@ class BambuPrinterMCPServer {
|
|
|
1673
1694
|
}
|
|
1674
1695
|
return inventory;
|
|
1675
1696
|
}
|
|
1676
|
-
|
|
1697
|
+
/**
|
|
1698
|
+
* `sliceArgs`: the slice_stl arguments a client would send with this file (slicer_profile, template_name,
|
|
1699
|
+
* template_3mf_path, layer_height, wall_loops, sparse_infill_density, quality, strength). `requestedTemplatePath`:
|
|
1700
|
+
* their template, resolved. own_process says whether that slice prints the process the 3MF carries, unchanged.
|
|
1701
|
+
*/
|
|
1702
|
+
async inspectSliceSettings(sourcePath, sliceArgs = {}, requestedTemplatePath) {
|
|
1677
1703
|
if (!fs.existsSync(sourcePath)) {
|
|
1678
1704
|
throw new Error(`Slice settings source not found: ${sourcePath}`);
|
|
1679
1705
|
}
|
|
@@ -1693,8 +1719,10 @@ class BambuPrinterMCPServer {
|
|
|
1693
1719
|
metadata_keys: Object.keys(parsed.metadata),
|
|
1694
1720
|
summary: summarizeSliceSettings(config),
|
|
1695
1721
|
raw_key_count: Object.keys(config).length,
|
|
1696
|
-
// ownProcessTemplate itself,
|
|
1697
|
-
|
|
1722
|
+
// ownProcessTemplate itself, with the slice's own arguments, so this can't drift from slicing; an explicit
|
|
1723
|
+
// value or a quality or strength other than standard changes the process the file carries.
|
|
1724
|
+
// Validated first, so an invalid value is refused even when a named profile or template decides the answer.
|
|
1725
|
+
own_process: !changesProcess(sliceArgs) && (await ownProcessTemplate(sliceArgs, sourcePath, requestedTemplatePath)) !== undefined,
|
|
1698
1726
|
};
|
|
1699
1727
|
}
|
|
1700
1728
|
const content = fs.readFileSync(sourcePath, "utf8");
|
|
@@ -2106,7 +2134,7 @@ class BambuPrinterMCPServer {
|
|
|
2106
2134
|
},
|
|
2107
2135
|
{
|
|
2108
2136
|
name: "get_slice_settings",
|
|
2109
|
-
description: "Inspect slicer settings from a saved 3MF template or a JSON/config slicer profile without slicing anything. For a 3MF, own_process is true when
|
|
2137
|
+
description: "Inspect slicer settings from a saved 3MF template or a JSON/config slicer profile without slicing anything. For a 3MF given as source_path, own_process is true when slice_stl, given this call's other arguments as it would take them (slicer_profile, template_name, template_3mf_path, layer_height, wall_loops, sparse_infill_density, quality, strength), prints the process the file carries (a designer's Bambu Studio or Orca project) unchanged.",
|
|
2110
2138
|
inputSchema: {
|
|
2111
2139
|
type: "object",
|
|
2112
2140
|
properties: {
|
|
@@ -2116,12 +2144,21 @@ class BambuPrinterMCPServer {
|
|
|
2116
2144
|
},
|
|
2117
2145
|
template_name: {
|
|
2118
2146
|
type: "string",
|
|
2119
|
-
description: "Optional named template from the local registry.
|
|
2147
|
+
description: "Optional named template from the local registry. Without source_path it is the source read; with source_path it is the slice's template, for own_process."
|
|
2120
2148
|
},
|
|
2121
2149
|
template_dir: {
|
|
2122
2150
|
type: "string",
|
|
2123
2151
|
description: "Optional template directory override when resolving template_name."
|
|
2124
|
-
}
|
|
2152
|
+
},
|
|
2153
|
+
slicer_profile: { type: "string", description: "For own_process: the slicer_profile slice_stl would be given." },
|
|
2154
|
+
template_3mf_path: { type: "string", description: "For own_process: the template_3mf_path slice_stl would be given." },
|
|
2155
|
+
layer_height: { type: "number", description: "For own_process: the layer_height slice_stl would be given." },
|
|
2156
|
+
nozzle_diameter: { type: "string", description: "For own_process: the nozzle_diameter slice_stl would be given; layer heights are checked against it." },
|
|
2157
|
+
wall_loops: { type: "number", description: "For own_process: the wall_loops slice_stl would be given." },
|
|
2158
|
+
sparse_infill_density: { type: "string", description: "For own_process: the sparse_infill_density slice_stl would be given." },
|
|
2159
|
+
enable_support: { type: "boolean", description: "For own_process: the enable_support slice_stl would be given." },
|
|
2160
|
+
support_type: { type: "string", description: "For own_process: the support_type slice_stl would be given." },
|
|
2161
|
+
...PRINT_QUALITY_PROPERTIES
|
|
2125
2162
|
}
|
|
2126
2163
|
}
|
|
2127
2164
|
},
|
|
@@ -3101,7 +3138,7 @@ class BambuPrinterMCPServer {
|
|
|
3101
3138
|
},
|
|
3102
3139
|
outputSchema: FUSION_EXPORT_OUTPUT_SCHEMA
|
|
3103
3140
|
}
|
|
3104
|
-
].map(withAnnotations)
|
|
3141
|
+
].map(withAnnotations).map(withRemote)
|
|
3105
3142
|
};
|
|
3106
3143
|
});
|
|
3107
3144
|
this.server.setRequestHandler(CallToolRequestSchema, async (request, extra) => {
|
|
@@ -3247,6 +3284,8 @@ class BambuPrinterMCPServer {
|
|
|
3247
3284
|
artifact: { sha256: inspection.sha256, byte_length: byteLength, file_format: path.extname(snapshot).toLowerCase() === ".3mf" ? "3mf" : "gcode" },
|
|
3248
3285
|
selection: { bambu_model: model, plate_index: plateIndex, ...(nozzles === undefined ? {} : { nozzle_diameters: nozzles }), ...(bedType === undefined ? {} : { bed_type: bedType }) },
|
|
3249
3286
|
inspection,
|
|
3287
|
+
// The plate's estimate and slice settings, and the file's plate count, read here so clients don't parse slicer files.
|
|
3288
|
+
sliced: await slicedFileFacts(snapshot, plateIndex),
|
|
3250
3289
|
...(preview ? { preview } : {}),
|
|
3251
3290
|
};
|
|
3252
3291
|
});
|
|
@@ -3717,7 +3756,10 @@ class BambuPrinterMCPServer {
|
|
|
3717
3756
|
if (!args?.source_path && !args?.template_name) {
|
|
3718
3757
|
throw new Error("Missing required parameter: source_path or template_name");
|
|
3719
3758
|
}
|
|
3720
|
-
|
|
3759
|
+
// With source_path, the other arguments are the slice's (template_name is its template), as slice_stl takes them.
|
|
3760
|
+
result = args?.source_path
|
|
3761
|
+
? await this.inspectSliceSettings(String(args.source_path), args, namedTemplatePath(args, typeof args?.template_name === "string" && args.template_name.trim() ? resolveTemplatePathFromName(["json", "config", "3mf"]) : undefined))
|
|
3762
|
+
: await this.inspectSliceSettings(String(resolveTemplatePathFromName(["3mf", "json", "config"])));
|
|
3721
3763
|
break;
|
|
3722
3764
|
case "slice_with_template": {
|
|
3723
3765
|
if (!args?.stl_path) {
|
|
@@ -3909,8 +3951,9 @@ class BambuPrinterMCPServer {
|
|
|
3909
3951
|
const sliced = await this.stlManipulator.sliceSTLWithResult(String(args.stl_path), slicerType, slicerPath, activeSlicerProfile, undefined, // progressCallback
|
|
3910
3952
|
printerPreset, sliceBambuOptions);
|
|
3911
3953
|
await progressNotifications;
|
|
3912
|
-
// The text stays the sliced file's path; slicer_result is the slicer's own result.json, verbatim
|
|
3913
|
-
|
|
3954
|
+
// The text stays the sliced file's path; slicer_result is the slicer's own result.json, verbatim, and
|
|
3955
|
+
// slicer_report its verdict and words, so clients don't read the slicer's format.
|
|
3956
|
+
return { content: [{ type: "text", text: sliced.path }], structuredContent: withSlicerReport(sliced) };
|
|
3914
3957
|
}
|
|
3915
3958
|
case "print_3mf": {
|
|
3916
3959
|
if (!args?.three_mf_path) {
|
|
@@ -4295,7 +4338,7 @@ class BambuPrinterMCPServer {
|
|
|
4295
4338
|
tool: name,
|
|
4296
4339
|
...(error instanceof PrinterRefusal ? { code: error.code } : {}),
|
|
4297
4340
|
// The slicer's own result.json; the message starts with its error_string and warnings.
|
|
4298
|
-
...(error instanceof SlicerRunError && error.slicerResult ? { slicer_result: error.slicerResult } : {}),
|
|
4341
|
+
...(error instanceof SlicerRunError && error.slicerResult ? { slicer_result: error.slicerResult, ...reportOf(error.slicerResult) } : {}),
|
|
4299
4342
|
};
|
|
4300
4343
|
// MCP clients validate structuredContent against a declared outputSchema even on
|
|
4301
4344
|
// errors (SDK 1.32 Client.callTool), so those tools return the error as text only.
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finished slices kept for reuse: slicing the same model the same way again (Print Again, or Print after a
|
|
3
|
+
* client's estimate) restores the slicer's outputs instead of running it. A slicer run is identified by exactly
|
|
4
|
+
* what it was given: the slicer executable (its path, size and modification time, so an update slices again),
|
|
5
|
+
* this server's version, and its full argument list, with every input file replaced by its content hash and every
|
|
6
|
+
* output location by a placeholder. Profiles, filament files, the model and generated override files are all
|
|
7
|
+
* arguments, so editing any of them slices again. A run whose arguments name any other directory (its contents
|
|
8
|
+
* can't be hashed) is never cached, nor is a failed slice. Presets the slicer finds by itself, not named in its
|
|
9
|
+
* arguments (its own bundled system presets), count through the executable: a slicer update slices again.
|
|
10
|
+
*
|
|
11
|
+
* SLICE_CACHE=off turns it off; SLICE_CACHE_DIR moves it (default ~/.cache/<package name>/slices). The newest
|
|
12
|
+
* SLICE_CACHE_ENTRIES (default 16) slices are kept.
|
|
13
|
+
*/
|
|
14
|
+
export interface SlicerRun {
|
|
15
|
+
command: string;
|
|
16
|
+
args: string[];
|
|
17
|
+
/** What the slicer writes, kept and restored: files, or directories it fills. */
|
|
18
|
+
outputs: string[];
|
|
19
|
+
/** Folders it writes into that aren't kept as a whole (an --outputdir shared with other files). */
|
|
20
|
+
locations?: string[];
|
|
21
|
+
}
|
|
22
|
+
export declare function sliceCacheRoot(): string;
|
|
23
|
+
/** The run's cache key, or undefined when it can't be identified exactly (then it is never cached). */
|
|
24
|
+
export declare function sliceCacheKey(run: SlicerRun): Promise<string | undefined>;
|
|
25
|
+
/** Puts a kept slice's outputs where this run would write them; false when there is none. */
|
|
26
|
+
export declare function restoreSlice(key: string, run: SlicerRun): Promise<boolean>;
|
|
27
|
+
/** Keeps a finished slice's outputs, whole or not at all; a failure to keep it never fails the slice. */
|
|
28
|
+
export declare function keepSlice(key: string, run: SlicerRun): Promise<void>;
|