logix-help-mcp 1.0.1__tar.gz

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,167 @@
1
+ Metadata-Version: 2.3
2
+ Name: logix-help-mcp
3
+ Version: 1.0.1
4
+ Summary: MCP server that indexes the Studio 5000 Logix Designer help into SQLite FTS5 and exposes search and read tools
5
+ Requires-Dist: mcp>=2.3
6
+ Requires-Dist: platformdirs>=4.14.5
7
+ Requires-Dist: selectolax>=1.0.2
8
+ Requires-Python: >=3.12
9
+ Description-Content-Type: text/markdown
10
+
11
+ # Logix Help MCP
12
+
13
+ `logix-help-mcp` is a Python MCP server that indexes the help that ships with Studio 5000 Logix Designer **v31–v38+** into a local SQLite FTS5 database so that a coding agent (Claude Code, VS Code Chat, etc.) can easily read through the help contents.
14
+
15
+ The index of help documents is built locally from your own licensed install of Studio 5000 Logix Designer. No Rockwell content is included. The tool automatically detects every version of the help it finds and builds an index for each one.
16
+
17
+ > [!NOTE]
18
+ > Versions older than v31 are currently unsupported as they use a different format for the help docs. Versions up to the current v38 have been tested.
19
+ >
20
+ > This project is not affiliated with Rockwell Automation in any way.
21
+
22
+ ## Quick Start
23
+
24
+ ### Install and Build the Index
25
+ [uv](https://docs.astral.sh/uv/getting-started/installation/) is the recommended way to install and run `logix-help-mcp`. `uvx` can cache the package and rerun it without the need to install it in a specific folder or environment.
26
+
27
+ ```bash
28
+ uvx logix-help-mcp --root "/path/to/Studio 5000" --build --all
29
+ ```
30
+
31
+ `--root` works in any shell. You can also set the `S5K_HELP_ROOT` environment variable instead.
32
+
33
+ On Linux
34
+ ```bash
35
+ S5K_HELP_ROOT="/path/to/Studio 5000" uvx logix-help-mcp --build --all
36
+ ```
37
+ Or in Powershell on Windows
38
+ ```
39
+ $env:S5K_HELP_ROOT = "C:\path\to\Studio 5000"
40
+ ```
41
+
42
+ Users on Windows with the default Studio 5000 install location (`C:\Program Files (x86)\Rockwell Software\Studio 5000`) can skip `--root` and simply run:
43
+
44
+ ```bash
45
+ uvx logix-help-mcp --build --all
46
+ ```
47
+
48
+ > [!NOTE]
49
+ > With the `--build --all` options the server will not run, it will just build the indexes. The server will typically be run by your MCP client (see [Register the MCP Server](#register-the-mcp-server))
50
+
51
+
52
+ ### Install as a Tool with UV (optional)
53
+
54
+ `uv` can also register `logix-help-mcp` as a tool. This puts `logix-help-mcp` on your PATH to make calling it easier.
55
+
56
+ ```bash
57
+ uv tool install logix-help-mcp
58
+ ```
59
+
60
+ Then to run it:
61
+ ```bash
62
+ logix-help-mcp --build --all
63
+ ```
64
+
65
+ ## Register the MCP Server
66
+
67
+ ### Claude Code
68
+
69
+ `-s user` registers the server for all your projects. Leave it out to add it to the current project only.
70
+
71
+ ```bash
72
+ claude mcp add -s user logix-help -e S5K_HELP_ROOT="/path/to/Studio 5000" -- uvx logix-help-mcp
73
+ ```
74
+
75
+ If you installed it with `uv tool install`, replace `uvx logix-help-mcp` with `logix-help-mcp`.
76
+
77
+ ### Other MCP clients
78
+
79
+ Other clients (e.g. VS Code `.vscode/mcp.json`) need the same command, arguments and environment.
80
+
81
+ ```json
82
+ {
83
+ "servers": {
84
+ "logix-help": {
85
+ "type": "stdio",
86
+ "command": "uvx",
87
+ "args": [
88
+ "logix-help-mcp"
89
+ ],
90
+ "env": { "S5K_HELP_ROOT": "/path/to/Studio 5000" }
91
+ }
92
+ }
93
+ }
94
+ ```
95
+
96
+ With `uv tool install`, use `"command": "logix-help-mcp"` and no `args`.
97
+
98
+ ## First start
99
+
100
+ The first start builds the default (newest) version's index in the background. That takes a few seconds to about a minute, depending on disk or network speed, and tools wait for it. Other versions build the first time a tool asks for them. Later starts reuse the cache unless the help files changed. The cache lives in your user cache directory, not in the install, so upgrading the package keeps it. A new schema version rebuilds it automatically.
101
+
102
+ ## Configuration
103
+
104
+ | Env var | CLI flag | Meaning |
105
+ | --------------- | ------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
106
+ | `S5K_HELP_ROOT` | `--root` | The `Studio 5000` install folder, the `Logix Designer/ENU` folder, or one `rs5000` help folder. Folders are scanned for `Logix Designer/ENU/v*/Bin/Help/ENU/rs5000` (v33+) and `Help/ENU/v*/rs5000` (v31, v32). |
107
+ | `S5K_VERSION` | `--version` | Default version, e.g. `v37` (default: the newest version found; a requested version that is not found falls back to it with a warning). |
108
+ | `S5K_DB_DIR` | | Where index databases are stored (default: user cache dir for `logix-help-mcp`). |
109
+ | | `--force-rebuild` | Rebuild even if the cache is fresh. |
110
+ | | `--transport stdio\|streamable-http` | Default `stdio`. |
111
+ | | `--build-only` | Build the index, print statistics and timing, exit. |
112
+ | | `--all` | With `--build-only`: build every discovered version, one summary line each (exit 1 if any fails). |
113
+
114
+ ### Index Builds
115
+ The index is one SQLite file per version. It is rebuilt when the help folder's HTML files, TOC files, or metadata files (`context-help-map.xml`, `rs5000.csv`, `index.xml`) change, or when the schema version changes. The help folder is only ever read.
116
+
117
+ ### Unreachable Help Folder
118
+ If the help folder is unreachable - such as when a network share is offline - versions that already have a cached index are still served from the cache. `get_image` does not work until the folder is back, because images are not cached.
119
+
120
+ If no help folder is found, the server looks again on later tool calls (at most every 30 s), so it picks the folder up once it is reachable, without a restart.
121
+
122
+ ### Multiple Versions
123
+
124
+ - Discovery: v33+ help is expected in `Logix Designer/ENU/v*/Bin/Help/ENU/rs5000` and v31–v32 help in `Help/ENU/v*/rs5000` under the `Studio 5000` install folder.
125
+ - The newest version is the default version. Every tool takes an optional `version`: `'v33'` or a project revision such as `'33.01'` so that an agent can check against the correct version.
126
+
127
+
128
+ ## Tools
129
+
130
+ | Tool | Description |
131
+ | --------------------- | --------------------------------------------------------------------------------------------------------------------------------- |
132
+ | `search_help` | Keyword search. Returns page IDs, titles, breadcrumbs and short snippets. Filter by `kind` (instruction, fault, module, dialog, command, concept) or `section`. |
133
+ | `get_page` | Read a page as Markdown, with tables kept. Long pages are paginated with `offset` / `max_chars`. |
134
+ | `get_instruction` | Read an instruction page by mnemonic, e.g. `TON`, `GSV`, `MAOC`. Paginated like `get_page`. |
135
+ | `get_image` | Get a graphic referenced in a page's Markdown, e.g. a ladder or FBD diagram. |
136
+ | `get_page_by_help_id` | Find the page for an F1 / context help ID, numeric (`131173`) or named (`about_the_status_bar`). |
137
+ | `browse_toc` | List the table of contents, one level at a time. |
138
+ | `get_breadcrumb` | Show where a page sits in the help hierarchy. |
139
+ | `list_versions` | List the help versions found, their format and index state. |
140
+ | `get_help_statistics` | Show index statistics and build progress. |
141
+
142
+ Every tool except `list_versions` takes an optional `version`. Search snippets are not page content; the server instructions tell the model to read the page with `get_page`.
143
+
144
+ - **Page IDs:** a page's `page_id` is its help topic ID (e.g. `ait_455128`) when that ID is unique. Some pages share a topic ID (duplicated pages such as the GSV/SSV pages); those use their help-relative path instead (e.g. `help/clinset/...html`). Every tool that takes a `page_id` accepts either form.
145
+ - **Renamed instructions:** v36 renamed several mnemonics (MOV→MOVE, EQU→EQ, NEQ→NE, LES→LT, GRT→GT, LEQ→LE, GEQ→GE, LIM→LIMIT, SQR→SQRT, XPY→EXPT, TOD→TO_BCD, FRD→BCD_TO, TRN→TRUNC, ACS→ACOS, ASN→ASIN, ATN→ATAN). `get_instruction` accepts either name in any version and says when it mapped one.
146
+ - **Errors:** failures (unknown page, TOC node or version, invalid `kind`, index still building) are returned as MCP tool errors (`is_error: true`). Valid empty outcomes, such as no search hits or an unmapped help ID, are normal results.
147
+
148
+ ## Development
149
+
150
+ ### Install from source
151
+
152
+ ```bash
153
+ git clone https://github.com/zwood16/logix-help-mcp
154
+ cd logix-help-mcp
155
+ uv run logix-help-mcp
156
+ ```
157
+
158
+ ### Running Tests
159
+ This project uses `pytest` for tests and `ruff` for linting. Both are dev dependencies, so from the repository run:
160
+
161
+ ```bash
162
+ uv run pytest
163
+ uv run ruff check
164
+ ```
165
+
166
+ Integration tests need a real help install and are skipped unless `S5K_HELP_ROOT` is set. `uv run pytest -m "not integration"` runs only the offline tests.
167
+
@@ -0,0 +1,157 @@
1
+ # Logix Help MCP
2
+
3
+ `logix-help-mcp` is a Python MCP server that indexes the help that ships with Studio 5000 Logix Designer **v31–v38+** into a local SQLite FTS5 database so that a coding agent (Claude Code, VS Code Chat, etc.) can easily read through the help contents.
4
+
5
+ The index of help documents is built locally from your own licensed install of Studio 5000 Logix Designer. No Rockwell content is included. The tool automatically detects every version of the help it finds and builds an index for each one.
6
+
7
+ > [!NOTE]
8
+ > Versions older than v31 are currently unsupported as they use a different format for the help docs. Versions up to the current v38 have been tested.
9
+ >
10
+ > This project is not affiliated with Rockwell Automation in any way.
11
+
12
+ ## Quick Start
13
+
14
+ ### Install and Build the Index
15
+ [uv](https://docs.astral.sh/uv/getting-started/installation/) is the recommended way to install and run `logix-help-mcp`. `uvx` can cache the package and rerun it without the need to install it in a specific folder or environment.
16
+
17
+ ```bash
18
+ uvx logix-help-mcp --root "/path/to/Studio 5000" --build --all
19
+ ```
20
+
21
+ `--root` works in any shell. You can also set the `S5K_HELP_ROOT` environment variable instead.
22
+
23
+ On Linux
24
+ ```bash
25
+ S5K_HELP_ROOT="/path/to/Studio 5000" uvx logix-help-mcp --build --all
26
+ ```
27
+ Or in Powershell on Windows
28
+ ```
29
+ $env:S5K_HELP_ROOT = "C:\path\to\Studio 5000"
30
+ ```
31
+
32
+ Users on Windows with the default Studio 5000 install location (`C:\Program Files (x86)\Rockwell Software\Studio 5000`) can skip `--root` and simply run:
33
+
34
+ ```bash
35
+ uvx logix-help-mcp --build --all
36
+ ```
37
+
38
+ > [!NOTE]
39
+ > With the `--build --all` options the server will not run, it will just build the indexes. The server will typically be run by your MCP client (see [Register the MCP Server](#register-the-mcp-server))
40
+
41
+
42
+ ### Install as a Tool with UV (optional)
43
+
44
+ `uv` can also register `logix-help-mcp` as a tool. This puts `logix-help-mcp` on your PATH to make calling it easier.
45
+
46
+ ```bash
47
+ uv tool install logix-help-mcp
48
+ ```
49
+
50
+ Then to run it:
51
+ ```bash
52
+ logix-help-mcp --build --all
53
+ ```
54
+
55
+ ## Register the MCP Server
56
+
57
+ ### Claude Code
58
+
59
+ `-s user` registers the server for all your projects. Leave it out to add it to the current project only.
60
+
61
+ ```bash
62
+ claude mcp add -s user logix-help -e S5K_HELP_ROOT="/path/to/Studio 5000" -- uvx logix-help-mcp
63
+ ```
64
+
65
+ If you installed it with `uv tool install`, replace `uvx logix-help-mcp` with `logix-help-mcp`.
66
+
67
+ ### Other MCP clients
68
+
69
+ Other clients (e.g. VS Code `.vscode/mcp.json`) need the same command, arguments and environment.
70
+
71
+ ```json
72
+ {
73
+ "servers": {
74
+ "logix-help": {
75
+ "type": "stdio",
76
+ "command": "uvx",
77
+ "args": [
78
+ "logix-help-mcp"
79
+ ],
80
+ "env": { "S5K_HELP_ROOT": "/path/to/Studio 5000" }
81
+ }
82
+ }
83
+ }
84
+ ```
85
+
86
+ With `uv tool install`, use `"command": "logix-help-mcp"` and no `args`.
87
+
88
+ ## First start
89
+
90
+ The first start builds the default (newest) version's index in the background. That takes a few seconds to about a minute, depending on disk or network speed, and tools wait for it. Other versions build the first time a tool asks for them. Later starts reuse the cache unless the help files changed. The cache lives in your user cache directory, not in the install, so upgrading the package keeps it. A new schema version rebuilds it automatically.
91
+
92
+ ## Configuration
93
+
94
+ | Env var | CLI flag | Meaning |
95
+ | --------------- | ------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
96
+ | `S5K_HELP_ROOT` | `--root` | The `Studio 5000` install folder, the `Logix Designer/ENU` folder, or one `rs5000` help folder. Folders are scanned for `Logix Designer/ENU/v*/Bin/Help/ENU/rs5000` (v33+) and `Help/ENU/v*/rs5000` (v31, v32). |
97
+ | `S5K_VERSION` | `--version` | Default version, e.g. `v37` (default: the newest version found; a requested version that is not found falls back to it with a warning). |
98
+ | `S5K_DB_DIR` | | Where index databases are stored (default: user cache dir for `logix-help-mcp`). |
99
+ | | `--force-rebuild` | Rebuild even if the cache is fresh. |
100
+ | | `--transport stdio\|streamable-http` | Default `stdio`. |
101
+ | | `--build-only` | Build the index, print statistics and timing, exit. |
102
+ | | `--all` | With `--build-only`: build every discovered version, one summary line each (exit 1 if any fails). |
103
+
104
+ ### Index Builds
105
+ The index is one SQLite file per version. It is rebuilt when the help folder's HTML files, TOC files, or metadata files (`context-help-map.xml`, `rs5000.csv`, `index.xml`) change, or when the schema version changes. The help folder is only ever read.
106
+
107
+ ### Unreachable Help Folder
108
+ If the help folder is unreachable - such as when a network share is offline - versions that already have a cached index are still served from the cache. `get_image` does not work until the folder is back, because images are not cached.
109
+
110
+ If no help folder is found, the server looks again on later tool calls (at most every 30 s), so it picks the folder up once it is reachable, without a restart.
111
+
112
+ ### Multiple Versions
113
+
114
+ - Discovery: v33+ help is expected in `Logix Designer/ENU/v*/Bin/Help/ENU/rs5000` and v31–v32 help in `Help/ENU/v*/rs5000` under the `Studio 5000` install folder.
115
+ - The newest version is the default version. Every tool takes an optional `version`: `'v33'` or a project revision such as `'33.01'` so that an agent can check against the correct version.
116
+
117
+
118
+ ## Tools
119
+
120
+ | Tool | Description |
121
+ | --------------------- | --------------------------------------------------------------------------------------------------------------------------------- |
122
+ | `search_help` | Keyword search. Returns page IDs, titles, breadcrumbs and short snippets. Filter by `kind` (instruction, fault, module, dialog, command, concept) or `section`. |
123
+ | `get_page` | Read a page as Markdown, with tables kept. Long pages are paginated with `offset` / `max_chars`. |
124
+ | `get_instruction` | Read an instruction page by mnemonic, e.g. `TON`, `GSV`, `MAOC`. Paginated like `get_page`. |
125
+ | `get_image` | Get a graphic referenced in a page's Markdown, e.g. a ladder or FBD diagram. |
126
+ | `get_page_by_help_id` | Find the page for an F1 / context help ID, numeric (`131173`) or named (`about_the_status_bar`). |
127
+ | `browse_toc` | List the table of contents, one level at a time. |
128
+ | `get_breadcrumb` | Show where a page sits in the help hierarchy. |
129
+ | `list_versions` | List the help versions found, their format and index state. |
130
+ | `get_help_statistics` | Show index statistics and build progress. |
131
+
132
+ Every tool except `list_versions` takes an optional `version`. Search snippets are not page content; the server instructions tell the model to read the page with `get_page`.
133
+
134
+ - **Page IDs:** a page's `page_id` is its help topic ID (e.g. `ait_455128`) when that ID is unique. Some pages share a topic ID (duplicated pages such as the GSV/SSV pages); those use their help-relative path instead (e.g. `help/clinset/...html`). Every tool that takes a `page_id` accepts either form.
135
+ - **Renamed instructions:** v36 renamed several mnemonics (MOV→MOVE, EQU→EQ, NEQ→NE, LES→LT, GRT→GT, LEQ→LE, GEQ→GE, LIM→LIMIT, SQR→SQRT, XPY→EXPT, TOD→TO_BCD, FRD→BCD_TO, TRN→TRUNC, ACS→ACOS, ASN→ASIN, ATN→ATAN). `get_instruction` accepts either name in any version and says when it mapped one.
136
+ - **Errors:** failures (unknown page, TOC node or version, invalid `kind`, index still building) are returned as MCP tool errors (`is_error: true`). Valid empty outcomes, such as no search hits or an unmapped help ID, are normal results.
137
+
138
+ ## Development
139
+
140
+ ### Install from source
141
+
142
+ ```bash
143
+ git clone https://github.com/zwood16/logix-help-mcp
144
+ cd logix-help-mcp
145
+ uv run logix-help-mcp
146
+ ```
147
+
148
+ ### Running Tests
149
+ This project uses `pytest` for tests and `ruff` for linting. Both are dev dependencies, so from the repository run:
150
+
151
+ ```bash
152
+ uv run pytest
153
+ uv run ruff check
154
+ ```
155
+
156
+ Integration tests need a real help install and are skipped unless `S5K_HELP_ROOT` is set. `uv run pytest -m "not integration"` runs only the offline tests.
157
+
@@ -0,0 +1,49 @@
1
+ [project]
2
+ name = "logix-help-mcp"
3
+ version = "1.0.1"
4
+ description = "MCP server that indexes the Studio 5000 Logix Designer help into SQLite FTS5 and exposes search and read tools"
5
+ readme = "README.md"
6
+ requires-python = ">=3.12"
7
+ dependencies = [
8
+ "mcp>=2.3",
9
+ "platformdirs>=4.14.5",
10
+ "selectolax>=1.0.2",
11
+ ]
12
+
13
+ [project.scripts]
14
+ logix-help-mcp = "logix_help_mcp:main"
15
+
16
+ [build-system]
17
+ requires = ["uv_build>=0.12.14,<0.13.0"]
18
+ build-backend = "uv_build"
19
+
20
+ [dependency-groups]
21
+ dev = [
22
+ "pytest>=9.2.0",
23
+ "pyyaml>=6.0.3",
24
+ "ruff>=0.17.5",
25
+ ]
26
+
27
+ [tool.pytest.ini_options]
28
+ testpaths = ["tests"]
29
+ markers = ["integration: needs a real Studio 5000 help install (set S5K_HELP_ROOT)"]
30
+
31
+ [tool.ruff]
32
+ line-length = 88
33
+
34
+ [tool.ruff.lint]
35
+ select = [
36
+ "E",
37
+ "F",
38
+ "I",
39
+ "UP",
40
+ "B",
41
+ "SIM",
42
+ "D",
43
+ ]
44
+
45
+ [tool.ruff.lint.pydocstyle]
46
+ convention = "google"
47
+
48
+ [tool.ruff.lint.per-file-ignores]
49
+ "tests/**" = ["D"]
@@ -0,0 +1,50 @@
1
+ [project]
2
+ name = "logix-help-mcp"
3
+ version = "1.0.1"
4
+ description = "MCP server that indexes the Studio 5000 Logix Designer help into SQLite FTS5 and exposes search and read tools"
5
+ readme = "README.md"
6
+ requires-python = ">=3.12"
7
+ dependencies = [
8
+ "mcp>=2.3",
9
+ "platformdirs>=4.14.5",
10
+ "selectolax>=1.0.2",
11
+ ]
12
+
13
+ [project.scripts]
14
+ logix-help-mcp = "logix_help_mcp:main"
15
+
16
+ [build-system]
17
+ requires = ["uv_build>=0.12.14,<0.13.0"]
18
+ build-backend = "uv_build"
19
+
20
+ [dependency-groups]
21
+ dev = [
22
+ "pytest>=9.2.0",
23
+ "pyyaml>=6.0.3",
24
+ "ruff>=0.17.5",
25
+ ]
26
+
27
+ [tool.pytest.ini_options]
28
+ testpaths = ["tests"]
29
+ markers = ["integration: needs a real Studio 5000 help install (set S5K_HELP_ROOT)"]
30
+
31
+ [tool.ruff]
32
+ line-length = 88
33
+
34
+ [tool.ruff.lint]
35
+ # The rule categories to enable.
36
+ select = [
37
+ "E", # pycodestyle (catches formatting-adjacent errors)
38
+ "F", # Pyflakes (catches logical errors, like unused variables)
39
+ "I", # isort (automatically sorts your imports)
40
+ "UP", # pyupgrade (suggests modern syntax upgrades)
41
+ "B", # flake8-bugbear (catches common bugs and gotchas)
42
+ "SIM", # flake8-simplify (suggests cleaner, more elegant code)
43
+ "D", # pydocstyle (docstrings, for generated API docs)
44
+ ]
45
+
46
+ [tool.ruff.lint.pydocstyle]
47
+ convention = "google"
48
+
49
+ [tool.ruff.lint.per-file-ignores]
50
+ "tests/**" = ["D"]
@@ -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
+ )