@uniquedivine/mcp-slack 0.0.0-stage → 4.4.3
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/LICENSE +21 -0
- package/README.md +146 -2
- package/docs/API.md +484 -0
- package/docs/DEPLOYMENT-MODES.md +56 -0
- package/docs/SETUP.md +211 -0
- package/docs/TROUBLESHOOTING.md +289 -0
- package/docs/assets/icon-512.png +0 -0
- package/docs/assets/icon.svg +27 -0
- package/lib/handlers.js +945 -0
- package/lib/jiyuuMcpInfo.ts +415 -0
- package/lib/lifeboat.js +171 -0
- package/lib/public-metadata.js +22 -0
- package/lib/public-pages.js +139 -0
- package/lib/rich-message-fields.js +38 -0
- package/lib/slack-client.js +376 -0
- package/lib/token-store.js +641 -0
- package/lib/tools.js +591 -0
- package/lib/workflow-store.js +203 -0
- package/package.json +138 -4
- package/public/index.html +509 -0
- package/public/share.html +130 -0
- package/scripts/apply-template.js +117 -0
- package/scripts/setup-wizard.js +564 -0
- package/scripts/token-cli.js +160 -0
- package/server.json +48 -0
- package/src/cli.js +64 -0
- package/src/server-http.js +220 -0
- package/src/server.js +366 -0
- package/src/web-server.js +471 -0
- package/templates/workflow-profiles/customer-feedback.json +10 -0
- package/templates/workflow-profiles/exec-monday.json +10 -0
- package/templates/workflow-profiles/incident-room.json +10 -0
- package/templates/workflow-profiles/oncall-handoff.json +10 -0
- package/templates/workflow-profiles/sprint-tracker.json +10 -0
- package/templates/workflow-profiles/support-triage.json +16 -0
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mcp_info and the update notice for Jiyuu's TypeScript MCP servers.
|
|
3
|
+
*
|
|
4
|
+
* Keep this file byte-identical in mcp/discord/src/jiyuuMcpInfo.ts and
|
|
5
|
+
* mcp/slack/lib/jiyuuMcpInfo.ts. Each published package must run on its own,
|
|
6
|
+
* so the module is copied rather than shared through a workspace dependency.
|
|
7
|
+
* mcp/discord/src/jiyuuMcpInfo.test.ts covers its behavior, and both packages'
|
|
8
|
+
* tests fail when the copies drift apart.
|
|
9
|
+
*
|
|
10
|
+
* The server never updates itself: the client config pins an exact version,
|
|
11
|
+
* so what runs is what was reviewed. At startup the server reads the npm
|
|
12
|
+
* registry's latest version in the background (2 s timeout, 24 h cache,
|
|
13
|
+
* silent on failure). The first successful tool result after that check
|
|
14
|
+
* carries a one-line notice when the running version is behind. Set
|
|
15
|
+
* JIYUU_MCP_NO_UPDATE_CHECK=1 to turn the check off.
|
|
16
|
+
*/
|
|
17
|
+
import { execFile } from "node:child_process";
|
|
18
|
+
import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
19
|
+
import { homedir } from "node:os";
|
|
20
|
+
import { dirname, join, resolve, sep } from "node:path";
|
|
21
|
+
|
|
22
|
+
export const OPT_OUT_ENV = "JIYUU_MCP_NO_UPDATE_CHECK";
|
|
23
|
+
export const CACHE_TTL_MS = 24 * 60 * 60 * 1000;
|
|
24
|
+
export const FETCH_TIMEOUT_MS = 2000;
|
|
25
|
+
/** Only names Jiyuu publishes are checked; upstream names are someone else's. */
|
|
26
|
+
export const PUBLISHED_SCOPE = "@uniquedivine/";
|
|
27
|
+
|
|
28
|
+
type Env = Record<string, string | undefined>;
|
|
29
|
+
|
|
30
|
+
/** Return the response body for a URL, or throw on any failure. */
|
|
31
|
+
export type Fetcher = (url: string, timeoutMs: number) => Promise<string>;
|
|
32
|
+
|
|
33
|
+
export interface PackageIdentity {
|
|
34
|
+
name: string;
|
|
35
|
+
version: string;
|
|
36
|
+
/** "installed" when the package root sits under node_modules. */
|
|
37
|
+
provenance: "source-checkout" | "installed";
|
|
38
|
+
root: string;
|
|
39
|
+
upstream: unknown;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface UpdateStatus {
|
|
43
|
+
name: string;
|
|
44
|
+
runningVersion: string;
|
|
45
|
+
latestVersion: string;
|
|
46
|
+
checkedAt: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface GitState {
|
|
50
|
+
commit: string;
|
|
51
|
+
dirty: boolean;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function readPackageIdentity(packageRoot: string): PackageIdentity {
|
|
55
|
+
const root = resolve(packageRoot);
|
|
56
|
+
const pkg = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
|
|
57
|
+
return {
|
|
58
|
+
name: String(pkg.name),
|
|
59
|
+
version: String(pkg.version),
|
|
60
|
+
provenance: root.split(sep).includes("node_modules") ? "installed" : "source-checkout",
|
|
61
|
+
root,
|
|
62
|
+
upstream: pkg.jiyuu?.upstream ?? null,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface GitOptions {
|
|
67
|
+
/** The git executable; tests pass a stand-in. */
|
|
68
|
+
command?: string;
|
|
69
|
+
timeoutMs?: number;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Commit and dirty flag for a checkout, or null. Runs git asynchronously so a
|
|
74
|
+
* slow `git status` never blocks other tool calls on the event loop.
|
|
75
|
+
*/
|
|
76
|
+
export async function gitState(root: string, options: GitOptions = {}): Promise<GitState | null> {
|
|
77
|
+
const git = (...args: string[]) =>
|
|
78
|
+
new Promise<string>((resolve, reject) => {
|
|
79
|
+
execFile(
|
|
80
|
+
options.command ?? "git",
|
|
81
|
+
["-C", root, ...args],
|
|
82
|
+
{ encoding: "utf8", timeout: options.timeoutMs ?? 2000 },
|
|
83
|
+
(err, stdout) => (err ? reject(err) : resolve(stdout.trim())),
|
|
84
|
+
);
|
|
85
|
+
});
|
|
86
|
+
try {
|
|
87
|
+
const [commit, status] = await Promise.all([
|
|
88
|
+
git("rev-parse", "HEAD"),
|
|
89
|
+
git("status", "--porcelain", "--", "."),
|
|
90
|
+
]);
|
|
91
|
+
return { commit, dirty: status !== "" };
|
|
92
|
+
} catch {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function isPublishedName(name: string): boolean {
|
|
98
|
+
return name.startsWith(PUBLISHED_SCOPE);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function checkDisabled(env: Env): boolean {
|
|
102
|
+
const value = (env[OPT_OUT_ENV] ?? "").trim().toLowerCase();
|
|
103
|
+
return !["", "0", "false", "no"].includes(value);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function pinCommand(name: string, version: string): string {
|
|
107
|
+
return `bunx ${name}@${version}`;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
type Identifier = [0, number] | [1, string];
|
|
111
|
+
interface ParsedVersion {
|
|
112
|
+
release: number[];
|
|
113
|
+
prerelease: Identifier[];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// SemVer 2.0.0 (semver.org), with an optional leading "v".
|
|
117
|
+
const VERSION_RE =
|
|
118
|
+
/^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[A-Za-z-][0-9A-Za-z-]*)(?:\.(?:0|[1-9]\d*|\d*[A-Za-z-][0-9A-Za-z-]*))*))?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/;
|
|
119
|
+
|
|
120
|
+
export function parseVersion(version: string): ParsedVersion | null {
|
|
121
|
+
const match = VERSION_RE.exec(version.trim());
|
|
122
|
+
if (!match) return null;
|
|
123
|
+
const release = [match[1], match[2], match[3]].map(Number);
|
|
124
|
+
// Prerelease identifiers split on dots only (SemVer 11.4).
|
|
125
|
+
const prerelease = match[4] === undefined
|
|
126
|
+
? []
|
|
127
|
+
: match[4].split(".").map((part): Identifier =>
|
|
128
|
+
/^\d+$/.test(part) ? [0, Number(part)] : [1, part],
|
|
129
|
+
);
|
|
130
|
+
return { release, prerelease };
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** SemVer 11.4: numeric < alphanumeric, numbers numerically, text in ASCII order. */
|
|
134
|
+
function compareIdentifiers(a: Identifier[], b: Identifier[]): number {
|
|
135
|
+
for (let i = 0; i < Math.min(a.length, b.length); i++) {
|
|
136
|
+
if (a[i][0] !== b[i][0]) return a[i][0] - b[i][0];
|
|
137
|
+
if (a[i][1] !== b[i][1]) return a[i][1] < b[i][1] ? -1 : 1;
|
|
138
|
+
}
|
|
139
|
+
// With an equal prefix, the larger set of identifiers sorts higher.
|
|
140
|
+
return a.length - b.length;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Whether `candidate` sorts after `running`; unparseable means no. */
|
|
144
|
+
export function isNewer(candidate: string, running: string): boolean {
|
|
145
|
+
const c = parseVersion(candidate);
|
|
146
|
+
const r = parseVersion(running);
|
|
147
|
+
if (!c || !r) return false;
|
|
148
|
+
for (let i = 0; i < 3; i++) {
|
|
149
|
+
const diff = c.release[i] - r.release[i];
|
|
150
|
+
if (diff !== 0) return diff > 0;
|
|
151
|
+
}
|
|
152
|
+
if (c.prerelease.length === 0 || r.prerelease.length === 0) {
|
|
153
|
+
// A final release sorts after any prerelease of the same version.
|
|
154
|
+
return c.prerelease.length === 0 && r.prerelease.length > 0;
|
|
155
|
+
}
|
|
156
|
+
return compareIdentifiers(c.prerelease, r.prerelease) > 0;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function updateAvailable(status: UpdateStatus | null): boolean {
|
|
160
|
+
return status !== null && isNewer(status.latestVersion, status.runningVersion);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function latestVersionUrl(name: string): string {
|
|
164
|
+
return `https://registry.npmjs.org/${name}/latest`;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function parseLatestVersion(body: string): string | null {
|
|
168
|
+
try {
|
|
169
|
+
const version = JSON.parse(body)?.version;
|
|
170
|
+
return typeof version === "string" && parseVersion(version) ? version : null;
|
|
171
|
+
} catch {
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function defaultCacheDir(env: Env): string {
|
|
177
|
+
return join(env.XDG_CACHE_HOME || join(homedir(), ".cache"), "jiyuu-mcp");
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function cacheFile(cacheDir: string, name: string): string {
|
|
181
|
+
return join(cacheDir, `${name.replace(/[^A-Za-z0-9._-]/g, "_")}.json`);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function readCache(
|
|
185
|
+
path: string,
|
|
186
|
+
name: string,
|
|
187
|
+
now: Date,
|
|
188
|
+
): { latestVersion: string; checkedAt: string } | null {
|
|
189
|
+
try {
|
|
190
|
+
const data = JSON.parse(readFileSync(path, "utf8"));
|
|
191
|
+
if (data?.name !== name || typeof data.latestVersion !== "string") return null;
|
|
192
|
+
const age = now.getTime() - Date.parse(data.checkedAt);
|
|
193
|
+
if (!(age >= 0 && age < CACHE_TTL_MS)) return null;
|
|
194
|
+
return { latestVersion: data.latestVersion, checkedAt: data.checkedAt };
|
|
195
|
+
} catch {
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export function writeCache(path: string, entry: Record<string, string>): void {
|
|
201
|
+
try {
|
|
202
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
203
|
+
const tmp = `${path}.${process.pid}.tmp`;
|
|
204
|
+
writeFileSync(tmp, JSON.stringify(entry) + "\n");
|
|
205
|
+
renameSync(tmp, path);
|
|
206
|
+
} catch {
|
|
207
|
+
// A read-only cache dir only costs a registry read per start.
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export const fetchText: Fetcher = async (url, timeoutMs) => {
|
|
212
|
+
const response = await fetch(url, {
|
|
213
|
+
headers: { Accept: "application/json", "User-Agent": "jiyuu-mcp-update-check" },
|
|
214
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
215
|
+
});
|
|
216
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
217
|
+
return await response.text();
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
export interface CheckOptions {
|
|
221
|
+
fetch?: Fetcher;
|
|
222
|
+
env?: Env;
|
|
223
|
+
cacheDir?: string;
|
|
224
|
+
now?: Date;
|
|
225
|
+
timeoutMs?: number;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Return the latest published version, from cache or the registry. Returns
|
|
230
|
+
* null when the check is off, the name isn't Jiyuu's, or the registry can't be
|
|
231
|
+
* read. Never throws.
|
|
232
|
+
*/
|
|
233
|
+
export async function checkForUpdate(
|
|
234
|
+
name: string,
|
|
235
|
+
runningVersion: string,
|
|
236
|
+
options: CheckOptions = {},
|
|
237
|
+
): Promise<UpdateStatus | null> {
|
|
238
|
+
const env = options.env ?? process.env;
|
|
239
|
+
if (checkDisabled(env) || !isPublishedName(name)) return null;
|
|
240
|
+
try {
|
|
241
|
+
const now = options.now ?? new Date();
|
|
242
|
+
const path = cacheFile(options.cacheDir ?? defaultCacheDir(env), name);
|
|
243
|
+
const cached = readCache(path, name, now);
|
|
244
|
+
if (cached) return { name, runningVersion, ...cached };
|
|
245
|
+
const body = await (options.fetch ?? fetchText)(
|
|
246
|
+
latestVersionUrl(name),
|
|
247
|
+
options.timeoutMs ?? FETCH_TIMEOUT_MS,
|
|
248
|
+
);
|
|
249
|
+
const latestVersion = parseLatestVersion(body);
|
|
250
|
+
if (!latestVersion) return null;
|
|
251
|
+
const checkedAt = now.toISOString().replace(/\.\d{3}Z$/, "Z");
|
|
252
|
+
writeCache(path, { name, latestVersion, checkedAt });
|
|
253
|
+
return { name, runningVersion, latestVersion, checkedAt };
|
|
254
|
+
} catch {
|
|
255
|
+
return null;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export function formatNotice(status: UpdateStatus): string {
|
|
260
|
+
return (
|
|
261
|
+
`Update available: ${status.name} ${status.latestVersion} ` +
|
|
262
|
+
`(running ${status.runningVersion}). Change the pin to ` +
|
|
263
|
+
`\`${pinCommand(status.name, status.latestVersion)}\` and reload the MCP server.`
|
|
264
|
+
);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** One background check per process; its notice is handed out once. */
|
|
268
|
+
export class UpdateNotifier {
|
|
269
|
+
private pending: Promise<void> | null = null;
|
|
270
|
+
private done = false;
|
|
271
|
+
private result: UpdateStatus | null = null;
|
|
272
|
+
private noticeSent = false;
|
|
273
|
+
|
|
274
|
+
constructor(private readonly check: () => Promise<UpdateStatus | null>) {}
|
|
275
|
+
|
|
276
|
+
start(): void {
|
|
277
|
+
if (this.pending) return;
|
|
278
|
+
this.pending = this.check()
|
|
279
|
+
.catch(() => null)
|
|
280
|
+
.then((status) => {
|
|
281
|
+
this.result = status;
|
|
282
|
+
this.done = true;
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** The result so far, waiting up to `waitMs` for an in-flight check. */
|
|
287
|
+
async status(waitMs = 0): Promise<UpdateStatus | null> {
|
|
288
|
+
if (waitMs > 0 && this.pending && !this.done) {
|
|
289
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
290
|
+
await Promise.race([
|
|
291
|
+
this.pending,
|
|
292
|
+
new Promise((resolve) => {
|
|
293
|
+
timer = setTimeout(resolve, waitMs);
|
|
294
|
+
}),
|
|
295
|
+
]);
|
|
296
|
+
clearTimeout(timer);
|
|
297
|
+
}
|
|
298
|
+
return this.done ? this.result : null;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
takeNotice(): string | null {
|
|
302
|
+
if (!this.done || this.noticeSent || !updateAvailable(this.result)) return null;
|
|
303
|
+
this.noticeSent = true;
|
|
304
|
+
return formatNotice(this.result as UpdateStatus);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
interface ToolResult {
|
|
309
|
+
content?: unknown[];
|
|
310
|
+
isError?: boolean;
|
|
311
|
+
[key: string]: unknown;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** Append the one-time notice to a successful tool result. */
|
|
315
|
+
export function withUpdateNotice<T extends ToolResult>(result: T, notifier: UpdateNotifier): T {
|
|
316
|
+
if (!result || result.isError) return result;
|
|
317
|
+
const notice = notifier.takeNotice();
|
|
318
|
+
if (!notice) return result;
|
|
319
|
+
return { ...result, content: [...(result.content ?? []), { type: "text", text: notice }] };
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export const MCP_INFO_TOOL = {
|
|
323
|
+
name: "mcp_info",
|
|
324
|
+
description:
|
|
325
|
+
"Report this server's version, install provenance and update status. Call " +
|
|
326
|
+
"this to learn where the server runs from, which version it is, and whether " +
|
|
327
|
+
"a newer release exists. Never read MCP client config files for this: they " +
|
|
328
|
+
"hold secrets. The report holds names and paths only.",
|
|
329
|
+
inputSchema: { type: "object" as const, properties: {}, required: [] as string[] },
|
|
330
|
+
annotations: { title: "MCP Info", readOnlyHint: true },
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
export const MCP_INFO_INSTRUCTIONS =
|
|
334
|
+
"To learn this server's version, where it runs from, or whether an update " +
|
|
335
|
+
"exists, call mcp_info. Never read MCP client config files for this; they " +
|
|
336
|
+
"hold secrets.";
|
|
337
|
+
|
|
338
|
+
export interface McpInfoInput {
|
|
339
|
+
server: string;
|
|
340
|
+
identity: PackageIdentity;
|
|
341
|
+
git: GitState | null;
|
|
342
|
+
status: UpdateStatus | null;
|
|
343
|
+
env: Env;
|
|
344
|
+
/** Env var prefixes whose set names (never values) the report lists. */
|
|
345
|
+
envPrefixes: string[];
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
export function buildMcpInfo(input: McpInfoInput): Record<string, unknown> {
|
|
349
|
+
const { identity, status, env } = input;
|
|
350
|
+
let notChecked: string | null = null;
|
|
351
|
+
if (checkDisabled(env)) notChecked = `disabled by ${OPT_OUT_ENV}`;
|
|
352
|
+
else if (!isPublishedName(identity.name))
|
|
353
|
+
notChecked = `'${identity.name}' is not a name Jiyuu publishes`;
|
|
354
|
+
else if (!status) notChecked = "no registry result yet (offline, pending, or failed)";
|
|
355
|
+
return {
|
|
356
|
+
server: input.server,
|
|
357
|
+
name: identity.name,
|
|
358
|
+
version: identity.version,
|
|
359
|
+
provenance: {
|
|
360
|
+
kind: identity.provenance,
|
|
361
|
+
packageRoot: identity.root,
|
|
362
|
+
git: input.git,
|
|
363
|
+
},
|
|
364
|
+
upstream: identity.upstream,
|
|
365
|
+
update: {
|
|
366
|
+
latestVersion: status?.latestVersion ?? null,
|
|
367
|
+
updateAvailable: updateAvailable(status),
|
|
368
|
+
checkedAt: status?.checkedAt ?? null,
|
|
369
|
+
pinCommand: status ? pinCommand(status.name, status.latestVersion) : null,
|
|
370
|
+
notChecked,
|
|
371
|
+
},
|
|
372
|
+
runtime: {
|
|
373
|
+
bun: process.versions.bun ?? null,
|
|
374
|
+
node: process.versions.node,
|
|
375
|
+
execPath: process.execPath,
|
|
376
|
+
pid: process.pid,
|
|
377
|
+
},
|
|
378
|
+
envVarsSet: Object.keys(env)
|
|
379
|
+
.filter((key) => env[key] && input.envPrefixes.some((prefix) => key.startsWith(prefix)))
|
|
380
|
+
.sort(),
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** Wire-up for one server process: identity, notifier and the tool body. */
|
|
385
|
+
export class JiyuuMcpInfo {
|
|
386
|
+
readonly identity: PackageIdentity;
|
|
387
|
+
readonly notifier: UpdateNotifier;
|
|
388
|
+
|
|
389
|
+
constructor(
|
|
390
|
+
private readonly server: string,
|
|
391
|
+
packageRoot: string,
|
|
392
|
+
private readonly envPrefixes: string[],
|
|
393
|
+
check?: () => Promise<UpdateStatus | null>,
|
|
394
|
+
) {
|
|
395
|
+
this.identity = readPackageIdentity(packageRoot);
|
|
396
|
+
const { name, version } = this.identity;
|
|
397
|
+
this.notifier = new UpdateNotifier(check ?? (() => checkForUpdate(name, version)));
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
async callTool(env: Env = process.env) {
|
|
401
|
+
this.notifier.start();
|
|
402
|
+
const status = await this.notifier.status(FETCH_TIMEOUT_MS + 500);
|
|
403
|
+
const git =
|
|
404
|
+
this.identity.provenance === "source-checkout" ? await gitState(this.identity.root) : null;
|
|
405
|
+
const report = buildMcpInfo({
|
|
406
|
+
server: this.server,
|
|
407
|
+
identity: this.identity,
|
|
408
|
+
git,
|
|
409
|
+
status,
|
|
410
|
+
env,
|
|
411
|
+
envPrefixes: this.envPrefixes,
|
|
412
|
+
});
|
|
413
|
+
return { content: [{ type: "text" as const, text: JSON.stringify(report, null, 2) }] };
|
|
414
|
+
}
|
|
415
|
+
}
|
package/lib/lifeboat.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OAuth Lifeboat
|
|
3
|
+
*
|
|
4
|
+
* Detects Slack session-token death at the moment of pain and returns a
|
|
5
|
+
* genuinely helpful recovery message instead of a raw Slack API error.
|
|
6
|
+
*
|
|
7
|
+
* Session tokens (xoxc/xoxd) are extracted from the browser and Slack rotates
|
|
8
|
+
* them roughly every 1-2 weeks. When they die, every tool call fails with an
|
|
9
|
+
* auth error. This module classifies that failure and hands the user the
|
|
10
|
+
* self-fix first (re-extract) and the permanent fix second (hosted OAuth).
|
|
11
|
+
*
|
|
12
|
+
* Design rules:
|
|
13
|
+
* - Self-fix is always shown; the hosted option is shown second and is
|
|
14
|
+
* suppressed entirely by SLACK_MCP_NO_UPSELL=1.
|
|
15
|
+
* - The long-form message is emitted at most once per process per hour;
|
|
16
|
+
* subsequent failures inside that window get a one-line version so agents
|
|
17
|
+
* in retry loops do not spam.
|
|
18
|
+
* - Never reads or logs token values — it only inspects error codes.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
// Slack API error codes (and the HTTP 401 case) that mean the session token
|
|
22
|
+
// is dead and cannot be auto-healed — the point where the Lifeboat fires.
|
|
23
|
+
export const AUTH_DEATH_SLACK_CODES = new Set([
|
|
24
|
+
"invalid_auth",
|
|
25
|
+
"not_authed",
|
|
26
|
+
"token_expired",
|
|
27
|
+
"token_revoked",
|
|
28
|
+
"account_inactive",
|
|
29
|
+
]);
|
|
30
|
+
|
|
31
|
+
const THROTTLE_WINDOW_MS = 60 * 60 * 1000; // one long-form message per process per hour
|
|
32
|
+
|
|
33
|
+
const SETUP_CMD = "bunx @uniquedivine/mcp-slack --setup";
|
|
34
|
+
const README_ANCHOR_URL =
|
|
35
|
+
"https://github.com/Unique-Divine/jiyuu/tree/main/mcp/slack#authenticate";
|
|
36
|
+
const HOSTED_SETUP_URL =
|
|
37
|
+
"https://mcp.revasserlabs.com/setup?utm_source=lifeboat&utm_medium=npm&utm_campaign=token_death";
|
|
38
|
+
|
|
39
|
+
// Module-level throttle state (shared across every transport in one process).
|
|
40
|
+
let lastLongFormAt = 0;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Reset the throttle. Intended for tests.
|
|
44
|
+
*/
|
|
45
|
+
export function resetLifeboatThrottle() {
|
|
46
|
+
lastLongFormAt = 0;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function upsellEnabled() {
|
|
50
|
+
return process.env.SLACK_MCP_NO_UPSELL !== "1";
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Pull the specific Slack auth-death code out of an error, if present.
|
|
55
|
+
* Returns the code string (e.g. "token_revoked") or null.
|
|
56
|
+
*/
|
|
57
|
+
export function authDeathCode(err) {
|
|
58
|
+
if (!err) return null;
|
|
59
|
+
|
|
60
|
+
if (typeof err === "string") {
|
|
61
|
+
const s = err.trim().toLowerCase();
|
|
62
|
+
return AUTH_DEATH_SLACK_CODES.has(s) ? s : null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const slackCode =
|
|
66
|
+
typeof err?.slack_error === "string" ? err.slack_error.trim().toLowerCase() : null;
|
|
67
|
+
if (slackCode && AUTH_DEATH_SLACK_CODES.has(slackCode)) return slackCode;
|
|
68
|
+
|
|
69
|
+
const msg = typeof err?.message === "string" ? err.message.trim().toLowerCase() : "";
|
|
70
|
+
if (AUTH_DEATH_SLACK_CODES.has(msg)) return msg;
|
|
71
|
+
for (const code of AUTH_DEATH_SLACK_CODES) {
|
|
72
|
+
if (msg.includes(code)) return code;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Classify an error as AUTH-DEATH.
|
|
80
|
+
* Accepts a thrown Error (optionally carrying `.slack_error`, `.code`,
|
|
81
|
+
* `.status`), a raw Slack error-code string, or an HTTP-401 signal.
|
|
82
|
+
*/
|
|
83
|
+
export function isAuthDeath(err) {
|
|
84
|
+
if (!err) return false;
|
|
85
|
+
|
|
86
|
+
// The wrapped auth-failure lib/slack-client.js throws after a failed auto-heal.
|
|
87
|
+
if (typeof err === "object" && err.code === "token_auth_failed") return true;
|
|
88
|
+
|
|
89
|
+
if (authDeathCode(err)) return true;
|
|
90
|
+
|
|
91
|
+
// HTTP 401 (defensive — Slack normally returns HTTP 200 with ok:false).
|
|
92
|
+
if (typeof err === "string") {
|
|
93
|
+
const s = err.trim().toLowerCase();
|
|
94
|
+
return s === "401" || s.includes("http 401") || s.includes("status 401");
|
|
95
|
+
}
|
|
96
|
+
const status = typeof err === "number" ? err : (err?.status ?? err?.statusCode ?? err?.httpStatus ?? null);
|
|
97
|
+
if (status === 401) return true;
|
|
98
|
+
const msg = typeof err?.message === "string" ? err.message.toLowerCase() : "";
|
|
99
|
+
return msg.includes("http 401") || msg.includes("status 401");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Build the structured recovery payload for an auth-death error.
|
|
104
|
+
* First call within the throttle window returns the long form; later calls
|
|
105
|
+
* return the one-line form. Honors SLACK_MCP_NO_UPSELL.
|
|
106
|
+
*/
|
|
107
|
+
export function buildLifeboatPayload(err, options = {}) {
|
|
108
|
+
const now = options?.now ?? Date.now();
|
|
109
|
+
const slackError = authDeathCode(err);
|
|
110
|
+
const upsell = upsellEnabled();
|
|
111
|
+
|
|
112
|
+
const throttled = lastLongFormAt > 0 && now - lastLongFormAt < THROTTLE_WINDOW_MS;
|
|
113
|
+
|
|
114
|
+
if (throttled) {
|
|
115
|
+
const message = upsell
|
|
116
|
+
? `Slack session token is still expired or revoked. Re-extract with \`${SETUP_CMD}\`. Permanent fix (hosted OAuth, free tier): ${HOSTED_SETUP_URL}`
|
|
117
|
+
: `Slack session token is still expired or revoked. Re-extract with \`${SETUP_CMD}\`.`;
|
|
118
|
+
return {
|
|
119
|
+
status: "error",
|
|
120
|
+
code: "slack_auth_expired",
|
|
121
|
+
slack_error: slackError,
|
|
122
|
+
throttled: true,
|
|
123
|
+
message,
|
|
124
|
+
next_action: `Run \`${SETUP_CMD}\`.`,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
lastLongFormAt = now;
|
|
129
|
+
|
|
130
|
+
const payload = {
|
|
131
|
+
status: "error",
|
|
132
|
+
code: "slack_auth_expired",
|
|
133
|
+
slack_error: slackError,
|
|
134
|
+
throttled: false,
|
|
135
|
+
message:
|
|
136
|
+
"Your Slack session token has expired or been revoked. Slack rotates browser session tokens (xoxc/xoxd) roughly every 1-2 weeks, so this is expected — the server can no longer authenticate until you supply fresh tokens.",
|
|
137
|
+
self_fix:
|
|
138
|
+
`Re-extract fresh tokens: run \`${SETUP_CMD}\`. On macOS with Slack open in Chrome you can instead call the slack_refresh_tokens tool. Full steps: ${README_ANCHOR_URL}`,
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
// Preserve the auto-heal diagnostic when the wrapped error carried one.
|
|
142
|
+
if (err && typeof err === "object" && err.extraction_error) {
|
|
143
|
+
payload.extraction_error = err.extraction_error;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (upsell) {
|
|
147
|
+
payload.hosted_option =
|
|
148
|
+
`To stop re-extracting every 1-2 weeks, switch to hosted OAuth (it never rotates): ${HOSTED_SETUP_URL} — free tier available, no credit card.`;
|
|
149
|
+
payload.next_action = `Run \`${SETUP_CMD}\` to re-extract tokens, or switch to permanent hosted OAuth (see hosted_option).`;
|
|
150
|
+
} else {
|
|
151
|
+
payload.next_action = `Run \`${SETUP_CMD}\` to re-extract tokens (macOS: call the slack_refresh_tokens tool).`;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return payload;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Wrap the recovery payload as an MCP tool-error response.
|
|
159
|
+
* Used by both transports (stdio + HTTP) so the recovery surface is identical.
|
|
160
|
+
*/
|
|
161
|
+
export function lifeboatResponse(err) {
|
|
162
|
+
return {
|
|
163
|
+
content: [
|
|
164
|
+
{
|
|
165
|
+
type: "text",
|
|
166
|
+
text: JSON.stringify(buildLifeboatPayload(err), null, 2),
|
|
167
|
+
},
|
|
168
|
+
],
|
|
169
|
+
isError: true,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
|
|
3
|
+
const packageJson = JSON.parse(
|
|
4
|
+
readFileSync(new URL("../package.json", import.meta.url), "utf8")
|
|
5
|
+
);
|
|
6
|
+
|
|
7
|
+
export const RELEASE_VERSION = packageJson.version;
|
|
8
|
+
|
|
9
|
+
export const PUBLIC_METADATA = Object.freeze({
|
|
10
|
+
projectName: "slack-mcp-server",
|
|
11
|
+
packageName: packageJson.name,
|
|
12
|
+
canonicalShortDescription: packageJson.description,
|
|
13
|
+
canonicalRepoUrl: "https://github.com/jtalk22/slack-mcp-server",
|
|
14
|
+
canonicalSiteUrl: "https://mcp.revasserlabs.com",
|
|
15
|
+
cloudPricingUrl: "https://mcp.revasserlabs.com/pricing",
|
|
16
|
+
cloudDocsUrl: "https://mcp.revasserlabs.com/docs",
|
|
17
|
+
cloudSecurityUrl: "https://mcp.revasserlabs.com/security",
|
|
18
|
+
cloudSupportUrl: "https://mcp.revasserlabs.com/support",
|
|
19
|
+
cloudStatusUrl: "https://mcp.revasserlabs.com/status",
|
|
20
|
+
supportEmail: "support@revasserlabs.com",
|
|
21
|
+
selfHostedToolCount: 21,
|
|
22
|
+
});
|