Skip to content

Commit abe5ae2

Browse files
committed
fix: change default log path to user home dir to avoid read-only errors
1 parent 575a5a8 commit abe5ae2

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

src/ha_mcp/utils/usage_logger.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,22 @@ class UsageLogger:
108108

109109
def __init__(
110110
self,
111-
log_file_path: str = "logs/mcp_usage.jsonl",
111+
log_file_path: str | None = None,
112112
ring_buffer_size: int = DEFAULT_RING_BUFFER_SIZE,
113113
):
114114
self._enabled = True
115-
self.log_file_path = Path(log_file_path)
115+
116+
if log_file_path:
117+
self.log_file_path = Path(log_file_path)
118+
else:
119+
# Use user's home directory by default to avoid read-only filesystem errors
120+
# when running via uvx/npx which might have read-only CWD
121+
self.log_file_path = Path.home() / ".ha-mcp" / "logs" / "mcp_usage.jsonl"
122+
116123
try:
117124
self.log_file_path.parent.mkdir(parents=True, exist_ok=True)
118125
except OSError:
119-
# Directory creation failed (e.g., read-only filesystem when running via uvx)
126+
# Directory creation failed (e.g., read-only filesystem)
120127
# Disable logging silently to avoid disrupting the MCP server
121128
self._enabled = False
122129

tests/src/unit/test_usage_logger.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,17 @@ def test_ring_buffer_with_error_entries(self, logger):
167167
assert entries[0]["error_message"] == "Entity not found"
168168

169169

170+
class TestUsageLoggerDefaults:
171+
"""Test UsageLogger default behavior."""
172+
173+
def test_default_log_path(self):
174+
"""Test that default log path is in user home directory."""
175+
logger = UsageLogger()
176+
assert str(logger.log_file_path).startswith(str(Path.home()))
177+
assert ".ha-mcp" in str(logger.log_file_path)
178+
logger.shutdown()
179+
180+
170181
class TestUsageLoggerConstants:
171182
"""Test constants are properly defined."""
172183

0 commit comments

Comments
 (0)