Skip to content

Commit 5773170

Browse files
committed
cleanup
1 parent a4aba11 commit 5773170

4 files changed

Lines changed: 64 additions & 8 deletions

File tree

dimos/agents/mcp/test_mcp_server.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@
1616

1717
import asyncio
1818
import json
19+
import socket
20+
import time
1921
from unittest.mock import MagicMock
2022

21-
from dimos.agents.mcp.mcp_server import handle_request
23+
import requests
24+
25+
from dimos.agents.mcp.mcp_server import McpServer, handle_request
2226
from dimos.core.module import SkillInfo
2327

2428

@@ -111,3 +115,56 @@ def test_mcp_module_initialize_and_unknown() -> None:
111115

112116
response = asyncio.run(handle_request({"method": "unknown/method", "id": 2}, [], {}))
113117
assert response["error"]["code"] == -32601
118+
119+
120+
def _free_port() -> int:
121+
with socket.socket() as s:
122+
s.bind(("", 0))
123+
return s.getsockname()[1]
124+
125+
126+
def test_mcp_server_lifecycle() -> None:
127+
"""Start a real McpServer, hit the HTTP endpoint, then stop it.
128+
129+
This exercises the AsyncModuleThread event loop integration that the
130+
unit tests above do not cover.
131+
"""
132+
port = _free_port()
133+
134+
server = McpServer()
135+
server._start_server(port=port)
136+
url = f"http://127.0.0.1:{port}/mcp"
137+
138+
# Wait for the server to be ready
139+
for _ in range(40):
140+
try:
141+
resp = requests.post(
142+
url,
143+
json={"jsonrpc": "2.0", "method": "initialize", "id": 1},
144+
timeout=0.5,
145+
)
146+
if resp.status_code == 200:
147+
break
148+
except requests.ConnectionError:
149+
time.sleep(0.1)
150+
else:
151+
server.stop()
152+
raise AssertionError("McpServer did not become ready")
153+
154+
# Verify it responds
155+
data = resp.json()
156+
assert data["result"]["serverInfo"]["name"] == "dimensional"
157+
158+
# Stop and verify it shuts down
159+
server.stop()
160+
time.sleep(0.3)
161+
162+
with socket.socket() as s:
163+
# Port should be released after stop
164+
try:
165+
s.connect(("127.0.0.1", port))
166+
s.close()
167+
# If we could connect, the server is still up — that's a bug
168+
raise AssertionError("McpServer still listening after stop()")
169+
except ConnectionRefusedError:
170+
pass # expected — server is down

dimos/core/module.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
from dimos.utils.generic import classproperty
4747
from dimos.utils.thread_utils import AsyncModuleThread, ThreadSafeVal
4848

49-
ModState = Literal["init", "started", "stopping", "stopped"]
49+
ModState = Literal["init", "started", "stopped"]
5050

5151
if TYPE_CHECKING:
5252
from dimos.core.blueprints import Blueprint
@@ -130,9 +130,9 @@ def stop(self) -> None:
130130

131131
def _stop(self) -> None:
132132
with self.mod_state as state:
133-
if state in ("stopping", "stopped"):
133+
if state == "stopped":
134134
return
135-
self.mod_state.set("stopping")
135+
self.mod_state.set("stopped")
136136

137137
if self.rpc:
138138
self.rpc.stop() # type: ignore[attr-defined]

dimos/utils/test_thread_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,11 @@ def test_string_literal_type(self) -> None:
210210
assert v.get() == "started"
211211

212212
with v as state:
213-
if state in ("stopping", "stopped"):
213+
if state == "stopped":
214214
pass # no-op
215215
else:
216-
v.set("stopping")
217-
assert v.get() == "stopping"
216+
v.set("stopped")
217+
assert v.get() == "stopped"
218218

219219
def test_nested_with_no_deadlock(self) -> None:
220220
"""RLock should allow the same thread to nest with blocks."""

dimos/utils/thread_utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,6 @@ def start(self) -> None:
301301
302302
@rpc
303303
def stop(self) -> None:
304-
# ModuleProcess.stop() is also called automatically via disposables
305304
super().stop()
306305
"""
307306

0 commit comments

Comments
 (0)