Skip to content

Commit 3f840b2

Browse files
Merge pull request #99 from kevinbackhouse/autogen-tmpdirs
Add utility for generating data directories
2 parents 4377732 + 96d9cc1 commit 3f840b2

File tree

5 files changed

+40
-8
lines changed

5 files changed

+40
-8
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ dependencies = [
7272
"parse==1.20.2",
7373
"parso==0.8.4",
7474
"pathable==0.4.4",
75+
"platformdirs==4.5.0",
7576
"pluggy==1.6.0",
7677
"pycparser==2.23",
7778
"pydantic==2.11.7",

src/seclab_taskflow_agent/mcp_servers/codeql/mcp_server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
import re
2121
from urllib.parse import urlparse, unquote
2222
import zipfile
23+
from seclab_taskflow_agent.path_utils import mcp_data_dir
2324

2425
mcp = FastMCP("CodeQL")
2526

26-
CODEQL_DBS_BASE_PATH = Path(os.getenv('CODEQL_DBS_BASE_PATH', default='/workspaces'))
27+
CODEQL_DBS_BASE_PATH = mcp_data_dir('seclab-taskflow-agent', 'codeql', 'CODEQL_DBS_BASE_PATH')
2728

2829
# tool name -> templated query lookup for supported languages
2930
TEMPLATED_QUERY_PATHS = {

src/seclab_taskflow_agent/mcp_servers/logbook/logbook.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212
from fastmcp import FastMCP # move to FastMCP 2.0
1313
import json
1414
from pathlib import Path
15-
import os
15+
from seclab_taskflow_agent.path_utils import mcp_data_dir
1616

1717
mcp = FastMCP("Logbook")
1818

1919
LOG = {}
2020

21-
LOGBOOK = Path(__file__).parent.resolve() / Path(os.getenv('LOGBOOK_STATE_DIR', default='./')) / Path("logbook.json")
22-
21+
LOGBOOK = mcp_data_dir('seclab-taskflow-agent', 'logbook', 'LOGBOOK_STATE_DIR') / Path("logbook.json")
2322

2423
def ensure_log():
2524
global LOG

src/seclab_taskflow_agent/mcp_servers/memcache/memcache.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from typing import Any
1717
from .memcache_backend.dictionary_file import MemcacheDictionaryFileBackend
1818
from .memcache_backend.sqlite import SqliteBackend
19+
from seclab_taskflow_agent.path_utils import mcp_data_dir
1920

2021
mcp = FastMCP("Memcache")
2122

@@ -24,10 +25,7 @@
2425
'sqlite': SqliteBackend,
2526
}
2627

27-
# if MEMCACHE_STATE_DIR contains an absolute path we WANT the user to be able
28-
# to override the relative path in that case this path join will return
29-
# /MEMCACHE_STATE_DIR/memory.json
30-
MEMORY = Path(__file__).parent.resolve() / Path(os.getenv('MEMCACHE_STATE_DIR', default='./'))
28+
MEMORY = mcp_data_dir('seclab-taskflow-agent', 'memcache', 'MEMCACHE_STATE_DIR')
3129
BACKEND = os.getenv('MEMCACHE_BACKEND', default='sqlite')
3230

3331
backend = backends.get(BACKEND)(str(MEMORY))
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# SPDX-FileCopyrightText: 2025 GitHub
2+
# SPDX-License-Identifier: MIT
3+
4+
import platformdirs
5+
import os
6+
from pathlib import Path
7+
8+
9+
def mcp_data_dir(packagename: str, mcpname: str, env_override: str | None) -> Path:
10+
"""
11+
Create a directory for an MCP to store its data.
12+
13+
Parameters:
14+
packagename (str): The name of the package. Used as a subdirectory under the data directory.
15+
mcpname (str): The name of the MCP server. Used as a subdirectory under the package directory.
16+
env_override (str | None): The name of an environment variable that, if set, overrides the default data directory location. If None, the default location is used.
17+
18+
Returns:
19+
Path: The path to the created data directory for the MCP server.
20+
"""
21+
if env_override:
22+
p = os.getenv(env_override)
23+
if p:
24+
return Path(p)
25+
# Use [platformdirs](https://pypi.org/project/platformdirs/) to
26+
# choose an appropriate location.
27+
d = platformdirs.user_data_dir(appname="seclab-taskflow-agent",
28+
appauthor="GitHubSecurityLab",
29+
ensure_exists=True)
30+
# Each MCP server gets its own sub-directory
31+
p = Path(d).joinpath(packagename).joinpath(mcpname)
32+
p.mkdir(parents=True, exist_ok=True)
33+
return p

0 commit comments

Comments
 (0)