File size: 14,515 Bytes
f9f5b1d
 
 
 
 
 
 
def69a7
 
f9f5b1d
 
 
def69a7
f9f5b1d
def69a7
 
d4df2a7
def69a7
f9f5b1d
d4df2a7
 
 
f9f5b1d
 
 
def69a7
f9f5b1d
def69a7
 
 
 
 
 
 
 
 
 
 
f9f5b1d
bbd9cd6
f9f5b1d
 
 
 
 
 
bbd9cd6
f9f5b1d
 
 
 
def69a7
f9f5b1d
d4df2a7
bbd9cd6
 
 
 
 
 
 
 
 
 
 
 
 
def69a7
f9f5b1d
 
 
 
 
bbd9cd6
 
 
 
 
 
 
 
 
 
 
def69a7
f9f5b1d
 
 
 
 
 
 
 
 
def69a7
f9f5b1d
d4df2a7
 
 
 
 
 
 
 
 
 
 
 
 
def69a7
f9f5b1d
 
 
 
 
def69a7
 
 
 
 
 
 
 
 
 
d4df2a7
def69a7
 
 
d4df2a7
 
def69a7
 
f9f5b1d
 
bbd9cd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
def69a7
bbd9cd6
 
f9f5b1d
def69a7
f9f5b1d
def69a7
f9f5b1d
def69a7
f9f5b1d
def69a7
f9f5b1d
 
 
 
def69a7
f9f5b1d
def69a7
f9f5b1d
 
 
 
 
 
def69a7
f9f5b1d
def69a7
f9f5b1d
 
 
def69a7
f9f5b1d
def69a7
f9f5b1d
def69a7
f9f5b1d
def69a7
f9f5b1d
 
 
 
def69a7
f9f5b1d
def69a7
f9f5b1d
 
 
 
 
 
 
def69a7
f9f5b1d
def69a7
f9f5b1d
def69a7
f9f5b1d
def69a7
f9f5b1d
 
 
def69a7
f9f5b1d
def69a7
f9f5b1d
 
 
 
 
 
def69a7
f9f5b1d
def69a7
f9f5b1d
 
 
 
def69a7
f9f5b1d
def69a7
f9f5b1d
 
 
 
def69a7
f9f5b1d
def69a7
f9f5b1d
 
 
 
 
 
def69a7
f9f5b1d
def69a7
f9f5b1d
 
 
 
 
def69a7
f9f5b1d
def69a7
f9f5b1d
 
 
 
 
def69a7
f9f5b1d
def69a7
f9f5b1d
 
def69a7
 
 
f9f5b1d
def69a7
f9f5b1d
 
 
def69a7
bbd9cd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
def69a7
 
 
 
 
f9f5b1d
bbd9cd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f9f5b1d
 
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
"""
MCP client for interacting with the TutorX MCP server.
This module provides functions that interact with the MCP server
for use by the Gradio interface.
"""

import json
import aiohttp
import asyncio
from typing import Dict, Any, List, Optional
import base64
from datetime import datetime
import os

# Get server configuration from environment variables with defaults
DEFAULT_HOST = os.getenv("MCP_HOST", "127.0.0.1")
DEFAULT_PORT = int(os.getenv("MCP_PORT", "8001"))  # Default port updated to 8001
DEFAULT_SERVER_URL = f"http://{DEFAULT_HOST}:{DEFAULT_PORT}"

# API endpoints
API_PREFIX = "/api"

class TutorXClient:
    """Client for interacting with the TutorX MCP server"""
    
    def __init__(self, server_url=DEFAULT_SERVER_URL):
        self.server_url = server_url
        self.session = None
    
    async def _ensure_session(self):
        """Ensure aiohttp session exists"""
        if self.session is None:
            self.session = aiohttp.ClientSession(
                headers={
                    "Content-Type": "application/json",
                    "Accept": "application/json"
                }
            )
    
    async def _call_tool(self, tool_name: str, params: Dict[str, Any], method: str = "POST") -> Dict[str, Any]:
        """
        Call an MCP tool on the server
        
        Args:
            tool_name: Name of the tool to call
            params: Parameters to pass to the tool
            method: HTTP method to use (GET or POST)
            
        Returns:
            Tool response
        """
        await self._ensure_session()
        try:
            url = f"{self.server_url}{API_PREFIX}/{tool_name}"
            
            # Convert params to query string for GET requests
            if method.upper() == "GET":
                from urllib.parse import urlencode
                if params:
                    query_string = urlencode(params, doseq=True)
                    url = f"{url}?{query_string}"
                async with self.session.get(url, timeout=30) as response:
                    return await self._handle_response(response)
            else:
                async with self.session.post(url, json=params, timeout=30) as response:
                    return await self._handle_response(response)
                    
        except Exception as e:
            return {
                "error": f"Failed to call tool: {str(e)}",
                "timestamp": datetime.now().isoformat()
            }
    
    async def _handle_response(self, response) -> Dict[str, Any]:
        """Handle the HTTP response"""
        if response.status == 200:
            return await response.json()
        else:
            error = await response.text()
            return {
                "error": f"API error ({response.status}): {error}",
                "timestamp": datetime.now().isoformat()
            }
    
    async def _get_resource(self, resource_uri: str) -> Dict[str, Any]:
        """
        Get an MCP resource from the server
        
        Args:
            resource_uri: URI of the resource to get
            
        Returns:
            Resource data
        """
        await self._ensure_session()
        try:
            # Extract the resource name from the URI (e.g., 'concept-graph' from 'concept-graph://')
            resource_name = resource_uri.split('://')[0]
            url = f"{self.server_url}{API_PREFIX}/{resource_name}"
            
            async with self.session.get(url, timeout=30) as response:
                if response.status == 200:
                    return await response.json()
                else:
                    error = await response.text()
                    return {
                        "error": f"Failed to get resource: {error}",
                        "timestamp": datetime.now().isoformat()
                    }
        except Exception as e:
            return {
                "error": f"Failed to get resource: {str(e)}",
                "timestamp": datetime.now().isoformat()
            }
    
    async def check_server_connection(self) -> bool:
        """
        Check if the server is accessible
        
        Returns:
            bool: True if server is accessible, False otherwise
        """
        await self._ensure_session()
        try:
            async with self.session.get(
                f"{self.server_url}{API_PREFIX}/health",
                timeout=5
            ) as response:
                return response.status == 200
        except Exception as e:
            print(f"Server connection check failed: {str(e)}")
            return False
    
    # ------------ Core Features ------------
    
    async def get_concept_graph(self, concept_id: str = None, use_mcp: bool = False) -> Dict[str, Any]:
        """
        Get the concept graph for a specific concept or all concepts.
        
        Args:
            concept_id: Optional ID of the concept to fetch. If None, returns all concepts.
            use_mcp: If True, uses the MCP tool interface instead of direct API call.
            
        Returns:
            Dict containing concept data or error information.
        """
        try:
            # Ensure we have a session
            await self._ensure_session()
            
            if use_mcp:
                # Use MCP tool interface
                print(f"[CLIENT] Using MCP tool to get concept graph for: {concept_id}")
                return await self._call_tool("get_concept_graph", {"concept_id": concept_id} if concept_id else {})
            
            # Use direct API call (default)
            url = f"{self.server_url}/api/concept_graph"
            params = {}
            if concept_id:
                params["concept_id"] = concept_id
            
            print(f"[CLIENT] Fetching concept graph from {url} with params: {params}")
            
            async with self.session.get(
                url,
                params=params,
                timeout=30
            ) as response:
                print(f"[CLIENT] Response status: {response.status}")
                
                if response.status == 404:
                    error_msg = f"Concept {concept_id} not found"
                    print(f"[CLIENT] {error_msg}")
                    return {"error": error_msg}
                    
                response.raise_for_status()
                
                # Parse the JSON response
                result = await response.json()
                print(f"[CLIENT] Received response: {result}")
                
                return result
                
        except asyncio.TimeoutError:
            error_msg = "Request timed out"
            print(f"[CLIENT] {error_msg}")
            return {"error": error_msg}
            
        except aiohttp.ClientError as e:
            error_msg = f"HTTP client error: {str(e)}"
            print(f"[CLIENT] {error_msg}")
            return {"error": error_msg}
            
        except Exception as e:
            error_msg = f"Unexpected error: {str(e)}"
            print(f"[CLIENT] {error_msg}")
            import traceback
            traceback.print_exc()
            return {"error": error_msg}
        
    async def assess_skill(self, student_id: str, concept_id: str) -> Dict[str, Any]:
        """Assess a student's skill on a specific concept"""
        return await self._call_tool("assess_skill", {"student_id": student_id, "concept_id": concept_id})
    
    async def get_learning_path(self, student_id: str) -> Dict[str, Any]:
        """Get personalized learning path for a student"""
        return await self._get_resource(f"learning-path://{student_id}")
    
    async def generate_quiz(self, concept_ids: List[str], difficulty: int = 2) -> Dict[str, Any]:
        """Generate a quiz based on specified concepts and difficulty"""
        return await self._call_tool("generate_quiz", {
            "concept_ids": concept_ids,
            "difficulty": difficulty
        })
    
    async def analyze_error_patterns(self, student_id: str, concept_id: str) -> Dict[str, Any]:
        """Analyze common error patterns for a student on a specific concept"""
        return await self._call_tool("analyze_error_patterns", {
            "student_id": student_id,
            "concept_id": concept_id
        })
    
    # ------------ Advanced Features ------------
    
    async def analyze_cognitive_state(self, eeg_data: Dict[str, Any]) -> Dict[str, Any]:
        """Analyze EEG data to determine cognitive state"""
        return await self._call_tool("analyze_cognitive_state", {
            "eeg_data": eeg_data
        })
    
    async def get_curriculum_standards(self, country_code: str) -> Dict[str, Any]:
        """Get curriculum standards for a specific country"""
        return await self._get_resource(f"curriculum-standards://{country_code}")
    
    async def align_content_to_standard(self, content_id: str, standard_id: str) -> Dict[str, Any]:
        """Align educational content to a specific curriculum standard"""
        return await self._call_tool("align_content_to_standard", {
            "content_id": content_id,
            "standard_id": standard_id
        })
    
    async def generate_lesson(self, topic: str, grade_level: int, duration_minutes: int = 45) -> Dict[str, Any]:
        """Generate a complete lesson plan on a topic"""
        return await self._call_tool("generate_lesson", {
            "topic": topic,
            "grade_level": grade_level,
            "duration_minutes": duration_minutes
        })
    
    # ------------ User Experience ------------
    
    async def get_student_dashboard(self, student_id: str) -> Dict[str, Any]:
        """Get dashboard data for a specific student"""
        return await self._get_resource(f"student-dashboard://{student_id}")
    
    async def get_accessibility_settings(self, student_id: str) -> Dict[str, Any]:
        """Get accessibility settings for a student"""
        return await self._call_tool("get_accessibility_settings", {
            "student_id": student_id
        })
    
    async def update_accessibility_settings(self, student_id: str, settings: Dict[str, Any]) -> Dict[str, Any]:
        """Update accessibility settings for a student"""
        return await self._call_tool("update_accessibility_settings", {
            "student_id": student_id,
            "settings": settings
        })
    
    # ------------ Multi-Modal Interaction ------------
    
    async def text_interaction(self, query: str, student_id: str) -> Dict[str, Any]:
        """Process a text query from the student"""
        return await self._call_tool("text_interaction", {
            "query": query,
            "student_id": student_id
        })
    
    async def voice_interaction(self, audio_data_base64: str, student_id: str) -> Dict[str, Any]:
        """Process voice input from the student"""
        return await self._call_tool("voice_interaction", {
            "audio_data_base64": audio_data_base64,
            "student_id": student_id
        })
    
    async def handwriting_recognition(self, image_data_base64: str, student_id: str) -> Dict[str, Any]:
        """Process handwritten input from the student"""
        return await self._call_tool("handwriting_recognition", {
            "image_data_base64": image_data_base64,
            "student_id": student_id
        })
    
    # ------------ Assessment ------------
    
    async def create_assessment(self, concept_ids: List[str], num_questions: int, difficulty: int = 3) -> Dict[str, Any]:
        """Create a complete assessment for given concepts"""
        return await self._call_tool("create_assessment", {
            "concept_ids": concept_ids,
            "num_questions": num_questions,
            "difficulty": difficulty
        })
    
    async def grade_assessment(self, assessment_id: str, student_answers: Dict[str, str], questions: List[Dict[str, Any]]) -> Dict[str, Any]:
        """Grade a completed assessment"""
        return await self._call_tool("grade_assessment", {
            "assessment_id": assessment_id,
            "student_answers": student_answers,
            "questions": questions
        })
    
    async def get_student_analytics(self, student_id: str, timeframe_days: int = 30) -> Dict[str, Any]:
        """Get comprehensive analytics for a student"""
        return await self._call_tool("get_student_analytics", {
            "student_id": student_id,
            "timeframe_days": timeframe_days
        })
    
    async def check_submission_originality(self, submission: str, reference_sources: List[str]) -> Dict[str, Any]:
        """Check student submission for potential plagiarism"""
        return await self._call_tool("check_submission_originality", {
            "submission": submission,
            "reference_sources": reference_sources
        })
    

    async def get_curriculum_standards(self, country_code: str = "us") -> Dict[str, Any]:
        """
        Get curriculum standards for a specific country
        
        Args:
            country_code: ISO country code (e.g., 'us', 'uk')
            
        Returns:
            Dictionary containing curriculum standards
        """
        return await self._call_tool(
            "curriculum-standards",  # Note the endpoint name matches the API route
            {"country": country_code.lower()},  # Note the parameter name matches the API
            method="GET"  # Use GET for this endpoint
        )
    
    async def close(self):
        """Close the aiohttp session"""
        if self.session:
            await self.session.close()
            self.session = None

    async def generate_lesson(self, topic: str, grade_level: int, duration_minutes: int) -> Dict[str, Any]:
        """
        Generate a lesson plan for the given topic, grade level, and duration
        
        Args:
            topic: The topic for the lesson
            grade_level: The grade level (1-12)
            duration_minutes: Duration of the lesson in minutes
            
        Returns:
            Dictionary containing the generated lesson plan
        """
        return await self._call_tool(
            "generate_lesson",
            {
                "topic": topic,
                "grade_level": grade_level,
                "duration_minutes": duration_minutes
            }
        )

# Create a default client instance for easy import
client = TutorXClient()