Coverage for mcpgateway / utils / internal_http.py: 100%

9 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-06 00:56 +0100

1# -*- coding: utf-8 -*- 

2"""Location: ./mcpgateway/utils/internal_http.py 

3Copyright 2026 

4SPDX-License-Identifier: Apache-2.0 

5Authors: Mihai Criveti 

6 

7Helpers for gateway-internal loopback HTTP calls. 

8 

9These helpers centralize protocol and TLS verification behavior for 

10self-calls to local endpoints like /rpc. 

11""" 

12 

13# Standard 

14import os 

15 

16# First-Party 

17from mcpgateway.config import settings 

18 

19 

20def _is_ssl_enabled() -> bool: 

21 """Check whether the gateway is running with SSL enabled. 

22 

23 Returns: 

24 bool: ``True`` when ``SSL=true`` is set in the environment. 

25 """ 

26 return os.getenv("SSL", "false") == "true" 

27 

28 

29def internal_loopback_base_url() -> str: 

30 """Return loopback base URL for gateway self-calls. 

31 

32 Uses HTTPS when runtime is started with SSL=true, otherwise HTTP. 

33 

34 Returns: 

35 str: The base URL string (e.g. ``http://127.0.0.1:4444``). 

36 """ 

37 scheme = "https" if _is_ssl_enabled() else "http" 

38 return f"{scheme}://127.0.0.1:{settings.port}" 

39 

40 

41def internal_loopback_verify() -> bool: 

42 """Return TLS verification policy for loopback self-calls. 

43 

44 Loopback HTTPS frequently uses a self-signed local cert, so verification 

45 is disabled for HTTPS loopback self-calls and enabled otherwise. 

46 

47 Returns: 

48 bool: ``False`` when the loopback URL is HTTPS, ``True`` otherwise. 

49 """ 

50 return not _is_ssl_enabled()