@foldbun/stack-mcp 0.0.0-stage → 0.1.0
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/CHANGELOG.md +17 -0
- package/LICENSE +21 -0
- package/README.md +165 -2
- package/bin/foldbun-stack-mcp.mjs +8 -0
- package/package.json +45 -4
- package/src/batch.js +129 -0
- package/src/client.js +179 -0
- package/src/common.js +59 -0
- package/src/config.js +116 -0
- package/src/files.js +209 -0
- package/src/index.js +3 -0
- package/src/instructions.js +23 -0
- package/src/net.js +198 -0
- package/src/ops.js +46 -0
- package/src/overlay.js +96 -0
- package/src/png.js +335 -0
- package/src/product.js +49 -0
- package/src/server.js +53 -0
- package/src/sniff.js +8 -0
- package/src/tools.js +336 -0
- package/src/workflows.js +525 -0
package/src/overlay.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// Raster overlays for segmentation results, and inline-image sizing for MCP image content blocks.
|
|
2
|
+
|
|
3
|
+
import { MIB } from './config.js'
|
|
4
|
+
import { decodePng, encodePng, downscale, createImage, parseColor, fillRings, strokeRings, strokeRect, drawLabel } from './png.js'
|
|
5
|
+
|
|
6
|
+
/** Largest image (bytes) returned inline as an MCP image content block. */
|
|
7
|
+
export const INLINE_CAP = 1 * MIB
|
|
8
|
+
export const PREVIEW_SIDE = 1024
|
|
9
|
+
|
|
10
|
+
export const PALETTE = ['#e6194b', '#3cb44b', '#4363d8', '#f58231', '#911eb4', '#008080', '#9a6324', '#800000', '#808000', '#000075']
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Prepare an image for inline return. PNG over the cap is downscaled to a 1024 px preview; other
|
|
14
|
+
* formats over the cap are not returned inline. Returns { base64, mimeType, downscaled } or undefined.
|
|
15
|
+
*/
|
|
16
|
+
export function inlineImage(bytes, mimeType) {
|
|
17
|
+
if (bytes.length <= INLINE_CAP) return { base64: bytes.toString('base64'), mimeType, downscaled: false }
|
|
18
|
+
if (mimeType !== 'image/png') return undefined
|
|
19
|
+
try {
|
|
20
|
+
const small = encodePng(downscale(decodePng(bytes), PREVIEW_SIDE))
|
|
21
|
+
if (small.length <= INLINE_CAP) return { base64: small.toString('base64'), mimeType, downscaled: true }
|
|
22
|
+
} catch {
|
|
23
|
+
// too large to decode, or an unsupported PNG variant: file only
|
|
24
|
+
}
|
|
25
|
+
return undefined
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const ringsOf = (ins, s) => (ins.polygon ?? []).flatMap((part) => [part.outer, ...(part.holes ?? [])].map((r) => r.map((v) => v * s)))
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Draw the instances of a segmentation response on a PNG. `source` is { bytes, type } of the input
|
|
32
|
+
* image. For a PNG input the overlay is drawn on the picture itself. Otherwise (JPEG and WebP are
|
|
33
|
+
* not decoded here) it is drawn on a plain grey canvas of the same size: outlines and labels only.
|
|
34
|
+
*
|
|
35
|
+
* mode 'overlay': translucent polygon fill, outline, box and label (stack_segment)
|
|
36
|
+
* mode 'boxes': box and numbered label only (stack_annotate)
|
|
37
|
+
*
|
|
38
|
+
* Returns { png: Buffer, onPhoto: boolean }.
|
|
39
|
+
*/
|
|
40
|
+
export function renderOverlay(source, j, labels, mode = 'overlay') {
|
|
41
|
+
const w = j.image?.width
|
|
42
|
+
const h = j.image?.height
|
|
43
|
+
let base
|
|
44
|
+
let onPhoto = false
|
|
45
|
+
if (source?.type === 'image/png') {
|
|
46
|
+
try {
|
|
47
|
+
base = decodePng(source.bytes)
|
|
48
|
+
onPhoto = true
|
|
49
|
+
} catch {
|
|
50
|
+
base = undefined
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (!base) base = createImage(w, h, [236, 236, 236, 255])
|
|
54
|
+
const s = base.width / w
|
|
55
|
+
const scale = Math.max(1, Math.min(4, Math.round(Math.min(base.width, base.height) / 300)))
|
|
56
|
+
const t = Math.max(2, scale)
|
|
57
|
+
;(j.instances ?? []).forEach((ins, n) => {
|
|
58
|
+
const color = parseColor(PALETTE[(ins.prompt ?? n) % PALETTE.length])
|
|
59
|
+
if (mode === 'overlay') {
|
|
60
|
+
const rings = ringsOf(ins, s)
|
|
61
|
+
if (rings.length) {
|
|
62
|
+
fillRings(base, rings, color, 0.3)
|
|
63
|
+
strokeRings(base, rings, color, Math.max(1, scale))
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
const [x1, y1, x2, y2] = ins.bbox.map((v) => v * s)
|
|
67
|
+
strokeRect(base, x1, y1, x2 - x1, y2 - y1, color, t)
|
|
68
|
+
const label = `${n} ${ins.label ?? labels[ins.prompt] ?? 'object'} ${Number(ins.score).toFixed(2)}`
|
|
69
|
+
const ly = y1 - (7 * scale + 4 * scale) >= 0 ? y1 - (7 * scale + 4 * scale) : y1
|
|
70
|
+
drawLabel(base, label.slice(0, 40), Math.max(0, x1), ly, [255, 255, 255, 255], color, scale)
|
|
71
|
+
})
|
|
72
|
+
return { png: encodePng(base), onPhoto }
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** SVG overlay: the original image embedded, with outlines, boxes and labels (any input format). */
|
|
76
|
+
export function overlaySvg(img, j, labels) {
|
|
77
|
+
const w = j.image?.width
|
|
78
|
+
const h = j.image?.height
|
|
79
|
+
const parts = [`<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="${w}" height="${h}" viewBox="0 0 ${w} ${h}">`]
|
|
80
|
+
if (img.bytes.length <= 8 * 1024 * 1024) parts.push(`<image width="${w}" height="${h}" xlink:href="data:${img.type};base64,${img.base64}"/>`)
|
|
81
|
+
else parts.push(`<rect width="${w}" height="${h}" fill="#fff"/>`)
|
|
82
|
+
;(j.instances ?? []).forEach((ins, n) => {
|
|
83
|
+
const color = PALETTE[(ins.prompt ?? n) % PALETTE.length]
|
|
84
|
+
for (const part of ins.polygon ?? []) {
|
|
85
|
+
const ring = (r) => `M${r.map((v, i) => (i % 2 === 0 ? (i ? 'L' : '') + v : ',' + v)).join('')}Z`
|
|
86
|
+
const d = [part.outer, ...(part.holes ?? [])].map(ring).join(' ')
|
|
87
|
+
parts.push(`<path d="${d}" fill="${color}" fill-opacity="0.3" fill-rule="evenodd" stroke="${color}" stroke-width="2"/>`)
|
|
88
|
+
}
|
|
89
|
+
const [x1, y1, x2, y2] = ins.bbox
|
|
90
|
+
parts.push(`<rect x="${x1}" y="${y1}" width="${x2 - x1}" height="${y2 - y1}" fill="none" stroke="${color}" stroke-width="2"/>`)
|
|
91
|
+
const label = String(ins.label ?? labels[ins.prompt] ?? 'object').replace(/[<>&"]/g, '')
|
|
92
|
+
parts.push(`<text x="${x1 + 3}" y="${Math.max(12, y1 - 4)}" font-family="sans-serif" font-size="14" fill="${color}">#${n} ${label} ${Number(ins.score).toFixed(2)}</text>`)
|
|
93
|
+
})
|
|
94
|
+
parts.push('</svg>')
|
|
95
|
+
return parts.join('\n')
|
|
96
|
+
}
|
package/src/png.js
ADDED
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
// Pure-JS PNG support (node:zlib only): decode to RGBA, encode, box-filter downscale, and a few
|
|
2
|
+
// drawing primitives (alpha-blended polygon fill, lines, rectangles, 5x7 bitmap text). Used for
|
|
3
|
+
// overlays, annotations and product-photo compositing. JPEG and WebP are not decoded.
|
|
4
|
+
|
|
5
|
+
import zlib from 'node:zlib'
|
|
6
|
+
|
|
7
|
+
const SIG = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
|
|
8
|
+
export const MAX_DECODE_PIXELS = 40_000_000
|
|
9
|
+
|
|
10
|
+
/** An RGBA image: { width, height, data: Uint8Array(width*height*4) }. */
|
|
11
|
+
export function createImage(width, height, fill = [0, 0, 0, 0]) {
|
|
12
|
+
const data = new Uint8Array(width * height * 4)
|
|
13
|
+
if (fill[0] || fill[1] || fill[2] || fill[3]) for (let i = 0; i < data.length; i += 4) { data[i] = fill[0]; data[i + 1] = fill[1]; data[i + 2] = fill[2]; data[i + 3] = fill[3] }
|
|
14
|
+
return { width, height, data }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function paeth(a, b, c) {
|
|
18
|
+
const p = a + b - c
|
|
19
|
+
const pa = Math.abs(p - a)
|
|
20
|
+
const pb = Math.abs(p - b)
|
|
21
|
+
const pc = Math.abs(p - c)
|
|
22
|
+
return pa <= pb && pa <= pc ? a : pb <= pc ? b : c
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Decode a non-interlaced PNG (any colour type, depth 1 to 16) to RGBA. Throws on anything else. */
|
|
26
|
+
export function decodePng(buf) {
|
|
27
|
+
if (buf.length < 33 || !buf.subarray(0, 8).equals(SIG)) throw new Error('not a PNG')
|
|
28
|
+
let pos = 8
|
|
29
|
+
let ihdr
|
|
30
|
+
let plte
|
|
31
|
+
let trns
|
|
32
|
+
const idat = []
|
|
33
|
+
while (pos + 8 <= buf.length) {
|
|
34
|
+
const len = buf.readUInt32BE(pos)
|
|
35
|
+
const type = buf.toString('latin1', pos + 4, pos + 8)
|
|
36
|
+
const body = buf.subarray(pos + 8, pos + 8 + len)
|
|
37
|
+
pos += 12 + len
|
|
38
|
+
if (type === 'IHDR') ihdr = body
|
|
39
|
+
else if (type === 'PLTE') plte = body
|
|
40
|
+
else if (type === 'tRNS') trns = body
|
|
41
|
+
else if (type === 'IDAT') idat.push(body)
|
|
42
|
+
else if (type === 'IEND') break
|
|
43
|
+
}
|
|
44
|
+
if (!ihdr) throw new Error('PNG without IHDR')
|
|
45
|
+
const width = ihdr.readUInt32BE(0)
|
|
46
|
+
const height = ihdr.readUInt32BE(4)
|
|
47
|
+
const depth = ihdr[8]
|
|
48
|
+
const ctype = ihdr[9]
|
|
49
|
+
if (ihdr[12] !== 0) throw new Error('interlaced PNG is not supported')
|
|
50
|
+
if (!width || !height || width * height > MAX_DECODE_PIXELS) throw new Error('PNG too large to process')
|
|
51
|
+
const channels = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 }[ctype]
|
|
52
|
+
if (!channels || ![1, 2, 4, 8, 16].includes(depth)) throw new Error('unsupported PNG format')
|
|
53
|
+
const bpp = Math.max(1, (channels * depth) >> 3)
|
|
54
|
+
const stride = Math.ceil((width * channels * depth) / 8)
|
|
55
|
+
const raw = zlib.inflateSync(Buffer.concat(idat))
|
|
56
|
+
if (raw.length < (stride + 1) * height) throw new Error('truncated PNG data')
|
|
57
|
+
const px = new Uint8Array(stride * height)
|
|
58
|
+
for (let y = 0; y < height; y++) {
|
|
59
|
+
const ft = raw[y * (stride + 1)]
|
|
60
|
+
const src = y * (stride + 1) + 1
|
|
61
|
+
const dst = y * stride
|
|
62
|
+
for (let x = 0; x < stride; x++) {
|
|
63
|
+
const v = raw[src + x]
|
|
64
|
+
const a = x >= bpp ? px[dst + x - bpp] : 0
|
|
65
|
+
const b = y > 0 ? px[dst - stride + x] : 0
|
|
66
|
+
const c = x >= bpp && y > 0 ? px[dst - stride + x - bpp] : 0
|
|
67
|
+
let r
|
|
68
|
+
switch (ft) {
|
|
69
|
+
case 0: r = v; break
|
|
70
|
+
case 1: r = v + a; break
|
|
71
|
+
case 2: r = v + b; break
|
|
72
|
+
case 3: r = v + ((a + b) >> 1); break
|
|
73
|
+
case 4: r = v + paeth(a, b, c); break
|
|
74
|
+
default: throw new Error('bad PNG filter')
|
|
75
|
+
}
|
|
76
|
+
px[dst + x] = r & 0xff
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const out = new Uint8Array(width * height * 4)
|
|
80
|
+
const sample = (row, idx) => {
|
|
81
|
+
// idx-th sample of the row, as 0..2^depth-1
|
|
82
|
+
if (depth === 8) return px[row + idx]
|
|
83
|
+
if (depth === 16) return (px[row + idx * 2] << 8) | px[row + idx * 2 + 1]
|
|
84
|
+
const bitPos = idx * depth
|
|
85
|
+
const byte = px[row + (bitPos >> 3)]
|
|
86
|
+
return (byte >> (8 - depth - (bitPos & 7))) & ((1 << depth) - 1)
|
|
87
|
+
}
|
|
88
|
+
const max = (1 << depth) - 1
|
|
89
|
+
const to8 = (v) => (depth === 8 ? v : depth === 16 ? v >> 8 : Math.round((v * 255) / max))
|
|
90
|
+
const trnsGray = ctype === 0 && trns && trns.length >= 2 ? trns.readUInt16BE(0) : -1
|
|
91
|
+
const trnsRgb = ctype === 2 && trns && trns.length >= 6 ? [trns.readUInt16BE(0), trns.readUInt16BE(2), trns.readUInt16BE(4)] : undefined
|
|
92
|
+
for (let y = 0; y < height; y++) {
|
|
93
|
+
const row = y * stride
|
|
94
|
+
for (let x = 0; x < width; x++) {
|
|
95
|
+
const o = (y * width + x) * 4
|
|
96
|
+
if (ctype === 0) {
|
|
97
|
+
const g = sample(row, x)
|
|
98
|
+
out[o] = out[o + 1] = out[o + 2] = to8(g)
|
|
99
|
+
out[o + 3] = g === trnsGray ? 0 : 255
|
|
100
|
+
} else if (ctype === 2) {
|
|
101
|
+
const r = sample(row, x * 3)
|
|
102
|
+
const g = sample(row, x * 3 + 1)
|
|
103
|
+
const b = sample(row, x * 3 + 2)
|
|
104
|
+
out[o] = to8(r); out[o + 1] = to8(g); out[o + 2] = to8(b)
|
|
105
|
+
out[o + 3] = trnsRgb && r === trnsRgb[0] && g === trnsRgb[1] && b === trnsRgb[2] ? 0 : 255
|
|
106
|
+
} else if (ctype === 3) {
|
|
107
|
+
const i = sample(row, x)
|
|
108
|
+
if (!plte || i * 3 + 2 >= plte.length) throw new Error('bad PNG palette')
|
|
109
|
+
out[o] = plte[i * 3]; out[o + 1] = plte[i * 3 + 1]; out[o + 2] = plte[i * 3 + 2]
|
|
110
|
+
out[o + 3] = trns && i < trns.length ? trns[i] : 255
|
|
111
|
+
} else if (ctype === 4) {
|
|
112
|
+
out[o] = out[o + 1] = out[o + 2] = to8(sample(row, x * 2))
|
|
113
|
+
out[o + 3] = to8(sample(row, x * 2 + 1))
|
|
114
|
+
} else {
|
|
115
|
+
out[o] = to8(sample(row, x * 4)); out[o + 1] = to8(sample(row, x * 4 + 1)); out[o + 2] = to8(sample(row, x * 4 + 2)); out[o + 3] = to8(sample(row, x * 4 + 3))
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return { width, height, data: out }
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function chunk(type, data) {
|
|
123
|
+
const len = Buffer.alloc(4)
|
|
124
|
+
len.writeUInt32BE(data.length)
|
|
125
|
+
const body = Buffer.concat([Buffer.from(type, 'latin1'), data])
|
|
126
|
+
const crc = Buffer.alloc(4)
|
|
127
|
+
crc.writeUInt32BE(zlib.crc32(body) >>> 0)
|
|
128
|
+
return Buffer.concat([len, body, crc])
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Encode RGBA as PNG. Writes colour type 2 (RGB) when every pixel is opaque, else type 6. Sub filter. */
|
|
132
|
+
export function encodePng({ width, height, data }) {
|
|
133
|
+
let opaque = true
|
|
134
|
+
for (let i = 3; i < data.length; i += 4) if (data[i] !== 255) { opaque = false; break }
|
|
135
|
+
const ch = opaque ? 3 : 4
|
|
136
|
+
const stride = width * ch
|
|
137
|
+
const raw = Buffer.alloc((stride + 1) * height)
|
|
138
|
+
for (let y = 0; y < height; y++) {
|
|
139
|
+
const o = y * (stride + 1)
|
|
140
|
+
raw[o] = 1
|
|
141
|
+
for (let x = 0; x < width; x++) {
|
|
142
|
+
for (let k = 0; k < ch; k++) {
|
|
143
|
+
const v = data[(y * width + x) * 4 + k]
|
|
144
|
+
const left = x > 0 ? data[(y * width + x - 1) * 4 + k] : 0
|
|
145
|
+
raw[o + 1 + x * ch + k] = (v - left) & 0xff
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
const ihdr = Buffer.alloc(13)
|
|
150
|
+
ihdr.writeUInt32BE(width, 0)
|
|
151
|
+
ihdr.writeUInt32BE(height, 4)
|
|
152
|
+
ihdr[8] = 8
|
|
153
|
+
ihdr[9] = opaque ? 2 : 6
|
|
154
|
+
return Buffer.concat([SIG, chunk('IHDR', ihdr), chunk('IDAT', zlib.deflateSync(raw)), chunk('IEND', Buffer.alloc(0))])
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Box-filter downscale so the long side is at most `maxSide` (alpha-weighted). Returns the input if already small. */
|
|
158
|
+
export function downscale(img, maxSide) {
|
|
159
|
+
const long = Math.max(img.width, img.height)
|
|
160
|
+
if (long <= maxSide) return img
|
|
161
|
+
const f = maxSide / long
|
|
162
|
+
const w = Math.max(1, Math.round(img.width * f))
|
|
163
|
+
const h = Math.max(1, Math.round(img.height * f))
|
|
164
|
+
const out = createImage(w, h)
|
|
165
|
+
for (let y = 0; y < h; y++) {
|
|
166
|
+
const y0 = Math.floor((y * img.height) / h)
|
|
167
|
+
const y1 = Math.max(y0 + 1, Math.floor(((y + 1) * img.height) / h))
|
|
168
|
+
for (let x = 0; x < w; x++) {
|
|
169
|
+
const x0 = Math.floor((x * img.width) / w)
|
|
170
|
+
const x1 = Math.max(x0 + 1, Math.floor(((x + 1) * img.width) / w))
|
|
171
|
+
let r = 0, g = 0, b = 0, a = 0, n = 0
|
|
172
|
+
for (let yy = y0; yy < y1; yy++) {
|
|
173
|
+
for (let xx = x0; xx < x1; xx++) {
|
|
174
|
+
const i = (yy * img.width + xx) * 4
|
|
175
|
+
const al = img.data[i + 3]
|
|
176
|
+
r += img.data[i] * al; g += img.data[i + 1] * al; b += img.data[i + 2] * al; a += al; n++
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
const o = (y * w + x) * 4
|
|
180
|
+
if (a > 0) { out.data[o] = Math.round(r / a); out.data[o + 1] = Math.round(g / a); out.data[o + 2] = Math.round(b / a) }
|
|
181
|
+
out.data[o + 3] = Math.round(a / n)
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return out
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// ---- drawing ----
|
|
188
|
+
|
|
189
|
+
export function parseColor(input, fallback = [255, 255, 255, 255]) {
|
|
190
|
+
if (typeof input !== 'string') return fallback
|
|
191
|
+
const s = input.trim().toLowerCase()
|
|
192
|
+
if (s === 'transparent' || s === 'none') return [0, 0, 0, 0]
|
|
193
|
+
const named = { white: '#ffffff', black: '#000000', red: '#ff0000', green: '#00aa00', blue: '#0000ff', gray: '#808080', grey: '#808080', yellow: '#ffff00', orange: '#ff8000' }
|
|
194
|
+
const hex = (named[s] ?? s).replace(/^#/, '')
|
|
195
|
+
if (/^[0-9a-f]{3}$/.test(hex)) return [...hex].map((c) => parseInt(c + c, 16)).concat(255)
|
|
196
|
+
if (/^[0-9a-f]{6}$/.test(hex)) return [parseInt(hex.slice(0, 2), 16), parseInt(hex.slice(2, 4), 16), parseInt(hex.slice(4, 6), 16), 255]
|
|
197
|
+
if (/^[0-9a-f]{8}$/.test(hex)) return [parseInt(hex.slice(0, 2), 16), parseInt(hex.slice(2, 4), 16), parseInt(hex.slice(4, 6), 16), parseInt(hex.slice(6, 8), 16)]
|
|
198
|
+
return undefined
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** Blend `color` ([r,g,b], opacity 0..1) over the pixel at (x, y), source-over. */
|
|
202
|
+
export function blendPixel(img, x, y, color, opacity = 1) {
|
|
203
|
+
if (x < 0 || y < 0 || x >= img.width || y >= img.height) return
|
|
204
|
+
const i = (y * img.width + x) * 4
|
|
205
|
+
const d = img.data
|
|
206
|
+
const sa = opacity
|
|
207
|
+
const da = d[i + 3] / 255
|
|
208
|
+
const oa = sa + da * (1 - sa)
|
|
209
|
+
if (oa <= 0) return
|
|
210
|
+
for (let k = 0; k < 3; k++) d[i + k] = Math.round((color[k] * sa + d[i + k] * da * (1 - sa)) / oa)
|
|
211
|
+
d[i + 3] = Math.round(oa * 255)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export function fillRect(img, x, y, w, h, color, opacity = 1) {
|
|
215
|
+
const x0 = Math.max(0, Math.floor(x))
|
|
216
|
+
const y0 = Math.max(0, Math.floor(y))
|
|
217
|
+
const x1 = Math.min(img.width, Math.ceil(x + w))
|
|
218
|
+
const y1 = Math.min(img.height, Math.ceil(y + h))
|
|
219
|
+
for (let yy = y0; yy < y1; yy++) for (let xx = x0; xx < x1; xx++) blendPixel(img, xx, yy, color, opacity)
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export function strokeRect(img, x, y, w, h, color, t = 2) {
|
|
223
|
+
fillRect(img, x, y, w, t, color)
|
|
224
|
+
fillRect(img, x, y + h - t, w, t, color)
|
|
225
|
+
fillRect(img, x, y, t, h, color)
|
|
226
|
+
fillRect(img, x + w - t, y, t, h, color)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export function drawLine(img, x0, y0, x1, y1, color, t = 2) {
|
|
230
|
+
const dx = Math.abs(x1 - x0)
|
|
231
|
+
const dy = Math.abs(y1 - y0)
|
|
232
|
+
const n = Math.max(dx, dy, 1)
|
|
233
|
+
const half = Math.floor(t / 2)
|
|
234
|
+
for (let i = 0; i <= n; i++) {
|
|
235
|
+
const x = Math.round(x0 + ((x1 - x0) * i) / n)
|
|
236
|
+
const y = Math.round(y0 + ((y1 - y0) * i) / n)
|
|
237
|
+
fillRect(img, x - half, y - half, t, t, color)
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** Even-odd fill of polygon rings (each a flat [x0,y0,x1,y1,...] array), alpha-blended. */
|
|
242
|
+
export function fillRings(img, rings, color, opacity) {
|
|
243
|
+
let minY = Infinity
|
|
244
|
+
let maxY = -Infinity
|
|
245
|
+
for (const r of rings) for (let i = 1; i < r.length; i += 2) { minY = Math.min(minY, r[i]); maxY = Math.max(maxY, r[i]) }
|
|
246
|
+
if (!Number.isFinite(minY)) return
|
|
247
|
+
const y0 = Math.max(0, Math.floor(minY))
|
|
248
|
+
const y1 = Math.min(img.height - 1, Math.ceil(maxY))
|
|
249
|
+
for (let y = y0; y <= y1; y++) {
|
|
250
|
+
const yc = y + 0.5
|
|
251
|
+
const xs = []
|
|
252
|
+
for (const r of rings) {
|
|
253
|
+
const n = r.length / 2
|
|
254
|
+
for (let i = 0; i < n; i++) {
|
|
255
|
+
const ax = r[i * 2], ay = r[i * 2 + 1]
|
|
256
|
+
const j = (i + 1) % n
|
|
257
|
+
const bx = r[j * 2], by = r[j * 2 + 1]
|
|
258
|
+
if ((ay <= yc && by > yc) || (by <= yc && ay > yc)) xs.push(ax + ((yc - ay) * (bx - ax)) / (by - ay))
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
xs.sort((a, b) => a - b)
|
|
262
|
+
for (let k = 0; k + 1 < xs.length; k += 2) {
|
|
263
|
+
const xa = Math.max(0, Math.round(xs[k]))
|
|
264
|
+
const xb = Math.min(img.width, Math.round(xs[k + 1]))
|
|
265
|
+
for (let x = xa; x < xb; x++) blendPixel(img, x, y, color, opacity)
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export function strokeRings(img, rings, color, t = 2) {
|
|
271
|
+
for (const r of rings) {
|
|
272
|
+
const n = r.length / 2
|
|
273
|
+
for (let i = 0; i < n; i++) {
|
|
274
|
+
const j = (i + 1) % n
|
|
275
|
+
drawLine(img, r[i * 2], r[i * 2 + 1], r[j * 2], r[j * 2 + 1], color, t)
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// ---- 5x7 bitmap font (upper case, digits, a little punctuation; anything else draws as "?") ----
|
|
281
|
+
|
|
282
|
+
const GLYPHS = {
|
|
283
|
+
A: '01110 10001 10001 11111 10001 10001 10001', B: '11110 10001 10001 11110 10001 10001 11110',
|
|
284
|
+
C: '01110 10001 10000 10000 10000 10001 01110', D: '11110 10001 10001 10001 10001 10001 11110',
|
|
285
|
+
E: '11111 10000 10000 11110 10000 10000 11111', F: '11111 10000 10000 11110 10000 10000 10000',
|
|
286
|
+
G: '01110 10001 10000 10111 10001 10001 01111', H: '10001 10001 10001 11111 10001 10001 10001',
|
|
287
|
+
I: '01110 00100 00100 00100 00100 00100 01110', J: '00111 00010 00010 00010 00010 10010 01100',
|
|
288
|
+
K: '10001 10010 10100 11000 10100 10010 10001', L: '10000 10000 10000 10000 10000 10000 11111',
|
|
289
|
+
M: '10001 11011 10101 10101 10001 10001 10001', N: '10001 11001 10101 10101 10011 10001 10001',
|
|
290
|
+
O: '01110 10001 10001 10001 10001 10001 01110', P: '11110 10001 10001 11110 10000 10000 10000',
|
|
291
|
+
Q: '01110 10001 10001 10001 10101 10010 01101', R: '11110 10001 10001 11110 10100 10010 10001',
|
|
292
|
+
S: '01111 10000 10000 01110 00001 00001 11110', T: '11111 00100 00100 00100 00100 00100 00100',
|
|
293
|
+
U: '10001 10001 10001 10001 10001 10001 01110', V: '10001 10001 10001 10001 10001 01010 00100',
|
|
294
|
+
W: '10001 10001 10001 10101 10101 10101 01010', X: '10001 10001 01010 00100 01010 10001 10001',
|
|
295
|
+
Y: '10001 10001 01010 00100 00100 00100 00100', Z: '11111 00001 00010 00100 01000 10000 11111',
|
|
296
|
+
0: '01110 10001 10011 10101 11001 10001 01110', 1: '00100 01100 00100 00100 00100 00100 01110',
|
|
297
|
+
2: '01110 10001 00001 00110 01000 10000 11111', 3: '11110 00001 00001 01110 00001 00001 11110',
|
|
298
|
+
4: '00010 00110 01010 10010 11111 00010 00010', 5: '11111 10000 11110 00001 00001 10001 01110',
|
|
299
|
+
6: '00110 01000 10000 11110 10001 10001 01110', 7: '11111 00001 00010 00100 01000 01000 01000',
|
|
300
|
+
8: '01110 10001 10001 01110 10001 10001 01110', 9: '01110 10001 10001 01111 00001 00010 01100',
|
|
301
|
+
'.': '00000 00000 00000 00000 00000 01100 01100', ',': '00000 00000 00000 00000 01100 00100 01000',
|
|
302
|
+
':': '00000 01100 01100 00000 01100 01100 00000', '-': '00000 00000 00000 11111 00000 00000 00000',
|
|
303
|
+
_: '00000 00000 00000 00000 00000 00000 11111', '#': '01010 01010 11111 01010 11111 01010 01010',
|
|
304
|
+
'%': '11001 11010 00010 00100 01000 01011 10011', '(': '00010 00100 01000 01000 01000 00100 00010',
|
|
305
|
+
')': '01000 00100 00010 00010 00010 00100 01000', '/': '00001 00010 00010 00100 01000 01000 10000',
|
|
306
|
+
'+': '00000 00100 00100 11111 00100 00100 00000', '?': '01110 10001 00001 00010 00100 00000 00100',
|
|
307
|
+
'!': '00100 00100 00100 00100 00100 00000 00100', "'": '00100 00100 01000 00000 00000 00000 00000',
|
|
308
|
+
' ': '00000 00000 00000 00000 00000 00000 00000',
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
export function textWidth(str, scale) {
|
|
312
|
+
return str.length * 6 * scale - scale
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** Draw `str` with the top-left at (x, y); `scale` is the pixel size of one font dot. */
|
|
316
|
+
export function drawText(img, str, x, y, color, scale = 2) {
|
|
317
|
+
let cx = x
|
|
318
|
+
for (const raw of str) {
|
|
319
|
+
const ch = raw.toUpperCase()
|
|
320
|
+
const rows = (GLYPHS[ch] ?? GLYPHS['?']).split(' ')
|
|
321
|
+
for (let ry = 0; ry < 7; ry++) {
|
|
322
|
+
for (let rx = 0; rx < 5; rx++) if (rows[ry][rx] === '1') fillRect(img, cx + rx * scale, y + ry * scale, scale, scale, color)
|
|
323
|
+
}
|
|
324
|
+
cx += 6 * scale
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/** Text on a filled box (for labels over busy images). Returns the box height. */
|
|
329
|
+
export function drawLabel(img, str, x, y, fg, bg, scale = 2) {
|
|
330
|
+
const w = textWidth(str, scale) + 4 * scale
|
|
331
|
+
const h = 7 * scale + 4 * scale
|
|
332
|
+
fillRect(img, x, y, w, h, bg, 0.85)
|
|
333
|
+
drawText(img, str, x + 2 * scale, y + 2 * scale, fg, scale)
|
|
334
|
+
return h
|
|
335
|
+
}
|
package/src/product.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// Product-photo compositing: crop a cutout to its subject and place it on a solid or transparent canvas.
|
|
2
|
+
|
|
3
|
+
import { createImage } from './png.js'
|
|
4
|
+
|
|
5
|
+
/** Bounding box [x0, y0, x1, y1) of pixels with alpha above `threshold`, or the whole image if none. */
|
|
6
|
+
export function alphaBounds(img, threshold = 8) {
|
|
7
|
+
let x0 = img.width, y0 = img.height, x1 = -1, y1 = -1
|
|
8
|
+
for (let y = 0; y < img.height; y++) {
|
|
9
|
+
for (let x = 0; x < img.width; x++) {
|
|
10
|
+
if (img.data[(y * img.width + x) * 4 + 3] > threshold) {
|
|
11
|
+
if (x < x0) x0 = x
|
|
12
|
+
if (x > x1) x1 = x
|
|
13
|
+
if (y < y0) y0 = y
|
|
14
|
+
if (y > y1) y1 = y
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return x1 < 0 ? [0, 0, img.width, img.height] : [x0, y0, x1 + 1, y1 + 1]
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param cutout RGBA image (decoded PNG from background removal)
|
|
23
|
+
* @param opts { background: [r,g,b,a], padding: fraction of the longer subject side, square, crop }
|
|
24
|
+
*/
|
|
25
|
+
export function composeProduct(cutout, { background = [255, 255, 255, 255], padding = 0.1, square = false, crop = true } = {}) {
|
|
26
|
+
const [x0, y0, x1, y1] = crop ? alphaBounds(cutout) : [0, 0, cutout.width, cutout.height]
|
|
27
|
+
const w = x1 - x0
|
|
28
|
+
const h = y1 - y0
|
|
29
|
+
const pad = Math.round(padding * Math.max(w, h))
|
|
30
|
+
let cw = w + 2 * pad
|
|
31
|
+
let ch = h + 2 * pad
|
|
32
|
+
if (square) cw = ch = Math.max(cw, ch)
|
|
33
|
+
const out = createImage(cw, ch, background)
|
|
34
|
+
const ox = Math.floor((cw - w) / 2)
|
|
35
|
+
const oy = Math.floor((ch - h) / 2)
|
|
36
|
+
const bgA = background[3] / 255
|
|
37
|
+
for (let y = 0; y < h; y++) {
|
|
38
|
+
for (let x = 0; x < w; x++) {
|
|
39
|
+
const s = ((y0 + y) * cutout.width + x0 + x) * 4
|
|
40
|
+
const sa = cutout.data[s + 3] / 255
|
|
41
|
+
if (sa === 0) continue
|
|
42
|
+
const d = ((oy + y) * cw + ox + x) * 4
|
|
43
|
+
const oa = sa + bgA * (1 - sa)
|
|
44
|
+
for (let k = 0; k < 3; k++) out.data[d + k] = Math.round((cutout.data[s + k] * sa + background[k] * bgA * (1 - sa)) / oa)
|
|
45
|
+
out.data[d + 3] = Math.round(oa * 255)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return out
|
|
49
|
+
}
|
package/src/server.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
|
|
2
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
|
|
3
|
+
import { StackClient, configureProxyFromEnv } from './client.js'
|
|
4
|
+
import { VERSION, loadConfig, resolveAuth } from './config.js'
|
|
5
|
+
import { INSTRUCTIONS } from './instructions.js'
|
|
6
|
+
import { buildTools } from './tools.js'
|
|
7
|
+
import { buildWorkflowTools } from './workflows.js'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Create the MCP server. Nothing is sent over the network and nothing is written to disk here:
|
|
11
|
+
* registration of an agent project happens only when the model calls the register tool.
|
|
12
|
+
*/
|
|
13
|
+
export function createStackServer({ env = process.env, config = loadConfig(env) } = {}) {
|
|
14
|
+
const getAuth = () => resolveAuth(config)
|
|
15
|
+
const client = new StackClient(config, getAuth)
|
|
16
|
+
const server = new McpServer({ name: 'foldbun-stack', version: VERSION }, { instructions: INSTRUCTIONS })
|
|
17
|
+
|
|
18
|
+
const registered = {}
|
|
19
|
+
const ctx = {
|
|
20
|
+
config,
|
|
21
|
+
client,
|
|
22
|
+
getAuth,
|
|
23
|
+
// Keep the tool list stable after a registration: a tools/list_changed notification makes some
|
|
24
|
+
// clients (DeepSeek Harness) rebuild their catalog, and tool calls made in the same step then fail.
|
|
25
|
+
// The register tool itself refuses once a credential exists.
|
|
26
|
+
onRegistered: () => {},
|
|
27
|
+
}
|
|
28
|
+
const t = buildTools(ctx)
|
|
29
|
+
const w = buildWorkflowTools(ctx)
|
|
30
|
+
for (const tool of [t.ocr, t.segment, t.removeBackground, t.parseDocument, t.embed, ...Object.values(w), t.usage, t.register]) {
|
|
31
|
+
registered[tool === t.register ? 'register' : tool.name] = server.registerTool(
|
|
32
|
+
tool.name,
|
|
33
|
+
{ title: tool.title, description: tool.description, inputSchema: tool.inputSchema },
|
|
34
|
+
tool.handler,
|
|
35
|
+
)
|
|
36
|
+
}
|
|
37
|
+
// The register tool is offered only if no credential exists when the server starts.
|
|
38
|
+
let kind = 'none'
|
|
39
|
+
try {
|
|
40
|
+
kind = resolveAuth(config).kind
|
|
41
|
+
} catch {
|
|
42
|
+
kind = 'invalid'
|
|
43
|
+
}
|
|
44
|
+
if (kind !== 'none') registered.register.disable()
|
|
45
|
+
|
|
46
|
+
return { server, config, client }
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export async function main() {
|
|
50
|
+
configureProxyFromEnv()
|
|
51
|
+
const { server } = createStackServer()
|
|
52
|
+
await server.connect(new StdioServerTransport())
|
|
53
|
+
}
|
package/src/sniff.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Image type detection by magic bytes.
|
|
2
|
+
|
|
3
|
+
export function sniffImage(buf) {
|
|
4
|
+
if (buf.length >= 3 && buf[0] === 0xff && buf[1] === 0xd8 && buf[2] === 0xff) return 'image/jpeg'
|
|
5
|
+
if (buf.length >= 8 && buf.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))) return 'image/png'
|
|
6
|
+
if (buf.length >= 12 && buf.toString('latin1', 0, 4) === 'RIFF' && buf.toString('latin1', 8, 12) === 'WEBP') return 'image/webp'
|
|
7
|
+
return undefined
|
|
8
|
+
}
|