-
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathmcp-set-ui-state.js
More file actions
225 lines (193 loc) · 7.76 KB
/
mcp-set-ui-state.js
File metadata and controls
225 lines (193 loc) · 7.76 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
217
218
219
220
221
222
223
224
225
#!/usr/bin/env node
// ─── Internal MCP Server: set_ui_state ────────────────────────────────────────
// Raw JSON-RPC 2.0 over stdio (newline-delimited). Zero external dependencies.
// Provides a "set_ui_state" tool that updates the UI toolbar state (mode, model, agent).
// Fire-and-forget — does NOT pause Claude.
//
// Environment variables (set by server.js at injection time):
// SET_UI_STATE_SERVER_URL — e.g. http://127.0.0.1:3000
// SET_UI_STATE_SESSION_ID — local session ID for routing to correct client
// SET_UI_STATE_SECRET — per-process auth secret
const http = require('http');
const { StringDecoder } = require('string_decoder');
const SERVER_URL = process.env.SET_UI_STATE_SERVER_URL || 'http://127.0.0.1:3000';
const SESSION_ID = process.env.SET_UI_STATE_SESSION_ID || '';
const SECRET = process.env.SET_UI_STATE_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 SET_UI_STATE_TOOL = {
name: 'set_ui_state',
description: 'Update the UI state (mode, model, agent). Use this when you transition between phases (e.g., from planning to execution) so the UI reflects your current state. This is fire-and-forget — execution continues immediately.',
inputSchema: {
type: 'object',
properties: {
mode: {
type: 'string',
enum: ['auto', 'planning', 'task'],
description: 'Execution mode: "planning" (analyze only, no modifications), "task" (execution mode), "auto" (default balanced mode)',
},
model: {
type: 'string',
enum: ['haiku', 'sonnet', 'opus'],
description: 'Model to switch to in the UI',
},
agent: {
type: 'string',
enum: ['single', 'multi'],
description: 'Agent mode: "single" or "multi"',
},
},
},
};
// ─── 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/set-ui-state',
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_set_ui_state', version: '1.0.0' },
});
break;
case 'tools/list':
if (!_initialized) { sendError(id, -32002, 'Server not initialized'); return; }
sendResponse(id, { tools: [SET_UI_STATE_TOOL] });
break;
case 'tools/call': {
if (!_initialized) { sendError(id, -32002, 'Server not initialized'); return; }
const toolName = params?.name;
if (toolName !== 'set_ui_state') {
sendError(id, -32602, `Unknown tool: ${toolName}`);
return;
}
const args = params?.arguments || {};
const { mode, model, agent } = args;
// Validate at least one parameter provided
if (!mode && !model && !agent) {
sendError(id, -32602, 'At least one of mode, model, or agent must be provided');
return;
}
// Validate enum values
const validModes = ['auto', 'planning', 'task'];
const validModels = ['haiku', 'sonnet', 'opus'];
const validAgents = ['single', 'multi'];
if (mode && !validModes.includes(mode)) {
sendError(id, -32602, `Invalid mode: ${mode}. Valid values: ${validModes.join(', ')}`);
return;
}
if (model && !validModels.includes(model)) {
sendError(id, -32602, `Invalid model: ${model}. Valid values: ${validModels.join(', ')}`);
return;
}
if (agent && !validAgents.includes(agent)) {
sendError(id, -32602, `Invalid agent: ${agent}. Valid values: ${validAgents.join(', ')}`);
return;
}
try {
await postToServer({
sessionId: SESSION_ID,
mode,
model,
agent,
});
sendResponse(id, {
content: [{ type: 'text', text: `UI state updated: ${mode ? `mode=${mode}` : ''}${model ? ` model=${model}` : ''}${agent ? ` agent=${agent}` : ''}`.trim() }],
});
} catch (err) {
// Still return success to Claude — UI state update failure should never block work
sendResponse(id, {
content: [{ type: 'text', text: `UI state update 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(`set_ui_state 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));