@mcpcn/mcp-image-compressor 1.0.6 → 1.0.7
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/dist/index.js +32 -20
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -145,11 +145,23 @@ function generateOutputPath(inputPath, outputPath, suffix, format) {
|
|
|
145
145
|
const newExt = format ? `.${format}` : parsedPath.ext;
|
|
146
146
|
const newName = `${parsedPath.name}${suffix}${newExt}`;
|
|
147
147
|
if (outputPath) {
|
|
148
|
-
//
|
|
149
|
-
|
|
150
|
-
|
|
148
|
+
// 判断outputPath是文件路径还是目录路径
|
|
149
|
+
const hasExtension = path.extname(outputPath) !== '';
|
|
150
|
+
if (hasExtension) {
|
|
151
|
+
// outputPath是完整的文件路径,直接使用
|
|
152
|
+
const outputDir = path.dirname(outputPath);
|
|
153
|
+
if (!fs.existsSync(outputDir)) {
|
|
154
|
+
fs.mkdirSync(outputDir, { recursive: true });
|
|
155
|
+
}
|
|
156
|
+
return outputPath;
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
// outputPath是目录路径,在目录中生成文件
|
|
160
|
+
if (!fs.existsSync(outputPath)) {
|
|
161
|
+
fs.mkdirSync(outputPath, { recursive: true });
|
|
162
|
+
}
|
|
163
|
+
return path.join(outputPath, newName);
|
|
151
164
|
}
|
|
152
|
-
return path.join(outputPath, newName);
|
|
153
165
|
}
|
|
154
166
|
return path.join(parsedPath.dir, newName);
|
|
155
167
|
}
|
|
@@ -355,26 +367,26 @@ async function compressToPercent(inputPaths, percent, overwrite = false, outputP
|
|
|
355
367
|
}
|
|
356
368
|
const COMPRESS_TO_SIZE_TOOL = {
|
|
357
369
|
name: "image_compress_to_size",
|
|
358
|
-
description: "
|
|
370
|
+
description: "Compress images down to a specific target size",
|
|
359
371
|
inputSchema: {
|
|
360
372
|
type: "object",
|
|
361
373
|
properties: {
|
|
362
374
|
inputPaths: {
|
|
363
375
|
type: "array",
|
|
364
376
|
items: { type: "string" },
|
|
365
|
-
description: "
|
|
377
|
+
description: "Array of image paths; files and directories are supported",
|
|
366
378
|
},
|
|
367
379
|
size: {
|
|
368
380
|
type: "string",
|
|
369
|
-
description: "
|
|
381
|
+
description: "Desired size such as 1mb/500kb/200k",
|
|
370
382
|
},
|
|
371
383
|
overwrite: {
|
|
372
384
|
type: "boolean",
|
|
373
|
-
description: "
|
|
385
|
+
description: "Whether to overwrite source files (default false)",
|
|
374
386
|
},
|
|
375
387
|
outputPath: {
|
|
376
388
|
type: "string",
|
|
377
|
-
description: "
|
|
389
|
+
description: "Optional directory for outputs when not overwriting",
|
|
378
390
|
},
|
|
379
391
|
},
|
|
380
392
|
required: ["inputPaths", "size"],
|
|
@@ -382,26 +394,26 @@ const COMPRESS_TO_SIZE_TOOL = {
|
|
|
382
394
|
};
|
|
383
395
|
const COMPRESS_TO_PERCENT_TOOL = {
|
|
384
396
|
name: "image_compress_to_percent",
|
|
385
|
-
description: "
|
|
397
|
+
description: "Compress images to a percentage of their original size",
|
|
386
398
|
inputSchema: {
|
|
387
399
|
type: "object",
|
|
388
400
|
properties: {
|
|
389
401
|
inputPaths: {
|
|
390
402
|
type: "array",
|
|
391
403
|
items: { type: "string" },
|
|
392
|
-
description: "
|
|
404
|
+
description: "Array of image paths; files and directories are supported",
|
|
393
405
|
},
|
|
394
406
|
percent: {
|
|
395
407
|
type: "string",
|
|
396
|
-
description: "
|
|
408
|
+
description: "Percent of the original size, e.g. 50 or 50%",
|
|
397
409
|
},
|
|
398
410
|
overwrite: {
|
|
399
411
|
type: "boolean",
|
|
400
|
-
description: "
|
|
412
|
+
description: "Whether to overwrite source files (default false)",
|
|
401
413
|
},
|
|
402
414
|
outputPath: {
|
|
403
415
|
type: "string",
|
|
404
|
-
description: "
|
|
416
|
+
description: "Optional directory for outputs when not overwriting",
|
|
405
417
|
},
|
|
406
418
|
},
|
|
407
419
|
required: ["inputPaths", "percent"],
|
|
@@ -420,8 +432,8 @@ async function handleCompressToSize(inputPaths, size, overwrite = false, outputP
|
|
|
420
432
|
text: JSON.stringify({
|
|
421
433
|
success: !hasErrors,
|
|
422
434
|
message: hasErrors
|
|
423
|
-
?
|
|
424
|
-
:
|
|
435
|
+
? `Processed ${totalProcessed} images; ${results.length} succeeded, ${errors.length} failed to reach ${size}`
|
|
436
|
+
: `Compressed ${results.length} images to target size ${size}`,
|
|
425
437
|
compressedFiles: results,
|
|
426
438
|
errors: hasErrors ? errors : undefined,
|
|
427
439
|
}, null, 2),
|
|
@@ -446,8 +458,8 @@ async function handleCompressToPercent(inputPaths, percent, overwrite = false, o
|
|
|
446
458
|
text: JSON.stringify({
|
|
447
459
|
success: !hasErrors,
|
|
448
460
|
message: hasErrors
|
|
449
|
-
?
|
|
450
|
-
:
|
|
461
|
+
? `Processed ${totalProcessed} images; ${results.length} succeeded, ${errors.length} failed to reach ${parsePercent(percent)}% of original size`
|
|
462
|
+
: `Compressed ${results.length} images to ${parsePercent(percent)}% of the original size`,
|
|
451
463
|
compressedFiles: results,
|
|
452
464
|
errors: hasErrors ? errors : undefined,
|
|
453
465
|
}, null, 2),
|
|
@@ -491,7 +503,7 @@ server.setRequestHandler(types_js_1.CallToolRequestSchema, async (request) => {
|
|
|
491
503
|
content: [
|
|
492
504
|
{
|
|
493
505
|
type: "text",
|
|
494
|
-
text:
|
|
506
|
+
text: `Unknown tool: ${request.params.name}`,
|
|
495
507
|
},
|
|
496
508
|
],
|
|
497
509
|
isError: true,
|
|
@@ -503,7 +515,7 @@ server.setRequestHandler(types_js_1.CallToolRequestSchema, async (request) => {
|
|
|
503
515
|
content: [
|
|
504
516
|
{
|
|
505
517
|
type: "text",
|
|
506
|
-
text:
|
|
518
|
+
text: `Error: ${error instanceof Error ? error.message : String(error)}`,
|
|
507
519
|
},
|
|
508
520
|
],
|
|
509
521
|
isError: true,
|