@notifkit/mcp 0.1.1 → 0.1.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/README.md +11 -11
- package/dist/client.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/tools.d.ts.map +1 -1
- package/dist/tools.js +71 -13
- package/dist/tools.js.map +1 -1
- package/package.json +2 -1
- package/src/client.ts +109 -0
- package/src/index.ts +84 -0
- package/src/tools.ts +1511 -0
package/src/client.ts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A minimal HTTP client for the notifkit API.
|
|
3
|
+
*
|
|
4
|
+
* Deliberately self-contained rather than importing `NotifkitClient` from the
|
|
5
|
+
* root package: that entrypoint re-exports the db, redis, and config modules,
|
|
6
|
+
* which would pull Postgres and Redis drivers into a process whose only job is
|
|
7
|
+
* to speak JSON-RPC over stdio.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export interface NotifkitApiOptions {
|
|
11
|
+
baseUrl: string;
|
|
12
|
+
apiKey: string;
|
|
13
|
+
/** Required when `apiKey` is the admin key; project-scoped keys carry their own project. */
|
|
14
|
+
projectId?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export class NotifkitApiError extends Error {
|
|
18
|
+
constructor(
|
|
19
|
+
message: string,
|
|
20
|
+
readonly status: number,
|
|
21
|
+
readonly body: unknown,
|
|
22
|
+
) {
|
|
23
|
+
super(message);
|
|
24
|
+
this.name = "NotifkitApiError";
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export class NotifkitApi {
|
|
29
|
+
private readonly baseUrl: string;
|
|
30
|
+
private readonly headers: Record<string, string>;
|
|
31
|
+
|
|
32
|
+
constructor(options: NotifkitApiOptions) {
|
|
33
|
+
this.baseUrl = options.baseUrl.replace(/\/$/, "");
|
|
34
|
+
this.headers = {
|
|
35
|
+
"Content-Type": "application/json",
|
|
36
|
+
Authorization: `Bearer ${options.apiKey}`,
|
|
37
|
+
...(options.projectId ? { "x-project-id": options.projectId } : {}),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async request<T>(
|
|
42
|
+
method: string,
|
|
43
|
+
path: string,
|
|
44
|
+
options?: { body?: unknown; query?: Record<string, string | number | undefined> },
|
|
45
|
+
): Promise<T> {
|
|
46
|
+
let url = `${this.baseUrl}${path}`;
|
|
47
|
+
|
|
48
|
+
if (options?.query) {
|
|
49
|
+
const params = new URLSearchParams();
|
|
50
|
+
for (const [key, value] of Object.entries(options.query)) {
|
|
51
|
+
if (value !== undefined) params.set(key, String(value));
|
|
52
|
+
}
|
|
53
|
+
const qs = params.toString();
|
|
54
|
+
if (qs) url += `?${qs}`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const res = await fetch(url, {
|
|
58
|
+
method,
|
|
59
|
+
headers: this.headers,
|
|
60
|
+
body: options?.body === undefined ? undefined : JSON.stringify(options.body),
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
if (res.status === 204) return undefined as T;
|
|
64
|
+
|
|
65
|
+
const text = await res.text();
|
|
66
|
+
let data: unknown = undefined;
|
|
67
|
+
if (text) {
|
|
68
|
+
try {
|
|
69
|
+
data = JSON.parse(text);
|
|
70
|
+
} catch {
|
|
71
|
+
data = text;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!res.ok) {
|
|
76
|
+
const record =
|
|
77
|
+
typeof data === "object" && data !== null ? (data as Record<string, unknown>) : {};
|
|
78
|
+
const message =
|
|
79
|
+
typeof record["message"] === "string"
|
|
80
|
+
? record["message"]
|
|
81
|
+
: typeof record["error"] === "string"
|
|
82
|
+
? record["error"]
|
|
83
|
+
: `Request failed with status ${res.status}`;
|
|
84
|
+
throw new NotifkitApiError(message, res.status, data);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return data as T;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
get<T>(path: string, query?: Record<string, string | number | undefined>): Promise<T> {
|
|
91
|
+
return this.request<T>("GET", path, { query });
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
post<T>(path: string, body?: unknown): Promise<T> {
|
|
95
|
+
return this.request<T>("POST", path, { body });
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
put<T>(path: string, body?: unknown): Promise<T> {
|
|
99
|
+
return this.request<T>("PUT", path, { body });
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
patch<T>(path: string, body?: unknown): Promise<T> {
|
|
103
|
+
return this.request<T>("PATCH", path, { body });
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
delete<T>(path: string): Promise<T> {
|
|
107
|
+
return this.request<T>("DELETE", path);
|
|
108
|
+
}
|
|
109
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* notifkit MCP server (stdio).
|
|
4
|
+
*
|
|
5
|
+
* Exposes the notifkit HTTP API as MCP tools so a terminal agent can send
|
|
6
|
+
* notifications, manage templates and users, and drive workflows.
|
|
7
|
+
*
|
|
8
|
+
* stdout is the JSON-RPC transport — never write to it. Diagnostics go to stderr.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
12
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
13
|
+
import { NotifkitApi } from "./client.js";
|
|
14
|
+
import { registerTools } from "./tools.js";
|
|
15
|
+
|
|
16
|
+
import { fileURLToPath } from "node:url";
|
|
17
|
+
|
|
18
|
+
const DEFAULT_URL = "http://localhost:3000";
|
|
19
|
+
|
|
20
|
+
export function createServer(options: {
|
|
21
|
+
baseUrl: string;
|
|
22
|
+
apiKey: string;
|
|
23
|
+
projectId?: string;
|
|
24
|
+
}): McpServer {
|
|
25
|
+
const server = new McpServer(
|
|
26
|
+
{ name: "notifkit", version: "0.1.1" },
|
|
27
|
+
{
|
|
28
|
+
instructions:
|
|
29
|
+
"Tools for notifkit, a self-hosted notification service.\n\n" +
|
|
30
|
+
"Sending: send_campaign takes a list of email addresses directly and is the right tool when the " +
|
|
31
|
+
"user supplies recipients themselves. send_notification targets existing users, a segment, or a " +
|
|
32
|
+
"topic. Both accept a `campaign` label — set one on any send whose results might be asked about " +
|
|
33
|
+
"later, and tell the user the label, because without it the send cannot be reported on " +
|
|
34
|
+
"afterwards. Multi-step sequences (drips, onboarding, reminders) are workflows: create_workflow " +
|
|
35
|
+
"defines one, trigger_workflow runs it per user.\n\n" +
|
|
36
|
+
"Reporting: get_campaign_stats answers 'how did that send do?'. Pass on its `warnings` verbatim — " +
|
|
37
|
+
"an untracked open rate is not a zero open rate.\n\n" +
|
|
38
|
+
"Discover ids with list_campaigns, list_segments, list_templates, and list_workflows rather than " +
|
|
39
|
+
"guessing them.\n\n" +
|
|
40
|
+
"Every send reaches real people and cannot be recalled, so confirm the audience before " +
|
|
41
|
+
"broadcasting. Never remove a suppression to improve a delivery figure: suppressions record that " +
|
|
42
|
+
"somebody asked not to be contacted.",
|
|
43
|
+
},
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
registerTools(server, new NotifkitApi(options));
|
|
47
|
+
return server;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function runServer(): Promise<void> {
|
|
51
|
+
const baseUrl = process.env["NOTIFKIT_URL"] ?? DEFAULT_URL;
|
|
52
|
+
const apiKey = process.env["NOTIFKIT_API_KEY"];
|
|
53
|
+
const projectId = process.env["NOTIFKIT_PROJECT_ID"];
|
|
54
|
+
|
|
55
|
+
if (!apiKey) {
|
|
56
|
+
console.error(
|
|
57
|
+
"notifkit-mcp: NOTIFKIT_API_KEY is not set.\n" +
|
|
58
|
+
" Set it to a project API key, or to your ADMIN_API_KEY together with NOTIFKIT_PROJECT_ID.\n" +
|
|
59
|
+
` NOTIFKIT_URL defaults to ${DEFAULT_URL}.`,
|
|
60
|
+
);
|
|
61
|
+
process.exit(1);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const server = createServer({ baseUrl, apiKey, projectId });
|
|
65
|
+
await server.connect(new StdioServerTransport());
|
|
66
|
+
|
|
67
|
+
console.error(`notifkit-mcp: connected, talking to ${baseUrl}`);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const entryFile = process.argv[1];
|
|
71
|
+
const isDirectRun =
|
|
72
|
+
Boolean(entryFile) &&
|
|
73
|
+
!process.env["VITEST"] &&
|
|
74
|
+
(fileURLToPath(import.meta.url) === entryFile ||
|
|
75
|
+
entryFile?.endsWith("notifkit-mcp") ||
|
|
76
|
+
entryFile?.endsWith("dist/index.js") ||
|
|
77
|
+
entryFile?.endsWith("src/index.ts"));
|
|
78
|
+
|
|
79
|
+
if (isDirectRun) {
|
|
80
|
+
runServer().catch((error: unknown) => {
|
|
81
|
+
console.error("notifkit-mcp: fatal:", error instanceof Error ? error.message : error);
|
|
82
|
+
process.exit(1);
|
|
83
|
+
});
|
|
84
|
+
}
|