logix-help-mcp 1.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,14 @@
1
+ """MCP server that indexes the Studio 5000 Logix Designer help for search."""
2
+
3
+
4
+ def main() -> None:
5
+ """Run the MCP server command-line entry point.
6
+
7
+ Imports `server` lazily so importing the package stays cheap.
8
+ """
9
+ from .server import main as _m
10
+
11
+ _m()
12
+
13
+
14
+ __all__ = ["main"]
@@ -0,0 +1,139 @@
1
+ """Configuration from environment variables and CLI flags."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import argparse
6
+ import os
7
+ import re
8
+ from dataclasses import dataclass
9
+ from pathlib import Path
10
+
11
+ from platformdirs import user_cache_dir
12
+
13
+ APP_NAME = "logix-help-mcp"
14
+
15
+
16
+ @dataclass
17
+ class Config:
18
+ """Runtime configuration for the server.
19
+
20
+ Attributes:
21
+ help_root: Help root from S5K_HELP_ROOT or --root.
22
+ version: Default version from S5K_VERSION or --version (e.g. "v37" or "37").
23
+ db_dir: Index database directory from S5K_DB_DIR.
24
+ force_rebuild: Rebuild the index even if the cache is fresh.
25
+ transport: MCP transport, "stdio" or "streamable-http".
26
+ build_only: Build the index, print statistics, then exit.
27
+ build_all: With build_only (--all), build every discovered version.
28
+ """
29
+
30
+ help_root: Path | None = None
31
+ version: str | None = None
32
+ db_dir: Path | None = None
33
+ force_rebuild: bool = False
34
+ transport: str = "stdio"
35
+ build_only: bool = False
36
+ build_all: bool = False
37
+
38
+ def resolved_db_dir(self) -> Path:
39
+ """Return the index database directory.
40
+
41
+ Returns:
42
+ `db_dir` if set, else the platformdirs user cache directory for the app.
43
+ """
44
+ return self.db_dir if self.db_dir else Path(user_cache_dir(APP_NAME))
45
+
46
+
47
+ _VERSION_RE = re.compile(r"^v?(\d+)(?:\.\d+)*$")
48
+
49
+
50
+ def normalize_version(v: str | None) -> str | None:
51
+ """Normalize a version string to its major version.
52
+
53
+ 'v37', '37', '37.00' and 'v33.01' become 'v37', 'v37', 'v37' and 'v33' (major
54
+ only).
55
+
56
+ Args:
57
+ v: Version string, or None.
58
+
59
+ Returns:
60
+ The normalized 'v<major>' string, or None if `v` is empty. Anything that is
61
+ not a version is returned stripped and lowercased so lookups fail as unknown.
62
+ """
63
+ if not v:
64
+ return None
65
+ v = v.strip().lower()
66
+ if not v:
67
+ return None
68
+ m = _VERSION_RE.match(v)
69
+ return f"v{int(m.group(1))}" if m else v
70
+
71
+
72
+ def build_parser() -> argparse.ArgumentParser:
73
+ """Build the command-line argument parser.
74
+
75
+ Returns:
76
+ The parser with --root, --version, --force-rebuild, --transport,
77
+ --build-only and --all.
78
+ """
79
+ p = argparse.ArgumentParser(
80
+ prog=APP_NAME,
81
+ description=(
82
+ "MCP server for searching the Studio 5000 Logix Designer help "
83
+ "(Oxygen WebHelp v37; legacy v31+ detected)."
84
+ ),
85
+ )
86
+ p.add_argument(
87
+ "--root",
88
+ help=(
89
+ "Help root: the 'Studio 5000' install folder, the "
90
+ "'Logix Designer/ENU' folder, or one rs5000 folder (env S5K_HELP_ROOT)"
91
+ ),
92
+ )
93
+ p.add_argument("--version", help="Default help version, e.g. v37 (env S5K_VERSION)")
94
+ p.add_argument(
95
+ "--force-rebuild",
96
+ action="store_true",
97
+ help="Rebuild the index even if the cache is fresh",
98
+ )
99
+ p.add_argument("--transport", choices=["stdio", "streamable-http"], default="stdio")
100
+ p.add_argument(
101
+ "--build-only",
102
+ action="store_true",
103
+ help="Build the index, print statistics and timing, then exit",
104
+ )
105
+ p.add_argument(
106
+ "--all",
107
+ action="store_true",
108
+ help="With --build-only: build every discovered version, one line each",
109
+ )
110
+ return p
111
+
112
+
113
+ def load_config(argv: list[str] | None = None) -> Config:
114
+ """Parse CLI flags and environment variables into a `Config`.
115
+
116
+ Flags win over environment variables for the help root and version.
117
+ S5K_DB_DIR is environment-only.
118
+
119
+ Args:
120
+ argv: Argument list to parse; None uses `sys.argv`.
121
+
122
+ Returns:
123
+ The resulting configuration.
124
+ """
125
+ parser = build_parser()
126
+ a = parser.parse_args(argv)
127
+ if a.all and not a.build_only:
128
+ parser.error("--all requires --build-only")
129
+ root = a.root or os.environ.get("S5K_HELP_ROOT")
130
+ db_dir = os.environ.get("S5K_DB_DIR")
131
+ return Config(
132
+ help_root=Path(root).expanduser() if root else None,
133
+ version=normalize_version(a.version or os.environ.get("S5K_VERSION")),
134
+ db_dir=Path(db_dir).expanduser() if db_dir else None,
135
+ force_rebuild=a.force_rebuild,
136
+ transport=a.transport,
137
+ build_only=a.build_only,
138
+ build_all=a.all,
139
+ )
@@ -0,0 +1,189 @@
1
+ """Find Logix Designer help folders and their versions."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import glob
6
+ import os
7
+ import re
8
+ import sys
9
+ from dataclasses import dataclass
10
+ from pathlib import Path
11
+
12
+ from .config import Config, normalize_version
13
+ from .sources.legacy import is_legacy_root
14
+ from .sources.oxygen import is_oxygen_root
15
+
16
+ _VER = re.compile(r"^v(\d+)$", re.I)
17
+
18
+ MIN_VERSION = 31 # older help (v30 and below) is .chm only or out of scope
19
+
20
+
21
+ @dataclass
22
+ class VersionInfo:
23
+ """One discovered help version.
24
+
25
+ Attributes:
26
+ version: Version label, e.g. "v37".
27
+ root: The rs5000 folder.
28
+ format: "oxygen", "legacy" or "unknown".
29
+ cached: True if the help folder is unreachable and the version is served
30
+ from the cached index.
31
+ """
32
+
33
+ version: str
34
+ root: Path
35
+ format: str
36
+ cached: bool = False
37
+
38
+ @property
39
+ def number(self) -> int:
40
+ """Major version number parsed from `version`, or -1 if it has none."""
41
+ m = _VER.match(self.version)
42
+ return int(m.group(1)) if m else -1
43
+
44
+
45
+ def _format_of(root: Path) -> str:
46
+ """Classify a help root as "oxygen", "legacy" or "unknown"."""
47
+ if is_oxygen_root(root):
48
+ return "oxygen"
49
+ if is_legacy_root(root):
50
+ return "legacy"
51
+ return "unknown"
52
+
53
+
54
+ def _version_from_path(p: Path) -> str | None:
55
+ """Return the lowercased 'v<N>' path component closest to the end, if any."""
56
+ for part in reversed(p.parts):
57
+ if _VER.match(part):
58
+ return part.lower()
59
+ return None
60
+
61
+
62
+ def windows_probe_bases() -> list[Path]:
63
+ """Return default Studio 5000 install folders to probe on Windows.
64
+
65
+ Covers the Program Files (x86) and Program Files locations, deduplicated.
66
+ """
67
+ # Default Studio 5000 install location (confirmed on Windows 11, v30-v37 under
68
+ # Program Files (x86)). Both help trees live under this folder.
69
+ # Set S5K_HELP_ROOT for non-default installs.
70
+ # Some MCP clients strip environment variables, so fall back to the defaults.
71
+ bases: list[Path] = []
72
+ seen: set[str] = set()
73
+ for env, default in (
74
+ ("ProgramFiles(x86)", r"C:\Program Files (x86)"),
75
+ ("ProgramFiles", r"C:\Program Files"),
76
+ ):
77
+ base = os.environ.get(env) or default
78
+ key = base.lower()
79
+ if key not in seen:
80
+ seen.add(key)
81
+ bases.append(Path(base) / "Rockwell Software" / "Studio 5000")
82
+ return bases
83
+
84
+
85
+ def _scan_enu(enu: Path) -> list[VersionInfo]:
86
+ """Scan a `Logix Designer/ENU` folder for `v*/Bin/Help/ENU/rs5000`."""
87
+ found: list[VersionInfo] = []
88
+ pattern = os.path.join(glob.escape(str(enu)), "v*", "Bin", "Help", "ENU", "rs5000")
89
+ for d in glob.glob(pattern):
90
+ p = Path(d)
91
+ ver = _version_from_path(p)
92
+ if ver and p.is_dir():
93
+ found.append(VersionInfo(ver, p, _format_of(p)))
94
+ return found
95
+
96
+
97
+ def _scan_help_enu(enu: Path) -> list[VersionInfo]:
98
+ """Scan a `Help/ENU` folder for `v*/rs5000` (v31, v32)."""
99
+ found: list[VersionInfo] = []
100
+ pattern = os.path.join(glob.escape(str(enu)), "v*", "rs5000")
101
+ for d in glob.glob(pattern):
102
+ p = Path(d)
103
+ ver = _version_from_path(p)
104
+ if ver and p.is_dir():
105
+ found.append(VersionInfo(ver, p, _format_of(p)))
106
+ return found
107
+
108
+
109
+ def _scan_studio5000(base: Path) -> list[VersionInfo]:
110
+ """Scan a Studio 5000 install folder; Logix Designer tree wins on duplicates."""
111
+ by_ver: dict[str, VersionInfo] = {}
112
+ for info in _scan_help_enu(base / "Help" / "ENU"):
113
+ by_ver[info.version] = info
114
+ for info in _scan_enu(base / "Logix Designer" / "ENU"):
115
+ by_ver[info.version] = info
116
+ return [v for v in by_ver.values() if v.number >= MIN_VERSION]
117
+
118
+
119
+ def _studio5000_base(root: Path) -> Path | None:
120
+ """Return the Studio 5000 install folder that `root` is or sits in, else None."""
121
+ if (root / "Logix Designer").is_dir() or (root / "Help").is_dir():
122
+ return root
123
+ if root.name.lower() == "enu" and root.parent.name.lower() == "logix designer":
124
+ return root.parent.parent
125
+ if root.name.lower() == "logix designer":
126
+ return root.parent
127
+ return None
128
+
129
+
130
+ def discover(cfg: Config) -> dict[str, VersionInfo]:
131
+ """Find help versions from the configured root, or the Windows defaults.
132
+
133
+ Args:
134
+ cfg: Configuration; `help_root` and `version` are used.
135
+
136
+ Returns:
137
+ Mapping of version label to `VersionInfo`, sorted by version number. Empty
138
+ when nothing is found.
139
+ """
140
+ found: list[VersionInfo] = []
141
+ if cfg.help_root:
142
+ root = cfg.help_root
143
+ if root.is_dir():
144
+ if is_oxygen_root(root) or root.name.lower() == "rs5000":
145
+ ver = _version_from_path(root) or cfg.version or "v?"
146
+ found.append(VersionInfo(ver, root, _format_of(root)))
147
+ else:
148
+ base = _studio5000_base(root)
149
+ if base:
150
+ found.extend(_scan_studio5000(base))
151
+ else:
152
+ for cand in (root, root / "ENU", root / "Logix Designer" / "ENU"):
153
+ found.extend(
154
+ v for v in _scan_enu(cand) if v.number >= MIN_VERSION
155
+ )
156
+ if found:
157
+ break
158
+ elif os.name == "nt":
159
+ for base in windows_probe_bases():
160
+ found.extend(_scan_studio5000(base))
161
+ return {v.version: v for v in sorted(found, key=lambda v: v.number)}
162
+
163
+
164
+ def default_version(
165
+ versions: dict[str, VersionInfo], requested: str | None
166
+ ) -> str | None:
167
+ """Pick the default version.
168
+
169
+ Prints a notice to stderr if a requested version is not found.
170
+
171
+ Args:
172
+ versions: Discovered versions.
173
+ requested: Requested version (any form `normalize_version` accepts), or None.
174
+
175
+ Returns:
176
+ The requested version if found, else the newest oxygen or legacy one, else
177
+ None.
178
+ """
179
+ requested = normalize_version(requested)
180
+ if requested and requested in versions:
181
+ return requested
182
+ if requested:
183
+ print(
184
+ f"logix-help-mcp: requested version {requested!r} not found; "
185
+ "using the newest available version.",
186
+ file=sys.stderr,
187
+ )
188
+ ok = [v for v in versions.values() if v.format in ("oxygen", "legacy")]
189
+ return max(ok, key=lambda v: v.number).version if ok else None