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
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
"""
Advanced Automated Content Generation tools for TutorX.
Generates interactive content, exercises, and adaptive learning materials.
"""

import json
import uuid
from datetime import datetime
from typing import Dict, Any, List, Optional
from mcp_server.mcp_instance import mcp
from mcp_server.model.gemini_flash import GeminiFlash

MODEL = GeminiFlash()

def extract_json_from_text(text: str):
    """Extract JSON from text response"""
    import re
    if not text or not isinstance(text, str):
        return None
    # Remove code fences
    text = re.sub(r'^\s*```(?:json)?\s*', '', text, flags=re.IGNORECASE)
    text = re.sub(r'\s*```\s*$', '', text, flags=re.IGNORECASE)
    text = text.strip()
    # Remove trailing commas
    text = re.sub(r',([ \t\r\n]*[}}\]])', r'\1', text)
    return json.loads(text)

@mcp.tool()
async def generate_interactive_exercise(concept: str, exercise_type: str = "problem_solving",
                                      difficulty_level: float = 0.5, 
                                      student_level: str = "intermediate") -> dict:
    """
    Generate interactive exercises with multiple components and adaptive features.
    
    Args:
        concept: The concept to create exercises for
        exercise_type: Type of exercise ('problem_solving', 'simulation', 'case_study', 'lab', 'project')
        difficulty_level: Difficulty level (0.0 to 1.0)
        student_level: Student's academic level
        
    Returns:
        Dictionary with interactive exercise content
    """
    try:
        prompt = f"""
        Create an interactive {exercise_type} exercise for the concept: "{concept}"
        
        Exercise Parameters:
        - Difficulty Level: {difficulty_level:.2f}/1.0
        - Student Level: {student_level}
        - Exercise Type: {exercise_type}
        
        Generate a comprehensive interactive exercise that includes:
        1. Clear learning objectives
        2. Step-by-step instructions
        3. Interactive components that engage the student
        4. Multiple difficulty levels within the exercise
        5. Real-time feedback mechanisms
        6. Assessment criteria
        7. Extension activities for advanced students
        
        Return JSON with:
        - "exercise_id": unique identifier
        - "title": engaging title for the exercise
        - "learning_objectives": list of specific learning goals
        - "introduction": engaging introduction to the exercise
        - "instructions": detailed step-by-step instructions
        - "interactive_components": list of interactive elements
        - "materials_needed": required materials or resources
        - "estimated_time": estimated completion time in minutes
        - "difficulty_variations": easier and harder versions
        - "assessment_rubric": criteria for evaluating student work
        - "feedback_prompts": questions for self-reflection
        - "extension_activities": additional challenges for advanced students
        - "real_world_connections": how this relates to real-world applications
        """
        
        response = await MODEL.generate_text(prompt, temperature=0.7)
        exercise_data = extract_json_from_text(response)
        
        # Add metadata
        exercise_data.update({
            "success": True,
            "concept": concept,
            "exercise_type": exercise_type,
            "difficulty_level": difficulty_level,
            "student_level": student_level,
            "generated_at": datetime.utcnow().isoformat(),
            "ai_generated": True
        })
        
        return exercise_data
        
    except Exception as e:
        return {
            "success": False,
            "error": f"Failed to generate interactive exercise: {str(e)}"
        }

@mcp.tool()
async def generate_adaptive_content_sequence(topic: str, student_profile: dict,
                                           sequence_length: int = 5) -> dict:
    """
    Generate a sequence of adaptive content that adjusts based on student profile.
    
    Args:
        topic: Main topic for the content sequence
        student_profile: Student's learning profile and preferences
        sequence_length: Number of content pieces in the sequence
        
    Returns:
        Dictionary with adaptive content sequence
    """
    try:
        prompt = f"""
        Create an adaptive content sequence for the topic: "{topic}"
        
        Student Profile:
        {json.dumps(student_profile, indent=2)}
        
        Generate a sequence of {sequence_length} interconnected content pieces that:
        1. Build upon each other progressively
        2. Adapt to the student's learning style and preferences
        3. Include multiple content types (text, visual descriptions, interactive elements)
        4. Provide branching paths based on understanding
        5. Include assessment checkpoints
        6. Offer remediation and enrichment options
        
        Return JSON with:
        - "sequence_id": unique identifier for the sequence
        - "topic": main topic
        - "total_pieces": number of content pieces
        - "estimated_total_time": total estimated time in minutes
        - "content_sequence": array of content pieces, each with:
          - "piece_id": unique identifier
          - "title": content title
          - "content_type": type of content (explanation, example, practice, assessment)
          - "content": main content text
          - "visual_elements": descriptions of visual components
          - "interactive_elements": interactive components
          - "difficulty_level": difficulty rating
          - "prerequisites": what student should know
          - "learning_objectives": specific objectives for this piece
          - "assessment_questions": questions to check understanding
          - "branching_options": different paths based on performance
          - "estimated_time": time for this piece
        - "adaptation_rules": rules for how content adapts to student responses
        - "success_criteria": criteria for successful completion
        """
        
        response = await MODEL.generate_text(prompt, temperature=0.6)
        sequence_data = extract_json_from_text(response)
        
        # Add metadata
        sequence_data.update({
            "success": True,
            "topic": topic,
            "student_profile": student_profile,
            "generated_at": datetime.utcnow().isoformat(),
            "ai_generated": True
        })
        
        return sequence_data
        
    except Exception as e:
        return {
            "success": False,
            "error": f"Failed to generate adaptive content sequence: {str(e)}"
        }

@mcp.tool()
async def generate_scenario_based_learning(concept: str, scenario_type: str = "real_world",
                                         complexity_level: str = "moderate") -> dict:
    """
    Generate scenario-based learning content with realistic situations.
    
    Args:
        concept: The concept to teach through scenarios
        scenario_type: Type of scenario ('real_world', 'historical', 'futuristic', 'problem_solving')
        complexity_level: Complexity of the scenario ('simple', 'moderate', 'complex')
        
    Returns:
        Dictionary with scenario-based learning content
    """
    try:
        prompt = f"""
        Create a {scenario_type} scenario-based learning experience for: "{concept}"
        
        Scenario Parameters:
        - Type: {scenario_type}
        - Complexity: {complexity_level}
        
        Design a realistic scenario that:
        1. Presents the concept in a meaningful context
        2. Requires students to apply their knowledge
        3. Includes decision points and consequences
        4. Provides multiple solution paths
        5. Connects to real-world applications
        6. Engages students emotionally and intellectually
        
        Return JSON with:
        - "scenario_id": unique identifier
        - "title": compelling scenario title
        - "setting": detailed description of the scenario setting
        - "background_story": engaging background narrative
        - "main_challenge": primary challenge students must solve
        - "characters": key characters in the scenario (if applicable)
        - "decision_points": critical decisions students must make
        - "possible_outcomes": different outcomes based on decisions
        - "learning_integration": how the concept is woven into the scenario
        - "assessment_criteria": how to evaluate student responses
        - "discussion_questions": questions for reflection and discussion
        - "extension_scenarios": related scenarios for further exploration
        - "real_world_connections": connections to actual situations
        - "resources": additional resources students might need
        """
        
        response = await MODEL.generate_text(prompt, temperature=0.8)
        scenario_data = extract_json_from_text(response)
        
        # Add metadata
        scenario_data.update({
            "success": True,
            "concept": concept,
            "scenario_type": scenario_type,
            "complexity_level": complexity_level,
            "generated_at": datetime.utcnow().isoformat(),
            "ai_generated": True
        })
        
        return scenario_data
        
    except Exception as e:
        return {
            "success": False,
            "error": f"Failed to generate scenario-based learning: {str(e)}"
        }

@mcp.tool()
async def generate_multimodal_content(concept: str, modalities: List[str] = None,
                                    target_audience: str = "general") -> dict:
    """
    Generate multi-modal content that appeals to different learning styles.
    
    Args:
        concept: The concept to create content for
        modalities: List of modalities to include ('visual', 'auditory', 'kinesthetic', 'reading')
        target_audience: Target audience description
        
    Returns:
        Dictionary with multi-modal content
    """
    try:
        if not modalities:
            modalities = ["visual", "auditory", "kinesthetic", "reading"]
            
        prompt = f"""
        Create multi-modal content for the concept: "{concept}"
        
        Target Audience: {target_audience}
        Modalities to include: {modalities}
        
        Generate content that appeals to different learning styles:
        
        Return JSON with:
        - "content_id": unique identifier
        - "concept": the main concept
        - "visual_content": content for visual learners (descriptions of diagrams, charts, images)
        - "auditory_content": content for auditory learners (descriptions of sounds, music, verbal explanations)
        - "kinesthetic_content": content for kinesthetic learners (hands-on activities, movement, manipulation)
        - "reading_content": content for reading/writing learners (text-based materials, written exercises)
        - "integrated_activities": activities that combine multiple modalities
        - "accessibility_features": features for students with different abilities
        - "technology_integration": how technology can enhance each modality
        - "assessment_variations": different ways to assess understanding for each modality
        - "adaptation_guidelines": how to adapt content for different needs
        """
        
        response = await MODEL.generate_text(prompt, temperature=0.7)
        content_data = extract_json_from_text(response)
        
        # Add metadata
        content_data.update({
            "success": True,
            "concept": concept,
            "modalities": modalities,
            "target_audience": target_audience,
            "generated_at": datetime.utcnow().isoformat(),
            "ai_generated": True
        })
        
        return content_data
        
    except Exception as e:
        return {
            "success": False,
            "error": f"Failed to generate multimodal content: {str(e)}"
        }

@mcp.tool()
async def generate_adaptive_assessment(concept: str, assessment_type: str = "formative",
                                     student_data: dict = None) -> dict:
    """
    Generate adaptive assessments that adjust based on student responses.

    Args:
        concept: The concept to assess
        assessment_type: Type of assessment ('formative', 'summative', 'diagnostic', 'peer')
        student_data: Optional student performance data

    Returns:
        Dictionary with adaptive assessment content
    """
    try:
        prompt = f"""
        Create an adaptive {assessment_type} assessment for: "{concept}"

        Student Data: {json.dumps(student_data or {}, indent=2)}

        Design an assessment that:
        1. Adapts difficulty based on student responses
        2. Provides immediate feedback
        3. Identifies learning gaps
        4. Offers multiple question types
        5. Includes branching logic
        6. Provides detailed analytics

        Return JSON with:
        - "assessment_id": unique identifier
        - "title": assessment title
        - "instructions": clear instructions for students
        - "question_pool": large pool of questions with metadata:
          - "question_id": unique identifier
          - "question_text": the question
          - "question_type": type (multiple_choice, short_answer, essay, etc.)
          - "difficulty_level": difficulty rating (0.0 to 1.0)
          - "cognitive_level": Bloom's taxonomy level
          - "options": answer options (if applicable)
          - "correct_answer": correct answer
          - "explanation": detailed explanation
          - "hints": progressive hints
          - "common_misconceptions": common wrong answers and why
        - "adaptive_rules": rules for question selection and difficulty adjustment
        - "feedback_templates": templates for different types of feedback
        - "scoring_rubric": detailed scoring criteria
        - "analytics_tracking": what data to track for analysis
        - "remediation_suggestions": suggestions based on performance
        """

        response = await MODEL.generate_text(prompt, temperature=0.6)
        assessment_data = extract_json_from_text(response)

        # Add metadata
        assessment_data.update({
            "success": True,
            "concept": concept,
            "assessment_type": assessment_type,
            "generated_at": datetime.utcnow().isoformat(),
            "ai_generated": True
        })

        return assessment_data

    except Exception as e:
        return {
            "success": False,
            "error": f"Failed to generate adaptive assessment: {str(e)}"
        }

@mcp.tool()
async def generate_gamified_content(concept: str, game_type: str = "quest",
                                  target_age_group: str = "teen") -> dict:
    """
    Generate gamified learning content with game mechanics and engagement features.

    Args:
        concept: The concept to gamify
        game_type: Type of game ('quest', 'puzzle', 'simulation', 'competition', 'story')
        target_age_group: Target age group ('child', 'teen', 'adult')

    Returns:
        Dictionary with gamified content
    """
    try:
        prompt = f"""
        Create gamified learning content for: "{concept}"

        Game Parameters:
        - Game Type: {game_type}
        - Target Age Group: {target_age_group}

        Design engaging game-based learning that includes:
        1. Clear game objectives aligned with learning goals
        2. Progressive difficulty levels
        3. Reward systems and achievements
        4. Interactive challenges
        5. Narrative elements (if applicable)
        6. Social features for collaboration/competition

        Return JSON with:
        - "game_id": unique identifier
        - "title": engaging game title
        - "theme": game theme and setting
        - "storyline": narrative framework (if applicable)
        - "learning_objectives": educational goals
        - "game_mechanics": core game mechanics and rules
        - "levels": progressive levels with increasing difficulty
        - "challenges": specific challenges and tasks
        - "reward_system": points, badges, achievements
        - "character_progression": how players advance
        - "social_features": multiplayer or collaborative elements
        - "assessment_integration": how learning is assessed within the game
        - "feedback_mechanisms": how players receive feedback
        - "customization_options": ways to personalize the experience
        - "accessibility_features": features for different abilities
        """

        response = await MODEL.generate_text(prompt, temperature=0.8)
        game_data = extract_json_from_text(response)

        # Add metadata
        game_data.update({
            "success": True,
            "concept": concept,
            "game_type": game_type,
            "target_age_group": target_age_group,
            "generated_at": datetime.utcnow().isoformat(),
            "ai_generated": True
        })

        return game_data

    except Exception as e:
        return {
            "success": False,
            "error": f"Failed to generate gamified content: {str(e)}"
        }

@mcp.tool()
async def validate_generated_content(content_data: dict, validation_criteria: dict = None) -> dict:
    """
    Validate and quality-check generated educational content.

    Args:
        content_data: The content to validate
        validation_criteria: Specific criteria for validation

    Returns:
        Dictionary with validation results and suggestions
    """
    try:
        default_criteria = {
            "educational_alignment": True,
            "age_appropriateness": True,
            "clarity": True,
            "engagement": True,
            "accessibility": True,
            "accuracy": True
        }

        criteria = {**default_criteria, **(validation_criteria or {})}

        prompt = f"""
        Validate this educational content against quality criteria:

        Content to Validate:
        {json.dumps(content_data, indent=2)}

        Validation Criteria:
        {json.dumps(criteria, indent=2)}

        Perform comprehensive validation and return JSON with:
        - "overall_quality_score": score from 0.0 to 1.0
        - "validation_results": detailed results for each criterion
        - "strengths": identified strengths in the content
        - "areas_for_improvement": specific areas that need work
        - "suggestions": concrete suggestions for improvement
        - "compliance_check": compliance with educational standards
        - "accessibility_assessment": accessibility for different learners
        - "engagement_analysis": analysis of engagement potential
        - "accuracy_verification": verification of factual accuracy
        - "recommended_modifications": specific modifications to make
        """

        response = await MODEL.generate_text(prompt, temperature=0.3)
        validation_data = extract_json_from_text(response)

        return {
            "success": True,
            "content_validated": True,
            "validation_timestamp": datetime.utcnow().isoformat(),
            **validation_data
        }

    except Exception as e:
        return {
            "success": False,
            "error": f"Failed to validate content: {str(e)}"
        }