-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmcp.js
More file actions
756 lines (665 loc) · 20.6 KB
/
mcp.js
File metadata and controls
756 lines (665 loc) · 20.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
import { tools } from './tools.js';
import { existsSync, readFileSync, writeFileSync } from 'fs';
import { resolve } from 'path';
import { createInterface } from 'readline';
// Individual tool imports are now redundant since we import from tools.js
// import * as BashTool from './tools/bash.js';
// import * as LSTool from './tools/ls.js';
// Enable server debugging
const DEBUG = process.env.DEBUG === 'true';
// Logger function that conditionally logs based on DEBUG flag
function debug(...args) {
if (DEBUG) {
console.error('[SERVER DEBUG]', ...args);
}
writeFileSync('/tmp/mcp.log', `${new Date().toISOString()}: ${args.join(' ')}\n`, { flag: 'a' });
}
// Constants for MCP protocol
const JSON_RPC_VERSION = '2.0';
const MCP_VERSION = '2024-11-05';
const SUPPORTED_VERSIONS = [MCP_VERSION];
// A buffer class for reading JSON-RPC messages from a stream
class ReadBuffer {
constructor() {
this.buffer = Buffer.alloc(0);
}
append(chunk) {
this.buffer = Buffer.concat([this.buffer, chunk]);
}
readMessage() {
// Try to find a complete JSON message
let message = null;
let start = 0;
// Skip any leading whitespace
while (start < this.buffer.length &&
(this.buffer[start] === 0x20 || // space
this.buffer[start] === 0x09 || // tab
this.buffer[start] === 0x0A || // LF
this.buffer[start] === 0x0D)) { // CR
start++;
}
if (start === this.buffer.length) {
// Only whitespace, reset buffer
this.buffer = Buffer.alloc(0);
return null;
}
// Look for newline that ends a complete message
for (let i = start; i < this.buffer.length; i++) {
if (this.buffer[i] === 0x0A) { // LF
try {
const jsonStr = this.buffer.slice(start, i).toString('utf8');
if (jsonStr.trim()) {
message = JSON.parse(jsonStr);
}
// Update buffer to remove the message we just read
this.buffer = this.buffer.slice(i + 1);
return message;
} catch (e) {
debug("Error parsing JSON message:", e.message);
// Invalid JSON, keep looking
continue;
}
}
}
// No complete message found
return null;
}
clear() {
this.buffer = Buffer.alloc(0);
}
}
// Serialize a message to a JSON string with a newline
function serializeMessage(message) {
return JSON.stringify(message) + '\n';
}
// Define some basic resources
const RESOURCES = [
{
uri: 'koding://tools',
name: 'Available Tools',
description: 'List of all available tools in the system',
mimeType: 'application/json'
},
{
uri: 'koding://config',
name: 'Configuration',
description: 'System configuration',
mimeType: 'application/json'
},
{
uri: 'file:///package.json',
name: 'Package Configuration',
description: 'Node.js package configuration',
mimeType: 'application/json'
}
];
// Define resource templates
const RESOURCE_TEMPLATES = [
{
uriTemplate: 'file:///{path}',
name: 'File Access',
description: 'Access file contents',
mimeType: 'application/octet-stream'
},
{
uriTemplate: 'koding://tool/{name}',
name: 'Tool Schema',
description: 'Get schema for a specific tool',
mimeType: 'application/json'
}
];
class MCPServer {
constructor() {
debug('Starting MCP server');
this.nextId = 1;
this.tools = {};
this.toolSchemas = {};
this.initialized = false;
this.clientCapabilities = null;
this.serverCapabilities = {
tools: {
listChanged: false
},
resources: {
subscribe: false,
listChanged: false
},
logging: {}
};
this.resources = [
{
name: 'Available Tools',
description: 'List of available tools',
uri: 'mcp://tools'
},
{
name: 'Configuration',
description: 'Server configuration',
uri: 'mcp://config'
},
{
name: 'Package Configuration',
description: 'Information about the package configuration',
uri: 'mcp://package'
}
];
// Register all tools immediately during initialization
for (const tool of tools) {
debug(`Registering tool: ${tool.name}`);
this.registerTool(tool);
debug(`Tool ${tool.name} registered successfully`);
}
}
registerTool(tool) {
if (tool.schema) {
// Create a properly formatted tool object
const formattedTool = {
name: tool.name,
description: tool.schema.description || "No description provided",
// Map parameters to input_schema if that's what's available
input_schema: tool.schema.input_schema || tool.schema.parameters || { type: "object", properties: {} },
handler: tool.handler
};
this.tools[tool.name] = formattedTool;
this.toolSchemas[tool.name] = formattedTool.input_schema;
} else {
// If no schema, just register as-is
this.tools[tool.name] = tool;
this.toolSchemas[tool.name] = { type: "object", properties: {} };
}
}
/**
* Handle JSON-RPC requests directly
*/
async handleRequest(request) {
debug('Handling request:', JSON.stringify(request));
// Check for required fields
if (!request.jsonrpc || request.jsonrpc !== JSON_RPC_VERSION) {
debug('Invalid JSON-RPC version:', request.jsonrpc);
return {
jsonrpc: JSON_RPC_VERSION,
id: request.id,
error: {
code: -32600,
message: 'Invalid JSON-RPC version'
}
};
}
// Handle notifications (no id field)
if (request.id === undefined) {
debug('Received notification:', request.method);
this.handleNotification(request);
return null; // No response for notifications
}
// Ensure initialize is called first
if (!this.initialized && request.method !== 'initialize' && request.method !== 'ping') {
debug('Server not initialized, rejecting request:', request.method);
return {
jsonrpc: JSON_RPC_VERSION,
id: request.id,
error: {
code: -32001,
message: 'Server not initialized'
}
};
}
// Handle methods
try {
debug('Processing method:', request.method);
let result;
switch (request.method) {
case 'initialize':
result = await this.handleInitialize(request.params);
break;
case 'ping':
result = { timestamp: Date.now() };
break;
case 'resources/list':
result = await this.handleResourcesList(request.params);
break;
case 'resources/read':
result = await this.handleResourceRead(request.params);
break;
case 'resources/subscribe':
result = await this.handleResourceSubscribe(request.params);
break;
case 'resources/templates/list':
result = await this.handleResourceTemplatesList(request.params);
break;
case 'tools/list':
result = await this.handleToolsList(request.params);
break;
case 'tools/call':
result = await this.handleToolCall(request.params);
break;
default:
debug('Unknown method:', request.method);
return {
jsonrpc: JSON_RPC_VERSION,
id: request.id,
error: {
code: -32601,
message: `Method not found: ${request.method}`
}
};
}
debug('Request successful, returning result');
return {
jsonrpc: JSON_RPC_VERSION,
id: request.id,
result
};
} catch (error) {
debug('Error handling request:', error);
return {
jsonrpc: JSON_RPC_VERSION,
id: request.id,
error: {
code: -32000,
message: `Server error: ${error.message}`
}
};
}
}
/**
* Handle notification messages (no response needed)
*/
handleNotification(notification) {
debug('Processing notification:', notification.method);
switch (notification.method) {
case 'notifications/initialized':
debug('Client initialization complete');
// Client has finished initialization, we're now in the Operation phase
if (!this.initialized) {
debug('Warning: Received initialized notification before successful initialization');
} else {
debug('MCP session fully established, entering operational phase');
// We could emit an event or set a state flag here
}
break;
case 'disconnect':
debug('Client requested disconnect');
// Client is disconnecting, we can shut down gracefully
process.exit(0);
break;
case 'notifications/resources/list_changed':
debug('Resources list changed notification');
// Handle any resources list change logic
break;
case 'notifications/tools/list_changed':
debug('Tools list changed notification');
// Handle any tools list change logic
break;
default:
debug('Unknown notification method:', notification.method);
// Ignore unknown notifications
break;
}
}
/**
* Handle initialize request (handshake)
*/
async handleInitialize(params) {
debug('Processing initialize with params:', params);
if (this.initialized) {
debug('Server already initialized, rejecting');
throw new Error('Server already initialized');
}
// Check protocol version
const clientVersion = params?.protocolVersion;
if (!clientVersion) {
debug('No protocol version specified');
throw {
code: -32602,
message: 'Missing protocol version',
data: {
supported: SUPPORTED_VERSIONS
}
};
}
// Check if we support the requested version
if (!SUPPORTED_VERSIONS.includes(clientVersion)) {
debug('Unsupported protocol version:', clientVersion, 'supported:', SUPPORTED_VERSIONS);
// Return a supported version as per spec
const serverVersion = SUPPORTED_VERSIONS[0]; // Use our preferred version
debug('Using server version:', serverVersion);
// Store client capabilities and mark as initialized
this.clientCapabilities = params?.capabilities || {};
this.initialized = true;
// Return server info with our version
return {
protocolVersion: serverVersion,
serverInfo: {
name: 'mcp-js-server',
version: '1.0.0'
},
capabilities: this.serverCapabilities
};
}
// Client version is supported
// Store client capabilities
this.clientCapabilities = params?.capabilities || {};
this.initialized = true;
debug('Initialization successful with protocol version:', clientVersion);
// Return server capabilities using the client's version
return {
protocolVersion: clientVersion,
serverInfo: {
name: 'mcp-js-server',
version: '1.0.0'
},
capabilities: this.serverCapabilities // Use the capabilities defined in the constructor
};
}
/**
* Handle resources/list request
*/
async handleResourcesList(params) {
debug('Processing resources/list with params:', params);
// Check capability
if (!this.initialized) {
debug('Server not initialized, rejecting');
throw new Error('Server not initialized');
}
// Return list of resources
const resources = Object.values(this.resources);
debug(`Returning ${resources.length} resources`);
return {
resources,
nextCursor: ""
};
}
/**
* Handle resources/read request
*/
async handleResourceRead(params) {
debug('Processing resources/read with params:', params);
const { uri } = params;
if (!uri) {
debug('Missing URI parameter');
throw new Error('URI is required');
}
// Check if resource exists
if (!this.resources[uri]) {
debug('Resource not found:', uri);
throw new Error(`Resource not found: ${uri}`);
}
// For this example, we'll just return the resource metadata
// In a real implementation, you'd return the actual resource content
debug('Returning resource:', uri);
return {
uri,
content: JSON.stringify(this.resources[uri])
};
}
/**
* Handle resources/templates/list request
*/
async handleResourceTemplatesList(params) {
debug('Processing resources/templates/list');
// Return list of resource templates
// In this example, we'll just return an empty list
return {
resourceTemplates: []
};
}
/**
* Handle resource subscribe request
*/
async handleResourceSubscribe(params) {
debug('Processing resources/subscribe with params:', params);
// Not implemented in this example
throw new Error('Resource subscription not supported');
}
/**
* Handle tools/list request
*/
handleToolsList(params = {}) {
debug('Processing tools/list');
// No need to register tools here since they're already registered in the constructor
// Extract pagination parameters
const limit = Number(params.limit) || 20;
const cursor = params.cursor ? Number(params.cursor) : 0;
// Get the tool names
const toolNames = Object.keys(this.tools);
// Calculate the indices for slicing the array
const startIndex = cursor;
const endIndex = Math.min(startIndex + limit, toolNames.length);
// Create the list of tools with their descriptions and schemas
const tools = toolNames.slice(startIndex, endIndex).map(name => {
const result = {
name,
description: this.tools[name].description
};
if (this.toolSchemas[name]) {
result.inputSchema = this.toolSchemas[name];
}
return result;
});
// Calculate the next cursor
const nextCursor = endIndex < toolNames.length ? endIndex.toString() : "";
debug(`Returning ${tools.length} tools (total: ${toolNames.length}, page: ${startIndex}-${endIndex}, nextCursor: ${nextCursor})`);
return {
tools,
nextCursor
};
}
/**
* Handle tools/call request
*/
async handleToolCall(params) {
debug('Processing tools/call with params:', params);
const { name, arguments: args } = params;
if (!name) {
debug('Missing tool name');
throw new Error('Tool name is required');
}
const tool = this.tools[name];
if (!tool) {
debug('Tool not found:', name);
throw new Error(`Tool not found: ${name}`);
}
// Call the tool with the provided arguments
try {
debug(`Executing tool: ${name}`);
// Create a toolCall object similar to what the handler expects
const toolCall = {
input: args,
abortController: new AbortController()
};
// Call the tool handler
const result = await tool.handler(toolCall);
debug('Tool execution successful');
// Convert the result to the MCP format
let content = [];
if (result.type === 'error' || result.error) {
// Handle error result
const errorText = result.resultForAssistant || result.error || 'An error occurred';
debug(`Tool execution error: ${errorText}`);
content.push({
type: 'text',
text: errorText
});
return {
content,
isError: true
};
} else if (result.type === 'image') {
// Handle image result
debug(`Tool returned image result`);
content.push({
type: 'image',
data: result.base64,
mimeType: result.mediaType || 'image/png'
});
} else {
// Handle regular result (text)
debug(`Tool returned text/data result`);
const text = result.resultForAssistant || JSON.stringify(result.data, null, 2);
content.push({
type: 'text',
text: text
});
}
return {
content,
isError: false
};
} catch (error) {
debug('Tool execution failed:', error);
throw new Error(`Tool execution failed: ${error.message}`);
}
}
}
class StdioTransport {
constructor(server) {
this.server = server;
this.readBuffer = new ReadBuffer();
this.started = false;
this.awaitingInitialized = false; // Track if we're waiting for initialized notification
}
async start() {
if (this.started) {
debug('Stdio transport already started');
return;
}
this.started = true;
debug('Starting stdio transport');
// Set up data handling for stdin
process.stdin.on('data', this._onData.bind(this));
process.stdin.on('error', this._onError.bind(this));
process.stdin.on('end', this._onEnd.bind(this));
// Set up resuming stdin - important for proper protocol handling
process.stdin.resume();
// Set up error handling for stdout
process.stdout.on('error', (error) => {
debug('Error on stdout:', error);
process.exit(1);
});
// Start health check for stdout
this._startHealthCheck();
}
_onData(chunk) {
this.readBuffer.append(chunk);
this._processBuffer();
}
_onError(error) {
debug('Error on stdin:', error);
process.exit(1);
}
_onEnd() {
debug('End of stdin, shutting down');
process.exit(0);
}
_processBuffer() {
let message;
while ((message = this.readBuffer.readMessage()) !== null) {
this._handleMessage(message);
}
}
async _handleMessage(message) {
try {
debug('Processing message:', JSON.stringify(message));
// Check for initialized notification after init
if (this.awaitingInitialized &&
message.method === 'notifications/initialized') {
debug('Received initialized notification, server fully ready');
this.awaitingInitialized = false;
}
const response = await this.server.handleRequest(message);
// If this was an initialize request, we should now wait for initialized
if (response && message.method === 'initialize') {
this.awaitingInitialized = true;
debug('Waiting for initialized notification');
}
if (response) {
this.send(response);
}
} catch (error) {
debug('Error handling message:', error);
if (message.id !== undefined) {
this.send({
jsonrpc: JSON_RPC_VERSION,
id: message.id,
error: {
code: -32603,
message: `Internal error: ${error.message}`
}
});
}
}
}
send(message) {
return new Promise((resolve) => {
const data = serializeMessage(message);
debug('Sending response:', data.trim());
if (process.stdout.write(data)) {
resolve();
} else {
process.stdout.once('drain', resolve);
}
});
}
_startHealthCheck() {
const interval = setInterval(() => {
try {
if (!process.stdout.writable) {
debug('Stdout is no longer writable, shutting down');
clearInterval(interval);
process.exit(1);
}
} catch (e) {
debug('Health check error:', e);
clearInterval(interval);
process.exit(1);
}
}, 5000);
// Ensure the interval doesn't keep the process alive
interval.unref();
}
close() {
debug('Closing stdio transport');
process.stdin.removeListener('data', this._onData);
process.stdin.removeListener('error', this._onError);
process.stdin.removeListener('end', this._onEnd);
this.readBuffer.clear();
this.started = false;
}
}
// Start the MCP server with stdio transport
async function main() {
try {
debug('Starting MCP server');
const server = new MCPServer();
// Create and start the transport
const transport = new StdioTransport(server);
await transport.start();
debug('MCP server started and ready for requests');
// Handle process termination
process.on('SIGINT', () => {
debug('Received SIGINT, shutting down');
transport.close();
process.exit(0);
});
process.on('SIGTERM', () => {
debug('Received SIGTERM, shutting down');
transport.close();
process.exit(0);
});
// Keep the process alive until explicitly terminated
// This prevents the process from exiting prematurely
const keepAlive = setInterval(() => {
debug('Server heartbeat');
}, 30000);
keepAlive.unref(); // Don't let this interval prevent process exit when requested
} catch (error) {
debug('Error starting server:', error);
process.exit(1);
}
}
// Start the server
main().catch(error => {
debug('Unhandled error:', error);
process.exit(1);
});
export default MCPServer;