π The fast, Pythonic way to build MCP servers and clients.
Bypass proxy env when fastmcp dev apps lists tools (#6422)
nate nowack59cc67115cd0
diff --git a/fastmcp_slim/fastmcp/cli/apps_dev.py b/fastmcp_slim/fastmcp/cli/apps_dev.pyindex 8ad95463c..546ed88d6 100644--- a/fastmcp_slim/fastmcp/cli/apps_dev.py+++ b/fastmcp_slim/fastmcp/cli/apps_dev.py@@ -1100,6 +1100,11 @@ def _build_picker_html(tools: list[dict[str, Any]]) -> str:# ---------------------------------------------------------------------------+def _local_http_client() -> httpx2.AsyncClient:+ """Client for the local MCP server, which is never reached through a proxy."""+ return httpx2.AsyncClient(timeout=httpx2.Timeout(30.0, read=300.0), trust_env=False)++async def _list_tools(mcp_url: str) -> list[dict[str, Any]]:"""Return raw tool dicts from the MCP server at mcp_url."""try:@@ -1109,11 +1114,14 @@ async def _list_tools(mcp_url: str) -> list[dict[str, Any]]:return []try:- async with streamable_http_client(mcp_url) as (read, write): # noqa: SIM117- async with ClientSession(read, write) as session:- await session.initialize()- result = await session.list_tools()- return [t.model_dump(by_alias=True) for t in result.tools]+ async with (+ _local_http_client() as http_client,+ streamable_http_client(mcp_url, http_client=http_client) as (read, write),+ ClientSession(read, write) as session,+ ):+ await session.initialize()+ result = await session.list_tools()+ return [t.model_dump(by_alias=True) for t in result.tools]except Exception as exc:logger.debug(f"Could not list tools from {mcp_url}: {exc}")return []@@ -1128,14 +1136,17 @@ async def _read_mcp_resource(mcp_url: str, uri: str) -> str | None:return Nonetry:- async with streamable_http_client(mcp_url) as (read, write): # noqa: SIM117- async with ClientSession(read, write) as session:- await session.initialize()- result = await session.read_resource(uri)- for content in result.contents:- text = getattr(content, "text", None)- if text:- return text+ async with (+ _local_http_client() as http_client,+ streamable_http_client(mcp_url, http_client=http_client) as (read, write),+ ClientSession(read, write) as session,+ ):+ await session.initialize()+ result = await session.read_resource(uri)+ for content in result.contents:+ text = getattr(content, "text", None)+ if text:+ return textreturn Noneexcept Exception as exc:logger.debug(f"Could not read resource {uri} from {mcp_url}: {exc}")diff --git a/tests/cli/test_apps_dev_server.py b/tests/cli/test_apps_dev_server.pyindex 38f151b02..b3dbe69a8 100644--- a/tests/cli/test_apps_dev_server.py+++ b/tests/cli/test_apps_dev_server.py@@ -1,6 +1,7 @@"""Sessions, launches, and app frames in the `fastmcp dev apps` server."""import html+from collections.abc import AsyncIteratorfrom typing import Anyfrom unittest.mock import AsyncMock@@ -10,6 +11,8 @@ from starlette.testclient import TestClientfrom starlette.websockets import WebSocketDisconnectimport fastmcp.cli.apps_dev as apps_dev+from fastmcp import FastMCP+from fastmcp.utilities.tests import find_available_port, run_server_asyncTOKEN = "test-process-session-token"COOKIE = "fastmcp_dev_session_8080"@@ -722,6 +725,48 @@ class TestSpawnedServer:assert env["FASTMCP_HTTP_HOST_ORIGIN_PROTECTION"] == "true"+class TestServerHelpers:+ """The picker talks to the local MCP server directly, like the readiness check."""++ @pytest.fixture+ def unreachable_proxy(self, monkeypatch: pytest.MonkeyPatch) -> None:+ proxy = f"http://127.0.0.1:{find_available_port()}"+ for name in ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY"):+ monkeypatch.setenv(name, proxy)+ monkeypatch.setenv(name.lower(), proxy)+ monkeypatch.delenv("NO_PROXY", raising=False)+ monkeypatch.delenv("no_proxy", raising=False)++ @pytest.fixture+ async def mcp_url(self) -> AsyncIterator[str]:+ server = FastMCP("helpers")++ @server.tool+ def greet(name: str) -> str:+ return f"Hello, {name}!"++ @server.resource("ui://helpers/view.html")+ def view() -> str:+ return "<html>view</html>"++ async with run_server_async(server) as url:+ yield url++ async def test_list_tools_ignores_proxy_environment(+ self, unreachable_proxy: None, mcp_url: str+ ):+ tools = await apps_dev._list_tools(mcp_url)++ assert [tool["name"] for tool in tools] == ["greet"]++ async def test_read_resource_ignores_proxy_environment(+ self, unreachable_proxy: None, mcp_url: str+ ):+ text = await apps_dev._read_mcp_resource(mcp_url, "ui://helpers/view.html")++ assert text == "<html>view</html>"++class TestNpmRegistry:@pytest.fixture(autouse=True)def _clear_registry_env(self, monkeypatch: pytest.MonkeyPatch):