File size: 5,456 Bytes
881f9f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Cell B tool builder: MCP stdio connections to the team's hardened
Smart Grid MCP servers.

Returns connected MCPServerStdio objects. The AaT runner passes them
to Agent(mcp_servers=...) and calls .cleanup() on each in a finally
block — dangling stdio subprocesses after a Slurm trial would leak.

Cell A uses in-process callables for the same tool set; see
aat_tools_direct.py. The test_direct_and_mcp_tool_schemas_match test
(Task 5) asserts the two paths expose identical tool surfaces.
"""

from __future__ import annotations

import asyncio
import os
import shlex
import sys
from pathlib import Path
from typing import List

from agents.mcp import MCPServerStdio

SERVER_MODULES: list[tuple[str, str]] = [
    ("iot", "mcp_servers/iot_server/server.py"),
    ("fmsr", "mcp_servers/fmsr_server/server.py"),
    ("tsfm", "mcp_servers/tsfm_server/server.py"),
    ("wo", "mcp_servers/wo_server/server.py"),
]

SERVER_UV_DEPS = [
    "mcp[cli]==1.27.0",
    "pandas",
    "numpy",
]


def _client_timeout_seconds() -> float:
    raw = os.environ.get("AAT_MCP_CLIENT_TIMEOUT_SECONDS", "30").strip()
    try:
        timeout = float(raw)
    except ValueError as exc:
        raise ValueError(
            f"AAT_MCP_CLIENT_TIMEOUT_SECONDS must be numeric, got {raw!r}"
        ) from exc
    if timeout <= 0:
        raise ValueError(f"AAT_MCP_CLIENT_TIMEOUT_SECONDS must be > 0, got {timeout!r}")
    return timeout


def _server_launch_mode() -> str:
    mode = os.environ.get("AAT_MCP_SERVER_LAUNCH_MODE", "python").strip().lower()
    if mode not in {"python", "uv"}:
        raise ValueError(
            "AAT_MCP_SERVER_LAUNCH_MODE must be either 'python' or 'uv', "
            f"got {mode!r}"
        )
    return mode


def _server_params(repo_root: Path, abs_path: Path) -> dict[str, object]:
    bootstrap_path = repo_root / "scripts" / "aat_mcp_server_bootstrap.py"
    if not bootstrap_path.exists():
        raise FileNotFoundError(f"AaT MCP server bootstrap missing: {bootstrap_path}")

    launch_mode = _server_launch_mode()
    env = {"PYTHONUNBUFFERED": "1", "AAT_MCP_REPO_ROOT": str(repo_root)}

    if launch_mode == "uv":
        return {
            "command": "uv",
            "args": [
                "run",
                *(arg for dep in SERVER_UV_DEPS for arg in ("--with", dep)),
                "python",
                "-u",
                str(bootstrap_path),
                str(abs_path),
            ],
            "cwd": str(repo_root),
            "env": env,
        }

    server_python = os.environ.get("AAT_MCP_SERVER_PYTHON", "").strip()
    if not server_python:
        raise ValueError(
            "AAT_MCP_SERVER_LAUNCH_MODE=python requires AAT_MCP_SERVER_PYTHON. "
            "scripts/run_experiment.sh sets this to .venv-insomnia/bin/python "
            "for Insomnia LAUNCH_VLLM=1 runs; use "
            "AAT_MCP_SERVER_LAUNCH_MODE=uv for local uv-managed launches."
        )
    python_path = Path(server_python)
    if not python_path.exists():
        raise FileNotFoundError(f"AAT_MCP_SERVER_PYTHON not found: {python_path}")
    return {
        "command": str(python_path),
        "args": ["-u", str(bootstrap_path), str(abs_path)],
        "cwd": str(repo_root),
        "env": env,
    }


async def build_mcp_servers(repo_root: Path) -> List[MCPServerStdio]:
    """Return a list of connected MCPServerStdio objects.

    On failure mid-way through, cleans up already-connected servers
    before re-raising, so callers don't have to handle partial state.
    """
    connected: List[MCPServerStdio] = []
    try:
        client_timeout = _client_timeout_seconds()
        print(
            f"AaT MCP client initialize timeout: {client_timeout:g}s",
            file=sys.stderr,
        )
        print(
            f"AaT MCP server launch mode: {_server_launch_mode()}",
            file=sys.stderr,
        )
        for name, rel in SERVER_MODULES:
            abs_path = repo_root / rel
            if not abs_path.exists():
                raise FileNotFoundError(
                    f"MCP server module missing: {abs_path} "
                    f"(expected under the shared team checkout)"
                )
            # Use an explicit uv dependency envelope: the AaT runner itself runs
            # from a temporary uv env, which may not include server data deps.
            # cache_tools_list=True avoids a list_tools round-trip per turn.
            srv = MCPServerStdio(
                name=name,
                params=_server_params(repo_root, abs_path),
                cache_tools_list=True,
                client_session_timeout_seconds=client_timeout,
            )
            command_line = [srv.params.command, *srv.params.args]
            print(
                f"Connecting MCP server {name}: {shlex.join(command_line)}",
                file=sys.stderr,
            )
            await srv.connect()
            print(f"Connected MCP server {name}", file=sys.stderr)
            connected.append(srv)
        return connected
    except Exception:
        for srv in connected:
            try:
                await srv.cleanup()
            except (asyncio.CancelledError, Exception):
                # asyncio.CancelledError is a BaseException in Py3.8+, not an
                # Exception; widen the catch so partial-failure cleanup can't
                # mask the original connection error with a teardown leak.
                pass
        raise