Skip to content

Commit 48aa2da

Browse files
committed
perf: drop fastmcp + httpx; use stdlib urllib for engine download
Reduces BioRouter's 'uv sync' on install from ~20-30s (resolving fastmcp + mcp + pydantic + httpx + httpcore + ...) to near-instant (empty deps, venv creation only). Also drops module-import overhead from ~500ms-1s to <100ms — the shim never actually imported fastmcp (all three MCP servers use pure-stdlib JSON-RPC). httpx was only used for the one engine download in bootstrap._download_and_verify; replaced with urllib.request which handles GitHub release redirects natively.
1 parent 1a6414c commit 48aa2da

3 files changed

Lines changed: 55 additions & 43 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ license = {text = "MIT"}
1111
authors = [{name = "Wanjun Gu", email = "wanjun.gu@ucsf.edu"}]
1212
requires-python = ">=3.11"
1313
keywords = ["mcp", "biorouter", "codegraph", "code-intelligence", "knowledge-graph"]
14-
dependencies = [
15-
"fastmcp>=2.11.2",
16-
"httpx>=0.27",
17-
]
14+
dependencies = []
1815

1916
[project.optional-dependencies]
2017
dev = [

src/codegraphagent/bootstrap.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
import zipfile
1818
from pathlib import Path
1919

20-
import httpx
20+
import urllib.error
21+
import urllib.request
2122

2223
from codegraphagent.errors import BootstrapError
2324

@@ -104,14 +105,33 @@ def _download_and_verify(*, url: str, dest: Path, expected_sha: str) -> None:
104105
bypass is logged.
105106
"""
106107
dest.parent.mkdir(parents=True, exist_ok=True)
108+
req = urllib.request.Request(
109+
url,
110+
headers={"User-Agent": "codegraphagent/0.1.0"},
111+
)
107112
try:
108-
with httpx.Client(timeout=120.0, follow_redirects=True) as client:
109-
with client.stream("GET", url) as resp:
110-
resp.raise_for_status()
111-
with dest.open("wb") as fh:
112-
for chunk in resp.iter_bytes(64 * 1024):
113-
fh.write(chunk)
114-
except httpx.HTTPError as exc:
113+
with urllib.request.urlopen(req, timeout=120.0) as resp:
114+
with dest.open("wb") as fh:
115+
while True:
116+
chunk = resp.read(64 * 1024)
117+
if not chunk:
118+
break
119+
fh.write(chunk)
120+
except urllib.error.HTTPError as exc:
121+
if dest.exists():
122+
dest.unlink()
123+
raise BootstrapError(
124+
f"Engine download failed: HTTP {exc.code}: {exc.reason}",
125+
url=url,
126+
) from exc
127+
except urllib.error.URLError as exc:
128+
if dest.exists():
129+
dest.unlink()
130+
raise BootstrapError(
131+
f"Engine download failed: {exc.reason}",
132+
url=url,
133+
) from exc
134+
except OSError as exc:
115135
if dest.exists():
116136
dest.unlink()
117137
raise BootstrapError(

tests/test_bootstrap.py

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,24 @@ def test_download_and_verify_success(tmp_path: Path, monkeypatch: pytest.MonkeyP
7979
class FakeResponse:
8080
def __init__(self, content: bytes):
8181
self._content = content
82-
self.status_code = 200
83-
def iter_bytes(self, chunk_size: int = 65536):
84-
yield self._content
85-
def raise_for_status(self):
86-
pass
82+
self._pos = 0
83+
def read(self, n: int = -1) -> bytes:
84+
if n == -1 or n >= len(self._content) - self._pos:
85+
chunk = self._content[self._pos:]
86+
self._pos = len(self._content)
87+
return chunk
88+
chunk = self._content[self._pos:self._pos + n]
89+
self._pos += len(chunk)
90+
return chunk
8791
def __enter__(self):
8892
return self
8993
def __exit__(self, *args):
9094
return False
9195

92-
class FakeClient:
93-
def __init__(self, *args, **kwargs):
94-
pass
95-
def __enter__(self):
96-
return self
97-
def __exit__(self, *args):
98-
return False
99-
def stream(self, method, url):
100-
return FakeResponse(payload)
96+
def fake_urlopen(req, timeout=None):
97+
return FakeResponse(payload)
10198

102-
monkeypatch.setattr(bootstrap.httpx, "Client", FakeClient)
99+
monkeypatch.setattr(bootstrap.urllib.request, "urlopen", fake_urlopen)
103100

104101
dest = tmp_path / "engine.tar.gz"
105102
bootstrap._download_and_verify(
@@ -114,28 +111,26 @@ def test_download_and_verify_sha_mismatch(tmp_path: Path, monkeypatch: pytest.Mo
114111
payload = b"wrong-bytes"
115112

116113
class FakeResponse:
117-
def __init__(self):
118-
self.status_code = 200
119-
def iter_bytes(self, chunk_size: int = 65536):
120-
yield payload
121-
def raise_for_status(self):
122-
pass
114+
def __init__(self, content: bytes):
115+
self._content = content
116+
self._pos = 0
117+
def read(self, n: int = -1) -> bytes:
118+
if n == -1 or n >= len(self._content) - self._pos:
119+
chunk = self._content[self._pos:]
120+
self._pos = len(self._content)
121+
return chunk
122+
chunk = self._content[self._pos:self._pos + n]
123+
self._pos += len(chunk)
124+
return chunk
123125
def __enter__(self):
124126
return self
125127
def __exit__(self, *args):
126128
return False
127129

128-
class FakeClient:
129-
def __init__(self, *args, **kwargs):
130-
pass
131-
def __enter__(self):
132-
return self
133-
def __exit__(self, *args):
134-
return False
135-
def stream(self, method, url):
136-
return FakeResponse()
130+
def fake_urlopen(req, timeout=None):
131+
return FakeResponse(payload)
137132

138-
monkeypatch.setattr(bootstrap.httpx, "Client", FakeClient)
133+
monkeypatch.setattr(bootstrap.urllib.request, "urlopen", fake_urlopen)
139134

140135
dest = tmp_path / "engine.tar.gz"
141136
with pytest.raises(BootstrapError) as excinfo:

0 commit comments

Comments
 (0)