@sketric/favstash-mcp 0.3.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.
@@ -0,0 +1,501 @@
1
+ import { z } from "zod";
2
+
3
+ const httpsUrl = z
4
+ .string()
5
+ .url()
6
+ .regex(/^https:\/\//);
7
+ const localPath = z
8
+ .string()
9
+ .min(1)
10
+ .describe("Absolute path to a local JPEG, PNG, WebP, MP4, MOV, or PDF file.");
11
+ const nonEmptyString = z.string().min(1);
12
+ const normalizedPosition = z.number().min(0).max(1);
13
+ const mediaIndex = z.number().int().min(0).max(9);
14
+
15
+ function uniqueStrings(maxItems) {
16
+ return z
17
+ .array(nonEmptyString)
18
+ .max(maxItems)
19
+ .refine((values) => new Set(values).size === values.length, {
20
+ message: "Values must be unique.",
21
+ });
22
+ }
23
+
24
+ export function mediaItemSchema() {
25
+ return z
26
+ .object({
27
+ url: httpsUrl.optional(),
28
+ localPath: localPath.optional(),
29
+ kind: z.enum(["image", "video", "document"]).optional(),
30
+ title: z.string().min(1).max(200).optional(),
31
+ thumbnailUrl: httpsUrl.optional(),
32
+ thumbnailPath: localPath.optional(),
33
+ thumbnailTimestampMs: z.number().int().min(0).optional(),
34
+ altText: z.string().max(2_200).optional(),
35
+ durationSeconds: z.number().positive().optional(),
36
+ width: z.number().int().positive().optional(),
37
+ height: z.number().int().positive().optional(),
38
+ })
39
+ .strict()
40
+ .superRefine((value, context) => {
41
+ if (Boolean(value.url) === Boolean(value.localPath)) {
42
+ context.addIssue({
43
+ code: z.ZodIssueCode.custom,
44
+ message: "Provide exactly one of media[].url or media[].localPath.",
45
+ });
46
+ }
47
+ if (value.thumbnailUrl && value.thumbnailPath) {
48
+ context.addIssue({
49
+ code: z.ZodIssueCode.custom,
50
+ message:
51
+ "Provide at most one of media[].thumbnailUrl or media[].thumbnailPath.",
52
+ });
53
+ }
54
+ });
55
+ }
56
+
57
+ const instagramSettings = z
58
+ .object({
59
+ contentType: z
60
+ .enum(["auto", "feed_image", "reel", "story", "carousel"])
61
+ .optional(),
62
+ shareToFeed: z.boolean().optional(),
63
+ isAiGenerated: z.boolean().optional(),
64
+ collaborators: uniqueStrings(3).optional(),
65
+ userTags: z
66
+ .array(
67
+ z
68
+ .object({
69
+ username: nonEmptyString,
70
+ x: normalizedPosition.optional(),
71
+ y: normalizedPosition.optional(),
72
+ mediaIndex: mediaIndex.optional(),
73
+ })
74
+ .strict(),
75
+ )
76
+ .max(20)
77
+ .optional(),
78
+ locationId: nonEmptyString.optional(),
79
+ productTags: z
80
+ .array(
81
+ z
82
+ .object({
83
+ productId: nonEmptyString,
84
+ x: normalizedPosition.optional(),
85
+ y: normalizedPosition.optional(),
86
+ mediaIndex: mediaIndex.optional(),
87
+ })
88
+ .strict(),
89
+ )
90
+ .max(20)
91
+ .optional(),
92
+ brandedContentSponsorIds: uniqueStrings(2).optional(),
93
+ trialReel: z
94
+ .object({
95
+ enabled: z.boolean(),
96
+ graduationStrategy: z.enum(["MANUAL", "SS_PERFORMANCE"]).optional(),
97
+ })
98
+ .strict()
99
+ .optional(),
100
+ audio: z
101
+ .object({
102
+ id: nonEmptyString,
103
+ audioVolume: z.number().min(0).optional(),
104
+ videoVolume: z.number().min(0).optional(),
105
+ })
106
+ .strict()
107
+ .optional(),
108
+ })
109
+ .strict();
110
+
111
+ const youtubeSettingsFields = {
112
+ title: z
113
+ .string()
114
+ .min(2)
115
+ .max(100)
116
+ .regex(/^[^<>]*$/),
117
+ visibility: z.enum(["public", "private", "unlisted"]),
118
+ madeForKids: z.boolean(),
119
+ uploadCertified: z.boolean(),
120
+ tags: z.array(nonEmptyString).max(100),
121
+ thumbnailUrl: httpsUrl,
122
+ categoryId: z.string().regex(/^[0-9]+$/),
123
+ containsSyntheticMedia: z.boolean(),
124
+ notifySubscribers: z.boolean(),
125
+ };
126
+ const youtubeDraftSettings = z
127
+ .object(
128
+ Object.fromEntries(
129
+ Object.entries(youtubeSettingsFields).map(([key, schema]) => [
130
+ key,
131
+ schema.optional(),
132
+ ]),
133
+ ),
134
+ )
135
+ .strict();
136
+ const youtubePublishSettings = youtubeDraftSettings.extend({
137
+ title: youtubeSettingsFields.title,
138
+ madeForKids: youtubeSettingsFields.madeForKids,
139
+ uploadCertified: z.literal(true),
140
+ });
141
+
142
+ const tiktokSettingsFields = {
143
+ postingMethod: z.enum(["UPLOAD", "DIRECT_POST"]),
144
+ privacy: z.enum([
145
+ "PUBLIC_TO_EVERYONE",
146
+ "MUTUAL_FOLLOW_FRIENDS",
147
+ "FOLLOWER_OF_CREATOR",
148
+ "SELF_ONLY",
149
+ ]),
150
+ allowComments: z.boolean(),
151
+ allowDuets: z.boolean(),
152
+ allowStitches: z.boolean(),
153
+ aiGenerated: z.boolean(),
154
+ acceptTerms: z.boolean(),
155
+ commercialContent: z.boolean(),
156
+ promotesOwnBrand: z.boolean(),
157
+ brandedContent: z.boolean(),
158
+ autoAddMusic: z.boolean(),
159
+ title: z.string().max(90),
160
+ };
161
+ const tiktokDraftSettings = z
162
+ .object(
163
+ Object.fromEntries(
164
+ Object.entries(tiktokSettingsFields).map(([key, schema]) => [
165
+ key,
166
+ schema.optional(),
167
+ ]),
168
+ ),
169
+ )
170
+ .strict();
171
+ const tiktokPublishSettings = tiktokDraftSettings
172
+ .extend({ postingMethod: tiktokSettingsFields.postingMethod })
173
+ .superRefine((value, context) => {
174
+ if (value.postingMethod === "DIRECT_POST") {
175
+ if (!value.privacy) {
176
+ context.addIssue({
177
+ code: z.ZodIssueCode.custom,
178
+ path: ["privacy"],
179
+ message: "TikTok Direct Post requires privacy.",
180
+ });
181
+ }
182
+ if (value.acceptTerms !== true) {
183
+ context.addIssue({
184
+ code: z.ZodIssueCode.custom,
185
+ path: ["acceptTerms"],
186
+ message: "TikTok Direct Post requires acceptTerms=true.",
187
+ });
188
+ }
189
+ }
190
+ if (
191
+ value.commercialContent === true &&
192
+ value.promotesOwnBrand !== true &&
193
+ value.brandedContent !== true
194
+ ) {
195
+ context.addIssue({
196
+ code: z.ZodIssueCode.custom,
197
+ path: ["commercialContent"],
198
+ message:
199
+ "Commercial TikTok content must promote the creator's brand or identify branded content.",
200
+ });
201
+ }
202
+ if (value.privacy === "SELF_ONLY" && value.brandedContent === true) {
203
+ context.addIssue({
204
+ code: z.ZodIssueCode.custom,
205
+ path: ["brandedContent"],
206
+ message: "TikTok branded content cannot use SELF_ONLY privacy.",
207
+ });
208
+ }
209
+ });
210
+
211
+ const linkedinSettings = z.object({}).strict();
212
+
213
+ function targetSchema(platform, settings, { requiredSettings = false } = {}) {
214
+ return z
215
+ .object({
216
+ connectionId: nonEmptyString,
217
+ platform: z.literal(platform),
218
+ settings: requiredSettings ? settings : settings.optional(),
219
+ content: z.string().max(5_000).optional(),
220
+ media: z
221
+ .array(mediaItemSchema())
222
+ .max(platform === "linkedin" ? 20 : 10)
223
+ .optional(),
224
+ })
225
+ .strict();
226
+ }
227
+
228
+ export const draftPostTarget = z.discriminatedUnion("platform", [
229
+ targetSchema("instagram", instagramSettings),
230
+ targetSchema("youtube", youtubeDraftSettings),
231
+ targetSchema("tiktok", tiktokDraftSettings),
232
+ targetSchema("linkedin", linkedinSettings),
233
+ ]);
234
+
235
+ const publishPostTarget = z.discriminatedUnion("platform", [
236
+ targetSchema("instagram", instagramSettings),
237
+ targetSchema("youtube", youtubePublishSettings, { requiredSettings: true }),
238
+ targetSchema("tiktok", tiktokPublishSettings, { requiredSettings: true }),
239
+ targetSchema("linkedin", linkedinSettings),
240
+ ]);
241
+
242
+ const clearableSettings = {
243
+ instagram: [
244
+ "collaborators",
245
+ "userTags",
246
+ "locationId",
247
+ "productTags",
248
+ "brandedContentSponsorIds",
249
+ "trialReel",
250
+ "audio",
251
+ ],
252
+ youtube: ["tags", "thumbnailUrl"],
253
+ tiktok: [
254
+ "privacy",
255
+ "title",
256
+ "autoAddMusic",
257
+ "acceptTerms",
258
+ "promotesOwnBrand",
259
+ "brandedContent",
260
+ ],
261
+ linkedin: [],
262
+ };
263
+
264
+ function updateTargetSchema(platform, settings) {
265
+ const allowedClearSettings = clearableSettings[platform];
266
+ return z
267
+ .object({
268
+ connectionId: nonEmptyString,
269
+ platform: z.literal(platform),
270
+ settings: settings.optional(),
271
+ content: z.string().max(5_000).optional(),
272
+ media: z
273
+ .array(mediaItemSchema())
274
+ .max(platform === "linkedin" ? 20 : 10)
275
+ .optional(),
276
+ ...(allowedClearSettings.length
277
+ ? {
278
+ clearSettings: z
279
+ .array(z.enum(allowedClearSettings))
280
+ .max(allowedClearSettings.length)
281
+ .refine((values) => new Set(values).size === values.length, {
282
+ message: "clearSettings values must be unique.",
283
+ })
284
+ .optional(),
285
+ }
286
+ : {}),
287
+ })
288
+ .strict();
289
+ }
290
+
291
+ export const updateTarget = z.discriminatedUnion("platform", [
292
+ updateTargetSchema("instagram", instagramSettings),
293
+ updateTargetSchema("youtube", youtubeDraftSettings),
294
+ updateTargetSchema("tiktok", tiktokDraftSettings),
295
+ updateTargetSchema("linkedin", linkedinSettings),
296
+ ]);
297
+
298
+ export const postClientRequestId = z
299
+ .string()
300
+ .min(16)
301
+ .max(128)
302
+ .regex(/^[A-Za-z0-9][A-Za-z0-9._:-]*$/)
303
+ .describe(
304
+ "Stable request key for this post creation. Start with a letter or number and reuse the exact same value only when retrying the same complete request.",
305
+ );
306
+
307
+ export function hasMeaningfulPostContent(value) {
308
+ if (typeof value.content === "string" && value.content.trim()) return true;
309
+ if (value.media?.length) return true;
310
+ return value.targets.some((target) => {
311
+ if (typeof target.content === "string" && target.content.trim()) {
312
+ return true;
313
+ }
314
+ if (target.media?.length) return true;
315
+ return (
316
+ typeof target.settings?.title === "string" &&
317
+ target.settings.title.trim().length > 0
318
+ );
319
+ });
320
+ }
321
+
322
+ function inferredMediaKind(item) {
323
+ if (item.kind) return item.kind;
324
+ const source = item.localPath ?? item.url ?? "";
325
+ let pathname = source;
326
+ try {
327
+ pathname = new URL(source).pathname;
328
+ } catch {
329
+ // Local paths are not URLs; their suffix is sufficient for preflight.
330
+ }
331
+ const normalized = pathname.toLowerCase().split("?")[0];
332
+ if (normalized.endsWith(".pdf")) return "document";
333
+ if (normalized.endsWith(".mp4") || normalized.endsWith(".mov")) {
334
+ return "video";
335
+ }
336
+ if (/\.(?:jpe?g|png|webp)$/.test(normalized)) return "image";
337
+ return undefined;
338
+ }
339
+
340
+ function mediaSuffix(item) {
341
+ const source = item.localPath ?? item.url ?? "";
342
+ let pathname = source;
343
+ try {
344
+ pathname = new URL(source).pathname;
345
+ } catch {
346
+ // Local paths are not URLs; preserve the path for extension checks.
347
+ }
348
+ return pathname.toLowerCase().split("?")[0];
349
+ }
350
+
351
+ function mediaExtension(item) {
352
+ return mediaSuffix(item).match(/\.([a-z0-9]+)$/)?.[1];
353
+ }
354
+
355
+ function captionLengthForPlatform(platform, content) {
356
+ if (platform !== "linkedin") return content.length;
357
+ const mentionPattern = /@\[.+?]\(urn:li:organization.+?\)/g;
358
+ const mentions = content.match(mentionPattern) ?? [];
359
+ return content
360
+ .split(mentionPattern)
361
+ .map((segment) =>
362
+ segment.replace(/[\\<>#~_|[\]*(){}@]/g, (character) => `\\${character}`),
363
+ )
364
+ .flatMap((segment, index) =>
365
+ index < mentions.length ? [segment, mentions[index]] : [segment],
366
+ )
367
+ .join("").length;
368
+ }
369
+
370
+ function liveMediaError(platform, media, settings = {}) {
371
+ const kinds = media.map(inferredMediaKind);
372
+ if (platform === "instagram") {
373
+ if (!media.length) return "Instagram requires at least one media item.";
374
+ if (kinds.includes("document")) {
375
+ return "Instagram does not support document attachments.";
376
+ }
377
+ const contentType = settings.contentType ?? "auto";
378
+ const resolvedType =
379
+ contentType === "auto"
380
+ ? media.length > 1
381
+ ? "carousel"
382
+ : kinds[0] === "video"
383
+ ? "reel"
384
+ : "feed_image"
385
+ : contentType;
386
+ if (
387
+ resolvedType === "feed_image" &&
388
+ (media.length !== 1 || kinds[0] !== "image")
389
+ ) {
390
+ return "Instagram feed image posts require exactly one image.";
391
+ }
392
+ if (
393
+ resolvedType === "reel" &&
394
+ (media.length !== 1 || kinds[0] !== "video")
395
+ ) {
396
+ return "Instagram Reels require exactly one video.";
397
+ }
398
+ if (resolvedType === "story" && media.length !== 1) {
399
+ return "Instagram Stories require exactly one media item.";
400
+ }
401
+ if (resolvedType === "carousel" && media.length < 2) {
402
+ return "Instagram carousels require 2-10 media items.";
403
+ }
404
+ }
405
+ if (platform === "youtube") {
406
+ if (media.length !== 1 || kinds[0] !== "video") {
407
+ return "YouTube requires exactly one MP4 or MOV video.";
408
+ }
409
+ }
410
+ if (platform === "tiktok") {
411
+ if (!media.length) return "TikTok requires at least one media item.";
412
+ if (kinds.includes("document")) {
413
+ return "TikTok does not support document attachments.";
414
+ }
415
+ const videoCount = kinds.filter((kind) => kind === "video").length;
416
+ if (videoCount && (videoCount !== 1 || media.length !== 1)) {
417
+ return "TikTok supports exactly one video, or 1-10 images without mixing.";
418
+ }
419
+ if (
420
+ !videoCount &&
421
+ media.some((item) => mediaSuffix(item).endsWith(".png"))
422
+ ) {
423
+ return "TikTok photo posts require JPEG or WebP images, not PNG.";
424
+ }
425
+ }
426
+ if (platform === "linkedin") {
427
+ const documentCount = kinds.filter((kind) => kind === "document").length;
428
+ if (documentCount && (documentCount !== 1 || media.length !== 1)) {
429
+ return "LinkedIn document posts require exactly one PDF and no other media.";
430
+ }
431
+ const videoCount = kinds.filter((kind) => kind === "video").length;
432
+ if (videoCount && (videoCount !== 1 || media.length !== 1)) {
433
+ return "LinkedIn video posts require exactly one video attachment.";
434
+ }
435
+ if (
436
+ videoCount &&
437
+ media.some(
438
+ (item, index) =>
439
+ kinds[index] === "video" && mediaExtension(item) === "mov",
440
+ )
441
+ ) {
442
+ return "LinkedIn video posts require MP4 media; MOV is not supported.";
443
+ }
444
+ if (
445
+ !documentCount &&
446
+ media.some(
447
+ (item, index) =>
448
+ kinds[index] === "image" &&
449
+ mediaExtension(item) !== undefined &&
450
+ !["jpg", "jpeg", "png"].includes(mediaExtension(item)),
451
+ )
452
+ ) {
453
+ return "LinkedIn image posts require JPEG or PNG files.";
454
+ }
455
+ }
456
+ return undefined;
457
+ }
458
+
459
+ export function validatePublishTargetsBeforeUpload(value, context) {
460
+ if (value.draft === true) return;
461
+ value.targets.forEach((target, index) => {
462
+ const result = publishPostTarget.safeParse(target);
463
+ if (!result.success) {
464
+ for (const issue of result.error.issues) {
465
+ context.addIssue({
466
+ ...issue,
467
+ path: ["targets", index, ...issue.path],
468
+ });
469
+ }
470
+ return;
471
+ }
472
+ const effectiveContent = target.content ?? value.content ?? "";
473
+ const captionLimit = {
474
+ instagram: 2_200,
475
+ youtube: 5_000,
476
+ tiktok: 2_200,
477
+ linkedin: 3_000,
478
+ }[target.platform];
479
+ if (
480
+ captionLengthForPlatform(target.platform, effectiveContent) > captionLimit
481
+ ) {
482
+ context.addIssue({
483
+ code: z.ZodIssueCode.custom,
484
+ path:
485
+ target.content === undefined
486
+ ? ["content"]
487
+ : ["targets", index, "content"],
488
+ message: `${target.platform} captions are limited to ${captionLimit} characters.`,
489
+ });
490
+ }
491
+ const media = target.media ?? value.media ?? [];
492
+ const mediaError = liveMediaError(target.platform, media, target.settings);
493
+ if (mediaError) {
494
+ context.addIssue({
495
+ code: z.ZodIssueCode.custom,
496
+ path: ["targets", index, "media"],
497
+ message: mediaError,
498
+ });
499
+ }
500
+ });
501
+ }
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@sketric/favstash-mcp",
3
+ "version": "0.3.0",
4
+ "description": "FavStash MCP stdio bridge for AI agents.",
5
+ "type": "module",
6
+ "bin": {
7
+ "favstash-mcp": "./bin/favstash-mcp.js"
8
+ },
9
+ "files": [
10
+ "bin",
11
+ "README.md"
12
+ ],
13
+ "scripts": {
14
+ "test": "node --test test/*.test.js",
15
+ "typecheck": "tsc --noEmit"
16
+ },
17
+ "dependencies": {
18
+ "@modelcontextprotocol/sdk": "^1.29.0",
19
+ "zod": "3.25.76"
20
+ },
21
+ "devDependencies": {
22
+ "@types/node": "^25.3.3",
23
+ "typescript": "^5"
24
+ },
25
+ "engines": {
26
+ "node": ">=20"
27
+ },
28
+ "private": false
29
+ }