Skip to content

Commit 424fd8e

Browse files
refactor: extract core logic of router
1 parent c72f637 commit 424fd8e

File tree

12 files changed

+1020
-941
lines changed

12 files changed

+1020
-941
lines changed

src/mcpm/commands/router.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
from rich.prompt import Confirm
1717

1818
from mcpm.clients.client_registry import ClientRegistry
19+
from mcpm.core.utils.log_manager import get_log_directory
1920
from mcpm.router.share import Tunnel
2021
from mcpm.utils.config import MCPM_AUTH_HEADER, MCPM_PROFILE_HEADER, ConfigManager
21-
from mcpm.utils.platform import get_log_directory, get_pid_directory
22+
from mcpm.utils.platform import get_pid_directory
2223

2324
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
2425
logger = logging.getLogger(__name__)
File renamed without changes.

src/mcpm/core/router/router.py

Lines changed: 377 additions & 0 deletions
Large diffs are not rendered by default.

src/mcpm/utils/errlog_manager.py renamed to src/mcpm/core/utils/log_manager.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,45 @@
11
import os
2+
import sys
23
from pathlib import Path
34
from typing import Optional, TextIO
45

5-
from .platform import get_log_directory
6+
7+
def get_log_directory(app_name: str = "mcpm") -> Path:
8+
"""
9+
Return the appropriate log directory path based on the current operating system.
10+
11+
Args:
12+
app_name: The name of the application, used in the path
13+
14+
Returns:
15+
Path object representing the log directory
16+
"""
17+
# macOS
18+
if sys.platform == "darwin":
19+
return Path.home() / "Library" / "Logs" / app_name / "logs"
20+
21+
# Windows
22+
elif sys.platform == "win32":
23+
localappdata = os.environ.get("LOCALAPPDATA")
24+
if localappdata:
25+
return Path(localappdata) / app_name / "logs"
26+
return Path.home() / "AppData" / "Local" / app_name / "logs"
27+
28+
# Linux and other Unix-like systems
29+
else:
30+
# Check if XDG_DATA_HOME is defined
31+
xdg_data_home = os.environ.get("XDG_DATA_HOME")
32+
if xdg_data_home:
33+
return Path(xdg_data_home) / app_name / "logs"
34+
35+
# Default to ~/.local/share if XDG_DATA_HOME is not defined
36+
return Path.home() / ".local" / "share" / app_name / "logs"
37+
638

739
DEFAULT_ROOT_STDERR_LOG_DIR = get_log_directory("mcpm") / "errlogs"
840

941

10-
class ServerErrorLogManager:
42+
class ServerLogManager:
1143
"""
1244
A manager for server error logs.
1345
"""

src/mcpm/monitor/event.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
TextContent,
1515
)
1616

17-
from mcpm.utils.config import PROMPT_SPLITOR, RESOURCE_SPLITOR, TOOL_SPLITOR
17+
from mcpm.core.router.router import PROMPT_SPLITOR, RESOURCE_SPLITOR, TOOL_SPLITOR
1818

1919
from .base import AccessEventType
2020
from .duckdb import DuckDBAccessMonitor

src/mcpm/router/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import logging
22
import os
33

4+
from mcpm.core.utils.log_manager import get_log_directory
45
from mcpm.monitor.event import monitor
56
from mcpm.router.router import MCPRouter
67
from mcpm.router.router_config import RouterConfig
78
from mcpm.utils.config import ConfigManager
8-
from mcpm.utils.platform import get_log_directory
99

1010
LOG_DIR = get_log_directory("mcpm")
1111
LOG_DIR.mkdir(parents=True, exist_ok=True)

src/mcpm/router/router.py

Lines changed: 34 additions & 365 deletions
Large diffs are not rendered by default.

src/mcpm/router/sse_app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
from starlette.routing import Mount, Route
1313
from starlette.types import Receive, Scope, Send
1414

15+
from mcpm.core.utils.log_manager import get_log_directory
1516
from mcpm.monitor.base import AccessEventType
1617
from mcpm.monitor.event import monitor
1718
from mcpm.router.router import MCPRouter
1819
from mcpm.router.transport import RouterSseTransport
1920
from mcpm.utils.config import ConfigManager
20-
from mcpm.utils.platform import get_log_directory
2121

2222
LOG_DIR = get_log_directory("mcpm")
2323
LOG_DIR.mkdir(parents=True, exist_ok=True)

src/mcpm/utils/config.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616
DEFAULT_HOST = "localhost"
1717
DEFAULT_PORT = 6276 # 6276 represents MCPM on a T9 keypad (6=M, 2=C, 7=P, 6=M)
1818
# default splitor pattern
19-
TOOL_SPLITOR = "_t_"
20-
RESOURCE_SPLITOR = ":"
21-
RESOURCE_TEMPLATE_SPLITOR = ":"
22-
PROMPT_SPLITOR = "_p_"
2319
DEFAULT_SHARE_ADDRESS = f"share.mcpm.sh:{DEFAULT_PORT}"
2420
MCPM_AUTH_HEADER = "X-MCPM-SECRET"
2521
MCPM_PROFILE_HEADER = "X-MCPM-PROFILE"

src/mcpm/utils/platform.py

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,6 @@
99
from pathlib import Path
1010

1111

12-
def get_log_directory(app_name: str = "mcpm") -> Path:
13-
"""
14-
Return the appropriate log directory path based on the current operating system.
15-
16-
Args:
17-
app_name: The name of the application, used in the path
18-
19-
Returns:
20-
Path object representing the log directory
21-
"""
22-
# macOS
23-
if sys.platform == "darwin":
24-
return Path.home() / "Library" / "Logs" / app_name / "logs"
25-
26-
# Windows
27-
elif sys.platform == "win32":
28-
localappdata = os.environ.get("LOCALAPPDATA")
29-
if localappdata:
30-
return Path(localappdata) / app_name / "logs"
31-
return Path.home() / "AppData" / "Local" / app_name / "logs"
32-
33-
# Linux and other Unix-like systems
34-
else:
35-
# Check if XDG_DATA_HOME is defined
36-
xdg_data_home = os.environ.get("XDG_DATA_HOME")
37-
if xdg_data_home:
38-
return Path(xdg_data_home) / app_name / "logs"
39-
40-
# Default to ~/.local/share if XDG_DATA_HOME is not defined
41-
return Path.home() / ".local" / "share" / app_name / "logs"
42-
43-
4412
def get_pid_directory(app_name: str = "mcpm") -> Path:
4513
"""
4614
Return the appropriate PID directory path based on the current operating system.

0 commit comments

Comments
 (0)