forked from Lexus2016/claude-code-studio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp-notify.js
More file actions
216 lines (186 loc) · 7.47 KB
/
mcp-notify.js
File metadata and controls
216 lines (186 loc) · 7.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env node
// ─── Internal MCP Server: notify_user ──────────────────────────────────────
// Raw JSON-RPC 2.0 over stdio (newline-delimited). Zero external dependencies.
// Provides a "notify_user" tool that sends non-blocking notifications to the UI.
// Unlike ask_user, this does NOT pause Claude — fire-and-forget.
//
// Environment variables (set by server.js at injection time):
// NOTIFY_SERVER_URL — e.g. http://127.0.0.1:3000
// NOTIFY_SESSION_ID — local session ID for routing to correct client
// NOTIFY_SECRET — per-process auth secret
const http = require('http');
const { StringDecoder } = require('string_decoder');
const SERVER_URL = process.env.NOTIFY_SERVER_URL || 'http://127.0.0.1:3000';
const SESSION_ID = process.env.NOTIFY_SESSION_ID || '';
const SECRET = process.env.NOTIFY_SECRET || '';
const MAX_STDIN_BUFFER = 10 * 1024 * 1024; // 10 MB
// ─── JSON-RPC helpers ────────────────────────────────────────────────────────
function sendResponse(id, result) {
const msg = JSON.stringify({ jsonrpc: '2.0', id, result });
process.stdout.write(msg + '\n');
}
function sendError(id, code, message) {
const msg = JSON.stringify({ jsonrpc: '2.0', id, error: { code, message } });
process.stdout.write(msg + '\n');
}
// ─── Tool definition ─────────────────────────────────────────────────────────
const NOTIFY_USER_TOOL = {
name: 'notify_user',
description: 'Send a non-blocking notification to the user. Use this to report progress, milestones, warnings, or errors WITHOUT pausing execution. You continue working immediately after calling this tool. Use sparingly — only for meaningful status changes, not every minor step.',
inputSchema: {
type: 'object',
properties: {
level: {
type: 'string',
enum: ['info', 'warning', 'milestone', 'error', 'progress'],
default: 'info',
description: 'Notification level: info (general update), warning (potential issue), milestone (significant achievement), error (something failed), progress (task progress with numeric tracking)',
},
title: {
type: 'string',
description: 'Short notification title (max 120 chars)',
},
detail: {
type: 'string',
description: 'Optional longer description or context',
},
progress: {
type: 'object',
properties: {
current: { type: 'number', description: 'Current step number' },
total: { type: 'number', description: 'Total number of steps' },
},
required: ['current', 'total'],
description: 'Progress tracking (used when level is "progress")',
},
},
required: ['title'],
},
};
// ─── HTTP POST to Express server (fire-and-forget) ──────────────────────────
function postToServer(body) {
return new Promise((resolve, reject) => {
const data = JSON.stringify(body);
const parsed = new URL(SERVER_URL);
const options = {
hostname: parsed.hostname,
port: parsed.port || 80,
path: '/api/internal/notify',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(data),
'Authorization': `Bearer ${SECRET}`,
},
timeout: 5000, // short timeout — non-blocking, don't wait long
};
const req = http.request(options, (res) => {
let responseBody = '';
res.on('data', (chunk) => { responseBody += chunk; });
res.on('end', () => {
try { resolve(JSON.parse(responseBody)); }
catch { resolve({ ok: true }); }
});
});
req.on('error', (err) => reject(new Error(`HTTP request failed: ${err.message}`)));
req.on('timeout', () => { req.destroy(); resolve({ ok: true }); });
req.write(data);
req.end();
});
}
// ─── Handle JSON-RPC messages ────────────────────────────────────────────────
let _initialized = false;
async function handleMessage(msg) {
const { id, method, params } = msg;
// Notifications (no id) — acknowledge silently
if (id === undefined || id === null) return;
switch (method) {
case 'initialize':
_initialized = true;
sendResponse(id, {
protocolVersion: '2024-11-05',
capabilities: { tools: {} },
serverInfo: { name: '_ccs_notify', version: '1.0.0' },
});
break;
case 'tools/list':
if (!_initialized) { sendError(id, -32002, 'Server not initialized'); return; }
sendResponse(id, { tools: [NOTIFY_USER_TOOL] });
break;
case 'tools/call': {
if (!_initialized) { sendError(id, -32002, 'Server not initialized'); return; }
const toolName = params?.name;
if (toolName !== 'notify_user') {
sendError(id, -32602, `Unknown tool: ${toolName}`);
return;
}
const args = params?.arguments || {};
const level = args.level || 'info';
const title = String(args.title || '').substring(0, 120);
const detail = String(args.detail || '').substring(0, 500);
const progress = args.progress || null;
try {
await postToServer({
sessionId: SESSION_ID,
level,
title,
detail,
progress,
});
// NON-BLOCKING: return immediately — don't wait for user
sendResponse(id, {
content: [{ type: 'text', text: `Notification sent: [${level}] ${title}` }],
});
} catch (err) {
// Still return success to Claude — notification failure should never block work
sendResponse(id, {
content: [{ type: 'text', text: `Notification delivery failed (${err.message}), continuing.` }],
});
}
break;
}
default:
// Unknown method — return method not found for requests, ignore notifications
if (id !== undefined && id !== null) {
sendError(id, -32601, `Method not found: ${method}`);
}
}
}
// ─── Stdin line reader ───────────────────────────────────────────────────────
const decoder = new StringDecoder('utf8');
let buffer = '';
process.stdin.on('data', (chunk) => {
buffer += decoder.write(chunk);
if (buffer.length > MAX_STDIN_BUFFER) {
process.stderr.write('[mcp] stdin buffer overflow, resetting\n');
buffer = '';
return;
}
const lines = buffer.split(/\r?\n/);
buffer = lines.pop() || '';
for (const line of lines) {
if (!line.trim()) continue;
try {
const msg = JSON.parse(line);
handleMessage(msg).catch((err) => {
process.stderr.write(`notify_user MCP error: ${err.message}\n`);
});
} catch {
// Ignore unparseable lines
}
}
});
process.stdin.on('end', () => {
// Flush remaining buffer
const remaining = buffer + decoder.end();
if (remaining.trim()) {
try {
const msg = JSON.parse(remaining);
handleMessage(msg).catch(() => {});
} catch {}
}
process.exit(0);
});
// Handle SIGTERM/SIGINT gracefully
process.on('SIGTERM', () => process.exit(0));
process.on('SIGINT', () => process.exit(0));