mle-kit-mcp 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ import fire # type: ignore
2
+
3
+ from .server import run
4
+
5
+
6
+ def main() -> None:
7
+ fire.Fire(run)
@@ -0,0 +1,5 @@
1
+ from . import main
2
+
3
+
4
+ if __name__ == "__main__":
5
+ main()
mle_kit_mcp/files.py ADDED
@@ -0,0 +1,12 @@
1
+ from pathlib import Path
2
+
3
+ DIR_PATH = Path(__file__).parent
4
+ ROOT_PATH = DIR_PATH.parent
5
+
6
+ WORKSPACE_DIR_PATH: Path = ROOT_PATH / "workdir"
7
+
8
+
9
+ def set_workspace_dir(path: Path) -> None:
10
+ global WORKSPACE_DIR_PATH
11
+ path.mkdir(parents=True, exist_ok=True)
12
+ WORKSPACE_DIR_PATH = path
mle_kit_mcp/py.typed ADDED
File without changes
mle_kit_mcp/server.py ADDED
@@ -0,0 +1,36 @@
1
+ import fire # type: ignore
2
+ from pathlib import Path
3
+ import uvicorn
4
+ from mcp.server.fastmcp import FastMCP
5
+
6
+ from .files import set_workspace_dir
7
+ from .tools.bash import bash
8
+ from .tools.text_editor import text_editor
9
+ from .tools.remote_gpu import (
10
+ remote_bash,
11
+ create_remote_text_editor,
12
+ remote_download,
13
+ )
14
+
15
+
16
+ server = FastMCP("MLE kit MCP", stateless_http=True)
17
+
18
+ remote_text_editor = create_remote_text_editor(text_editor)
19
+
20
+ server.add_tool(bash)
21
+ server.add_tool(text_editor)
22
+ server.add_tool(remote_bash)
23
+ server.add_tool(remote_text_editor)
24
+ server.add_tool(remote_download)
25
+
26
+ http_app = server.streamable_http_app()
27
+
28
+
29
+ def run(host: str = "0.0.0.0", port: int = 5050, workspace: str = "workdir") -> None:
30
+ workspace_path = Path(workspace)
31
+ set_workspace_dir(workspace_path)
32
+ uvicorn.run(http_app, host=host, port=port)
33
+
34
+
35
+ if __name__ == "__main__":
36
+ fire.Fire(run)
@@ -0,0 +1,14 @@
1
+ from .bash import bash
2
+ from .text_editor import text_editor
3
+ from .remote_gpu import (
4
+ remote_bash,
5
+ remote_download,
6
+ )
7
+
8
+
9
+ __all__ = [
10
+ "bash",
11
+ "text_editor",
12
+ "remote_bash",
13
+ "remote_download",
14
+ ]
@@ -0,0 +1,81 @@
1
+ import docker # type: ignore
2
+ import atexit
3
+ import signal
4
+ from typing import Optional, Any
5
+
6
+ from mle_kit_mcp.files import WORKSPACE_DIR_PATH
7
+
8
+
9
+ _container = None
10
+ _client = None
11
+
12
+ BASE_IMAGE = "python:3.9-slim"
13
+ DOCKER_WORKSPACE_DIR_PATH = "/workdir"
14
+
15
+
16
+ def cleanup_container(signum: Optional[Any] = None, frame: Optional[Any] = None) -> None:
17
+ global _container
18
+ if _container:
19
+ try:
20
+ _container.remove(force=True)
21
+ _container = None
22
+ except Exception:
23
+ pass
24
+ if signum == signal.SIGINT:
25
+ raise KeyboardInterrupt()
26
+
27
+
28
+ atexit.register(cleanup_container)
29
+ signal.signal(signal.SIGINT, cleanup_container)
30
+ signal.signal(signal.SIGTERM, cleanup_container)
31
+
32
+
33
+ def bash(command: str) -> str:
34
+ """
35
+ Run commands in a bash shell.
36
+ When invoking this tool, the contents of the "command" parameter does NOT need to be XML-escaped.
37
+ You don't have access to the internet via this tool.
38
+ You do have access to a mirror of common linux and python packages via apt and pip.
39
+ State is persistent across command calls and discussions with the user.
40
+ To inspect a particular line range of a file, e.g. lines 10-25, try 'sed -n 10,25p /path/to/the/file'.
41
+ Please avoid commands that may produce a very large amount of output.
42
+ Please run long lived commands in the background, e.g. 'sleep 10 &' or start a server in the background.
43
+
44
+ Args:
45
+ command: The bash command to run.
46
+ """
47
+
48
+ global _container, _client
49
+
50
+ if not _client:
51
+ _client = docker.from_env()
52
+
53
+ if not _container:
54
+ try:
55
+ _container = _client.containers.get("bash_runner")
56
+ except docker.errors.NotFound:
57
+ _container = _client.containers.run(
58
+ BASE_IMAGE,
59
+ "tail -f /dev/null",
60
+ detach=True,
61
+ remove=True,
62
+ name="bash_runner",
63
+ tty=True,
64
+ stdin_open=True,
65
+ volumes={
66
+ WORKSPACE_DIR_PATH: {
67
+ "bind": DOCKER_WORKSPACE_DIR_PATH,
68
+ "mode": "rw",
69
+ }
70
+ },
71
+ working_dir=DOCKER_WORKSPACE_DIR_PATH,
72
+ )
73
+
74
+ result = _container.exec_run(
75
+ ["bash", "-c", command],
76
+ workdir=DOCKER_WORKSPACE_DIR_PATH,
77
+ stdout=True,
78
+ stderr=True,
79
+ )
80
+ output: str = result.output.decode("utf-8").strip()
81
+ return output
@@ -0,0 +1,354 @@
1
+ import os
2
+ import time
3
+ import subprocess
4
+ import atexit
5
+ import signal
6
+ import inspect
7
+ import functools
8
+ from pathlib import Path
9
+ from typing import List, Optional, Any, Callable
10
+ from dataclasses import dataclass
11
+
12
+ from dotenv import load_dotenv
13
+ from vastai_sdk import VastAI # type: ignore
14
+
15
+ from mle_kit_mcp.files import WORKSPACE_DIR_PATH
16
+
17
+ BASE_IMAGE = "phoenix120/holosophos_mle"
18
+ DEFAULT_GPU_TYPE = "RTX_3090"
19
+ GLOBAL_TIMEOUT = 43200
20
+ VAST_AI_GREETING = """Welcome to vast.ai. If authentication fails, try again after a few seconds, and double check your ssh key.
21
+ Have fun!"""
22
+
23
+
24
+ @dataclass
25
+ class InstanceInfo:
26
+ instance_id: int
27
+ ip: str = ""
28
+ port: int = 0
29
+ username: str = ""
30
+ ssh_key_path: str = ""
31
+ gpu_name: str = ""
32
+ start_time: int = 0
33
+
34
+
35
+ _sdk: Optional[VastAI] = None
36
+ _instance_info: Optional[InstanceInfo] = None
37
+
38
+
39
+ def cleanup_machine(signum: Optional[Any] = None, frame: Optional[Any] = None) -> None:
40
+ print("Cleaning up...")
41
+ global _instance_info
42
+ signal.alarm(0)
43
+ if _instance_info and _sdk:
44
+ try:
45
+ _sdk.destroy_instance(id=_instance_info.instance_id)
46
+ _instance_info = None
47
+ except Exception:
48
+ pass
49
+ if signum == signal.SIGINT:
50
+ raise KeyboardInterrupt()
51
+
52
+
53
+ atexit.register(cleanup_machine)
54
+ signal.signal(signal.SIGINT, cleanup_machine)
55
+ signal.signal(signal.SIGTERM, cleanup_machine)
56
+ signal.signal(signal.SIGALRM, cleanup_machine)
57
+
58
+
59
+ def wait_for_instance(vast_sdk: VastAI, instance_id: str, max_wait_time: int = 300) -> bool:
60
+ print(f"Waiting for instance {instance_id} to be ready...")
61
+ start_wait = int(time.time())
62
+ instance_ready = False
63
+ while time.time() - start_wait < max_wait_time:
64
+ instance_details = vast_sdk.show_instance(id=instance_id)
65
+ if (
66
+ isinstance(instance_details, dict)
67
+ and instance_details.get("actual_status") == "running"
68
+ ):
69
+ instance_ready = True
70
+ print(f"Instance {instance_id} is running and ready.")
71
+ break
72
+ print(f"Instance {instance_id} not ready yet. Waiting...")
73
+ time.sleep(15)
74
+ return instance_ready
75
+
76
+
77
+ def get_offers(vast_sdk: VastAI, gpu_name: str) -> List[int]:
78
+ params = [
79
+ f"gpu_name={gpu_name}",
80
+ "cuda_vers>=12.1",
81
+ "num_gpus=1",
82
+ "reliability > 0.99",
83
+ "inet_up > 400",
84
+ "inet_down > 400",
85
+ "verified=True",
86
+ "rentable=True",
87
+ "disk_space > 100",
88
+ ]
89
+ query = " ".join(params)
90
+ order = "score-"
91
+ offers = vast_sdk.search_offers(query=query, order=order)
92
+ assert offers
93
+ return [int(o["id"]) for o in offers]
94
+
95
+
96
+ def run_command(
97
+ instance: InstanceInfo, command: str, timeout: int = 60
98
+ ) -> subprocess.CompletedProcess[str]:
99
+ cmd = [
100
+ "ssh",
101
+ "-i",
102
+ instance.ssh_key_path,
103
+ "-p",
104
+ str(instance.port),
105
+ "-o",
106
+ "StrictHostKeyChecking=no",
107
+ "-o",
108
+ "ConnectTimeout=10",
109
+ "-o",
110
+ "ServerAliveInterval=30",
111
+ "-o",
112
+ "ServerAliveCountMax=3",
113
+ "-o",
114
+ "TCPKeepAlive=yes",
115
+ f"{instance.username}@{instance.ip}",
116
+ command,
117
+ ]
118
+ try:
119
+ result = subprocess.run(cmd, capture_output=True, text=True, timeout=timeout)
120
+ if result.returncode != 0:
121
+ raise Exception(
122
+ f"Error running command: {command}; "
123
+ f"Output: {result.stdout}; "
124
+ f"Error: {result.stderr}"
125
+ )
126
+ except subprocess.TimeoutExpired:
127
+ raise Exception(
128
+ f"Command timed out after {timeout} seconds: {command}; "
129
+ f"Host: {instance.username}@{instance.ip}:{instance.port}"
130
+ )
131
+ return result
132
+
133
+
134
+ def recieve_rsync(
135
+ info: InstanceInfo, remote_path: str, local_path: str
136
+ ) -> subprocess.CompletedProcess[str]:
137
+ rsync_cmd = [
138
+ "rsync",
139
+ "-avz",
140
+ "-e",
141
+ f"ssh -i {info.ssh_key_path} -p {info.port} -o StrictHostKeyChecking=no",
142
+ f"{info.username}@{info.ip}:{remote_path}",
143
+ local_path,
144
+ ]
145
+
146
+ result = subprocess.run(rsync_cmd, capture_output=True, text=True)
147
+ if result.returncode != 0:
148
+ error_output = (
149
+ f"Error syncing directory: {remote_path} to {local_path}. Error: {result.stderr}"
150
+ )
151
+ raise Exception(error_output)
152
+ return result
153
+
154
+
155
+ def send_rsync(
156
+ info: InstanceInfo, local_path: str, remote_path: str
157
+ ) -> subprocess.CompletedProcess[str]:
158
+ rsync_cmd = [
159
+ "rsync",
160
+ "-avz",
161
+ "-e",
162
+ f"ssh -i {info.ssh_key_path} -p {info.port} -o StrictHostKeyChecking=no",
163
+ local_path,
164
+ f"{info.username}@{info.ip}:{remote_path}",
165
+ ]
166
+
167
+ result = subprocess.run(rsync_cmd, capture_output=True, text=True)
168
+ if result.returncode != 0:
169
+ error_output = (
170
+ f"Error syncing directory: {local_path} to {remote_path}. Error: {result.stderr}"
171
+ )
172
+ raise Exception(error_output)
173
+ return result
174
+
175
+
176
+ def launch_instance(vast_sdk: VastAI, gpu_name: str) -> Optional[InstanceInfo]:
177
+ print(f"Selecting instance with {gpu_name}...")
178
+ offer_ids = get_offers(vast_sdk, gpu_name)
179
+
180
+ instance_id = None
181
+ for offer_id in offer_ids:
182
+ print(f"Launching offer {offer_id}...")
183
+ instance = vast_sdk.create_instance(id=offer_id, image=BASE_IMAGE, disk=50.0)
184
+ if not instance["success"]:
185
+ continue
186
+ instance_id = instance["new_contract"]
187
+ assert instance_id
188
+ global _instance_info
189
+ _instance_info = InstanceInfo(instance_id=instance_id)
190
+ print(f"Instance launched successfully. ID: {instance_id}")
191
+ is_ready = wait_for_instance(vast_sdk, instance_id)
192
+ if not is_ready:
193
+ print(f"Destroying instance {instance_id}...")
194
+ vast_sdk.destroy_instance(id=instance_id)
195
+ continue
196
+
197
+ print("Attaching SSH key...")
198
+ ssh_key_path = Path("~/.ssh/id_rsa").expanduser()
199
+ if not ssh_key_path.exists():
200
+ print(f"Generating SSH key at {ssh_key_path}...")
201
+ os.makedirs(ssh_key_path.parent, exist_ok=True)
202
+ subprocess.run(
203
+ [
204
+ "ssh-keygen",
205
+ "-t",
206
+ "rsa",
207
+ "-b",
208
+ "4096",
209
+ "-f",
210
+ str(ssh_key_path),
211
+ "-N",
212
+ "",
213
+ ]
214
+ )
215
+
216
+ public_key = Path(f"{ssh_key_path}.pub").read_text().strip()
217
+ vast_sdk.attach_ssh(instance_id=instance_id, ssh_key=public_key)
218
+ instance_details = vast_sdk.show_instance(id=instance_id)
219
+
220
+ info = InstanceInfo(
221
+ instance_id=instance_details.get("id"),
222
+ ip=instance_details.get("ssh_host"),
223
+ port=instance_details.get("ssh_port"),
224
+ username="root",
225
+ ssh_key_path=str(ssh_key_path),
226
+ gpu_name=instance_details.get("gpu_name"),
227
+ start_time=int(time.time()),
228
+ )
229
+
230
+ print(info)
231
+ print(f"Checking SSH connection to {info.ip}:{info.port}...")
232
+ max_attempts = 10
233
+ is_okay = False
234
+ for attempt in range(max_attempts):
235
+ try:
236
+ result = run_command(info, "echo 'SSH connection successful'")
237
+ except Exception as e:
238
+ print(f"Waiting for SSH... {e}\n(Attempt {attempt+1}/{max_attempts})")
239
+ time.sleep(30)
240
+ continue
241
+ if "SSH connection successful" in result.stdout:
242
+ print("SSH connection established successfully!")
243
+ is_okay = True
244
+ break
245
+ print(f"Waiting for SSH... (Attempt {attempt+1}/{max_attempts})")
246
+ time.sleep(30)
247
+
248
+ if not is_okay:
249
+ print(f"Destroying instance {instance_id}...")
250
+ vast_sdk.destroy_instance(id=instance_id)
251
+ continue
252
+
253
+ break
254
+
255
+ return info
256
+
257
+
258
+ def send_scripts() -> None:
259
+ assert _instance_info
260
+ for name in os.listdir(WORKSPACE_DIR_PATH):
261
+ if name.endswith(".py"):
262
+ send_rsync(_instance_info, f"{WORKSPACE_DIR_PATH}/{name}", "/root")
263
+
264
+
265
+ def init_all() -> None:
266
+ global _sdk, _instance_info
267
+
268
+ load_dotenv()
269
+
270
+ if not _sdk:
271
+ _sdk = VastAI(api_key=os.getenv("VAST_AI_KEY"))
272
+ assert _sdk
273
+
274
+ signal.alarm(GLOBAL_TIMEOUT)
275
+ if not _instance_info:
276
+ _instance_info = launch_instance(_sdk, DEFAULT_GPU_TYPE)
277
+
278
+ if _instance_info:
279
+ send_scripts()
280
+
281
+ assert _instance_info, "Failed to connect to a remote instance! Try again"
282
+
283
+
284
+ def remote_bash(command: str, timeout: Optional[int] = 60) -> str:
285
+ """
286
+ Run commands in a bash shell on a remote machine with GPU cards.
287
+ When invoking this tool, the contents of the "command" parameter does NOT need to be XML-escaped.
288
+ You don't have access to the internet via this tool.
289
+ You do have access to a mirror of common linux and python packages via apt and pip.
290
+ State is persistent across command calls and discussions with the user.
291
+ To inspect a particular line range of a file, e.g. lines 10-25, try 'sed -n 10,25p /path/to/the/file'.
292
+ Please avoid commands that may produce a very large amount of output.
293
+ Do not run commands in the background.
294
+ You can use python3.
295
+
296
+ Args:
297
+ command: The bash command to run.
298
+ timeout: Timeout for the command execution. 60 seconds by default. Set a higher value for heavy jobs.
299
+ """
300
+
301
+ init_all()
302
+ assert _instance_info
303
+ assert timeout
304
+ result = run_command(_instance_info, command, timeout=timeout)
305
+ output = ("STDOUT: " + result.stdout + "\n") if result.stdout else ""
306
+ output += ("STDERR: " + result.stderr) if result.stderr else ""
307
+ return output.replace(VAST_AI_GREETING, "")
308
+
309
+
310
+ def remote_download(file_path: str) -> str:
311
+ """
312
+ Copies a file from a remote machine to the local work directory.
313
+ Use it to download final artefacts of the runs.
314
+ Args:
315
+ file_path: Path to the file on a remote machine.
316
+ """
317
+ init_all()
318
+ assert _instance_info
319
+ recieve_rsync(_instance_info, f"/root/{file_path}", f"{WORKSPACE_DIR_PATH}")
320
+ return f"File '{file_path}' downloaded!"
321
+
322
+
323
+ def create_remote_text_editor(
324
+ text_editor_func: Callable[..., str],
325
+ ) -> Callable[..., str]:
326
+ @functools.wraps(text_editor_func)
327
+ def wrapper(*args: Any, **kwargs: Any) -> str:
328
+ init_all()
329
+ assert _instance_info
330
+
331
+ args_dict = {k: v for k, v in kwargs.items()}
332
+ if args:
333
+ args_dict.update(dict(zip(("command", "path"), args)))
334
+ path = args_dict["path"]
335
+ command = args_dict["command"]
336
+
337
+ if command != "write":
338
+ recieve_rsync(_instance_info, f"/root/{path}", f"{WORKSPACE_DIR_PATH}")
339
+
340
+ result: str = text_editor_func(*args, **kwargs)
341
+
342
+ if command != "view":
343
+ send_rsync(_instance_info, f"{WORKSPACE_DIR_PATH}/{path}", "/root")
344
+
345
+ return result
346
+
347
+ orig_sig = inspect.signature(text_editor_func)
348
+ wrapper.__signature__ = orig_sig # type: ignore
349
+ if text_editor_func.__doc__:
350
+ orig_doc = text_editor_func.__doc__
351
+ new_doc = orig_doc.replace("text_editor", "remote_text_editor")
352
+ wrapper.__doc__ = "Executes on a remote machine with GPU.\n" + new_doc
353
+ wrapper.__name__ = "remote_text_editor"
354
+ return wrapper
@@ -0,0 +1,198 @@
1
+ from collections import defaultdict
2
+ from typing import Dict, List, Optional
3
+ from pathlib import Path
4
+
5
+ from mle_kit_mcp.files import WORKSPACE_DIR_PATH
6
+ from mle_kit_mcp.utils import truncate_content
7
+
8
+ WRITE_MAX_OUTPUT_LENGTH = 500
9
+ READ_MAX_OUTPUT_LENGTH = 3000
10
+
11
+ # Global state for undo operations
12
+ FILE_HISTORY: Dict[str, List[List[str]]] = defaultdict(list)
13
+
14
+
15
+ def _save_file_state(path: Path, content: List[str]) -> None:
16
+ FILE_HISTORY[str(path.resolve())].append(content.copy())
17
+
18
+
19
+ def _write(path: Path, file_text: str, overwrite: bool) -> str:
20
+ if not overwrite:
21
+ assert (
22
+ not path.exists()
23
+ ), f"Cannot write file, path already exists: {path}. Pass overwrite=True"
24
+ path.parent.mkdir(parents=True, exist_ok=True)
25
+ lines = []
26
+ if path.exists():
27
+ lines = path.read_text().splitlines(True)
28
+ _save_file_state(path, lines)
29
+ path.write_text(file_text)
30
+ return f"Write was successful, the content of the '{path.name}' has changed!"
31
+
32
+
33
+ def _append(path: Path, new_str: str) -> str:
34
+ assert path.exists(), "You can 'append' only to existing files"
35
+ content = path.open().read()
36
+ _save_file_state(path, content.splitlines(True))
37
+ new_content = "\n".join((content, new_str))
38
+ path.write_text(new_content)
39
+ return truncate_content(new_content, WRITE_MAX_OUTPUT_LENGTH, suffix_only=True)
40
+
41
+
42
+ def _insert(path: Path, insert_line: int, new_str: str) -> str:
43
+ assert path.is_file(), f"File not found: {path}"
44
+ lines = path.open().readlines()
45
+ assert 0 <= insert_line <= len(lines), f"Invalid insert_line: {insert_line}"
46
+ _save_file_state(path, lines)
47
+ lines.insert(insert_line, new_str if new_str.endswith("\n") else new_str + "\n")
48
+ new_content = "".join(lines)
49
+ path.write_text(new_content)
50
+ return truncate_content(new_content, WRITE_MAX_OUTPUT_LENGTH, target_line=insert_line)
51
+
52
+
53
+ def _str_replace(path: Path, old_str: str, new_str: str) -> str:
54
+ assert path.is_file(), f"File not found: {path}"
55
+ content = path.open().read()
56
+ count = content.count(old_str)
57
+ assert count != 0, "old_str not found in file"
58
+ assert count == 1, "old_str is not unique in file"
59
+ target_line = content[: content.find(old_str) + len(old_str)].count("\n")
60
+ _save_file_state(path, content.splitlines(True))
61
+ new_content = content.replace(old_str, new_str)
62
+ path.write_text(new_content)
63
+ return truncate_content(new_content, WRITE_MAX_OUTPUT_LENGTH, target_line=target_line)
64
+
65
+
66
+ def _undo_edit(path: Path) -> str:
67
+ text_path = str(path.resolve())
68
+ assert text_path in FILE_HISTORY, f"No edit history available for: {text_path}"
69
+ assert FILE_HISTORY[text_path], f"No edit history available for: {text_path}"
70
+ previous_state = FILE_HISTORY[text_path].pop()
71
+ new_content = "".join(previous_state)
72
+ path.write_text(new_content)
73
+ return truncate_content(new_content, WRITE_MAX_OUTPUT_LENGTH)
74
+
75
+
76
+ def _view(
77
+ path: Path,
78
+ view_start_line: Optional[int] = None,
79
+ view_end_line: Optional[int] = None,
80
+ show_lines: bool = False,
81
+ ) -> str:
82
+ assert path.exists(), f"Path does not exist: {path}"
83
+ if not path.is_file():
84
+ output = []
85
+ for level1 in path.iterdir():
86
+ if level1.name.startswith("."):
87
+ continue
88
+ output.append(str(level1.relative_to(path)))
89
+ if level1.is_dir():
90
+ for level2 in level1.iterdir():
91
+ if level2.name.startswith("."):
92
+ continue
93
+ output.append(f" {level2.relative_to(path)}")
94
+ return "\n".join(output)
95
+
96
+ lines = path.open().readlines()
97
+ enum_start_line = 1
98
+ if view_start_line or view_end_line:
99
+ if not view_start_line:
100
+ view_start_line = 1
101
+ if not view_end_line:
102
+ view_end_line = len(lines)
103
+ view_end_line = view_end_line if view_end_line <= len(lines) else len(lines)
104
+ view_end_line = view_end_line if view_end_line != -1 else len(lines)
105
+ assert view_start_line >= 1, "Line numbers must start at 1"
106
+ assert view_end_line >= 1, "Line numbers must start at 1"
107
+ assert (
108
+ view_start_line <= view_end_line
109
+ ), "Incorrect view parameters, start is higher than end"
110
+ lines = lines[view_start_line - 1 : view_end_line]
111
+ enum_start_line = view_start_line
112
+ output = []
113
+ for i, line in enumerate(lines, enum_start_line):
114
+ prefix = f"{i:6d}\t"
115
+ current_line = line
116
+ if show_lines:
117
+ current_line = prefix + current_line
118
+ output.append(current_line)
119
+ return truncate_content("".join(output), READ_MAX_OUTPUT_LENGTH)
120
+
121
+
122
+ def text_editor(
123
+ command: str,
124
+ path: str,
125
+ new_str: Optional[str] = None,
126
+ file_text: Optional[str] = None,
127
+ old_str: Optional[str] = None,
128
+ insert_line: Optional[int] = None,
129
+ view_start_line: Optional[int] = None,
130
+ view_end_line: Optional[int] = None,
131
+ show_lines: Optional[bool] = False,
132
+ ) -> str:
133
+ """
134
+ Custom editing tool for viewing, creating and editing files.
135
+ State is persistent across command calls and discussions with the user.
136
+ If `path` is a file and `show_lines` is True, `view` displays the result of applying `cat -n`.
137
+ If `path` is a file and `show_lines` is False, `view` displays the result of applying `cat`.
138
+ If `path` is a directory, `view` lists non-hidden files and directories up to 2 levels deep.
139
+ The `write` command cannot be used if the specified `path` already exists as a file.
140
+ If a `command` generates a long output, it will be truncated and marked with `<response clipped>`.
141
+ The `undo_edit` command will revert the last edit made to the file at `path`.
142
+ Always write arguments with keys, do not rely on positions.
143
+ Be careful with escaping strings and line breaks in Python code.
144
+
145
+ Notes for using the `text_editor` command:
146
+ - The `old_str` parameter should match EXACTLY one or more consecutive lines from the original file.
147
+ - Be mindful of whitespaces!
148
+ - If the `old_str` parameter is not unique in the file, the replacement will not be performed.
149
+ - Make sure to include enough context in `old_str` to make it unique
150
+ - The `new_str` parameter should contain the edited lines that should replace the `old_str`
151
+
152
+ Examples:
153
+ Write a file with "Hello world!": text_editor("write", "file.txt", file_text="Hello world!")
154
+ View a file with enumerated lines: text_editor("view", "file.txt")
155
+ View first three lines of a file: text_editor("view", "file.txt", view_start_line=1, view_end_line=3)
156
+ View lines from 5 to the end of a file: text_editor("view", "file.txt", view_start_line=5)
157
+ Replace "line1" with "line2": text_editor("str_replace", "file.txt", old_str="line", new_str="line2")
158
+ Insert "line2" after line 1: text_editor("insert", "file.txt", insert_line=1, new_str="line2")
159
+ Append "line2" to the file: text_editor("append", "file.txt", new_str="line2")
160
+
161
+ Args:
162
+ command: The commands to run. Allowed options are: `view`, `write`, `str_replace`, `insert`, `undo_edit`.
163
+ path: Path to file or directory inside current work directory. Should not be absolute.
164
+ view_start_line: Optional for view command, use to view specific lines
165
+ view_end_line: Optional for view command, use to view specific lines
166
+ file_text: Required for `write` command, with the content of the file to be writed.
167
+ insert_line: Required for `insert` command. `new_str` will be inserted AFTER the line `insert_line` of `path`.
168
+ new_str: Required for `str_replace`, `insert` and `append`.
169
+ old_str: Required for `str_replace` containing the string in `path` to replace.
170
+ show_lines: Optional for view command. If True, the command will also output line numbers.
171
+ """
172
+ assert not path.startswith(
173
+ "/"
174
+ ), "Absolute path is not supported, only relative to the work directory"
175
+ valid_commands = ("view", "write", "str_replace", "insert", "undo_edit", "append")
176
+
177
+ path_obj = WORKSPACE_DIR_PATH / path
178
+
179
+ if command == "view":
180
+ show_lines = show_lines if show_lines is not None else False
181
+ return _view(path_obj, view_start_line, view_end_line, show_lines)
182
+ if command == "write":
183
+ assert file_text is not None, "'file_text' is required for 'write' command"
184
+ return _write(path_obj, file_text, overwrite=True)
185
+ if command == "append":
186
+ assert new_str is not None, "'new_str' is required for 'append' command"
187
+ return _append(path_obj, new_str)
188
+ if command == "insert":
189
+ assert insert_line is not None, "'insert_line' is required for 'insert' command"
190
+ assert new_str is not None, "'new_str' is required for 'insert' command"
191
+ return _insert(path_obj, insert_line, new_str)
192
+ if command == "str_replace":
193
+ assert old_str is not None, "'old_str' is required for 'str_replace' command"
194
+ assert new_str is not None, "'new_str' is required for 'str_replace' command"
195
+ return _str_replace(path_obj, old_str, new_str)
196
+ if command == "undo_edit":
197
+ return _undo_edit(path_obj)
198
+ assert False, f"Not a valid command! List of commands: {valid_commands}"
mle_kit_mcp/utils.py ADDED
@@ -0,0 +1,50 @@
1
+ from typing import Optional
2
+
3
+
4
+ def truncate_content(
5
+ content: str,
6
+ max_length: int,
7
+ prefix_only: bool = False,
8
+ suffix_only: bool = False,
9
+ target_line: Optional[int] = None,
10
+ ) -> str:
11
+ assert int(prefix_only) + int(suffix_only) + int(target_line is not None) <= 1
12
+ disclaimer = (
13
+ f"\n\n..._This content has been truncated to stay below {max_length} characters_...\n\n"
14
+ )
15
+ half_length = max_length // 2
16
+ if len(content) <= max_length:
17
+ return content
18
+
19
+ if prefix_only:
20
+ prefix = content[:max_length]
21
+ return prefix + disclaimer
22
+
23
+ elif suffix_only:
24
+ suffix = content[-max_length:]
25
+ return disclaimer + suffix
26
+
27
+ elif target_line:
28
+ line_start_pos = 0
29
+ next_pos = content.find("\n") + 1
30
+ line_end_pos = next_pos
31
+ for _ in range(target_line):
32
+ next_pos = content.find("\n", next_pos) + 1
33
+ line_start_pos = line_end_pos
34
+ line_end_pos = next_pos
35
+ assert line_end_pos >= line_start_pos
36
+ length = line_end_pos - line_start_pos
37
+ remaining_length = max(0, max_length - length)
38
+ half_length = remaining_length // 2
39
+ start = max(0, line_start_pos - half_length)
40
+ end = min(len(content), line_end_pos + half_length)
41
+ final_content = content[start:end]
42
+ if start == 0:
43
+ return final_content + disclaimer
44
+ elif end == len(content):
45
+ return disclaimer + final_content
46
+ return disclaimer + content[start:end] + disclaimer
47
+
48
+ prefix = content[:half_length]
49
+ suffix = content[-half_length:]
50
+ return prefix + disclaimer + suffix
@@ -0,0 +1,43 @@
1
+ Metadata-Version: 2.4
2
+ Name: mle-kit-mcp
3
+ Version: 0.0.1
4
+ Summary: MCP server that provides different tools for MLE
5
+ Author-email: Ilya Gusev <phoenixilya@gmail.com>
6
+ Project-URL: Homepage, https://github.com/IlyaGusev/mle_kit_mcp
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Operating System :: OS Independent
10
+ Requires-Python: >=3.12
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE
13
+ Requires-Dist: mcp>=1.9.2
14
+ Requires-Dist: black==25.1.0
15
+ Requires-Dist: mypy==1.16.0
16
+ Requires-Dist: flake8==7.2.0
17
+ Requires-Dist: fire>=0.7.0
18
+ Requires-Dist: docker>=7.1.0
19
+ Requires-Dist: vastai-sdk==0.1.16
20
+ Requires-Dist: pytest>=8.4.1
21
+ Dynamic: license-file
22
+
23
+ # MLE KIT MCP
24
+
25
+ A collection of MCP tools related to the experiments on remote gpu:
26
+ - Bash and remote bash
27
+ - Text editor and remote text editor
28
+ - Remote download
29
+
30
+
31
+ Run the mcp server:
32
+ ```
33
+ uv run python -m mle_kit_mcp --host 127.0.0.1 --port 5056 --workspace workdir
34
+ ```
35
+
36
+ Claude Desktop config:
37
+ ```
38
+ {
39
+ "mcpServers": {
40
+ "mle_kit": {"url": "http://127.0.0.1:5056/mcp", "transport": "streamable-http"}
41
+ }
42
+ }
43
+ ```
@@ -0,0 +1,16 @@
1
+ mle_kit_mcp/__init__.py,sha256=2Ru2I5u4cE7DrkkAsibDUEF1K6sYtqppb9VyFrRoQKI,94
2
+ mle_kit_mcp/__main__.py,sha256=rcmsOtJd3SA82exjrcGBuxuptcoxF8AXI7jNjiVq2BY,59
3
+ mle_kit_mcp/files.py,sha256=loZnpQ7ZLxjX8NgUq-hrvhwbQwmGw1F6gZ3xD3UQI9k,286
4
+ mle_kit_mcp/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ mle_kit_mcp/server.py,sha256=l81KAfyZ_G_0Suy9381qV40I_UAZtfT0FKim3_89pSI,886
6
+ mle_kit_mcp/utils.py,sha256=wd7wSyddHRHOYdxmXw8uoAOBxVZOL2_vjNomss07inc,1654
7
+ mle_kit_mcp/tools/__init__.py,sha256=0aLl0gD-JteSvOs2PgVhbv0Wnh6fodFySgQWQvoI1xI,215
8
+ mle_kit_mcp/tools/bash.py,sha256=QcPKQBjD6-LHvNGOAhyWXHuzkM70GATN0Vp85iykSXY,2460
9
+ mle_kit_mcp/tools/remote_gpu.py,sha256=Wh02B-hFUFdRRtZ0J4feLw9P8Hq0AW7xwZ3Q27LcdW4,11370
10
+ mle_kit_mcp/tools/text_editor.py,sha256=zwxyHvf8DdJwuUa7HOWfSbhaSTveaE_WMwsOdDnbq1w,9228
11
+ mle_kit_mcp-0.0.1.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
12
+ mle_kit_mcp-0.0.1.dist-info/METADATA,sha256=SIyqk-Ay6o2v7SCaSZuX0UbVPU7pB80PP7JQ5xH1rT0,1126
13
+ mle_kit_mcp-0.0.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
14
+ mle_kit_mcp-0.0.1.dist-info/entry_points.txt,sha256=-iHSUVPN49jkBj1ySpc-P0rVF5-IPHw-KWNayNIiEsk,49
15
+ mle_kit_mcp-0.0.1.dist-info/top_level.txt,sha256=XeBtCq_CnVI0gh0Z_daZOLmGl5XPlkA8RgHaj5s5VQY,12
16
+ mle_kit_mcp-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.9.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ mle_kit_mcp = mle_kit_mcp:main
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1 @@
1
+ mle_kit_mcp