@nestrix-js/mcp 0.0.0-stage → 0.1.0-alpha.3
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.
- package/LICENSE +202 -0
- package/README.md +106 -2
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +31 -0
- package/dist/cli.js.map +1 -0
- package/dist/client.d.ts +41 -0
- package/dist/client.js +172 -0
- package/dist/client.js.map +1 -0
- package/dist/config.d.ts +16 -0
- package/dist/config.js +58 -0
- package/dist/config.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/render/explain.d.ts +17 -0
- package/dist/render/explain.js +113 -0
- package/dist/render/explain.js.map +1 -0
- package/dist/render/format.d.ts +16 -0
- package/dist/render/format.js +59 -0
- package/dist/render/format.js.map +1 -0
- package/dist/render/lists.d.ts +14 -0
- package/dist/render/lists.js +243 -0
- package/dist/render/lists.js.map +1 -0
- package/dist/render/profile.d.ts +4 -0
- package/dist/render/profile.js +43 -0
- package/dist/render/profile.js.map +1 -0
- package/dist/render/trace.d.ts +56 -0
- package/dist/render/trace.js +397 -0
- package/dist/render/trace.js.map +1 -0
- package/dist/tools.d.ts +12 -0
- package/dist/tools.js +290 -0
- package/dist/tools.js.map +1 -0
- package/package.json +47 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,107 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @nestrix-js/mcp
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An [MCP](https://modelcontextprotocol.io) server that lets an AI assistant (Claude Code, Claude Desktop, Cursor and others) read what your NestJS app is doing through [Nestrix](https://github.com/nestrix-js/nestrix): requests as traces, errors, per-route and per-class statistics, and what the app calls. It is a small program the assistant starts over stdio. It talks HTTP to Nestrix and runs nothing inside your app.
|
|
4
|
+
|
|
5
|
+
> **Status: alpha.** Needs Node 26.13 or later.
|
|
6
|
+
|
|
7
|
+
## What it can do
|
|
8
|
+
|
|
9
|
+
| Tool | What it is for |
|
|
10
|
+
|---|---|
|
|
11
|
+
| `overview` | Start here: the app, sampling, store size, the slowest and most failing routes, the stored error count. |
|
|
12
|
+
| `find_traces` | List stored traces with filters (route, method, status, minimum duration, text, type, release, user, service). |
|
|
13
|
+
| `get_trace` | One trace written for a model: repeated statements (a likely N+1) first, then errors with the first three stack frames, slow spans, and an indented span tree with self times. Capped at about 400 spans. |
|
|
14
|
+
| `list_errors` | Recent errors grouped by type and message, with trace ids. |
|
|
15
|
+
| `route_stats` | Request count, error rate and p50/p95/p99 per route over 15 or 60 minutes. |
|
|
16
|
+
| `class_stats` | Per-class calls, errors and own time (needs `NestrixInstrument` in the app). |
|
|
17
|
+
| `app_map` | What the app talks to: databases, caches, queues, outbound HTTP. |
|
|
18
|
+
| `explain_slowness` | For one route: its percentiles, then where the time goes in its 5 slowest stored traces, by span name and class, with repeated statements. |
|
|
19
|
+
| `start_profile` | Takes a CPU profile of the running app and returns the hottest functions. Embedded dashboards only. See [the warning](#start_profile-pauses-the-app). |
|
|
20
|
+
|
|
21
|
+
Everything except `start_profile` only reads.
|
|
22
|
+
|
|
23
|
+
## Two targets
|
|
24
|
+
|
|
25
|
+
`NESTRIX_URL` is either of:
|
|
26
|
+
|
|
27
|
+
- **An app's embedded dashboard**, for example `http://localhost:3000/_nestrix` (the dashboard's path, including a global prefix if you set one). `NESTRIX_TOKEN` is the app's `ui.auth.token`. The app must run the dashboard with a token:
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
NestrixModule.forRoot({
|
|
31
|
+
ui: { enabled: true, auth: { token: process.env.NESTRIX_UI_TOKEN } }, // 32 characters or more
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
The default `ui: 'auto'` is not enough: its startup token is a browser cookie flow and cannot be used by a tool. The dashboard answers `404` to a wrong token (so that it does not reveal itself), and this server reports that as "not a Nestrix dashboard or server, or wrong token".
|
|
36
|
+
|
|
37
|
+
- **The Nestrix server** (`@nestrix-js/server`), for example `http://nestrix-server:4318`. `NESTRIX_TOKEN` is a server API token: read-only and revocable, created in the server's settings.
|
|
38
|
+
|
|
39
|
+
| Variable | |
|
|
40
|
+
|---|---|
|
|
41
|
+
| `NESTRIX_URL` | Required. Base URL of the dashboard or server. A trailing slash is ignored. |
|
|
42
|
+
| `NESTRIX_TOKEN` | Required. Sent as `Authorization: Bearer <token>`, never in a URL and never logged. |
|
|
43
|
+
| `NESTRIX_TIMEOUT_MS` | Optional. Per-request timeout, 100 to 120000, default 10000. |
|
|
44
|
+
|
|
45
|
+
Use `https://` for anything that is not on your own machine: the token otherwise travels unencrypted (the program warns on stderr).
|
|
46
|
+
|
|
47
|
+
## Client configuration
|
|
48
|
+
|
|
49
|
+
**Claude Code**
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
claude mcp add nestrix \
|
|
53
|
+
--env NESTRIX_URL=http://localhost:3000/_nestrix \
|
|
54
|
+
--env NESTRIX_TOKEN="$NESTRIX_UI_TOKEN" \
|
|
55
|
+
-- npx -y @nestrix-js/mcp@alpha
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
**Claude Desktop** (`claude_desktop_config.json`) and **Cursor** (`.cursor/mcp.json`)
|
|
59
|
+
|
|
60
|
+
```json
|
|
61
|
+
{
|
|
62
|
+
"mcpServers": {
|
|
63
|
+
"nestrix": {
|
|
64
|
+
"command": "npx",
|
|
65
|
+
"args": ["-y", "@nestrix-js/mcp@alpha"],
|
|
66
|
+
"env": {
|
|
67
|
+
"NESTRIX_URL": "http://localhost:3000/_nestrix",
|
|
68
|
+
"NESTRIX_TOKEN": "the app's ui.auth.token"
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Keep the token out of files you commit: most clients can read it from your environment instead of a literal value.
|
|
76
|
+
|
|
77
|
+
## What the assistant can and cannot see
|
|
78
|
+
|
|
79
|
+
It sees exactly what the dashboard shows, which is already scrubbed and redacted inside your app (the `redact` hook and the privacy defaults apply): span names, timings, the allow-listed attributes, SQL statement text without bind values, error messages and stack traces. Request and response bodies, bind parameters and headers outside the allowlist are never stored, so they cannot be read. Traces are sampled and kept in memory, so a rare or old request may be missing; `route_stats` counts every request.
|
|
80
|
+
|
|
81
|
+
Text a tool returns is telemetry from your application: a span name or an error message can contain anything. The server tells the model to treat it as data, but give the assistant only the access you would give a colleague reading the dashboard.
|
|
82
|
+
|
|
83
|
+
A server API token can read the query API and nothing else: it is refused by the UI, token management, alert and SLO writes, and ingest.
|
|
84
|
+
|
|
85
|
+
## `start_profile` pauses the app
|
|
86
|
+
|
|
87
|
+
`start_profile` is the only tool that changes anything: it starts a CPU capture in the running app and waits for it to end (default 15 seconds, at most 60), then returns the top functions by self time.
|
|
88
|
+
|
|
89
|
+
- **Starting a capture pauses the whole app briefly**: about 10 ms per MB of compiled code, tens of milliseconds in a small app, a few hundred in a large one. Requests arriving meanwhile wait. Do not run it against production without the owner's agreement.
|
|
90
|
+
- One capture runs at a time (a second start is answered with "already running"), and a new one cannot start within 10 seconds of the last.
|
|
91
|
+
- Generate the traffic you want profiled while it runs; an idle app returns an empty profile.
|
|
92
|
+
- It works only on an embedded dashboard whose profiler is available (see `overview`). The Nestrix server has no profiler, and the tool says so.
|
|
93
|
+
- It sends what the endpoint requires from a trusted caller: a JSON body, an `Origin` equal to `NESTRIX_URL`'s origin and `Sec-Fetch-Site: same-origin`, along with the token.
|
|
94
|
+
- A long duration can exceed the tool timeout of your client.
|
|
95
|
+
|
|
96
|
+
## Errors you may see
|
|
97
|
+
|
|
98
|
+
| Message | Meaning |
|
|
99
|
+
|---|---|
|
|
100
|
+
| `NESTRIX_URL is not set` / `NESTRIX_TOKEN is not set` | The environment of the MCP entry is incomplete. The program exits with this on stderr. |
|
|
101
|
+
| `Nestrix refused the request (HTTP 401)` or `403` | Check `NESTRIX_TOKEN`. |
|
|
102
|
+
| `NESTRIX_URL is not a Nestrix dashboard or server` | `GET api/v1/meta` answered 404: wrong base path, or an embedded dashboard rejecting the token. |
|
|
103
|
+
| `Could not reach ...` / `No answer from ...` | The app or server is not running at that URL, or is too slow (`NESTRIX_TIMEOUT_MS`). |
|
|
104
|
+
|
|
105
|
+
## Licence
|
|
106
|
+
|
|
107
|
+
Apache-2.0.
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
3
|
+
import { ConfigError, isLoopbackUrl, loadConfig } from "./config.js";
|
|
4
|
+
import { createMcpServer } from "./tools.js";
|
|
5
|
+
/** Reads the environment, serves MCP over stdio. stdout carries the protocol, so messages go to stderr. */
|
|
6
|
+
async function main() {
|
|
7
|
+
let config;
|
|
8
|
+
try {
|
|
9
|
+
config = loadConfig(process.env);
|
|
10
|
+
}
|
|
11
|
+
catch (error) {
|
|
12
|
+
if (error instanceof ConfigError) {
|
|
13
|
+
process.stderr.write(`nestrix-mcp: ${error.message}\n`);
|
|
14
|
+
process.exit(1);
|
|
15
|
+
}
|
|
16
|
+
throw error;
|
|
17
|
+
}
|
|
18
|
+
if (config.url.startsWith("http://") && !isLoopbackUrl(config.url)) {
|
|
19
|
+
process.stderr.write("nestrix-mcp: warning: NESTRIX_URL is plain http to a non-local host, so NESTRIX_TOKEN travels unencrypted.\n");
|
|
20
|
+
}
|
|
21
|
+
await createMcpServer(config).connect(new StdioServerTransport());
|
|
22
|
+
}
|
|
23
|
+
// Only when run as the program, not when a file is loaded with require() or import (the
|
|
24
|
+
// no-top-level-await check loads every built file).
|
|
25
|
+
if (import.meta.main) {
|
|
26
|
+
main().catch((error) => {
|
|
27
|
+
process.stderr.write(`nestrix-mcp: ${error instanceof Error ? error.message : String(error)}\n`);
|
|
28
|
+
process.exit(1);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAC;AACjF,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACrE,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAE7C,2GAA2G;AAC3G,KAAK,UAAU,IAAI;IACjB,IAAI,MAAqC,CAAC;IAC1C,IAAI,CAAC;QACH,MAAM,GAAG,UAAU,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IACnC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,KAAK,YAAY,WAAW,EAAE,CAAC;YACjC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,gBAAgB,KAAK,CAAC,OAAO,IAAI,CAAC,CAAC;YACxD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAClB,CAAC;QACD,MAAM,KAAK,CAAC;IACd,CAAC;IACD,IAAI,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC,SAAS,CAAC,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC;QACnE,OAAO,CAAC,MAAM,CAAC,KAAK,CAClB,8GAA8G,CAC/G,CAAC;IACJ,CAAC;IACD,MAAM,eAAe,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,IAAI,oBAAoB,EAAE,CAAC,CAAC;AACpE,CAAC;AAED,wFAAwF;AACxF,oDAAoD;AACpD,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;IACrB,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,KAAc,EAAE,EAAE;QAC9B,OAAO,CAAC,MAAM,CAAC,KAAK,CAClB,gBAAgB,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAC3E,CAAC;QACF,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { AppMapResponse, ClassStatsResponse, ErrorListResponse, MetaResponse, ProfileDetail, ProfileListResponse, ProfileStartResponse, RouteStatsResponse, TraceListResponse, TraceResponse } from "@nestrix-js/protocol";
|
|
2
|
+
import type { McpConfig } from "./config.js";
|
|
3
|
+
/**
|
|
4
|
+
* A failed call to the query API. `message` is written for the model that will read it (and for
|
|
5
|
+
* the person behind it): it says what to check and never contains the token.
|
|
6
|
+
*/
|
|
7
|
+
export declare class NestrixError extends Error {
|
|
8
|
+
/** HTTP status; 0 when no response arrived. */
|
|
9
|
+
readonly status: number;
|
|
10
|
+
/** The parsed JSON body, when the answer had one. */
|
|
11
|
+
readonly body?: unknown | undefined;
|
|
12
|
+
name: string;
|
|
13
|
+
constructor(message: string,
|
|
14
|
+
/** HTTP status; 0 when no response arrived. */
|
|
15
|
+
status: number,
|
|
16
|
+
/** The parsed JSON body, when the answer had one. */
|
|
17
|
+
body?: unknown | undefined);
|
|
18
|
+
}
|
|
19
|
+
export type QueryValue = string | number | undefined;
|
|
20
|
+
/** The part of the query API the tools use, over HTTP with `fetch`. */
|
|
21
|
+
export declare class NestrixClient {
|
|
22
|
+
#private;
|
|
23
|
+
constructor(config: McpConfig, fetchImpl?: typeof fetch);
|
|
24
|
+
meta(): Promise<MetaResponse>;
|
|
25
|
+
traces(params: Record<string, QueryValue>): Promise<TraceListResponse>;
|
|
26
|
+
trace(id: string): Promise<TraceResponse>;
|
|
27
|
+
errors(params: Record<string, QueryValue>): Promise<ErrorListResponse>;
|
|
28
|
+
routes(params: Record<string, QueryValue>): Promise<RouteStatsResponse>;
|
|
29
|
+
classes(params: Record<string, QueryValue>): Promise<ClassStatsResponse>;
|
|
30
|
+
map(params: Record<string, QueryValue>): Promise<AppMapResponse>;
|
|
31
|
+
profiles(): Promise<ProfileListResponse>;
|
|
32
|
+
profile(id: string): Promise<ProfileDetail>;
|
|
33
|
+
/**
|
|
34
|
+
* Starts a capture. The endpoint accepts only what the dashboard page itself sends: a JSON body,
|
|
35
|
+
* an `Origin` equal to the request's own origin and `Sec-Fetch-Site: same-origin`. This client
|
|
36
|
+
* is the trusted caller (it holds the token), so it sets them.
|
|
37
|
+
*/
|
|
38
|
+
startProfile(durationSeconds: number): Promise<ProfileStartResponse>;
|
|
39
|
+
}
|
|
40
|
+
/** `error.message` of an `ApiError` body, if it has one. */
|
|
41
|
+
export declare function apiMessage(body: unknown): string | undefined;
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A failed call to the query API. `message` is written for the model that will read it (and for
|
|
3
|
+
* the person behind it): it says what to check and never contains the token.
|
|
4
|
+
*/
|
|
5
|
+
export class NestrixError extends Error {
|
|
6
|
+
status;
|
|
7
|
+
body;
|
|
8
|
+
name = "NestrixError";
|
|
9
|
+
constructor(message,
|
|
10
|
+
/** HTTP status; 0 when no response arrived. */
|
|
11
|
+
status,
|
|
12
|
+
/** The parsed JSON body, when the answer had one. */
|
|
13
|
+
body) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.status = status;
|
|
16
|
+
this.body = body;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/** The part of the query API the tools use, over HTTP with `fetch`. */
|
|
20
|
+
export class NestrixClient {
|
|
21
|
+
#base;
|
|
22
|
+
#origin;
|
|
23
|
+
#token;
|
|
24
|
+
#timeoutMs;
|
|
25
|
+
#fetch;
|
|
26
|
+
constructor(config, fetchImpl = fetch) {
|
|
27
|
+
this.#base = config.url;
|
|
28
|
+
this.#origin = new URL(config.url).origin;
|
|
29
|
+
this.#token = config.token;
|
|
30
|
+
this.#timeoutMs = config.timeoutMs;
|
|
31
|
+
this.#fetch = fetchImpl;
|
|
32
|
+
}
|
|
33
|
+
meta() {
|
|
34
|
+
return this.#get("api/v1/meta", {});
|
|
35
|
+
}
|
|
36
|
+
traces(params) {
|
|
37
|
+
return this.#get("api/v1/traces", params);
|
|
38
|
+
}
|
|
39
|
+
trace(id) {
|
|
40
|
+
return this.#get("api/v1/trace", { id });
|
|
41
|
+
}
|
|
42
|
+
errors(params) {
|
|
43
|
+
return this.#get("api/v1/errors", params);
|
|
44
|
+
}
|
|
45
|
+
routes(params) {
|
|
46
|
+
return this.#get("api/v1/routes", params);
|
|
47
|
+
}
|
|
48
|
+
classes(params) {
|
|
49
|
+
return this.#get("api/v1/classes", params);
|
|
50
|
+
}
|
|
51
|
+
map(params) {
|
|
52
|
+
return this.#get("api/v1/map", params);
|
|
53
|
+
}
|
|
54
|
+
profiles() {
|
|
55
|
+
return this.#get("api/v1/profiles", {});
|
|
56
|
+
}
|
|
57
|
+
profile(id) {
|
|
58
|
+
return this.#get(`api/v1/profiles/${encodeURIComponent(id)}`, {});
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Starts a capture. The endpoint accepts only what the dashboard page itself sends: a JSON body,
|
|
62
|
+
* an `Origin` equal to the request's own origin and `Sec-Fetch-Site: same-origin`. This client
|
|
63
|
+
* is the trusted caller (it holds the token), so it sets them.
|
|
64
|
+
*/
|
|
65
|
+
async startProfile(durationSeconds) {
|
|
66
|
+
try {
|
|
67
|
+
return await this.#startProfile(durationSeconds);
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
if (error instanceof NestrixError && error.status === 403) {
|
|
71
|
+
const said = apiMessage(error.body);
|
|
72
|
+
throw new NestrixError(`${said ? `The app said: ${said}. ` : ""}The app refused to start the capture (HTTP 403). If NESTRIX_TOKEN is right, the cause is usually the request origin: a reverse proxy in front of the app must pass the original Host header, because the endpoint accepts only requests whose Origin equals their own Host.`, 403, error.body);
|
|
73
|
+
}
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
#startProfile(durationSeconds) {
|
|
78
|
+
return this.#request("api/v1/profiles", {}, {
|
|
79
|
+
method: "POST",
|
|
80
|
+
headers: {
|
|
81
|
+
"content-type": "application/json",
|
|
82
|
+
origin: this.#origin,
|
|
83
|
+
"sec-fetch-site": "same-origin",
|
|
84
|
+
},
|
|
85
|
+
body: JSON.stringify({ durationSeconds }),
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
#get(path, params) {
|
|
89
|
+
return this.#request(path, params, { method: "GET" });
|
|
90
|
+
}
|
|
91
|
+
async #request(path, params, init) {
|
|
92
|
+
const url = new URL(`${this.#base}/${path}`);
|
|
93
|
+
for (const [key, value] of Object.entries(params)) {
|
|
94
|
+
if (value !== undefined && value !== "") {
|
|
95
|
+
url.searchParams.set(key, String(value));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
let response;
|
|
99
|
+
let text;
|
|
100
|
+
try {
|
|
101
|
+
response = await this.#fetch(url, {
|
|
102
|
+
...init,
|
|
103
|
+
headers: {
|
|
104
|
+
accept: "application/json",
|
|
105
|
+
...init.headers,
|
|
106
|
+
authorization: `Bearer ${this.#token}`,
|
|
107
|
+
},
|
|
108
|
+
redirect: "manual",
|
|
109
|
+
signal: AbortSignal.timeout(this.#timeoutMs),
|
|
110
|
+
});
|
|
111
|
+
text = await response.text();
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
throw this.#networkError(error);
|
|
115
|
+
}
|
|
116
|
+
let body;
|
|
117
|
+
try {
|
|
118
|
+
body = text === "" ? undefined : JSON.parse(text);
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
body = undefined;
|
|
122
|
+
}
|
|
123
|
+
if (response.ok) {
|
|
124
|
+
if (body === undefined || body === null || typeof body !== "object") {
|
|
125
|
+
throw new NestrixError(`${this.#base}/${path} answered HTTP ${response.status} but not with JSON: NESTRIX_URL does not look like a Nestrix dashboard or server.`, response.status);
|
|
126
|
+
}
|
|
127
|
+
return body;
|
|
128
|
+
}
|
|
129
|
+
throw this.#statusError(path, response.status, body);
|
|
130
|
+
}
|
|
131
|
+
#networkError(error) {
|
|
132
|
+
const name = error?.name;
|
|
133
|
+
if (name === "TimeoutError" || name === "AbortError") {
|
|
134
|
+
return new NestrixError(`No answer from ${this.#origin} within ${this.#timeoutMs} ms. Is the app running and NESTRIX_URL right? (NESTRIX_TIMEOUT_MS raises the limit.)`, 0);
|
|
135
|
+
}
|
|
136
|
+
// Never use `error.message`: undici can echo request headers (the token) in it.
|
|
137
|
+
const cause = error?.cause;
|
|
138
|
+
const code = error?.code;
|
|
139
|
+
const raw = typeof cause?.code === "string"
|
|
140
|
+
? cause.code
|
|
141
|
+
: typeof code === "string"
|
|
142
|
+
? code
|
|
143
|
+
: "";
|
|
144
|
+
const why = /^[A-Z0-9_]{1,40}$/.test(raw) ? raw : "network error";
|
|
145
|
+
return new NestrixError(`Could not reach ${this.#origin} (${why}). Is the app or server running, and is NESTRIX_URL right?`, 0);
|
|
146
|
+
}
|
|
147
|
+
#statusError(path, status, body) {
|
|
148
|
+
const detail = apiMessage(body);
|
|
149
|
+
const suffix = detail ? ` The server said: ${detail}` : "";
|
|
150
|
+
if (status === 401 || status === 403) {
|
|
151
|
+
return new NestrixError(`Nestrix refused the request (HTTP ${status}). Check NESTRIX_TOKEN: for an embedded dashboard it must equal the app's ui.auth.token, for the Nestrix server it must be a valid, unrevoked API token.${suffix}`, status, body);
|
|
152
|
+
}
|
|
153
|
+
if (status === 404 && path === "api/v1/meta") {
|
|
154
|
+
return new NestrixError("NESTRIX_URL is not a Nestrix dashboard or server: GET api/v1/meta answered 404. Use the dashboard's base path (for example http://localhost:3000/_nestrix) or the server's base URL. An embedded dashboard also answers 404 to a wrong NESTRIX_TOKEN, so check the token too, and that the app runs with `ui: { enabled: true, auth: { token } }`.", 404, body);
|
|
155
|
+
}
|
|
156
|
+
if (status === 404) {
|
|
157
|
+
return new NestrixError(`Not found: ${path}.${suffix || " This Nestrix version may not support it."}`, 404, body);
|
|
158
|
+
}
|
|
159
|
+
return new NestrixError(`Nestrix answered HTTP ${status} for ${path}.${suffix}`, status, body);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/** `error.message` of an `ApiError` body, if it has one. */
|
|
163
|
+
export function apiMessage(body) {
|
|
164
|
+
if (body === null || typeof body !== "object")
|
|
165
|
+
return undefined;
|
|
166
|
+
const error = body.error;
|
|
167
|
+
if (error === null || typeof error !== "object")
|
|
168
|
+
return undefined;
|
|
169
|
+
const message = error.message;
|
|
170
|
+
return typeof message === "string" ? message : undefined;
|
|
171
|
+
}
|
|
172
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAcA;;;GAGG;AACH,MAAM,OAAO,YAAa,SAAQ,KAAK;IAK1B;IAEA;IANF,IAAI,GAAG,cAAc,CAAC;IAC/B,YACE,OAAe;IACf,+CAA+C;IACtC,MAAc;IACvB,qDAAqD;IAC5C,IAAc;QAEvB,KAAK,CAAC,OAAO,CAAC,CAAC;QAJN,WAAM,GAAN,MAAM,CAAQ;QAEd,SAAI,GAAJ,IAAI,CAAU;IAGzB,CAAC;CACF;AAID,uEAAuE;AACvE,MAAM,OAAO,aAAa;IACf,KAAK,CAAS;IACd,OAAO,CAAS;IAChB,MAAM,CAAS;I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AAC,CAAC,CAAC,qBAAqB,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC3D,IAAI,MAAM,KAAK,GAAG,IAAI,MAAM,KAAK,GAAG,EAAE,CAAC;YACrC,OAAO,IAAI,YAAY,CACrB,qCAAqC,MAAM,2JAA2J,MAAM,EAAE,EAC9M,MAAM,EACN,IAAI,CACL,CAAC;QACJ,CAAC;QACD,IAAI,MAAM,KAAK,GAAG,IAAI,IAAI,KAAK,aAAa,EAAE,CAAC;YAC7C,OAAO,IAAI,YAAY,CACrB,oVAAoV,EACpV,GAAG,EACH,IAAI,CACL,CAAC;QACJ,CAAC;QACD,IAAI,MAAM,KAAK,GAAG,EAAE,CAAC;YACnB,OAAO,IAAI,YAAY,CACrB,cAAc,IAAI,IAAI,MAAM,IAAI,2CAA2C,EAAE,EAC7E,GAAG,EACH,IAAI,CACL,CAAC;QACJ,CAAC;QACD,OAAO,IAAI,YAAY,CACrB,yBAAyB,MAAM,QAAQ,IAAI,IAAI,MAAM,EAAE,EACvD,MAAM,EACN,IAAI,CACL,CAAC;IACJ,CAAC;CACF;AAED,4DAA4D;AAC5D,MAAM,UAAU,UAAU,CAAC,IAAa;IACtC,IAAI,IAAI,KAAK,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ;QAAE,OAAO,SAAS,CAAC;IAChE,MAAM,KAAK,GAAI,IAA4B,CAAC,KAAK,CAAC;IAClD,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,SAAS,CAAC;IAClE,MAAM,OAAO,GAAI,KAA+B,CAAC,OAAO,CAAC;IACzD,OAAO,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC;AAC3D,CAAC"}
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Configuration of the MCP server, read from environment variables. */
|
|
2
|
+
export interface McpConfig {
|
|
3
|
+
/** Base URL of the dashboard (`http://localhost:3000/_nestrix`) or the standalone server, without a trailing slash. */
|
|
4
|
+
url: string;
|
|
5
|
+
token: string;
|
|
6
|
+
/** Per-request timeout in milliseconds. */
|
|
7
|
+
timeoutMs: number;
|
|
8
|
+
}
|
|
9
|
+
/** A configuration problem, with a message meant to be shown to the person who set the variables. */
|
|
10
|
+
export declare class ConfigError extends Error {
|
|
11
|
+
name: string;
|
|
12
|
+
}
|
|
13
|
+
/** Reads `NESTRIX_URL`, `NESTRIX_TOKEN` and `NESTRIX_TIMEOUT_MS`. Throws `ConfigError`. */
|
|
14
|
+
export declare function loadConfig(env: Readonly<Record<string, string | undefined>>): McpConfig;
|
|
15
|
+
/** True for `localhost`, `127.x.y.z` and `[::1]`. */
|
|
16
|
+
export declare function isLoopbackUrl(url: string): boolean;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/** A configuration problem, with a message meant to be shown to the person who set the variables. */
|
|
2
|
+
export class ConfigError extends Error {
|
|
3
|
+
name = "ConfigError";
|
|
4
|
+
}
|
|
5
|
+
const DEFAULT_TIMEOUT_MS = 10_000;
|
|
6
|
+
/** Reads `NESTRIX_URL`, `NESTRIX_TOKEN` and `NESTRIX_TIMEOUT_MS`. Throws `ConfigError`. */
|
|
7
|
+
export function loadConfig(env) {
|
|
8
|
+
const rawUrl = env.NESTRIX_URL?.trim();
|
|
9
|
+
if (!rawUrl) {
|
|
10
|
+
throw new ConfigError("NESTRIX_URL is not set. Set it to the base URL of the Nestrix dashboard (for example http://localhost:3000/_nestrix) or of the Nestrix server (for example http://localhost:4318).");
|
|
11
|
+
}
|
|
12
|
+
let parsed;
|
|
13
|
+
try {
|
|
14
|
+
parsed = new URL(rawUrl);
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
throw new ConfigError(`NESTRIX_URL is not a valid URL: ${rawUrl}`);
|
|
18
|
+
}
|
|
19
|
+
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
|
|
20
|
+
throw new ConfigError("NESTRIX_URL must start with http:// or https://.");
|
|
21
|
+
}
|
|
22
|
+
if (parsed.username !== "" || parsed.password !== "") {
|
|
23
|
+
throw new ConfigError("NESTRIX_URL must not contain a user name or password. Put the token in NESTRIX_TOKEN.");
|
|
24
|
+
}
|
|
25
|
+
parsed.search = "";
|
|
26
|
+
parsed.hash = "";
|
|
27
|
+
const url = parsed.toString().replace(/\/+$/, "");
|
|
28
|
+
const token = env.NESTRIX_TOKEN?.trim();
|
|
29
|
+
if (!token) {
|
|
30
|
+
throw new ConfigError("NESTRIX_TOKEN is not set. For an app's embedded dashboard use the app's `ui.auth.token` (the app must run with `ui: { enabled: true, auth: { token } }`); for the Nestrix server use an API token created in its settings.");
|
|
31
|
+
}
|
|
32
|
+
if (!/^[\x21-\x7e]+$/.test(token)) {
|
|
33
|
+
throw new ConfigError("NESTRIX_TOKEN contains whitespace, control or non-ASCII characters, so it cannot be sent as an HTTP header. Check it for a stray newline or a bad copy-paste (the value is not shown).");
|
|
34
|
+
}
|
|
35
|
+
let timeoutMs = DEFAULT_TIMEOUT_MS;
|
|
36
|
+
const rawTimeout = env.NESTRIX_TIMEOUT_MS?.trim();
|
|
37
|
+
if (rawTimeout) {
|
|
38
|
+
const n = Number(rawTimeout);
|
|
39
|
+
if (!Number.isInteger(n) || n < 100 || n > 120_000) {
|
|
40
|
+
throw new ConfigError("NESTRIX_TIMEOUT_MS must be a whole number of milliseconds between 100 and 120000.");
|
|
41
|
+
}
|
|
42
|
+
timeoutMs = n;
|
|
43
|
+
}
|
|
44
|
+
return { url, token, timeoutMs };
|
|
45
|
+
}
|
|
46
|
+
/** True for `localhost`, `127.x.y.z` and `[::1]`. */
|
|
47
|
+
export function isLoopbackUrl(url) {
|
|
48
|
+
try {
|
|
49
|
+
const host = new URL(url).hostname;
|
|
50
|
+
return (host === "localhost" ||
|
|
51
|
+
host === "[::1]" ||
|
|
52
|
+
/^127\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(host));
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=config.js.map
|