forked from domdomegg/airtable-mcp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-debug.js
More file actions
160 lines (132 loc) · 4.68 KB
/
test-debug.js
File metadata and controls
160 lines (132 loc) · 4.68 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
#!/usr/bin/env node
// Test-debug.js - Direct API test to debug AITable connection issues
import 'dotenv/config';
import fs from 'fs';
import path from 'path';
import fetch from 'node-fetch';
// Create logs directory if it doesn't exist
const logDir = path.join(process.cwd(), 'logs');
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir, { recursive: true });
}
// Create debug log file
const timestamp = new Date().toISOString().replace(/:/g, '-');
const logFile = path.join(logDir, `aitable-debug-${timestamp}.log`);
// Helper to log to console and file
function log(message, obj) {
const timestamp = new Date().toISOString();
let msg = `[${timestamp}] ${message}`;
if (obj !== undefined) {
let objStr;
if (typeof obj === 'string') {
objStr = obj;
} else {
try {
objStr = JSON.stringify(obj, null, 2);
} catch (err) {
objStr = `[Error stringifying object: ${err.message}]`;
}
}
msg += `\n${objStr}`;
}
console.log(msg);
fs.appendFileSync(logFile, msg + '\n');
}
async function testDirectApiAccess() {
log('Starting direct AITable API test');
// Check for API key
const apiKey = process.env.AITABLE_API_KEY;
log(`API key defined: ${apiKey ? 'YES' : 'NO'}`);
if (!apiKey) {
log('ERROR: API key is not defined. Please set AITABLE_API_KEY in .env file');
return;
}
// Test space/base listing
log('Testing API connection to list bases/spaces...');
try {
// Try AITable spaces endpoint
const spacesUrl = 'https://aitable.ai/fusion/v1/spaces';
log(`Making request to: ${spacesUrl}`);
const headers = {
'Authorization': `Bearer ${apiKey}`,
'Accept': 'application/json',
'Content-Type': 'application/json',
'User-Agent': 'aitable-mcp-debug/0.1.0'
};
log('Request headers:', headers);
const response = await fetch(spacesUrl, { headers });
log(`Response status: ${response.status} ${response.statusText}`);
log('Response headers:', Object.fromEntries([...response.headers.entries()]));
const contentType = response.headers.get('content-type');
log(`Content-Type: ${contentType}`);
let body;
if (contentType && contentType.includes('application/json')) {
body = await response.json();
} else {
const text = await response.text();
log(`Raw response (first 1000 chars): ${text.substring(0, 1000)}`);
try {
body = JSON.parse(text);
} catch (err) {
log(`ERROR: Failed to parse response as JSON: ${err.message}`);
return;
}
}
log('Response body:', body);
if (response.ok) {
log('SUCCESS: API connection successful');
// If spaces are returned, try getting a datasheet
if (body.data && body.data.spaces && body.data.spaces.length > 0) {
const firstSpace = body.data.spaces[0];
log(`Found space: ${firstSpace.name} (${firstSpace.id})`);
// Try to get datasheets for this space
await testGetDatasheets(firstSpace.id, apiKey);
}
} else {
log('ERROR: API request failed', body);
}
} catch (error) {
log('ERROR: Exception during API request', error);
}
}
async function testGetDatasheets(spaceId, apiKey) {
log(`Testing retrieval of datasheets for space ${spaceId}...`);
try {
const url = `https://aitable.ai/fusion/v1/spaces/${spaceId}/nodes?type=Datasheet`;
log(`Making request to: ${url}`);
const headers = {
'Authorization': `Bearer ${apiKey}`,
'Accept': 'application/json',
'Content-Type': 'application/json',
'User-Agent': 'aitable-mcp-debug/0.1.0'
};
const response = await fetch(url, { headers });
log(`Response status: ${response.status} ${response.statusText}`);
const body = await response.json();
log('Response body (truncated):', {
success: body.success,
code: body.code,
message: body.message,
dataCount: body.data?.nodes?.length
});
if (response.ok && body.data && body.data.nodes) {
log(`SUCCESS: Found ${body.data.nodes.length} nodes`);
const datasheets = body.data.nodes.filter(node => node.type === 'Datasheet');
log(`Datasheets: ${datasheets.length}`);
for (const ds of datasheets.slice(0, 3)) { // Show first 3 datasheets only
log(`Datasheet: ${ds.name} (${ds.id})`);
}
} else {
log('ERROR: Failed to get datasheets', body);
}
} catch (error) {
log('ERROR: Exception during datasheet retrieval', error);
}
}
// Run the test
testDirectApiAccess().then(() => {
log('Test completed, see logs for details');
log(`Log file: ${logFile}`);
}).catch(err => {
log('Unhandled error during test', err);
});