-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp.js
More file actions
392 lines (357 loc) · 10.6 KB
/
Copy pathmcp.js
File metadata and controls
392 lines (357 loc) · 10.6 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
'use strict';
// OpenWorkdays MCP server — remote, anonymous, zero-signup.
// Single zero-dependency Node serverless function. Implements the MCP
// Streamable HTTP transport in STATELESS JSON mode: every POST gets a single
// application/json JSON-RPC 2.0 response. No sessions, no SSE streaming.
//
// Lets autonomous AI agents call OpenWorkdays as a tool with no account/key —
// an agent has no human to do a signup.
const DEFAULT_PROTOCOL_VERSION = '2025-06-18';
const UPSTREAM = 'https://openworkdays.vercel.app/api/businessdays';
const SERVER_INFO = { name: 'openworkdays', version: '0.1.0' };
const BUSINESSDAYS_TOOL = {
name: 'businessdays',
description:
'Business-day date math. Three modes inferred from params: add ' +
'(start+days -> the date N business days away, negative days = before), ' +
'diff (start+end -> count of business days between, inclusive of both ' +
'endpoints), is (date -> is it a business day). Optional weekend ' +
'(default sat,sun) and holidays (comma list of YYYY-MM-DD you supply). ' +
'No signup, no API key. Pure UTC date-only math; no ' +
'time-of-day/DST/timezone.',
inputSchema: {
type: 'object',
properties: {
start: {
type: 'string',
description: 'Start date YYYY-MM-DD (add & diff modes)',
},
days: {
type: 'integer',
description:
'Business days to add (add mode); negative subtracts',
},
end: {
type: 'string',
description: 'End date YYYY-MM-DD (diff mode)',
},
date: {
type: 'string',
description: 'Date YYYY-MM-DD to test (is mode)',
},
weekend: {
type: 'string',
description: 'Comma list of weekend days, e.g. "sat,sun" (default sat,sun)',
},
holidays: {
type: 'string',
description: 'Comma list of YYYY-MM-DD dates treated as non-working',
},
},
},
annotations: {
readOnlyHint: true,
openWorldHint: false,
destructiveHint: false,
idempotentHint: true,
},
};
// --- helpers ---------------------------------------------------------------
function readBody(req) {
return new Promise((resolve) => {
// Vercel may have already parsed/buffered the body.
if (req.body !== undefined && req.body !== null) {
if (typeof req.body === 'string') {
try {
resolve(req.body.length ? JSON.parse(req.body) : undefined);
} catch (e) {
resolve({ __parseError: true });
}
return;
}
if (Buffer.isBuffer(req.body)) {
const s = req.body.toString('utf8');
try {
resolve(s.length ? JSON.parse(s) : undefined);
} catch (e) {
resolve({ __parseError: true });
}
return;
}
if (typeof req.body === 'object') {
resolve(req.body);
return;
}
}
// Otherwise read the raw stream.
let data = '';
let done = false;
const finish = (val) => {
if (done) return;
done = true;
resolve(val);
};
try {
req.on('data', (chunk) => {
data += chunk;
});
req.on('end', () => {
if (!data.length) return finish(undefined);
try {
finish(JSON.parse(data));
} catch (e) {
finish({ __parseError: true });
}
});
req.on('error', () => finish({ __parseError: true }));
} catch (e) {
finish({ __parseError: true });
}
});
}
function jsonRpcError(id, code, message, data) {
const err = { code, message };
if (data !== undefined) err.data = data;
return { jsonrpc: '2.0', id: id === undefined ? null : id, error: err };
}
function jsonRpcResult(id, result) {
return { jsonrpc: '2.0', id, result };
}
function toolResult(text, structuredContent, isError) {
const r = { content: [{ type: 'text', text }], isError: !!isError };
if (structuredContent !== undefined) r.structuredContent = structuredContent;
return r;
}
async function callBusinessdays(args) {
try {
const qs = new URLSearchParams();
const keys = ['start', 'days', 'end', 'date', 'weekend', 'holidays'];
for (let i = 0; i < keys.length; i++) {
const k = keys[i];
const v = args[k];
if (v === undefined || v === null || v === '') continue;
qs.set(k, String(v));
}
const r = await fetch(UPSTREAM + '?' + qs.toString(), {
headers: { Accept: 'application/json' },
});
let j;
try {
j = await r.json();
} catch (e) {
const txt = '{"error":"upstream returned non-JSON"}';
j = JSON.parse(txt);
}
const isError =
!r.ok || (j && typeof j === 'object' && j.error != null);
return toolResult(JSON.stringify(j, null, 2), j, isError);
} catch (e) {
return toolResult(
'businessdays upstream request failed: ' +
(e && e.message ? e.message : 'error'),
undefined,
true
);
}
}
// Process a single JSON-RPC message. Returns:
// - a JSON-RPC response object (for requests), or
// - null (for notifications — no response).
async function handleMessage(msg) {
if (!msg || typeof msg !== 'object' || Array.isArray(msg)) {
return jsonRpcError(null, -32600, 'Invalid Request: expected a JSON-RPC object');
}
const { id, method } = msg;
const params = msg.params;
const isNotification = id === undefined || id === null;
if (typeof method !== 'string') {
if (isNotification) return null;
return jsonRpcError(id, -32600, 'Invalid Request: missing method');
}
// Any notifications/* message → no response (handled as HTTP 202 by caller).
if (method.indexOf('notifications/') === 0) {
return null;
}
if (method === 'initialize') {
const reqProto =
params && typeof params.protocolVersion === 'string'
? params.protocolVersion
: DEFAULT_PROTOCOL_VERSION;
return jsonRpcResult(id, {
protocolVersion: reqProto,
capabilities: { tools: {} },
serverInfo: SERVER_INFO,
});
}
if (method === 'ping') {
return jsonRpcResult(id, {});
}
if (method === 'tools/list') {
return jsonRpcResult(id, { tools: [BUSINESSDAYS_TOOL] });
}
if (method === 'tools/call') {
if (!params || typeof params !== 'object') {
return jsonRpcError(id, -32602, 'Invalid params: expected an object');
}
const name = params.name;
const args = params.arguments || {};
if (name !== 'businessdays') {
return jsonRpcResult(
id,
toolResult(
'Unknown tool "' +
String(name) +
'". This server exposes exactly one tool: "businessdays". ' +
'Call tools/list to see its schema.',
undefined,
true
)
);
}
if (
!args ||
typeof args !== 'object' ||
Array.isArray(args) ||
Object.keys(args).length === 0
) {
return jsonRpcResult(
id,
toolResult(
'Provide at least one mode: add needs start+days, diff needs ' +
'start+end, is needs date. See https://openworkdays.vercel.app',
undefined,
true
)
);
}
const result = await callBusinessdays(args);
return jsonRpcResult(id, result);
}
// Unknown method
if (isNotification) return null;
return jsonRpcError(id, -32601, 'Method not found: ' + String(method));
}
// --- handler ---------------------------------------------------------------
module.exports = async (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS, GET');
res.setHeader(
'Access-Control-Allow-Headers',
'Content-Type, MCP-Protocol-Version, Mcp-Session-Id'
);
const ip = (req.headers['x-forwarded-for'] || '').split(',')[0];
const ua = req.headers['user-agent'] || null;
if (req.method === 'OPTIONS') {
res.statusCode = 204;
return res.end();
}
res.setHeader('Content-Type', 'application/json');
if (req.method === 'GET') {
console.log(
JSON.stringify({ evt: 'mcp_hit', method: 'GET', ip, ua, ts: Date.now() })
);
res.statusCode = 405;
return res.end(
JSON.stringify({
error: 'Use POST for MCP JSON-RPC',
endpoint: 'https://openworkdays.vercel.app/api/mcp',
})
);
}
if (req.method !== 'POST') {
console.log(
JSON.stringify({
evt: 'mcp_hit',
method: req.method || null,
ip,
ua,
ts: Date.now(),
})
);
res.statusCode = 405;
return res.end(
JSON.stringify({
error: 'Use POST for MCP JSON-RPC',
endpoint: 'https://openworkdays.vercel.app/api/mcp',
})
);
}
const body = await readBody(req);
// Parse error → -32700
if (body && body.__parseError) {
console.log(
JSON.stringify({
evt: 'mcp_hit',
method: 'parse_error',
ip,
ua,
ts: Date.now(),
})
);
res.statusCode = 200;
return res.end(
JSON.stringify(jsonRpcError(null, -32700, 'Parse error: invalid JSON'))
);
}
// --- Batch (array of messages) -------------------------------------------
if (Array.isArray(body)) {
console.log(
JSON.stringify({
evt: 'mcp_hit',
method: 'batch',
ip,
ua,
ts: Date.now(),
})
);
if (body.length === 0) {
res.statusCode = 200;
return res.end(
JSON.stringify(jsonRpcError(null, -32600, 'Invalid Request: empty batch'))
);
}
const responses = [];
for (let i = 0; i < body.length; i++) {
// eslint-disable-next-line no-await-in-loop
const r = await handleMessage(body[i]);
if (r !== null) responses.push(r);
}
if (responses.length === 0) {
// All notifications → no response bodies.
res.statusCode = 202;
return res.end();
}
res.statusCode = 200;
return res.end(JSON.stringify(responses));
}
// --- Single message (common case) ----------------------------------------
if (!body || typeof body !== 'object') {
console.log(
JSON.stringify({
evt: 'mcp_hit',
method: 'invalid_request',
ip,
ua,
ts: Date.now(),
})
);
res.statusCode = 200;
return res.end(
JSON.stringify(
jsonRpcError(null, -32600, 'Invalid Request: expected a JSON-RPC object')
)
);
}
const method = typeof body.method === 'string' ? body.method : null;
console.log(
JSON.stringify({ evt: 'mcp_hit', method, ip, ua, ts: Date.now() })
);
const response = await handleMessage(body);
// Notification → HTTP 202, no JSON-RPC body.
if (response === null) {
res.statusCode = 202;
return res.end();
}
res.statusCode = 200;
return res.end(JSON.stringify(response));
};