forked from domdomegg/airtable-mcp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-mcp-connection.js
More file actions
executable file
Β·175 lines (152 loc) Β· 4.83 KB
/
test-mcp-connection.js
File metadata and controls
executable file
Β·175 lines (152 loc) Β· 4.83 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
#!/usr/bin/env node
import { spawn } from 'child_process';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import fs from 'fs';
// Get the directory of the current module
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Setup logging
const logsDir = join(__dirname, 'logs');
if (!fs.existsSync(logsDir)) {
fs.mkdirSync(logsDir, { recursive: true });
}
const logFile = join(logsDir, `mcp-connection-test-${new Date().toISOString().replace(/:/g, '-')}.log`);
const log = fs.createWriteStream(logFile, { flags: 'a' });
function logMessage(message) {
const timestamp = new Date().toISOString();
const formattedMsg = `[${timestamp}] ${message}`;
console.log(formattedMsg);
log.write(formattedMsg + '\n');
}
logMessage('Starting MCP connection test');
// Start the server process
const serverProcess = spawn('node', ['dist/index.js'], {
env: {
...process.env,
AITABLE_API_KEY: 'REMOVED_TOKEN',
SPACE: 'spc12q5HY4ay5',
LOG_LEVEL: 'debug',
DEBUG: 'true'
},
stdio: ['pipe', 'pipe', 'pipe']
});
// Track process state
let isServerReady = false;
let messagesSent = 0;
let responsesReceived = 0;
const MAX_TEST_MESSAGES = 3;
// Log process events
serverProcess.on('error', (err) => {
logMessage(`Server process error: ${err.message}`);
});
serverProcess.on('exit', (code, signal) => {
logMessage(`Server process exited with code ${code} and signal ${signal}`);
});
// Capture stdout
serverProcess.stdout.on('data', (data) => {
const output = data.toString();
logMessage(`Server stdout: ${output.trim()}`);
// Check for MCP responses
try {
const lines = output.split('\n').filter(line => line.trim());
for (const line of lines) {
if (line.includes('"id":')) {
responsesReceived++;
logMessage(`Received MCP response #${responsesReceived}`);
}
}
// If we've received responses to all our test messages, exit successfully
if (responsesReceived >= MAX_TEST_MESSAGES) {
logMessage('β
Connection test SUCCESSFUL - received responses to all test messages');
serverProcess.kill();
process.exit(0);
}
} catch (err) {
logMessage(`Error parsing server output: ${err.message}`);
}
});
// Capture stderr
serverProcess.stderr.on('data', (data) => {
const output = data.toString();
logMessage(`Server stderr: ${output.trim()}`);
// Check if server is ready
if (output.includes('AITable MCP Server Ready')) {
isServerReady = true;
logMessage('Server is ready. Sending test MCP messages...');
sendTestMessages();
}
});
// Function to send test MCP messages
function sendTestMessages() {
// Send a list_bases request
const listBasesRequest = {
jsonrpc: '2.0',
id: 'test-1',
method: 'mcp/tool',
params: {
name: 'list_bases',
input: {}
}
};
// Check if stdout is writable and send the message
if (serverProcess.stdin.writable) {
serverProcess.stdin.write(JSON.stringify(listBasesRequest) + '\n');
messagesSent++;
logMessage(`Sent test message #${messagesSent}: list_bases`);
// Send subsequent test messages with a delay
setTimeout(() => {
if (messagesSent < MAX_TEST_MESSAGES) {
const listTablesRequest = {
jsonrpc: '2.0',
id: 'test-2',
method: 'mcp/tool',
params: {
name: 'list_tables',
input: {
baseId: 'spc12q5HY4ay5'
}
}
};
serverProcess.stdin.write(JSON.stringify(listTablesRequest) + '\n');
messagesSent++;
logMessage(`Sent test message #${messagesSent}: list_tables`);
// Send one more test message
setTimeout(() => {
if (messagesSent < MAX_TEST_MESSAGES) {
const describeTableRequest = {
jsonrpc: '2.0',
id: 'test-3',
method: 'mcp/tool',
params: {
name: 'describe_table',
input: {
baseId: 'spc12q5HY4ay5',
tableId: 'dstq4onAEtjMleaeCm'
}
}
};
serverProcess.stdin.write(JSON.stringify(describeTableRequest) + '\n');
messagesSent++;
logMessage(`Sent test message #${messagesSent}: describe_table`);
}
}, 1000);
}
}, 1000);
} else {
logMessage('ERROR: Server stdin is not writable');
}
}
// Set a timeout to exit if the test doesn't complete
const testTimeout = setTimeout(() => {
logMessage('β Test FAILED: Timeout exceeded. Not all messages received responses.');
serverProcess.kill();
process.exit(1);
}, 30000);
// Handle process termination
process.on('SIGINT', () => {
logMessage('Test interrupted by user');
clearTimeout(testTimeout);
serverProcess.kill();
process.exit(1);
});