-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathexercise-mcp-server.cjs
More file actions
160 lines (150 loc) · 4.22 KB
/
Copy pathexercise-mcp-server.cjs
File metadata and controls
160 lines (150 loc) · 4.22 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
const { spawn } = require('child_process');
const serverCmd = 'npx';
const serverArgs = [
'-y',
'tsx',
'./src/index.ts',
'--config',
'./.cursor/db-config.json'
];
// Start the MCP server as a child process
const server = spawn(serverCmd, serverArgs, { stdio: ['pipe', 'pipe', 'inherit'] });
server.stdout.setEncoding('utf8');
// State for dynamic requests
let dbIds = [];
let currentRequestIndex = 0;
const pending = new Map();
const TIMEOUT_MS = 4000;
// Initial requests: list tools, then list databases
const requests = [
{
jsonrpc: "2.0",
id: 1,
method: "tools/list",
params: {}
},
{
jsonrpc: "2.0",
id: 2,
method: "tools/call",
params: {
name: "list_databases",
arguments: {}
}
}
];
let testInsightDbId = null;
let testInsightText = `Test insight at ${new Date().toISOString()}`;
function sendNextRequest() {
if (currentRequestIndex >= requests.length) {
// If we have dbIds, queue up list_tables requests for each
if (dbIds.length > 0) {
// Start at 3 to avoid id collision
let id = 3;
dbIds.forEach((dbId) => {
requests.push({
jsonrpc: "2.0",
id: id++,
method: "tools/call",
params: {
name: "list_tables",
arguments: { dbId }
}
});
});
// For insights test, pick the first dbId
testInsightDbId = dbIds[0];
// Add append_insight request
requests.push({
jsonrpc: "2.0",
id: id++,
method: "tools/call",
params: {
name: "append_insight",
arguments: { dbId: testInsightDbId, insight: testInsightText }
}
});
// Add list_insights request
requests.push({
jsonrpc: "2.0",
id: id++,
method: "tools/call",
params: {
name: "list_insights",
arguments: { dbId: testInsightDbId }
}
});
dbIds = []; // Prevent re-adding
sendNextRequest();
return;
}
// All requests sent, exit after a short delay
setTimeout(() => {
server.kill();
process.exit(0);
}, 1000);
return;
}
const req = requests[currentRequestIndex];
pending.set(req.id, setTimeout(() => {
console.error(`Timeout waiting for response to request id ${req.id}`);
sendNextRequest();
}, TIMEOUT_MS));
server.stdin.write(JSON.stringify(req) + '\n');
}
// Listen for responses
server.stdout.on('data', (data) => {
data.split('\n').filter(Boolean).forEach(line => {
let response;
try {
response = JSON.parse(line);
} catch (e) {
console.log('Non-JSON output:', line);
return;
}
if (response.id && pending.has(response.id)) {
clearTimeout(pending.get(response.id));
pending.delete(response.id);
// Improved output formatting
if (response.result && Array.isArray(response.result.content)) {
response.result.content.forEach((item) => {
if (item.type === 'text' && typeof item.text === 'string') {
// Try to parse as JSON
try {
const parsed = JSON.parse(item.text);
// If this is the list_databases response, extract dbIds
if (response.id === 2 && parsed.databases) {
dbIds = parsed.databases.map(db => db.id);
}
console.log('Received (pretty):', JSON.stringify(parsed, null, 2));
} catch (e) {
// Not JSON, print as-is
console.log('Received (text):', item.text);
}
} else {
console.log('Received (content):', JSON.stringify(item, null, 2));
}
});
} else {
console.log('Received:', JSON.stringify(response, null, 2));
}
currentRequestIndex++;
sendNextRequest();
} else {
// Notification or unexpected response
console.log('Received (no matching id):', JSON.stringify(response, null, 2));
}
});
});
server.on('error', (err) => {
console.error('Server process error:', err);
process.exit(1);
});
server.on('exit', (code, signal) => {
if (code !== 0) {
console.error(`Server exited with code ${code} (signal: ${signal})`);
process.exit(code);
}
});
// Start the sequence
sendNextRequest();