@sketric/favstash-mcp 0.3.0 → 0.4.1

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/bin/config.js ADDED
@@ -0,0 +1,39 @@
1
+ import { homedir } from "node:os";
2
+ import path from "node:path";
3
+ import { createHash } from "node:crypto";
4
+ import { readFileSync } from "node:fs";
5
+
6
+ export const PACKAGE = JSON.parse(
7
+ readFileSync(new URL("../package.json", import.meta.url), "utf8"),
8
+ );
9
+ export const DEFAULT_MCP_URL = "https://mcp.favstash.app/mcp";
10
+ export function endpoint(
11
+ value = process.env.FAVSTASH_MCP_URL || DEFAULT_MCP_URL,
12
+ ) {
13
+ const url = new URL(value);
14
+ const loopback = ["127.0.0.1", "[::1]", "localhost"].includes(url.hostname);
15
+ if (
16
+ (url.protocol !== "https:" && !(loopback && url.protocol === "http:")) ||
17
+ url.username ||
18
+ url.password ||
19
+ url.search ||
20
+ url.hash
21
+ ) {
22
+ throw new Error(
23
+ "FAVSTASH_MCP_URL must be HTTPS (or HTTP loopback for local development), without credentials, query, or fragment.",
24
+ );
25
+ }
26
+ return url.href.replace(/\/$/, "");
27
+ }
28
+ export function configDirectory() {
29
+ return (
30
+ process.env.FAVSTASH_CONFIG_DIR ||
31
+ path.join(
32
+ process.env.XDG_CONFIG_HOME || path.join(homedir(), ".config"),
33
+ "favstash",
34
+ )
35
+ );
36
+ }
37
+ export function endpointId(url) {
38
+ return createHash("sha256").update(endpoint(url)).digest("hex");
39
+ }
@@ -0,0 +1,142 @@
1
+ import { readFileSync } from "node:fs";
2
+ import Ajv from "ajv";
3
+ import addFormats from "ajv-formats";
4
+
5
+ export const contracts = JSON.parse(
6
+ readFileSync(new URL("./contracts.json", import.meta.url), "utf8"),
7
+ );
8
+ const ajv = addFormats(
9
+ new Ajv({
10
+ allErrors: true,
11
+ allowUnionTypes: true,
12
+ discriminator: true,
13
+ strict: false,
14
+ }),
15
+ );
16
+ const validators = new WeakMap();
17
+ const localPath = {
18
+ type: "string",
19
+ minLength: 1,
20
+ description:
21
+ "Absolute local file path. The bridge validates and uploads this file.",
22
+ };
23
+
24
+ // Only the media extension is local. Provider settings, analytics, and every
25
+ // other tool field come from the hosted contract, including validation branches.
26
+ export function withLocalMedia(schema) {
27
+ if (Array.isArray(schema)) return schema.map(withLocalMedia);
28
+ if (!schema || typeof schema !== "object") return schema;
29
+ const result = Object.fromEntries(
30
+ Object.entries(schema).map(([key, value]) => [key, withLocalMedia(value)]),
31
+ );
32
+ if (
33
+ result.properties?.url &&
34
+ result.properties?.kind &&
35
+ result.properties?.thumbnailUrl
36
+ ) {
37
+ result.properties.localPath = localPath;
38
+ result.properties.thumbnailPath = localPath;
39
+ result.required = (result.required || []).filter((name) => name !== "url");
40
+ result.oneOf = [
41
+ { required: ["url"], not: { required: ["localPath"] } },
42
+ { required: ["localPath"], not: { required: ["url"] } },
43
+ ];
44
+ result.allOf = [
45
+ ...(result.allOf || []),
46
+ { not: { required: ["thumbnailPath", "thumbnailUrl"] } },
47
+ ];
48
+ }
49
+ return result;
50
+ }
51
+
52
+ function importSchema(schema) {
53
+ const result = structuredClone(schema);
54
+ result.required = [];
55
+ result.properties.path = localPath;
56
+ result.properties.role = { type: "string", enum: ["media", "thumbnail"] };
57
+ result.properties.files = {
58
+ type: "array",
59
+ minItems: 1,
60
+ maxItems: 10,
61
+ items: {
62
+ type: "object",
63
+ properties: { path: localPath, role: result.properties.role },
64
+ required: ["path"],
65
+ additionalProperties: false,
66
+ },
67
+ };
68
+ result.oneOf = ["url", "path", "files"].map((key) => ({
69
+ required: [key],
70
+ not: {
71
+ anyOf: ["url", "path", "files"]
72
+ .filter((other) => other !== key)
73
+ .map((other) => ({ required: [other] })),
74
+ },
75
+ }));
76
+ result.allOf = [
77
+ { if: { required: ["role"] }, then: { required: ["path"] } },
78
+ { not: { required: ["title", "files"] } },
79
+ ];
80
+ return result;
81
+ }
82
+
83
+ const extend = (name, schema) =>
84
+ name === "import_post_media" ? importSchema(schema) : withLocalMedia(schema);
85
+ export const localTools = contracts.tools.map((tool) => {
86
+ const result = { ...tool, inputSchema: extend(tool.name, tool.inputSchema) };
87
+ if (["create_social_post", "update_social_post"].includes(tool.name)) {
88
+ result.description =
89
+ tool.description +
90
+ " Local bridge: media[].localPath and thumbnailPath upload local files inside this call; no external hosting is needed.";
91
+ }
92
+ if (tool.name === "import_post_media") {
93
+ result.description +=
94
+ " Local bridge also accepts path plus optional role, or files:[{path,role}] for a batch. Prefer media[].localPath inside post creation or update.";
95
+ // Local uploads return the legacy bridge's {url,type,...} / {imports,count}
96
+ // shape, which deliberately differs from the hosted importer output.
97
+ delete result.outputSchema;
98
+ }
99
+ return result;
100
+ });
101
+ export const localValidation = new Map(
102
+ contracts.validation.map(({ name, inputSchema }) => [
103
+ name,
104
+ extend(name, inputSchema),
105
+ ]),
106
+ );
107
+
108
+ export function validate(schema, value) {
109
+ let check = validators.get(schema);
110
+ if (!check) {
111
+ check = ajv.compile(schema);
112
+ validators.set(schema, check);
113
+ }
114
+ if (!check(value)) {
115
+ const issues = check.errors.map((error) => ({
116
+ path: error.instancePath.split("/").filter(Boolean),
117
+ message: `${error.instancePath || "arguments"} ${error.message}`,
118
+ code: "custom",
119
+ }));
120
+ const error = new Error(
121
+ issues
122
+ .slice(0, 5)
123
+ .map((issue) => issue.message)
124
+ .join("; "),
125
+ );
126
+ error.issues = issues;
127
+ throw error;
128
+ }
129
+ return value;
130
+ }
131
+ export function schemaAdapter(schema) {
132
+ return {
133
+ parse: (value) => validate(schema, value),
134
+ safeParse: (value) => {
135
+ try {
136
+ return { success: true, data: validate(schema, value) };
137
+ } catch (error) {
138
+ return { success: false, error };
139
+ }
140
+ },
141
+ };
142
+ }