-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathtest-notification.js
More file actions
150 lines (125 loc) · 4.61 KB
/
test-notification.js
File metadata and controls
150 lines (125 loc) · 4.61 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
#!/usr/bin/env node
/**
* Test script to verify MCP notification handling
* Tests both notifications (no id) and regular requests (with id)
*/
const http = require('http');
const PORT = process.env.MCP_PORT || 3456;
const HOST = 'localhost';
// Helper function to send JSON-RPC request
function sendRequest(data) {
return new Promise((resolve, reject) => {
const postData = JSON.stringify(data);
const options = {
hostname: HOST,
port: PORT,
path: '/mcp',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData)
}
};
const req = http.request(options, (res) => {
let body = '';
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', () => {
resolve({
statusCode: res.statusCode,
statusMessage: res.statusMessage,
headers: res.headers,
body: body ? JSON.parse(body) : null
});
});
});
req.on('error', reject);
req.write(postData);
req.end();
});
}
// Test cases
async function runTests() {
console.log('Testing MCP Server Notification Handling\n');
console.log('==========================================\n');
try {
// Test 1: Send notification (no id field)
console.log('Test 1: Sending notification/initialized (should return 204 No Content)');
const notificationRequest = {
jsonrpc: '2.0',
method: 'notifications/initialized',
params: {}
// Note: no 'id' field - this makes it a notification
};
const notificationResult = await sendRequest(notificationRequest);
console.log(` Status Code: ${notificationResult.statusCode}`);
console.log(` Status Message: ${notificationResult.statusMessage}`);
console.log(` Response Body: ${notificationResult.body ? JSON.stringify(notificationResult.body) : 'null (empty)'}`);
if (notificationResult.statusCode === 204) {
console.log(' ✅ PASS: Notification handled correctly with 204 No Content\n');
} else {
console.log(' ❌ FAIL: Expected 204, got', notificationResult.statusCode, '\n');
}
// Test 2: Send regular request (with id field)
console.log('Test 2: Sending tools/list request (should return 200 with JSON response)');
const requestWithId = {
jsonrpc: '2.0',
method: 'tools/list',
params: {},
id: 1
};
const requestResult = await sendRequest(requestWithId);
console.log(` Status Code: ${requestResult.statusCode}`);
console.log(` Response Body: ${requestResult.body ? 'JSON response received' : 'null'}`);
if (requestResult.statusCode === 200 && requestResult.body) {
console.log(' ✅ PASS: Regular request handled correctly with JSON response\n');
} else {
console.log(' ❌ FAIL: Expected 200 with JSON body\n');
}
// Test 3: Send notification/cancelled
console.log('Test 3: Sending notification/cancelled (should return 204 No Content)');
const cancelNotification = {
jsonrpc: '2.0',
method: 'notifications/cancelled',
params: { requestId: 123 }
// Note: no 'id' field
};
const cancelResult = await sendRequest(cancelNotification);
console.log(` Status Code: ${cancelResult.statusCode}`);
console.log(` Status Message: ${cancelResult.statusMessage}`);
if (cancelResult.statusCode === 204) {
console.log(' ✅ PASS: Cancel notification handled correctly\n');
} else {
console.log(' ❌ FAIL: Expected 204, got', cancelResult.statusCode, '\n');
}
// Test 4: Health check
console.log('Test 4: Health check (should return 200)');
const healthResult = await new Promise((resolve, reject) => {
http.get(`http://${HOST}:${PORT}/health`, (res) => {
let body = '';
res.on('data', chunk => body += chunk);
res.on('end', () => resolve({
statusCode: res.statusCode,
body: JSON.parse(body)
}));
}).on('error', reject);
});
console.log(` Status Code: ${healthResult.statusCode}`);
console.log(` Response: ${JSON.stringify(healthResult.body)}`);
if (healthResult.statusCode === 200) {
console.log(' ✅ PASS: Health check successful\n');
} else {
console.log(' ❌ FAIL: Expected 200\n');
}
console.log('==========================================');
console.log('All tests completed!');
} catch (error) {
console.error('Test failed with error:', error.message);
console.error('\nMake sure the MCP server is running with:');
console.error(' MCP_STANDALONE=true npm start');
process.exit(1);
}
}
// Run tests
runTests();