Spaces:
Running
Running
File size: 33,250 Bytes
16a4dbd c3ae75f 16a4dbd f435ef9 e9d7b34 16a4dbd e9d7b34 c3ae75f 822cbdd 16a4dbd c3ae75f f435ef9 707bbb4 c3ae75f a5cf716 f435ef9 a5cf716 c3ae75f f435ef9 c3ae75f f435ef9 c3ae75f 16a4dbd f435ef9 16a4dbd c3ae75f f435ef9 c3ae75f 16a4dbd c3ae75f 16a4dbd c3ae75f 16a4dbd f435ef9 c3ae75f 16a4dbd c3ae75f 16a4dbd e9d7b34 f435ef9 16a4dbd c3ae75f 16a4dbd f435ef9 16a4dbd e9d7b34 16a4dbd c3ae75f e9d7b34 c3ae75f e9d7b34 c3ae75f e9d7b34 c3ae75f aea1786 c3ae75f aea1786 c3ae75f e9d7b34 d686137 e9d7b34 aea1786 e9d7b34 75d362b f435ef9 75d362b d686137 f435ef9 d686137 f435ef9 d686137 163eb99 f435ef9 163eb99 36dbece 163eb99 f435ef9 163eb99 eb54b0f f435ef9 eb54b0f 163eb99 f435ef9 163eb99 eb54b0f 163eb99 16a4dbd c3ae75f e9d7b34 d686137 163eb99 eb54b0f 16a4dbd d686137 94c2cc5 d686137 c3ae75f e9d7b34 c3ae75f eb54b0f c3ae75f eb54b0f c3ae75f 9faf78d eb54b0f c3ae75f e9d7b34 eb54b0f e9d7b34 c3ae75f e9d7b34 16a4dbd c3ae75f e9d7b34 c3ae75f e9d7b34 c3ae75f e9d7b34 c3ae75f 16a4dbd c3ae75f e9d7b34 c3ae75f e9d7b34 c3ae75f 33d698c c3ae75f 33d698c c3ae75f 33d698c c3ae75f e9d7b34 33d698c c3ae75f 33d698c e9d7b34 f435ef9 c3ae75f f435ef9 e9d7b34 f435ef9 c3ae75f f435ef9 c3ae75f f435ef9 e9d7b34 33d698c 7f6d612 e9d7b34 33d698c 75d362b 7f6d612 e9d7b34 c3ae75f e9d7b34 7f6d612 e9d7b34 75d362b 7f6d612 75d362b 7f6d612 e9d7b34 7f6d612 e9d7b34 7f6d612 e9d7b34 75d362b c3ae75f 75d362b 163eb99 eb54b0f 163eb99 36dbece 163eb99 36dbece 163eb99 eb54b0f 163eb99 36dbece 163eb99 36dbece 163eb99 eb54b0f f435ef9 eb54b0f f435ef9 eb54b0f f435ef9 eb54b0f 7f6d612 eb54b0f 7f6d612 eb54b0f 7f6d612 eb54b0f 7f6d612 eb54b0f 7f6d612 eb54b0f 7f6d612 eb54b0f 7f6d612 eb54b0f 7f6d612 eb54b0f 7f6d612 23ca656 7f6d612 23ca656 7f6d612 23ca656 eb54b0f 7f6d612 eb54b0f 7f6d612 eb54b0f 7f6d612 eb54b0f 7f6d612 eb54b0f 7f6d612 eb54b0f 7f6d612 eb54b0f 7f6d612 eb54b0f f435ef9 eb54b0f 7f6d612 eb54b0f 7f6d612 eb54b0f 7f6d612 eb54b0f 7f6d612 eb54b0f 7f6d612 eb54b0f 7f6d612 eb54b0f 16a4dbd c3ae75f 16a4dbd |
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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 |
#!/usr/bin/env node
// mcp-server.js - MCP Server for Reuben OS with Passkey System
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import {
CallToolRequestSchema,
ListToolsRequestSchema,
} from '@modelcontextprotocol/sdk/types.js';
import fetch from 'node-fetch';
const BASE_URL = 'https://mcp-1st-birthday-reuben-os.hf.space';
const API_ENDPOINT = `${BASE_URL}/api/mcp-handler`;
class ReubenOSMCPServer {
constructor() {
this.server = new Server(
{
name: 'reubenos-mcp-server',
version: '3.0.0',
description: 'MCP Server for Reuben OS with secure passkey-based file storage',
icon: 'π₯οΈ',
},
{
capabilities: {
tools: {},
},
}
);
this.setupToolHandlers();
// Error handling
this.server.onerror = (error) => console.error('[MCP Error]', error);
process.on('SIGINT', async () => {
await this.server.close();
process.exit(0);
});
}
setupToolHandlers() {
this.server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [
{
name: 'save_file',
description: 'Save a file to Reuben OS. Passkey is OPTIONAL - provide for secure storage or leave empty for public folder.',
inputSchema: {
type: 'object',
properties: {
fileName: {
type: 'string',
description: 'File name (e.g., main.dart, document.tex, report.pdf)',
},
content: {
type: 'string',
description: 'File content to save',
},
passkey: {
type: 'string',
description: 'OPTIONAL: Your passkey for secure storage (min 4 characters). Leave empty to save to public folder.',
},
isPublic: {
type: 'boolean',
description: 'Set to true to save to public folder (accessible to everyone). Default: false',
},
},
required: ['fileName', 'content'],
},
},
{
name: 'list_files',
description: 'List all files in your secure storage or public folder. Passkey is OPTIONAL.',
inputSchema: {
type: 'object',
properties: {
passkey: {
type: 'string',
description: 'OPTIONAL: Your passkey to list secure files. Leave empty to list public files.',
},
isPublic: {
type: 'boolean',
description: 'Set to true to list public files. Default: false',
},
},
},
},
{
name: 'delete_file',
description: 'Delete a specific file from your storage. Passkey is OPTIONAL.',
inputSchema: {
type: 'object',
properties: {
fileName: {
type: 'string',
description: 'Name of the file to delete',
},
passkey: {
type: 'string',
description: 'OPTIONAL: Your passkey (required for secure files). Leave empty for public files.',
},
isPublic: {
type: 'boolean',
description: 'Set to true if deleting from public folder. Default: false',
},
},
required: ['fileName'],
},
},
{
name: 'deploy_quiz',
description: 'Deploy an interactive quiz to Reuben OS Quiz App. Passkey is REQUIRED for all quizzes.',
inputSchema: {
type: 'object',
properties: {
passkey: {
type: 'string',
description: 'Your passkey for secure quiz storage (REQUIRED - min 4 characters)',
},
quizData: {
type: 'object',
description: 'Quiz configuration',
properties: {
title: { type: 'string', description: 'Quiz title' },
description: { type: 'string', description: 'Quiz description' },
questions: {
type: 'array',
description: 'Array of questions',
items: {
type: 'object',
properties: {
id: { type: 'string' },
type: { type: 'string', enum: ['multiple_choice'] },
question: { type: 'string' },
options: { type: 'array', items: { type: 'string' } },
explanation: { type: 'string' },
points: { type: 'number' },
},
required: ['id', 'question', 'type'],
},
},
timeLimit: {
type: 'number',
description: 'Time limit in minutes for the quiz (optional)',
},
},
required: ['title', 'questions'],
},
},
required: ['passkey', 'quizData'],
},
},
{
name: 'read_file',
description: 'Read the content of a file from secure storage or public folder. Passkey is OPTIONAL. Useful for evaluating quiz answers.',
inputSchema: {
type: 'object',
properties: {
fileName: {
type: 'string',
description: 'Name of the file to read (e.g., quiz_answers.json)',
},
passkey: {
type: 'string',
description: 'OPTIONAL: Your passkey (required for secure files). Leave empty for public files.',
},
isPublic: {
type: 'boolean',
description: 'Set to true if reading from public folder. Default: false',
},
},
required: ['fileName'],
},
},
{
name: 'generate_song_audio',
description: 'Generate an AI song with audio using ElevenLabs Music API. NO passkey required - saves directly to Voice Studio app.',
inputSchema: {
type: 'object',
properties: {
title: {
type: 'string',
description: 'Title of the song',
},
style: {
type: 'string',
description: 'Musical style/genre (e.g., "pop", "rock", "jazz", "ballad")',
},
lyrics: {
type: 'string',
description: 'Song lyrics (will be used to generate music)',
},
},
required: ['title', 'style', 'lyrics'],
},
},
{
name: 'generate_story_audio',
description: 'Generate audio narration for a story using ElevenLabs Text-to-Speech API. NO passkey required - saves directly to Voice Studio app.',
inputSchema: {
type: 'object',
properties: {
title: {
type: 'string',
description: 'Title of the story',
},
content: {
type: 'string',
description: 'Story content/text to be narrated (max 2000 characters for best performance)',
},
},
required: ['title', 'content'],
},
},
{
name: 'analyze_quiz',
description: 'Analyze quiz answers from quiz_answers.json against the quiz.json questions and provide feedback on correctness. Passkey is REQUIRED.',
inputSchema: {
type: 'object',
properties: {
passkey: {
type: 'string',
description: 'Your passkey for accessing the quiz files (REQUIRED)',
},
},
required: ['passkey'],
},
},
],
}));
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
try {
switch (name) {
case 'save_file':
return await this.saveFile(args);
case 'list_files':
return await this.listFiles(args);
case 'delete_file':
return await this.deleteFile(args);
case 'deploy_quiz':
return await this.deployQuiz(args);
case 'read_file':
return await this.readFile(args);
case 'generate_song_audio':
return await this.generateSongAudio(args);
case 'generate_story_audio':
return await this.generateStoryAudio(args);
case 'analyze_quiz':
return await this.analyzeQuiz(args);
default:
throw new Error(`Unknown tool: ${name}`);
}
} catch (error) {
return {
content: [
{
type: 'text',
text: `Error: ${error.message}`,
},
],
};
}
});
}
async readFile(args) {
try {
const { fileName, passkey, isPublic = false } = args;
if (!fileName) {
return {
content: [{ type: 'text', text: 'β fileName is required' }],
};
}
if (!isPublic && !passkey) {
return {
content: [{ type: 'text', text: 'β Passkey is required (or set isPublic=true)' }],
};
}
const ext = fileName.split('.').pop().toLowerCase();
const isDocument = ['pdf', 'docx', 'xlsx', 'xls', 'pptx'].includes(ext);
if (isDocument) {
// Use document processing endpoint
const processUrl = `${BASE_URL}/api/documents/process`;
const response = await fetch(processUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
fileName,
key: passkey,
isPublic,
operation: 'read'
})
});
const data = await response.json();
if (response.ok && data.success) {
let textContent = '';
if (data.content.text) {
textContent = data.content.text;
} else if (data.content.sheets) {
textContent = JSON.stringify(data.content.sheets, null, 2);
} else {
textContent = JSON.stringify(data.content, null, 2);
}
return {
content: [
{
type: 'text',
text: `π Content of ${fileName} (${data.content.type}):\n\n${textContent}`,
},
],
};
} else {
return {
content: [{ type: 'text', text: `β Failed to process document: ${data.error || 'Unknown error'}` }],
};
}
}
const url = new URL(API_ENDPOINT);
if (passkey) url.searchParams.set('passkey', passkey);
if (isPublic) url.searchParams.set('isPublic', 'true');
const response = await fetch(url, {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
});
const data = await response.json();
if (response.ok && data.success) {
const file = data.files.find(f => f.name === fileName);
if (!file) {
return {
content: [{ type: 'text', text: `β File '${fileName}' not found in ${data.location}` }],
};
}
return {
content: [
{
type: 'text',
text: `π Content of ${fileName}:\n\n${file.content || '(Empty file)'}`,
},
],
};
} else {
return {
content: [{ type: 'text', text: `β Failed: ${data.error || 'Unknown error'}` }],
};
}
} catch (error) {
return {
content: [{ type: 'text', text: `β Error: ${error.message}` }],
};
}
}
async saveFile(args) {
try {
const { fileName, content, passkey, isPublic = false } = args;
if (!fileName || content === undefined) {
return {
content: [{ type: 'text', text: 'β fileName and content are required' }],
};
}
if (!isPublic && !passkey) {
return {
content: [{ type: 'text', text: 'β Passkey is required for secure storage (or set isPublic=true)' }],
};
}
// Ensure content is properly stringified if it's an object
let fileContent = content;
if (typeof content === 'object' && content !== null) {
fileContent = JSON.stringify(content, null, 2);
}
// Handle special characters in content for code files
const ext = fileName.split('.').pop().toLowerCase();
const isCodeFile = ['dart', 'js', 'ts', 'jsx', 'tsx', 'py', 'java', 'cpp', 'c', 'h', 'hpp', 'cs', 'swift', 'kt', 'go', 'rs', 'rb', 'php'].includes(ext);
// Ensure content is a string
fileContent = String(fileContent);
const response = await fetch(API_ENDPOINT, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
passkey,
action: 'save_file',
fileName,
content: fileContent,
isPublic,
}),
});
const data = await response.json();
if (response.ok && data.success) {
const location = isPublic ? 'Public Folder' : `Secure Data (passkey: ${passkey})`;
const fileType = isCodeFile ? `(${ext.toUpperCase()} code file)` : '';
return {
content: [
{
type: 'text',
text: `β
File saved: ${fileName} ${fileType}\nπ Saved to: ${location}\nπ Size: ${(new TextEncoder().encode(fileContent).length / 1024).toFixed(2)} KB`,
},
],
};
} else {
return {
content: [{ type: 'text', text: `β Failed to save: ${data.error || 'Unknown error'}` }],
};
}
} catch (error) {
return {
content: [{ type: 'text', text: `β Error: ${error.message}` }],
};
}
}
async listFiles(args) {
try {
const { passkey, isPublic = false } = args;
if (!isPublic && !passkey) {
return {
content: [{ type: 'text', text: 'β Passkey is required (or set isPublic=true)' }],
};
}
const url = new URL(API_ENDPOINT);
if (passkey) url.searchParams.set('passkey', passkey);
if (isPublic) url.searchParams.set('isPublic', 'true');
const response = await fetch(url, {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
});
const data = await response.json();
if (response.ok && data.success) {
if (data.files.length === 0) {
return {
content: [{ type: 'text', text: `π No files found in ${data.location}` }],
};
}
const fileList = data.files
.map(f => `π ${f.name} (${(f.size / 1024).toFixed(2)} KB)${f.isQuiz ? ' [QUIZ]' : ''}`)
.join('\n');
return {
content: [
{
type: 'text',
text: `π Files in ${data.location}:\n\n${fileList}\n\nTotal: ${data.count} file(s)`,
},
],
};
} else {
return {
content: [{ type: 'text', text: `β Failed: ${data.error || 'Unknown error'}` }],
};
}
} catch (error) {
return {
content: [{ type: 'text', text: `β Error: ${error.message}` }],
};
}
}
async deleteFile(args) {
try {
const { fileName, passkey, isPublic = false } = args;
if (!fileName) {
return {
content: [{ type: 'text', text: 'β fileName is required' }],
};
}
if (!isPublic && !passkey) {
return {
content: [{ type: 'text', text: 'β Passkey is required (or set isPublic=true)' }],
};
}
const response = await fetch(API_ENDPOINT, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
passkey,
action: 'delete_file',
fileName,
isPublic,
content: '', // Required by API but not used
}),
});
const data = await response.json();
if (response.ok && data.success) {
return {
content: [{ type: 'text', text: `β
File '${fileName}' deleted successfully` }],
};
} else {
return {
content: [{ type: 'text', text: `β Failed: ${data.error || 'File not found'}` }],
};
}
} catch (error) {
return {
content: [{ type: 'text', text: `β Error: ${error.message}` }],
};
}
}
async deployQuiz(args) {
try {
const { quizData, passkey } = args;
// Passkey is REQUIRED for quizzes
if (!passkey || passkey.length < 4) {
return {
content: [{ type: 'text', text: 'β Passkey is REQUIRED for quizzes (minimum 4 characters)' }],
};
}
if (!quizData || !quizData.questions || quizData.questions.length === 0) {
return {
content: [{ type: 'text', text: 'β Quiz must have at least one question' }],
};
}
// Strip correct answers from questions before saving quiz.json
// This prevents users from seeing the answers in the quiz file
const questionsWithoutAnswers = quizData.questions.map(q => {
const { correctAnswer, correct, answer, ...questionWithoutAnswer } = q;
return questionWithoutAnswer;
});
// Save quiz.json WITHOUT correct answers (for users to take)
const quizForUsers = {
title: quizData.title,
description: quizData.description,
timeLimit: quizData.timeLimit,
questions: questionsWithoutAnswers,
createdAt: new Date().toISOString(),
version: '1.0',
};
// Save quiz_key.json WITH correct answers (for grading) - hidden file
const quizAnswerKey = {
title: quizData.title,
questions: quizData.questions.map(q => ({
id: q.id,
correctAnswer: q.correctAnswer || q.correct || q.answer,
explanation: q.explanation,
points: q.points || 1,
})),
createdAt: new Date().toISOString(),
};
// Save quiz.json (without answers)
const quizResponse = await fetch(API_ENDPOINT, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
passkey,
action: 'deploy_quiz',
fileName: 'quiz.json',
content: JSON.stringify(quizForUsers, null, 2),
isPublic: false,
}),
});
const quizResult = await quizResponse.json();
if (!quizResponse.ok || !quizResult.success) {
return {
content: [{ type: 'text', text: `β Failed to save quiz: ${quizResult.error || 'Unknown error'}` }],
};
}
// Save quiz_key.json (with correct answers for grading)
const keyResponse = await fetch(API_ENDPOINT, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
passkey,
action: 'save_file',
fileName: 'quiz_key.json',
content: JSON.stringify(quizAnswerKey, null, 2),
isPublic: false,
}),
});
const keyResult = await keyResponse.json();
if (!keyResponse.ok || !keyResult.success) {
console.error('Failed to save quiz key:', keyResult.error);
// Don't fail the whole operation, just log it
}
const totalPoints = quizData.questions.reduce((sum, q) => sum + (q.points || 1), 0);
return {
content: [
{
type: 'text',
text: `β
Quiz deployed: ${quizData.title}\nπ ${quizData.questions.length} questions, ${totalPoints} points\nπ Secured with passkey: ${passkey}\nπ Answer key saved separately for grading`,
},
],
};
} catch (error) {
return {
content: [{ type: 'text', text: `β Error: ${error.message}` }],
};
}
}
async generateSongAudio(args) {
try {
const { title, style, lyrics } = args;
// Use a default passkey internally - users don't need to provide it
const passkey = 'voice_default';
if (!title || !style || !lyrics) {
return {
content: [{ type: 'text', text: 'β title, style, and lyrics are required' }],
};
}
const response = await fetch(`${BASE_URL}/api/voice/generate-song`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ title, style, lyrics, passkey }),
});
const data = await response.json();
if (response.ok && data.success) {
// Save the content to server storage
await fetch(`${BASE_URL}/api/voice/save`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
passkey,
content: data.content,
}),
});
return {
content: [
{
type: 'text',
text: `π΅ Song "${title}" generated successfully!\\nπΈ Style: ${style}\\nπ± Open the Voice Studio app to listen to your song!`,
},
],
};
} else {
return {
content: [{ type: 'text', text: `β Failed to generate song: ${data.error || 'Unknown error'}` }],
};
}
} catch (error) {
return {
content: [{ type: 'text', text: `β Error: ${error.message}` }],
};
}
}
async generateStoryAudio(args) {
try {
const { title, content } = args;
// Use a default passkey internally - users don't need to provide it
const passkey = 'voice_default';
if (!title || !content) {
return {
content: [{ type: 'text', text: 'β title and content are required' }],
};
}
const response = await fetch(`${BASE_URL}/api/voice/generate-story`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ title, content, passkey }),
});
const data = await response.json();
if (response.ok && data.success) {
// Save the content to server storage
await fetch(`${BASE_URL}/api/voice/save`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
passkey,
content: data.content,
}),
});
return {
content: [
{
type: 'text',
text: `π Story "${title}" audio generated successfully!\\nπ± Open the Voice Studio app to listen to your story!`,
},
],
};
} else {
return {
content: [{ type: 'text', text: `β Failed to generate story audio: ${data.error || 'Unknown error'}` }],
};
}
} catch (error) {
return {
content: [{ type: 'text', text: `β Error: ${error.message}` }],
};
}
}
async analyzeQuiz(args) {
try {
const { passkey } = args;
// Passkey is REQUIRED for quiz analysis
if (!passkey || passkey.length < 4) {
return {
content: [{ type: 'text', text: 'β Passkey is REQUIRED to access quiz files (minimum 4 characters)' }],
};
}
// Read all files from secure storage
const quizUrl = new URL(API_ENDPOINT);
quizUrl.searchParams.set('passkey', passkey);
console.log('Fetching quiz files from:', quizUrl.toString());
const quizResponse = await fetch(quizUrl, {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
});
const quizData = await quizResponse.json();
console.log('API Response:', JSON.stringify(quizData, null, 2).substring(0, 500));
if (!quizResponse.ok || !quizData.success) {
return {
content: [{ type: 'text', text: `β Failed to read files: ${quizData.error || 'Unknown error'}` }],
};
}
if (!quizData.files || quizData.files.length === 0) {
return {
content: [{ type: 'text', text: 'β No files found for this passkey. Make sure the quiz has been deployed and answered.' }],
};
}
// Find quiz.json (questions without answers)
const quizFile = quizData.files.find(f => f.name === 'quiz.json');
if (!quizFile) {
return {
content: [{ type: 'text', text: `β quiz.json not found. Available files: ${quizData.files.map(f => f.name).join(', ')}` }],
};
}
if (!quizFile.content) {
return {
content: [{ type: 'text', text: 'β quiz.json exists but content is empty or could not be read' }],
};
}
// Find quiz_key.json (correct answers for grading)
// For backward compatibility, if quiz_key.json doesn't exist, try to get answers from quiz.json
const keyFile = quizData.files.find(f => f.name === 'quiz_key.json');
const hasAnswerKey = keyFile && keyFile.content;
// For backward compatibility: if no quiz_key.json exists, we'll need to get answers from quiz.json (old format)
// This happens for quizzes created before the answer-key separation was implemented
// Find quiz_answers.json (user's answers)
const answersFile = quizData.files.find(f => f.name === 'quiz_answers.json');
if (!answersFile) {
return {
content: [{ type: 'text', text: `β quiz_answers.json not found. The user needs to complete the quiz first. Available files: ${quizData.files.map(f => f.name).join(', ')}` }],
};
}
if (!answersFile.content) {
return {
content: [{ type: 'text', text: 'β quiz_answers.json exists but content is empty or could not be read' }],
};
}
console.log('Quiz file content length:', quizFile.content?.length);
console.log('Key file found:', !!keyFile, 'content length:', keyFile?.content?.length);
console.log('Answers file content length:', answersFile.content?.length);
// Parse the quiz, answer key, and user answers
let quiz, answerKey, userAnswersData;
try {
quiz = typeof quizFile.content === 'string' ? JSON.parse(quizFile.content) : quizFile.content;
} catch (e) {
return {
content: [{ type: 'text', text: `β Failed to parse quiz.json: ${e.message}` }],
};
}
// Parse answer key (from quiz_key.json if exists, or fall back to quiz.json for backward compatibility)
if (hasAnswerKey) {
try {
answerKey = typeof keyFile.content === 'string' ? JSON.parse(keyFile.content) : keyFile.content;
} catch (e) {
return {
content: [{ type: 'text', text: `β Failed to parse quiz_key.json: ${e.message}` }],
};
}
} else {
// Backward compatibility: create answer key from quiz.json (old format had answers in quiz.json)
console.log('No quiz_key.json found - using backward compatibility mode with quiz.json');
answerKey = {
title: quiz.title,
questions: (quiz.questions || []).map(q => ({
id: q.id,
correctAnswer: q.correctAnswer || q.correct || q.answer,
explanation: q.explanation,
points: q.points || 1,
})),
};
}
try {
userAnswersData = typeof answersFile.content === 'string' ? JSON.parse(answersFile.content) : answersFile.content;
} catch (e) {
return {
content: [{ type: 'text', text: `β Failed to parse quiz_answers.json: ${e.message}` }],
};
}
console.log('Parsed quiz:', quiz.title, 'Questions:', quiz.questions?.length);
console.log('Parsed answer key:', answerKey.questions?.length, 'answers');
console.log('Parsed user answers:', JSON.stringify(userAnswersData, null, 2).substring(0, 300));
// Convert user answers array to object for easier lookup
const userAnswers = {};
if (userAnswersData.answers && Array.isArray(userAnswersData.answers)) {
// QuizApp format: { answers: [{ questionId, answer }], metadata: {...} }
userAnswersData.answers.forEach(item => {
userAnswers[item.questionId] = item.answer;
});
} else if (typeof userAnswersData === 'object') {
// Direct object format
Object.assign(userAnswers, userAnswersData);
}
// Convert answer key to object for easier lookup
const correctAnswers = {};
const explanations = {};
const pointsMap = {};
if (answerKey.questions && Array.isArray(answerKey.questions)) {
answerKey.questions.forEach(q => {
correctAnswers[q.id] = q.correctAnswer;
explanations[q.id] = q.explanation;
pointsMap[q.id] = q.points || 1;
});
}
console.log('Processed user answers:', userAnswers);
console.log('Processed correct answers:', correctAnswers);
// Analyze the answers
let correctCount = 0;
let totalPoints = 0;
let maxPoints = 0;
const feedback = [];
const questionsArray = quiz.questions || [];
if (questionsArray.length === 0) {
return {
content: [{ type: 'text', text: 'β Quiz has no questions to analyze' }],
};
}
questionsArray.forEach((question, index) => {
const questionId = question.id || `question_${index}`;
const userAnswer = userAnswers[questionId];
const correctAnswer = correctAnswers[questionId];
const explanation = explanations[questionId];
const points = pointsMap[questionId] || 1;
maxPoints += points;
if (!userAnswer) {
feedback.push(`β οΈ Q${index + 1}: "${question.question.substring(0, 50)}..." - Not answered`);
return;
}
if (!correctAnswer) {
// No correct answer in key - just show the user's answer for manual review
feedback.push(`π Q${index + 1}: "${question.question.substring(0, 50)}..." \n User answered: "${userAnswer}" \n (No correct answer in key - manual review needed)`);
return;
}
// Check if answer is correct
let isCorrect = false;
if (Array.isArray(correctAnswer)) {
isCorrect = correctAnswer.some(ca =>
String(ca).toLowerCase().trim() === String(userAnswer).toLowerCase().trim()
);
} else {
isCorrect = String(correctAnswer).toLowerCase().trim() === String(userAnswer).toLowerCase().trim();
}
if (isCorrect) {
correctCount++;
totalPoints += points;
feedback.push(`β
Q${index + 1}: Correct! (+${points} pts)`);
} else {
feedback.push(`β Q${index + 1}: "${question.question.substring(0, 40)}..."\n Your answer: "${userAnswer}"\n Correct: "${correctAnswer}"${explanation ? `\n π‘ ${explanation}` : ''}`);
}
});
const percentage = maxPoints > 0 ? Math.round((totalPoints / maxPoints) * 100) : 0;
const grade = percentage >= 90 ? 'A' :
percentage >= 80 ? 'B' :
percentage >= 70 ? 'C' :
percentage >= 60 ? 'D' : 'F';
// Include metadata if available
let metadataInfo = '';
if (userAnswersData.metadata) {
const meta = userAnswersData.metadata;
metadataInfo = `\nβ±οΈ Time taken: ${Math.floor((meta.timeTakenSeconds || 0) / 60)}m ${(meta.timeTakenSeconds || 0) % 60}s`;
if (meta.timeExceeded) {
metadataInfo += ' (Time limit exceeded!)';
}
metadataInfo += `\nπ
Completed: ${meta.completedAt || 'Unknown'}`;
}
return {
content: [
{
type: 'text',
text: `π Quiz Analysis Results for "${quiz.title || 'Untitled Quiz'}"
π Score: ${totalPoints}/${maxPoints} points (${percentage}%)
β
Correct: ${correctCount}/${questionsArray.length} questions
π― Grade: ${grade}${metadataInfo}
π Detailed Feedback:
${feedback.join('\n\n')}
${percentage >= 70 ? 'π Great job!' : 'π Keep studying and try again!'}`,
},
],
};
} catch (error) {
console.error('analyzeQuiz error:', error);
return {
content: [{ type: 'text', text: `β Error analyzing quiz: ${error.message}\n\nStack: ${error.stack}` }],
};
}
}
async run() {
const transport = new StdioServerTransport();
await this.server.connect(transport);
console.error('ReubenOS MCP Server running with passkey authentication...');
}
}
const server = new ReubenOSMCPServer();
server.run(); |