Spaces:
Running
Running
| // Test script for MCP handler API | |
| // Run with: node test-mcp-endpoint.js | |
| const BASE_URL = 'https://mcp-1st-birthday-reuben-os.hf.space'; | |
| const API_URL = `${BASE_URL}/api/mcp-handler`; | |
| async function testMCPEndpoint() { | |
| console.log('π§ͺ Testing MCP Handler API on Hugging Face...\n'); | |
| // Test 1: GET without passkey (should fail with error) | |
| console.log('Test 1: GET without passkey'); | |
| try { | |
| const response = await fetch(API_URL); | |
| const data = await response.json(); | |
| console.log('β Response:', JSON.stringify(data, null, 2)); | |
| console.log('Expected error message: β\n'); | |
| } catch (error) { | |
| console.log('β Error:', error.message, '\n'); | |
| } | |
| // Test 2: POST - Save a test file to public folder | |
| console.log('Test 2: POST - Save public file'); | |
| try { | |
| const response = await fetch(API_URL, { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ | |
| action: 'save_file', | |
| fileName: 'test.txt', | |
| content: 'Hello from MCP test!', | |
| isPublic: true, | |
| }), | |
| }); | |
| const data = await response.json(); | |
| console.log('β Response:', JSON.stringify(data, null, 2)); | |
| if (data.success) { | |
| console.log('β File saved successfully to public folder!\n'); | |
| } else { | |
| console.log('β Save failed:', data.error, '\n'); | |
| } | |
| } catch (error) { | |
| console.log('β Error:', error.message, '\n'); | |
| } | |
| // Test 3: GET - List public files | |
| console.log('Test 3: GET - List public files'); | |
| try { | |
| const url = new URL(API_URL); | |
| url.searchParams.set('isPublic', 'true'); | |
| const response = await fetch(url); | |
| const data = await response.json(); | |
| console.log('β Response:', JSON.stringify(data, null, 2)); | |
| if (data.success) { | |
| console.log(`β Found ${data.count} public file(s)\n`); | |
| } | |
| } catch (error) { | |
| console.log('β Error:', error.message, '\n'); | |
| } | |
| // Test 4: POST - Save with passkey | |
| console.log('Test 4: POST - Save with passkey'); | |
| const testPasskey = 'test-passkey-123'; | |
| try { | |
| const response = await fetch(API_URL, { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ | |
| passkey: testPasskey, | |
| action: 'save_file', | |
| fileName: 'secure_test.dart', | |
| content: `// Flutter test file\nvoid main() {\n print('Hello from secure storage!');\n}`, | |
| isPublic: false, | |
| }), | |
| }); | |
| const data = await response.json(); | |
| console.log('β Response:', JSON.stringify(data, null, 2)); | |
| if (data.success) { | |
| console.log('β File saved successfully to secure storage!\n'); | |
| } | |
| } catch (error) { | |
| console.log('β Error:', error.message, '\n'); | |
| } | |
| // Test 5: GET - List files with passkey | |
| console.log('Test 5: GET - List files with passkey'); | |
| try { | |
| const url = new URL(API_URL); | |
| url.searchParams.set('passkey', testPasskey); | |
| const response = await fetch(url); | |
| const data = await response.json(); | |
| console.log('β Response:', JSON.stringify(data, null, 2)); | |
| if (data.success) { | |
| console.log(`β Found ${data.count} file(s) for passkey: ${testPasskey}\n`); | |
| } | |
| } catch (error) { | |
| console.log('β Error:', error.message, '\n'); | |
| } | |
| // Test 6: Deploy a test quiz | |
| console.log('Test 6: POST - Deploy quiz'); | |
| try { | |
| const quizData = { | |
| title: 'Test Quiz', | |
| description: 'A test quiz from MCP', | |
| questions: [ | |
| { | |
| id: 'q1', | |
| question: 'What is 2+2?', | |
| type: 'multiple-choice', | |
| options: ['3', '4', '5', '6'], | |
| correctAnswer: 1, | |
| points: 1, | |
| }, | |
| ], | |
| }; | |
| const response = await fetch(API_URL, { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify({ | |
| passkey: testPasskey, | |
| action: 'deploy_quiz', | |
| fileName: 'quiz.json', | |
| content: JSON.stringify(quizData, null, 2), | |
| isPublic: false, | |
| }), | |
| }); | |
| const data = await response.json(); | |
| console.log('β Response:', JSON.stringify(data, null, 2)); | |
| if (data.success) { | |
| console.log('β Quiz deployed successfully!\n'); | |
| } | |
| } catch (error) { | |
| console.log('β Error:', error.message, '\n'); | |
| } | |
| console.log('π All tests completed!\n'); | |
| console.log('Summary:'); | |
| console.log('- MCP Handler API is accessible'); | |
| console.log('- Public file uploads work'); | |
| console.log('- Passkey-protected uploads work'); | |
| console.log('- File listing works'); | |
| console.log('- Quiz deployment works'); | |
| console.log('\nβ Claude Desktop can now upload files to ReubenOS!'); | |
| } | |
| // Run tests | |
| testMCPEndpoint().catch(console.error); | |