Skip to content

Commit 9b4721b

Browse files
committed
fix: plugin settings env override
Signed-off-by: Frederico Araujo <frederico.araujo@ibm.com>
1 parent 4c75b73 commit 9b4721b

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

mcpgateway/plugins/framework/settings.py

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
"""
1313

1414
# Standard
15-
from typing import Literal
15+
from functools import lru_cache
16+
import os
17+
from typing import Any, Literal
1618

1719
# Third-Party
1820
from pydantic import AliasChoices, Field
@@ -99,4 +101,56 @@ class PluginsSettings(BaseSettings):
99101
model_config = SettingsConfigDict(env_prefix="PLUGINS_")
100102

101103

102-
settings = PluginsSettings()
104+
@lru_cache()
105+
def get_settings(**kwargs: Any) -> PluginsSettings:
106+
"""Get cached plugins settings instance.
107+
108+
Args:
109+
**kwargs: Keyword arguments to pass to the PluginsSettings setup.
110+
111+
Returns:
112+
PluginsSettings: A cached instance of the PluginsSettings class.
113+
114+
Examples:
115+
>>> settings = get_settings()
116+
>>> isinstance(settings, PluginsSettings)
117+
True
118+
>>> # Second call returns the same cached instance
119+
>>> settings2 = get_settings()
120+
>>> settings is settings2
121+
True
122+
"""
123+
# Instantiate a fresh Pydantic PluginsSettings object,
124+
# loading from env vars or .env exactly once.
125+
return PluginsSettings(**kwargs)
126+
127+
128+
class LazySettingsWrapper:
129+
"""Lazily initialize plugins settings singleton on getattr with override support."""
130+
131+
@property
132+
def enabled(self) -> bool:
133+
"""Access plugin enabled flag with env override support.
134+
135+
Returns:
136+
True if plugin framework is enabled.
137+
"""
138+
env_flag = os.getenv("PLUGINS_ENABLED")
139+
if env_flag is not None:
140+
return env_flag.strip().lower() in {"1", "true", "yes", "on"}
141+
return False
142+
143+
def __getattr__(self, key: str) -> Any:
144+
"""Get the real settings object and forward to it
145+
146+
Args:
147+
key: The key to fetch from settings
148+
149+
Returns:
150+
Any: The value of the attribute on the settings
151+
"""
152+
153+
return getattr(get_settings(), key)
154+
155+
156+
settings = LazySettingsWrapper()

tests/unit/mcpgateway/plugins/framework/test_settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ class TestPluginsSettingsModuleSingleton:
249249

250250
def test_module_settings_instance_exists(self):
251251
from mcpgateway.plugins.framework.settings import settings
252-
assert isinstance(settings, PluginsSettings)
252+
assert settings
253253

254254
def test_module_settings_is_stable_reference(self):
255255
from mcpgateway.plugins.framework.settings import settings as s1

0 commit comments

Comments
 (0)