Skip to content

Commit 2d2e1bf

Browse files
committed
feat: configuration & logging
1 parent e70d8b6 commit 2d2e1bf

File tree

6 files changed

+95
-1
lines changed

6 files changed

+95
-1
lines changed

.env.example.local

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ENVIRONMENT="local"

.env.example.production

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ENVIRONMENT="production"
2+
BASE_URL="https://your-url-here.com/

mcp_server/logging.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import logging
2+
3+
from .settings import auto_config as cfg
4+
5+
if not cfg:
6+
raise RuntimeError(
7+
"configuration is not set. Please set the ENVIRONMENT environment variable."
8+
)
9+
10+
11+
logging.basicConfig(
12+
level=getattr(logging, cfg.LOG_LEVEL.upper()), format=cfg.LOG_FORMAT
13+
)
14+
15+
logger = logging.getLogger(cfg.MCP_SERVER_NAME)

mcp_server/main.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
from mcp.server.fastmcp import FastMCP
22

3-
mcp = FastMCP()
3+
from .logging import logger
4+
from .settings import auto_config as cfg
5+
6+
if not cfg:
7+
raise RuntimeError(
8+
"configuration is not set. Please set the ENVIRONMENT environment variable."
9+
)
10+
11+
mcp = FastMCP(name=cfg.MCP_SERVER_NAME)
12+
13+
logger.info(f"Starting {cfg.MCP_SERVER_NAME} v{cfg.MCP_SERVER_VERSION}")
14+
logger.debug(f"Debug mode: {cfg.DEBUG}")
15+
logger.info(f"Base URL: {cfg.BASE_URL}")
16+
logger.debug(f"Log level: {cfg.LOG_LEVEL}")

mcp_server/settings/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import os
2+
3+
from .config import Common, Local, Production, Staging
4+
5+
environment: str | None = os.getenv("ENVIRONMENT", None)
6+
7+
if not environment:
8+
raise EnvironmentError("ENVIRONMENT environment variable is not set!")
9+
10+
auto_config: type[Common] | None = None
11+
if environment == "local":
12+
auto_config = Local
13+
elif environment == "staging":
14+
auto_config = Staging
15+
elif environment == "production":
16+
auto_config = Production
17+
18+
19+
if not auto_config:
20+
raise RuntimeError(
21+
"Configuration is not set. Please set the ENVIRONMENT environment variable."
22+
)

mcp_server/settings/config.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import os
2+
3+
4+
class Common:
5+
"""Base configuration class with common settings"""
6+
7+
# MCP Server Settings
8+
MCP_SERVER_NAME: str = "mcp-production"
9+
MCP_SERVER_VERSION: str = "0.1.0"
10+
11+
# Application Settings
12+
DEBUG: bool = False
13+
BASE_URL: str = "http://localhost:8000"
14+
15+
# Logging
16+
LOG_LEVEL: str = "INFO"
17+
LOG_FORMAT: str = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
18+
19+
20+
class Local(Common):
21+
"""Local development configuration"""
22+
23+
DEBUG: bool = True
24+
LOG_LEVEL: str = os.getenv("SERVER_LOG_LEVEL", "DEBUG")
25+
BASE_URL: str = "http://localhost:8000"
26+
27+
28+
class Production(Common):
29+
"""Production configuration"""
30+
31+
DEBUG: bool = False
32+
LOG_LEVEL: str = os.getenv("SERVER_LOG_LEVEL", "WARNING")
33+
BASE_URL: str = os.getenv("PRODUCTION_BASE_URL", "https://api.production.com")
34+
35+
36+
class Staging(Production):
37+
"""Staging configuration - inherits from Production but with debug enabled"""
38+
39+
DEBUG: bool = True
40+
LOG_LEVEL: str = os.getenv("SERVER_LOG_LEVEL", "INFO")
41+
BASE_URL: str = os.getenv("STAGING_BASE_URL", "https://api.staging.com")

0 commit comments

Comments
 (0)