Skip to content

Commit c37b816

Browse files
committed
init
1 parent a30aed9 commit c37b816

2 files changed

Lines changed: 140 additions & 3 deletions

File tree

eval_protocol/pytest/default_mcp_gym_rollout_processor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
from typing import List, Optional
1010

1111
import eval_protocol as ep
12+
from eval_protocol.mcp.execution.manager import ExecutionManager
1213
from eval_protocol.models import EvaluationRow
1314
from eval_protocol.pytest.rollout_processor import RolloutProcessor
1415
from eval_protocol.pytest.types import RolloutProcessorConfig
15-
from eval_protocol.mcp.execution.manager import ExecutionManager
1616

1717

1818
class MCPServerManager:
@@ -181,8 +181,8 @@ def _signal_handler(cls, signum, frame):
181181
def _register_cleanup_handlers(cls):
182182
"""Register cleanup handlers - called only once"""
183183
atexit.register(cls._cleanup_all_servers)
184-
signal.signal(signal.SIGINT, cls._signal_handler) # Ctrl+C
185-
signal.signal(signal.SIGTERM, cls._signal_handler) # Termination signal
184+
# signal.signal(signal.SIGINT, cls._signal_handler) # Ctrl+C
185+
# signal.signal(signal.SIGTERM, cls._signal_handler) # Termination signal
186186

187187
def __enter__(self):
188188
"""Context manager entry"""
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
"""
2+
2048 Environment Adapter
3+
4+
This adapter implements the EnvironmentAdapter interface for 2048 environments,
5+
enabling integration with the MCP-Gym framework.
6+
"""
7+
8+
from typing import Any, Dict, Optional, Tuple
9+
10+
import gym
11+
import gym_2048
12+
from gym_2048.envs.game2048_env import Game2048Env
13+
14+
from eval_protocol.mcp.adapter import EnvironmentAdapter
15+
16+
17+
class Gym2048Adapter(EnvironmentAdapter):
18+
"""2048 adapter for MCP-Gym framework."""
19+
20+
ACTION_NAMES = ["UP", "RIGHT", "DOWN", "LEFT"]
21+
22+
def create_environment(self, config: Optional[Dict[str, Any]] = None) -> Game2048Env:
23+
"""
24+
Create 2048 environment.
25+
26+
Args:
27+
config: config is not used in this implementation
28+
29+
Returns:
30+
2048 environment instance
31+
"""
32+
print(f"🔍 Gym2048Adapter.create_environment: config: {config}")
33+
env = gym.make('2048-v0')
34+
return env
35+
36+
def create_environment_with_seed(
37+
self, config: Optional[Dict[str, Any]] = None, seed: Optional[int] = None
38+
) -> Tuple[Game2048Env, int, Dict[str, Any]]:
39+
"""
40+
Create 2048 environment with seed and return initial state.
41+
42+
Args:
43+
config: config is not used in this implementation
44+
seed: Seed for reproducible environments
45+
46+
Returns:
47+
Tuple of (environment, initial_observation, initial_info)
48+
"""
49+
print(f"🔍 Gym2048Adapter.create_environment_with_seed: seed: {seed}")
50+
config = config or {}
51+
52+
# Add seed to config for environment creation
53+
env_config = {**config, "seed": seed}
54+
print(f"🔍 Gym2048Adapter.create_environment_with_seed: env_config: {env_config}")
55+
56+
env = self.create_environment(env_config)
57+
print(f"🔍 Gym2048Adapter.create_environment_with_seed: created env, calling reset with seed: {seed}")
58+
obs, info = env.reset(seed=seed)
59+
print(f"🔍 Gym2048Adapter.create_environment_with_seed: reset returned obs: {obs}, info: {info}")
60+
61+
return env, obs, info
62+
63+
def reset_environment(self, env: Game2048Env, seed: Optional[int] = None) -> Tuple[int, Dict[str, Any]]:
64+
"""
65+
Reset environment.
66+
67+
Args:
68+
env: Environment instance
69+
seed: Optional seed for reset
70+
71+
Returns:
72+
Tuple of (observation, info)
73+
"""
74+
return env.reset(seed=seed)
75+
76+
def step_environment(self, env: Game2048Env, action: int) -> Tuple[int, float, bool, bool, Dict[str, Any]]:
77+
"""
78+
Execute environment step.
79+
80+
Args:
81+
env: Environment instance
82+
action: Action index
83+
84+
Returns:
85+
Tuple of (observation, reward, terminated, truncated, info)
86+
"""
87+
return env.step(action)
88+
89+
def close_environment(self, env: Game2048Env) -> None:
90+
"""
91+
Close environment.
92+
93+
Args:
94+
env: Environment instance
95+
"""
96+
env.close()
97+
98+
def parse_action(self, action_str: str) -> int:
99+
"""
100+
Parse action string to integer.
101+
102+
Args:
103+
action_str: Action string (UP, RIGHT, DOWN, LEFT)
104+
105+
Returns:
106+
Action index
107+
108+
Raises:
109+
ValueError: If action is invalid
110+
"""
111+
action_str = action_str.strip().upper()
112+
if action_str not in self.ACTION_NAMES:
113+
raise ValueError(f"Invalid action '{action_str}'. Valid actions: {self.ACTION_NAMES}")
114+
return self.ACTION_NAMES.index(action_str)
115+
116+
def format_observation(self, observation: int) -> int:
117+
"""
118+
Format observation for JSON serialization.
119+
120+
Args:
121+
observation: Raw observation from environment
122+
123+
Returns:
124+
Formatted observation
125+
"""
126+
return int(observation)
127+
128+
def get_default_config(self) -> Dict[str, Any]:
129+
"""
130+
Get default configuration.
131+
132+
Returns:
133+
Default configuration dictionary
134+
"""
135+
return {
136+
"is_slippery": False,
137+
}

0 commit comments

Comments
 (0)