Skip to content

Commit 50f2570

Browse files
perf: implement settings singleton pattern (#134)
* perf: implement settings singleton pattern * chore: run pre-commit hook
1 parent d122674 commit 50f2570

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

modelcontextprotocol/.cursor/rules/project-structure.mdc

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,13 @@ Factory for creating and configuring the Atlan client using application settings
117117
```python
118118
import logging
119119
from pyatlan.client.atlan import AtlanClient
120-
from settings import Settings
120+
from settings import get_settings
121121

122122
logger = logging.getLogger(__name__)
123123

124124
def get_atlan_client() -> AtlanClient:
125125
"""Create an Atlan client instance using settings loaded from environment."""
126-
settings = Settings()
126+
settings = get_settings()
127127
try:
128128
client = AtlanClient(
129129
base_url=settings.ATLAN_BASE_URL, api_key=settings.ATLAN_API_KEY
@@ -134,6 +134,22 @@ def get_atlan_client() -> AtlanClient:
134134
except Exception as e:
135135
logger.error(f"Error creating Atlan client: {e}")
136136
raise Exception(f"Error creating Atlan client: {e}")
137+
138+
_settings: Optional[Settings] = None
139+
140+
141+
def get_settings() -> Settings:
142+
"""
143+
Get the singleton Settings instance.
144+
Loads settings once from environment/file and reuses the instance.
145+
146+
Returns:
147+
Settings: The singleton settings instance
148+
"""
149+
global _settings
150+
if _settings is None:
151+
_settings = Settings()
152+
return _settings
137153
```
138154

139155
### tools/ Directory

modelcontextprotocol/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import Optional
55

66
from pyatlan.client.atlan import AtlanClient
7-
from settings import Settings
7+
from settings import get_settings
88

99
logger = logging.getLogger(__name__)
1010

@@ -24,7 +24,7 @@ def get_atlan_client() -> AtlanClient:
2424
global _client_instance
2525

2626
if _client_instance is None:
27-
settings = Settings()
27+
settings = get_settings()
2828
try:
2929
_client_instance = AtlanClient(
3030
base_url=settings.ATLAN_BASE_URL, api_key=settings.ATLAN_API_KEY

modelcontextprotocol/settings.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Configuration settings for the application."""
22

3+
from typing import Optional
34
from pydantic_settings import BaseSettings
45
from version import __version__ as MCP_VERSION
56

@@ -29,3 +30,20 @@ class Config:
2930
extra = "allow"
3031
# Allow case-insensitive environment variables
3132
case_sensitive = False
33+
34+
35+
_settings: Optional[Settings] = None
36+
37+
38+
def get_settings() -> Settings:
39+
"""
40+
Get the singleton Settings instance.
41+
Loads settings once from environment/file and reuses the instance.
42+
43+
Returns:
44+
Settings: The singleton settings instance
45+
"""
46+
global _settings
47+
if _settings is None:
48+
_settings = Settings()
49+
return _settings

0 commit comments

Comments
 (0)