Skip to content

Commit 7244708

Browse files
committed
Refactor config usage to use AppConfig instance
Replaced direct os.getenv calls with values from the AppConfig instance throughout backend modules. Updated environment variable loading and usage in agent models, agent factory, and orchestration manager for consistency and improved maintainability. Added new config options to .env.sample and AppConfig for MCP, Azure Search, and Bing integration.
1 parent cba9d98 commit 7244708

File tree

9 files changed

+47
-36
lines changed

9 files changed

+47
-36
lines changed

src/backend/.env.sample

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,15 @@ AZURE_BING_CONNECTION_NAME=
2121
REASONING_MODEL_NAME=o3
2222
APP_ENV=dev
2323
MCP_SERVER_ENDPOINT=http://localhost:9000/mcp
24+
MCP_SERVER_NAME=MyMC
25+
MCP_SERVER_DESCRIPTION=My MCP Server
26+
TENANT_ID=
27+
CLIENT_ID=
2428
BACKEND_API_URL=http://localhost:8000
25-
FRONTEND_SITE_NAME=*
29+
FRONTEND_SITE_NAME=*
30+
SUPPORTED_MODELS=
31+
AZURE_AI_SEARCH_CONNECTION_NAME=
32+
AZURE_AI_SEARCH_INDEX_NAME=
33+
AZURE_AI_SEARCH_ENDPOINT=
34+
AZURE_AI_SEARCH_API_KEY=
35+
BING_CONNECTION_NAME=

src/backend/app_kernel.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@
1717
InputTask, Plan, PlanStatus,
1818
PlanWithSteps, Step, UserLanguage)
1919
from common.utils.event_utils import track_event_if_configured
20-
from common.utils.utils_date import format_dates_in_messages
20+
2121
# Updated import for KernelArguments
2222
from common.utils.utils_kernel import rai_success
23-
from common.utils.websocket_streaming import (websocket_streaming_endpoint,
24-
ws_manager)
23+
2524
# FastAPI imports
2625
from fastapi import (FastAPI, HTTPException, Query, Request, WebSocket,
2726
WebSocketDisconnect)
@@ -30,7 +29,7 @@
3029
# Local imports
3130
from middleware.health_check import HealthCheckMiddleware
3231
from v3.api.router import app_v3
33-
from v3.config.settings import connection_config
32+
3433
# Semantic Kernel imports
3534
from v3.orchestration.orchestration_manager import OrchestrationManager
3635

src/backend/common/config/app_config.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def __init__(self):
5555
self.AZURE_BING_CONNECTION_NAME = self._get_optional(
5656
"AZURE_BING_CONNECTION_NAME"
5757
)
58-
58+
self.SUPPORTED_MODELS = self._get_optional("SUPPORTED_MODELS")
5959
# Frontend settings
6060
self.FRONTEND_SITE_NAME = self._get_optional(
6161
"FRONTEND_SITE_NAME", "http://127.0.0.1:3000"
@@ -74,6 +74,15 @@ def __init__(self):
7474
# Optional MCP server endpoint (for local MCP server or remote)
7575
# Example: http://127.0.0.1:8000/mcp
7676
self.MCP_SERVER_ENDPOINT = self._get_optional("MCP_SERVER_ENDPOINT")
77+
self.MCP_SERVER_NAME = self._get_optional("MCP_SERVER_NAME")
78+
self.MCP_SERVER_DESCRIPTION = self._get_optional("MCP_SERVER_DESCRIPTION")
79+
self.TENANT_ID = self._get_optional("TENANT_ID")
80+
self.CLIENT_ID = self._get_optional("CLIENT_ID")
81+
self.AZURE_AI_SEARCH_CONNECTION_NAME = self._get_optional("AZURE_AI_SEARCH_CONNECTION_NAME")
82+
self.AZURE_AI_SEARCH_INDEX_NAME = self._get_optional("AZURE_AI_SEARCH_INDEX_NAME")
83+
self.AZURE_AI_SEARCH_ENDPOINT = self._get_optional("AZURE_AI_SEARCH_ENDPOINT")
84+
self.AZURE_AI_SEARCH_API_KEY = self._get_optional("AZURE_AI_SEARCH_API_KEY")
85+
self.BING_CONNECTION_NAME = self._get_optional("BING_CONNECTION_NAME")
7786

7887
test_team_json = self._get_optional("TEST_TEAM_JSON")
7988

src/backend/v3/api/router.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import v3.models.messages as messages
99
from auth.auth_utils import get_authenticated_user_details
10-
from common.config.app_config import config
1110
from common.database.database_factory import DatabaseFactory
1211
from common.models.messages_kernel import (GeneratePlanRequest, InputTask,
1312
TeamSelectionRequest)

src/backend/v3/common/services/foundry_service.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ async def list_model_deployments(self) -> List[Dict[str, Any]]:
5454

5555
try:
5656
# Get Azure Management API token (not Cognitive Services token)
57-
credential = config.get_azure_credentials()
58-
token = credential.get_token(config.AZURE_MANAGEMENT_SCOPE)
57+
token = config.get_access_token()
5958

6059
# Extract Azure OpenAI resource name from endpoint URL
6160
openai_endpoint = config.AZURE_OPENAI_ENDPOINT

src/backend/v3/magentic_agents/magentic_agent_factory.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
SearchConfig)
1616
from v3.magentic_agents.proxy_agent import ProxyAgent
1717
from v3.magentic_agents.reasoning_agent import ReasoningAgentTemplate
18-
18+
from common.config.app_config import config
1919

2020
class UnsupportedModelError(Exception):
2121
"""Raised when an unsupported model is specified."""
@@ -65,7 +65,7 @@ async def create_agent_from_config(self, agent_obj: SimpleNamespace) -> Union[Fo
6565
return ProxyAgent(user_id=user_id)
6666

6767
# Validate supported models
68-
supported_models = json.loads(os.getenv("SUPPORTED_MODELS"))
68+
supported_models = json.loads(config.SUPPORTED_MODELS)
6969

7070
if deployment_name not in supported_models:
7171
raise UnsupportedModelError(f"Model '{deployment_name}' not supported. Supported: {supported_models}")
@@ -95,7 +95,7 @@ async def create_agent_from_config(self, agent_obj: SimpleNamespace) -> Union[Fo
9595
# Create appropriate agent
9696
if use_reasoning:
9797
# Get reasoning specific configuration
98-
azure_openai_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
98+
azure_openai_endpoint = config.AZURE_OPENAI_ENDPOINT
9999

100100
agent = ReasoningAgentTemplate(
101101
agent_name=agent_obj.name,

src/backend/v3/magentic_agents/models/agent_models.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import os
44
from dataclasses import dataclass
5-
5+
from common.config.app_config import config
66

77
@dataclass(slots=True)
88
class MCPConfig:
@@ -15,12 +15,12 @@ class MCPConfig:
1515

1616
@classmethod
1717
def from_env(cls) -> "MCPConfig":
18-
url = os.getenv("MCP_SERVER_ENDPOINT")
19-
name = os.getenv("MCP_SERVER_NAME")
20-
description = os.getenv("MCP_SERVER_DESCRIPTION")
21-
tenant_id = os.getenv("TENANT_ID")
22-
client_id = os.getenv("CLIENT_ID")
23-
18+
url = config.MCP_SERVER_ENDPOINT
19+
name = config.MCP_SERVER_NAME
20+
description = config.MCP_SERVER_DESCRIPTION
21+
tenant_id = config.TENANT_ID
22+
client_id = config.CLIENT_ID
23+
2424
# Raise exception if any required environment variable is missing
2525
if not all([url, name, description, tenant_id, client_id]):
2626
raise ValueError(f"{cls.__name__} Missing required environment variables")
@@ -40,7 +40,7 @@ class BingConfig:
4040

4141
@classmethod
4242
def from_env(cls) -> "BingConfig":
43-
connection_name = os.getenv("BING_CONNECTION_NAME")
43+
connection_name = config.BING_CONNECTION_NAME
4444

4545
# Raise exception if required environment variable is missing
4646
if not connection_name:
@@ -60,11 +60,11 @@ class SearchConfig:
6060

6161
@classmethod
6262
def from_env(cls) -> "SearchConfig":
63-
connection_name = os.getenv("AZURE_AI_SEARCH_CONNECTION_NAME")
64-
index_name = os.getenv("AZURE_AI_SEARCH_INDEX_NAME")
65-
endpoint = os.getenv("AZURE_AI_SEARCH_ENDPOINT")
66-
api_key = os.getenv("AZURE_AI_SEARCH_API_KEY")
67-
63+
connection_name = config.AZURE_AI_SEARCH_CONNECTION_NAME
64+
index_name = config.AZURE_AI_SEARCH_INDEX_NAME
65+
endpoint = config.AZURE_AI_SEARCH_ENDPOINT
66+
api_key = config.AZURE_AI_SEARCH_API_KEY
67+
6868
# Raise exception if any required environment variable is missing
6969
if not all([connection_name, index_name, endpoint]):
7070
raise ValueError(f"{cls.__name__} Missing required Azure Search environment variables")

src/backend/v3/magentic_agents/reasoning_agent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from v3.magentic_agents.common.lifecycle import MCPEnabledBase
1010
from v3.magentic_agents.models.agent_models import MCPConfig, SearchConfig
1111
from v3.magentic_agents.reasoning_search import ReasoningSearch
12-
12+
from common.config.app_config import config
1313

1414
class ReasoningAgentTemplate(MCPEnabledBase):
1515
"""
@@ -47,7 +47,7 @@ def ad_token_provider() -> str:
4747
chat = AzureChatCompletion(
4848
deployment_name=self._model_deployment_name,
4949
endpoint=self._openai_endpoint,
50-
ad_token_provider=ad_token_provider
50+
ad_token_provider=config.get_access_token()
5151
)
5252
self.kernel.add_service(chat)
5353

src/backend/v3/orchestration/orchestration_manager.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
streaming_agent_response_callback)
2121
from v3.config.settings import (config, connection_config, current_user_id,
2222
orchestration_config)
23+
from common.config.app_config import config
2324
from v3.magentic_agents.magentic_agent_factory import MagenticAgentFactory
2425
from v3.orchestration.human_approval_manager import \
2526
HumanApprovalMagenticManager
@@ -43,21 +44,15 @@ async def init_orchestration(cls, agents: List, user_id: str = None)-> MagenticO
4344
temperature=0.1
4445
)
4546

46-
# Create a token provider function for Azure OpenAI
47-
credential = SyncDefaultAzureCredential()
48-
49-
def get_token():
50-
token = credential.get_token("https://cognitiveservices.azure.com/.default")
51-
return token.token
5247

5348
# 1. Create a Magentic orchestration with Azure OpenAI
5449
magentic_orchestration = MagenticOrchestration(
5550
members=agents,
5651
manager=HumanApprovalMagenticManager(
5752
chat_completion_service=AzureChatCompletion(
58-
deployment_name=os.getenv("AZURE_OPENAI_MODEL_NAME"),
59-
endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
60-
ad_token_provider=get_token # Use token provider function
53+
deployment_name=config.AZURE_OPENAI_DEPLOYMENT_NAME,
54+
endpoint=config.AZURE_OPENAI_ENDPOINT,
55+
ad_token_provider=config.get_access_token()
6156
),
6257
execution_settings=execution_settings
6358
),

0 commit comments

Comments
 (0)