-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtest-prompts-api.js
More file actions
155 lines (132 loc) · 4.17 KB
/
test-prompts-api.js
File metadata and controls
155 lines (132 loc) · 4.17 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
#!/usr/bin/env node
import { spawn } from 'child_process';
import { join } from 'path';
// Start the server as a child process
const serverProc = spawn('node', ['chucknorris-mcp-server.js'], {
stdio: ['pipe', 'pipe', 'pipe']
});
// Track if server is ready
let serverReady = false;
// Setup logging
serverProc.stderr.on('data', (data) => {
const log = data.toString().trim();
console.log(`Server log: ${log}`);
if (log.includes('ChuckNorris MCP server running on stdio')) {
serverReady = true;
console.log('Server is ready, sending initialization...');
sendInitializeRequest();
}
});
// Handle server exit
serverProc.on('exit', (code) => {
console.log(`Server process exited with code ${code}`);
});
// For clean shutdown
process.on('SIGINT', () => {
console.log('Cleaning up and exiting...');
if (serverProc) serverProc.kill();
process.exit(0);
});
// Track message ID
let messageId = 0;
// Send message to server
function sendMessage(method, params = {}) {
const msg = {
jsonrpc: '2.0',
id: messageId++,
method,
params
};
console.log(`Sending ${method} request...`);
serverProc.stdin.write(JSON.stringify(msg) + '\n');
return msg.id;
}
// Send initialization request
function sendInitializeRequest() {
sendMessage('initialize', {
capabilities: {}
});
// Set up message handling after sending init
setupMessageHandling();
}
// Process server output
function setupMessageHandling() {
serverProc.stdout.on('data', (data) => {
const responseText = data.toString().trim();
try {
const response = JSON.parse(responseText);
handleResponse(response);
} catch (e) {
console.log('Server output (non-JSON):', responseText);
}
});
// Set a timeout to force exit if test hangs
setTimeout(() => {
console.log('Safety timeout reached (15 seconds), forcing exit');
console.log('Cleaning up and exiting...');
serverProc.kill();
process.exit(0);
}, 15000);
}
// Test sequence
async function handleResponse(response) {
console.log(`Processing response ID ${response.id}...`);
if (response.id === 0) {
// After initialization, list initial tools
sendMessage('tools/list');
}
else if (response.id === 1) {
// After receiving initial tools list, call easyChuckNorris
console.log('\nInitial tool list received!');
console.log('Calling easyChuckNorris for ANTHROPIC...');
sendMessage('tools/call', {
name: 'easyChuckNorris',
arguments: {
llmName: 'ANTHROPIC'
}
});
}
else if (response.id === 2) {
// After calling easyChuckNorris, check the prompts list
console.log('\neasyChuckNorris response received:');
console.log(response.result.content[0].text.substring(0, 100) + '...\n');
console.log('Requesting prompts list...');
sendMessage('prompts/list');
}
else if (response.id === 3) {
// After receiving prompts list, get the ANTHROPIC prompt
console.log('\nPrompts list received:');
if (response.result.prompts && response.result.prompts.length > 0) {
console.log(`Found ${response.result.prompts.length} prompts:`);
response.result.prompts.forEach(p => {
console.log(`- ${p.name}: ${p.description.substring(0, 50)}...`);
});
// Get the first prompt
const firstPrompt = response.result.prompts[0];
console.log(`\nGetting prompt: ${firstPrompt.name}`);
sendMessage('prompts/get', {
name: firstPrompt.name
});
} else {
console.log('No prompts found!');
// Exit the test
console.log('\nTest completed.');
serverProc.kill();
process.exit(0);
}
}
else if (response.id === 4) {
// After getting prompt content, finish the test
console.log('\nPrompt content received:');
if (response.result.messages && response.result.messages.length > 0) {
console.log(`Description: ${response.result.description}`);
console.log(`Content: ${response.result.messages[0].content.text.substring(0, 100)}...`);
} else {
console.log('No prompt messages received!');
}
// Exit the test
console.log('\nTest completed.');
serverProc.kill();
process.exit(0);
}
}