commit 6bc51b55638d7a3b6b8c2f5e319b0b6942550e83
parent 9f80a855401c37bb4d5e786369dcd0c760bd6a1c
Author: Henry Unite <unitehenry@gmail.com>
Date: Sat, 25 Jul 2026 23:57:08 +0000
feat: resolve_hostname
Diffstat:
2 files changed, 33 insertions(+), 1 deletion(-)
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
@@ -49,7 +49,7 @@ jobs:
run: |
docker run -d \
--network=mcp \
- -e CDP_PORT="http://$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' xpod-chromium):9222" \
+ -e CDP_PORT="http://xpod-chromium:9222" \
-p 127.0.0.1:8000:8000 \
--name agent-browser-mcp \
agent-browser-mcp
diff --git a/main.py b/main.py
@@ -11,6 +11,38 @@ from fastmcp.tools import Tool
from fastmcp.tools.tool_transform import ArgTransform
+def resolve_hostname_in_url(url: str) -> str:
+ """
+ Attempt to resolve the hostname in a URL to an IP address and replace it.
+ Returns the original URL unchanged if resolution fails or no hostname is present.
+
+ Example:
+ resolve_hostname_in_url("http://xpod-chromium:9222")
+ → "http://172.17.0.2:9222" (or whatever the IP is)
+ """
+ from urllib.parse import urlparse, urlunparse
+ import socket
+
+ parsed = urlparse(url)
+
+ if not parsed.hostname:
+ return url
+
+ try:
+ ip = socket.gethostbyname(parsed.hostname)
+ except socket.gaierror:
+ return url
+
+ # Rebuild netloc (host:port)
+ netloc = ip
+
+ if parsed.port is not None:
+ netloc = f"{ip}:{parsed.port}"
+
+ new_parsed = parsed._replace(netloc=netloc)
+
+ return urlunparse(new_parsed)
+
class AuthMiddleware(Middleware):
async def on_request(self, context: MiddlewareContext, call_next):
token = get_access_token()