File size: 13,986 Bytes
beb8990
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Generic HTTP Environment Server for MCP Integration.

This module provides a custom HTTP server that wraps any MCP environment
and exposes OpenEnv-compliant HTTP endpoints (/reset, /step, /state).

The server is fully generic and works with any MCP integration.
All MCP-specific configuration is loaded from config.py.
"""

import asyncio
import logging
import sqlite3
from typing import Any, Dict, Optional, List

from fastapi import Body, FastAPI, Request, Query
from openenv.core.env_server.http_server import HTTPEnvServer

from .config import (
    MCP_NAME,
    HTTP_HEADERS,
    HTTP_HEADER_DEFAULTS,
    get_session_manager_class,
    get_seed_data_function
)

logger = logging.getLogger(__name__)


class MCPHTTPEnvServer(HTTPEnvServer):
    """
    Generic HTTP Environment Server for any MCP integration.
    
    This server wraps any MCP environment and provides HTTP endpoints
    for OpenEnv integration. It's fully generic and works with any MCP.
    
    HTTP headers are configured via config.py:HTTP_HEADERS, making it easy
    to adapt to different MCP authentication and multi-tenancy patterns.
    """
    
    def __init__(self, env, action_cls, observation_cls):
        """Initialize custom HTTP server with MCP session manager."""
        # Store classes before calling super().__init__()
        self.action_cls = action_cls
        self.observation_cls = observation_cls
        
        # Call parent init
        super().__init__(env=env, action_cls=action_cls, observation_cls=observation_cls)
        
        # Create a persistent environment instance for HTTP endpoints
        # The parent class stores env_factory in self._env_factory
        # We create one instance for the HTTP endpoints (not WebSocket)
        if callable(self._env_factory):
            self.env = self._env_factory()
        else:
            self.env = self._env_factory
        
        # Dynamically load the session manager from config
        SessionManagerClass = get_session_manager_class()
        self.session_manager = SessionManagerClass()
    
    def _get_header_value(self, headers: dict, header_key: str) -> Optional[str]:
        """
        Get header value using configured header name.
        
        Args:
            headers: Request headers dictionary
            header_key: Key in HTTP_HEADERS config (e.g., "database_id", "access_token")
        
        Returns:
            Header value or default from config
        """
        header_name = HTTP_HEADERS.get(header_key)
        if not header_name:
            logger.warning(f"Header key '{header_key}' not configured in HTTP_HEADERS")
            return HTTP_HEADER_DEFAULTS.get(header_key)
        
        value = headers.get(header_name)
        if value is None:
            value = HTTP_HEADER_DEFAULTS.get(header_key)
        
        return value

    def register_routes(self, app: Any) -> None:
        if not isinstance(app, FastAPI):
            raise TypeError("app must be a FastAPI instance")

        # Register custom reset endpoint
        @app.post("/reset")
        async def reset_with_database_refresh(
            request: Request,
            body: Optional[Dict[str, Any]] = Body(default=None)
        ) -> Dict[str, Any]:
            """
            Reset the environment and optionally reset the database.
            
            The database_id can be provided via:
            1. Request body: {"database_id": "my_db", "sql_content": "INSERT INTO..."}
            2. HTTP header: x-database-id
            3. Default value if neither provided
            
            Args (in request body, all optional):
                database_id: Database identifier for multi-tenancy
                sql_content: Custom SQL content to use for seeding instead of default
            
            Returns:
                Observation with reset status and database reset result
            """
            headers = dict(request.headers)
            
            # Get database_id from body first, then header, then default
            body = body or {}
            database_id = body.get("database_id") or self._get_header_value(headers, "database_id")
            access_token = self._get_header_value(headers, "access_token")
            
            # Get optional sql_content from body
            sql_content = body.get("sql_content")

            logger.info(f"Reset request for database_id={database_id}, custom_sql={'yes' if sql_content else 'no'}")

            # Reset database to original state (with optional custom SQL)
            db_reset_result = self._reset_database(database_id, sql_content=sql_content)

            # Set request context in environment
            self.env.set_request_context(
                database_id=database_id,
                access_token=access_token
            )

            # Execute reset in thread pool (environments may use sync code)
            loop = asyncio.get_event_loop()
            observation = await loop.run_in_executor(None, self.env.reset)

            # Serialize observation manually
            result = {
                "observation": observation.model_dump() if hasattr(observation, 'model_dump') else observation.__dict__,
                "done": getattr(observation, 'done', False),
                "reward": getattr(observation, 'reward', 0.0)
            }

            # Add database reset info to observation metadata
            if isinstance(result, dict) and "observation" in result:
                if "metadata" not in result["observation"]:
                    result["observation"]["metadata"] = {}
                result["observation"]["metadata"]["database_reset_result"] = db_reset_result

            logger.info(f"Environment reset completed for database {database_id}, DB refresh: {db_reset_result['success']}")
            return result

        # Register custom step endpoint
        @app.post("/step")
        async def step_with_headers(request: Request, body: Dict[str, Any] = Body(...)) -> Dict[str, Any]:
            # Extract headers using dynamic header names from config
            headers = dict(request.headers)
            database_id = self._get_header_value(headers, "database_id")
            access_token = self._get_header_value(headers, "access_token")

            # Debug logging to see what headers we're receiving
            logger.info(f"Step request - database_id: {database_id}, has_access_token: {bool(access_token)}")
            if not access_token:
                logger.warning(f"No access token found in headers. Available headers: {list(headers.keys())}")

            # Set request context in environment
            self.env.set_request_context(database_id=database_id, access_token=access_token)

            logger.debug(f"Step request with database_id={database_id}, has_token={bool(access_token)}")

            # Support both {"action": {...}} and direct action fields
            action_data = body.get("action", body)

            # Deserialize action manually using Pydantic
            try:
                action = self.action_cls(**action_data)
            except Exception as e:
                logger.error(f"Failed to deserialize action: {e}")
                return {
                    "observation": {
                        "success": False,
                        "error_message": f"Invalid action: {str(e)}",
                        "done": False,
                        "reward": -1.0,
                        "metadata": {}
                    },
                    "done": False,
                    "reward": -1.0
                }

            # Execute step in thread pool (environments may use sync code)
            loop = asyncio.get_event_loop()
            observation = await loop.run_in_executor(None, self.env.step, action)

            # Serialize observation manually
            result = {
                "observation": observation.model_dump() if hasattr(observation, 'model_dump') else observation.__dict__,
                "done": getattr(observation, 'done', False),
                "reward": getattr(observation, 'reward', 0.0)
            }
            
            return result

        # Register state endpoint
        @app.get("/state")
        async def get_state(
            request: Request,
            verify_queries: List[str] = Query(default=[])
        ) -> Dict[str, Any]:
            headers = dict(request.headers)
            database_id = headers.get("x-database-id", "default")

            state = self.env.state
            result = {
                "episode_id": state.episode_id,
                "step_count": state.step_count,
                "database_id": database_id
            }

            db_path = self.session_manager.get_db_path(database_id)

            try:
                conn = sqlite3.connect(db_path)
                conn.row_factory = sqlite3.Row
                cursor = conn.cursor()

                if verify_queries:
                    result["verification_results"] = []
                    
                    for query in verify_queries:
                        try:
                            cursor.execute(query)
                            rows = cursor.fetchall()
                            result["verification_results"].append({
                                "query": query,
                                "result": [dict(row) for row in rows],
                                "success": True
                            })
                        except Exception as query_error:
                            result["verification_results"].append({
                                "query": query,
                                "error": str(query_error),
                                "success": False
                            })

                conn.close()
            except Exception as e:
                result["db_error"] = str(e)

            return result

    def _reset_database(self, database_id: str, sql_content: Optional[str] = None) -> Dict[str, Any]:
        """
        Reset database to clean state with seed data.
        
        This method is generic and works with any MCP that follows
        the standard session manager interface.
        
        Args:
            database_id: Database identifier for multi-tenancy
            sql_content: Optional custom SQL content for seeding. If provided,
                        this will be used instead of the default seed data.
        
        Returns:
            Dictionary with reset status and details
        """
        try:
            # Dispose any cached engine connections to prevent stale connections
            # (Only if the SessionManager has this method - some MCPs may not have it)
            if hasattr(self.session_manager, 'dispose_engine'):
                self.session_manager.dispose_engine(database_id)

            # Get database path using session manager
            db_path = self.session_manager.get_db_path(database_id)

            # Drop all existing tables using sqlite3
            conn = sqlite3.connect(db_path)
            cursor = conn.cursor()

            cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'")
            tables = cursor.fetchall()

            for table in tables:
                table_name = table[0]
                cursor.execute(f"DROP TABLE IF EXISTS {table_name}")

            conn.commit()
            conn.close()

            # Recreate tables using session manager
            self.session_manager.init_database(database_id, create_tables=True)

            # Use custom SQL content if provided, otherwise use default seed data
            if sql_content:
                logger.info(f"Using custom SQL content for database {database_id}")
                seed_sql = sql_content
                used_custom_sql = True
            else:
                seed_data_fn = get_seed_data_function()
                seed_sql = seed_data_fn()
                used_custom_sql = False

            conn = sqlite3.connect(db_path)
            cursor = conn.cursor()

            # Parse and execute SQL statements
            statements = []
            for line in seed_sql.split("\n"):
                line = line.strip()
                if line and not line.startswith("--"):
                    statements.append(line)

            full_sql = " ".join(statements)
            individual_statements = [stmt.strip() for stmt in full_sql.split(";") if stmt.strip()]

            executed_count = 0
            for statement in individual_statements:
                try:
                    if not statement.strip():
                        continue
                    cursor.execute(statement)
                    executed_count += 1
                except Exception as e:
                    logger.error(f"Error executing statement during seeding: {statement[:100]}...")
                    logger.error(f"Error details: {e}")
                    raise e

            conn.commit()
            conn.close()

            seed_source = "custom SQL" if used_custom_sql else "default seed data"
            logger.info(f"Database {database_id} reset and seeded with {seed_source} ({executed_count} statements)")

            return {
                "success": True,
                "message": f"Database reset to clean state and seeded with {seed_source}",
                "database_id": database_id,
                "seeded": True,
                "used_custom_sql": used_custom_sql,
                "statements_executed": executed_count,
            }

        except Exception as e:
            logger.error(f"Error resetting database {database_id}: {e}", exc_info=True)
            return {
                "success": False,
                "message": f"Database reset failed: {str(e)}",
                "database_id": database_id,
                "seeded": False,
                "used_custom_sql": sql_content is not None,
            }