@browserstack/mcp-server 1.5.1-beta.2 → 2.0.0
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/README.md +54 -1
- package/capability/tm.capability-index.json +50419 -0
- package/dist/lib/instrumentation.d.ts +14 -1
- package/dist/lib/instrumentation.js +8 -1
- package/dist/server-factory.js +5 -0
- package/dist/tools/capability-registry/bind.d.ts +28 -0
- package/dist/tools/capability-registry/bind.js +371 -0
- package/dist/tools/capability-registry/config.d.ts +62 -0
- package/dist/tools/capability-registry/config.js +218 -0
- package/dist/tools/capability-registry/discovery.d.ts +44 -0
- package/dist/tools/capability-registry/discovery.js +99 -0
- package/dist/tools/capability-registry/egress.d.ts +44 -0
- package/dist/tools/capability-registry/egress.js +174 -0
- package/dist/tools/capability-registry/index-loader.d.ts +162 -0
- package/dist/tools/capability-registry/index-loader.js +523 -0
- package/dist/tools/capability-registry/redact.d.ts +34 -0
- package/dist/tools/capability-registry/redact.js +98 -0
- package/dist/tools/capability-registry/register.d.ts +48 -0
- package/dist/tools/capability-registry/register.js +708 -0
- package/dist/tools/capability-registry/resolve.d.ts +38 -0
- package/dist/tools/capability-registry/resolve.js +83 -0
- package/dist/tools/capability-registry/search.d.ts +170 -0
- package/dist/tools/capability-registry/search.js +709 -0
- package/dist/tools/capability-registry/types.d.ts +302 -0
- package/dist/tools/capability-registry/types.js +33 -0
- package/package.json +9 -3
|
@@ -3,12 +3,25 @@ export type ClientInfo = {
|
|
|
3
3
|
version?: string;
|
|
4
4
|
};
|
|
5
5
|
export type ToolOutcome = "ok" | "error_result" | "threw";
|
|
6
|
+
/**
|
|
7
|
+
* Extra fields a caller may attach to its own row (the capability registry records what
|
|
8
|
+
* it invoked and how the product answered). Values are scalars so every field stays one
|
|
9
|
+
* queryable column.
|
|
10
|
+
*
|
|
11
|
+
* SOME OF IT IS FREE TEXT, and this comment used to claim the opposite. The registry
|
|
12
|
+
* records `search_query` and `change_summary`, both written by the agent from what the
|
|
13
|
+
* user asked for, and both leave the machine on every search and every write. They go
|
|
14
|
+
* through `capability-registry/redact.ts` first, which strips PII it can identify by
|
|
15
|
+
* SHAPE and caps the length — it cannot strip a person's name, and says so. Anything
|
|
16
|
+
* added here that carries user text must go through the same filter.
|
|
17
|
+
*/
|
|
18
|
+
export type MCPEventExtras = Record<string, string | number | boolean | undefined>;
|
|
6
19
|
/**
|
|
7
20
|
* Inside `withToolCall` this only records into the call's context; the single row is
|
|
8
21
|
* written when the handler settles. Outside one (`started` heartbeat, unwrapped host
|
|
9
22
|
* tools) it posts a row immediately, as before.
|
|
10
23
|
*/
|
|
11
|
-
export declare function trackMCP(toolName: string, clientInfo: ClientInfo, error?: unknown, config?: any): void;
|
|
24
|
+
export declare function trackMCP(toolName: string, clientInfo: ClientInfo, error?: unknown, config?: any, extras?: MCPEventExtras): void;
|
|
12
25
|
/**
|
|
13
26
|
* Runs a tool handler and writes one MCPInstrumentation row when it settles: success,
|
|
14
27
|
* duration_ms, outcome (ok / error_result / threw), error fields on failure.
|
|
@@ -47,7 +47,7 @@ const callContext = new AsyncLocalStorage();
|
|
|
47
47
|
* written when the handler settles. Outside one (`started` heartbeat, unwrapped host
|
|
48
48
|
* tools) it posts a row immediately, as before.
|
|
49
49
|
*/
|
|
50
|
-
export function trackMCP(toolName, clientInfo, error, config) {
|
|
50
|
+
export function trackMCP(toolName, clientInfo, error, config, extras) {
|
|
51
51
|
const ctx = callContext.getStore();
|
|
52
52
|
if (ctx) {
|
|
53
53
|
if (clientInfo?.name && !ctx.clientInfo?.name)
|
|
@@ -56,6 +56,9 @@ export function trackMCP(toolName, clientInfo, error, config) {
|
|
|
56
56
|
ctx.config = config;
|
|
57
57
|
if (error)
|
|
58
58
|
ctx.error = error;
|
|
59
|
+
// Later calls win, so a handler can record an identity first and the outcome after.
|
|
60
|
+
if (extras)
|
|
61
|
+
ctx.extras = { ...ctx.extras, ...extras };
|
|
59
62
|
return;
|
|
60
63
|
}
|
|
61
64
|
if (clientInfo?.name) {
|
|
@@ -70,6 +73,9 @@ export function trackMCP(toolName, clientInfo, error, config) {
|
|
|
70
73
|
...baseProperties(toolName, clientInfo),
|
|
71
74
|
success: !error,
|
|
72
75
|
...(error ? errorProperties(error) : {}),
|
|
76
|
+
// Undefined entries are dropped by JSON.stringify, so an absent extra is an
|
|
77
|
+
// absent column rather than a null one.
|
|
78
|
+
...(extras ?? {}),
|
|
73
79
|
},
|
|
74
80
|
};
|
|
75
81
|
sendEvent(event, config);
|
|
@@ -117,6 +123,7 @@ export async function withToolCall(toolName, getClientInfo, config, fn) {
|
|
|
117
123
|
duration_ms: Math.max(0, Math.round(performance.now() - startedAt)),
|
|
118
124
|
outcome,
|
|
119
125
|
...(ctx.error !== undefined ? errorProperties(ctx.error) : {}),
|
|
126
|
+
...(ctx.extras ?? {}),
|
|
120
127
|
},
|
|
121
128
|
};
|
|
122
129
|
sendEvent(event, ctx.config ?? config);
|
package/dist/server-factory.js
CHANGED
|
@@ -17,6 +17,7 @@ import addBuildInsightsTools from "./tools/build-insights.js";
|
|
|
17
17
|
import { setupOnInitialized } from "./oninitialized.js";
|
|
18
18
|
import addRCATools from "./tools/rca-agent.js";
|
|
19
19
|
import addAskBrowserStackAITool from "./tools/ask-browserstack/register.js";
|
|
20
|
+
import addCapabilityRegistryTools from "./tools/capability-registry/register.js";
|
|
20
21
|
import { instrumentToolLatency } from "./lib/tool-latency.js";
|
|
21
22
|
import { nodeUpgradeNotice } from "./lib/node-version-notice.js";
|
|
22
23
|
/**
|
|
@@ -58,6 +59,10 @@ export class BrowserStackMcpServer {
|
|
|
58
59
|
// permission asks back to this client, so a write can be confirmed by the human
|
|
59
60
|
// sitting in front of it rather than refused for want of anyone to ask.
|
|
60
61
|
addAskBrowserStackAITool,
|
|
62
|
+
// Driven by a prebuilt index rather than hand-written per endpoint. Registers
|
|
63
|
+
// nothing (and logs why) when the artifact is absent, so a packaging problem cannot
|
|
64
|
+
// take the other products' tools down with it.
|
|
65
|
+
addCapabilityRegistryTools,
|
|
61
66
|
];
|
|
62
67
|
toolAdders.forEach((adder) => {
|
|
63
68
|
// Each adder now returns a Record<string, Tool>
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turn grouped caller arguments into a path, a query and a body.
|
|
3
|
+
*
|
|
4
|
+
* Arguments arrive GROUPED — {path_params, query, body} — because spec parameter names
|
|
5
|
+
* collide across locations: four tm operations declare one name in two places (`bulk-move`
|
|
6
|
+
* has `folder_id` as both a path parameter and a body field). A flat map cannot say which
|
|
7
|
+
* one is meant, which is exactly why the Python side used to rename body fields `body_*`.
|
|
8
|
+
* Grouping removes the collision AND the rename, so a caller sends the spec's own names.
|
|
9
|
+
*/
|
|
10
|
+
import { Capability, WireParam } from "./types.js";
|
|
11
|
+
export interface GroupedArguments {
|
|
12
|
+
path_params?: Record<string, unknown>;
|
|
13
|
+
query?: Record<string, unknown>;
|
|
14
|
+
body?: Record<string, unknown>;
|
|
15
|
+
}
|
|
16
|
+
export interface BoundRequest {
|
|
17
|
+
path: string;
|
|
18
|
+
query: Record<string, unknown>;
|
|
19
|
+
body?: Record<string, unknown>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Check one argument against its declared schema, raising a caller-safe error.
|
|
23
|
+
*
|
|
24
|
+
* For path parameters this is only half the defence — see `pathSegment`, which covers the
|
|
25
|
+
* string ids that carry no type constraint worth the name.
|
|
26
|
+
*/
|
|
27
|
+
export declare function coerce(value: unknown, param: WireParam): unknown;
|
|
28
|
+
export declare function bind(capability: Capability, args: GroupedArguments): BoundRequest;
|
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turn grouped caller arguments into a path, a query and a body.
|
|
3
|
+
*
|
|
4
|
+
* Arguments arrive GROUPED — {path_params, query, body} — because spec parameter names
|
|
5
|
+
* collide across locations: four tm operations declare one name in two places (`bulk-move`
|
|
6
|
+
* has `folder_id` as both a path parameter and a body field). A flat map cannot say which
|
|
7
|
+
* one is meant, which is exactly why the Python side used to rename body fields `body_*`.
|
|
8
|
+
* Grouping removes the collision AND the rename, so a caller sends the spec's own names.
|
|
9
|
+
*/
|
|
10
|
+
import { InvocationError } from "./index-loader.js";
|
|
11
|
+
/**
|
|
12
|
+
* String formats worth ENFORCING, as opposed to merely publishing.
|
|
13
|
+
*
|
|
14
|
+
* A validator that rejects a correct value is worse than no validator: the caller cannot
|
|
15
|
+
* route around it, and the request that would have worked never leaves. So this covers only
|
|
16
|
+
* the formats where "wrong" is unambiguous and a legitimate value cannot trip it.
|
|
17
|
+
*
|
|
18
|
+
* tm declares eight formats. `date`, `date-time` and `uuid` are here. `email` and `uri` are
|
|
19
|
+
* deliberately NOT — every compact regex for either rejects addresses and URLs that servers
|
|
20
|
+
* accept, and being wrong in that direction blocks real work. `int64`, `float` and `binary`
|
|
21
|
+
* say what a number or blob is, which `type` already covers.
|
|
22
|
+
*/
|
|
23
|
+
const FORMATS = {
|
|
24
|
+
date: {
|
|
25
|
+
// A real calendar date, so 2026-02-31 fails rather than being reformatted.
|
|
26
|
+
test: (v) => /^\d{4}-\d{2}-\d{2}$/.test(v) &&
|
|
27
|
+
!Number.isNaN(Date.parse(`${v}T00:00:00Z`)) &&
|
|
28
|
+
new Date(`${v}T00:00:00Z`).toISOString().startsWith(v),
|
|
29
|
+
want: "a date (YYYY-MM-DD)",
|
|
30
|
+
},
|
|
31
|
+
"date-time": {
|
|
32
|
+
// Permissive on purpose: offset or Z, optional fractional seconds. Tightening this
|
|
33
|
+
// buys nothing and starts rejecting timestamps the product would have taken.
|
|
34
|
+
test: (v) => /^\d{4}-\d{2}-\d{2}[Tt ]\d{2}:\d{2}:\d{2}(\.\d+)?([Zz]|[+-]\d{2}:?\d{2})?$/.test(v) && !Number.isNaN(Date.parse(v)),
|
|
35
|
+
want: "a date-time (e.g. 2026-01-31T09:30:00Z)",
|
|
36
|
+
},
|
|
37
|
+
uuid: {
|
|
38
|
+
test: (v) => /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(v),
|
|
39
|
+
want: "a UUID",
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* A declared `pattern`, compiled once, or undefined if it will not compile here.
|
|
44
|
+
*
|
|
45
|
+
* `u` because tm's patterns use `\p{L}`/`\p{N}`, which are a syntax error without it. A
|
|
46
|
+
* pattern this engine cannot compile is SKIPPED rather than thrown: the index is generated
|
|
47
|
+
* from someone else's spec, and refusing to invoke a working endpoint because its regex
|
|
48
|
+
* dialect differs would be our bug landing on the caller.
|
|
49
|
+
*/
|
|
50
|
+
const patternCache = new Map();
|
|
51
|
+
function compiled(pattern) {
|
|
52
|
+
if (!patternCache.has(pattern)) {
|
|
53
|
+
let regex;
|
|
54
|
+
try {
|
|
55
|
+
regex = new RegExp(pattern, "u");
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
try {
|
|
59
|
+
regex = new RegExp(pattern);
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
regex = undefined;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
patternCache.set(pattern, regex);
|
|
66
|
+
}
|
|
67
|
+
return patternCache.get(pattern);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Every constraint the parameter declares, checked against the COERCED value.
|
|
71
|
+
*
|
|
72
|
+
* Runs inside `bind`, which register.ts calls dry before the write-consent gate — so a
|
|
73
|
+
* value that breaks a constraint is refused before the user is asked to approve anything,
|
|
74
|
+
* and nothing reaches egress.
|
|
75
|
+
*
|
|
76
|
+
* `label` carries the parent name for a nested field, so the message names `test_case.name`
|
|
77
|
+
* rather than a bare `name` the caller would have to go looking for.
|
|
78
|
+
*/
|
|
79
|
+
function checkConstraints(value, param, label = param.name) {
|
|
80
|
+
const fail = (what) => {
|
|
81
|
+
throw new InvocationError(`'${label}' ${what}`);
|
|
82
|
+
};
|
|
83
|
+
if (typeof value === "number") {
|
|
84
|
+
if (param.minimum !== undefined && value < param.minimum)
|
|
85
|
+
fail(`must be at least ${param.minimum}`);
|
|
86
|
+
if (param.maximum !== undefined && value > param.maximum)
|
|
87
|
+
fail(`must be at most ${param.maximum}`);
|
|
88
|
+
if (param.multipleOf !== undefined && param.multipleOf > 0) {
|
|
89
|
+
// Scale BOTH to integers before the modulo: 0.3 % 0.1 is 0.09999… in binary floating
|
|
90
|
+
// point and would reject a value the product accepts. The scale has to cover the
|
|
91
|
+
// value's decimals as well as the step's — scaling by the step alone rounds 0.25 to
|
|
92
|
+
// 3 against a step of 1, which then divides evenly and lets a bad value through.
|
|
93
|
+
const scale = 10 ** Math.max(decimals(param.multipleOf), decimals(value));
|
|
94
|
+
if (Math.round(value * scale) % Math.round(param.multipleOf * scale) !==
|
|
95
|
+
0)
|
|
96
|
+
fail(`must be a multiple of ${param.multipleOf}`);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (typeof value === "string") {
|
|
100
|
+
if (param.minLength !== undefined && value.length < param.minLength)
|
|
101
|
+
fail(`must be at least ${param.minLength} character(s)`);
|
|
102
|
+
if (param.maxLength !== undefined && value.length > param.maxLength)
|
|
103
|
+
fail(`must be at most ${param.maxLength} character(s)`);
|
|
104
|
+
if (param.pattern) {
|
|
105
|
+
const regex = compiled(param.pattern);
|
|
106
|
+
if (regex && !regex.test(value))
|
|
107
|
+
fail(`must match ${param.pattern}`);
|
|
108
|
+
}
|
|
109
|
+
const format = param.format ? FORMATS[param.format] : undefined;
|
|
110
|
+
if (format && !format.test(value))
|
|
111
|
+
fail(`must be ${format.want}`);
|
|
112
|
+
}
|
|
113
|
+
if (Array.isArray(value)) {
|
|
114
|
+
if (param.minItems !== undefined && value.length < param.minItems)
|
|
115
|
+
fail(`must have at least ${param.minItems} item(s)`);
|
|
116
|
+
if (param.maxItems !== undefined && value.length > param.maxItems)
|
|
117
|
+
fail(`must have at most ${param.maxItems} item(s)`);
|
|
118
|
+
if (param.uniqueItems) {
|
|
119
|
+
const seen = new Set(value.map((item) => JSON.stringify(item)));
|
|
120
|
+
if (seen.size !== value.length)
|
|
121
|
+
fail("must not contain duplicate items");
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/** Decimal places, for scaling `multipleOf` out of floating point. */
|
|
126
|
+
function decimals(n) {
|
|
127
|
+
const text = String(n);
|
|
128
|
+
const dot = text.indexOf(".");
|
|
129
|
+
return dot < 0 ? 0 : text.length - dot - 1;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* The declared fields one level inside an object, or inside each item of an array.
|
|
133
|
+
*
|
|
134
|
+
* Only what `fields` names is checked, and an undeclared key is left alone — `fields` is a
|
|
135
|
+
* projection of the shape, not a closed contract, so rejecting the rest would refuse valid
|
|
136
|
+
* bodies. That is the opposite of the top level, where the full parameter list IS known and
|
|
137
|
+
* an unknown name is an error.
|
|
138
|
+
*/
|
|
139
|
+
function checkFields(value, param) {
|
|
140
|
+
if (!param.fields?.length)
|
|
141
|
+
return;
|
|
142
|
+
const items = Array.isArray(value) ? value : [value];
|
|
143
|
+
for (const item of items) {
|
|
144
|
+
if (typeof item !== "object" || item === null || Array.isArray(item))
|
|
145
|
+
continue;
|
|
146
|
+
const record = item;
|
|
147
|
+
for (const field of param.fields) {
|
|
148
|
+
if (!(field.name in record)) {
|
|
149
|
+
if (field.required)
|
|
150
|
+
throw new InvocationError(`missing required parameter(s): ${param.name}.${field.name}`);
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
const label = `${param.name}.${field.name}`;
|
|
154
|
+
const coerced = coerceType(record[field.name], field, label);
|
|
155
|
+
checkConstraints(coerced, field, label);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* One path value, checked for the segments that silently retarget the request.
|
|
161
|
+
*
|
|
162
|
+
* `encodeURIComponent` does NOT escape a dot, and `new URL()` in egress resolves `.` and
|
|
163
|
+
* `..` the way a filesystem would. So a string path parameter carrying `..` walks up a
|
|
164
|
+
* segment and the call lands somewhere else entirely:
|
|
165
|
+
*
|
|
166
|
+
* update_test_case { project_id: "PR-1", test_case_id: ".." }
|
|
167
|
+
* -> PATCH /api/v2/projects/PR-1/ (the PROJECT, not a test case)
|
|
168
|
+
*
|
|
169
|
+
* That is worse than a failed call. The human approved "update this test case" and gave a
|
|
170
|
+
* change_summary saying so, and the request edits a different resource — with the approval
|
|
171
|
+
* already banked. A prompt injection sitting in test-case content is enough to steer an
|
|
172
|
+
* agent into it.
|
|
173
|
+
*
|
|
174
|
+
* Empty and `.` are refused for the same reason: both collapse the segment and leave the
|
|
175
|
+
* request pointing at the collection.
|
|
176
|
+
*
|
|
177
|
+
* TYPE CHECKING IS NOT THE DEFENCE HERE, though a comment used to claim it was. It holds
|
|
178
|
+
* only for `type: integer` ids; eleven enabled capabilities take a STRING path parameter
|
|
179
|
+
* with no pattern, enum or format, and every one of those values reaches the URL as given.
|
|
180
|
+
*/
|
|
181
|
+
function pathSegment(value, name) {
|
|
182
|
+
const text = String(value);
|
|
183
|
+
const trimmed = text.trim();
|
|
184
|
+
if (trimmed === "" || trimmed === "." || trimmed === "..") {
|
|
185
|
+
throw new InvocationError(`'${name}' is not a usable path value: ${JSON.stringify(text)} would ` +
|
|
186
|
+
`retarget the request at a different resource rather than identify one. ` +
|
|
187
|
+
`Pass the id exactly as a read returned it.`);
|
|
188
|
+
}
|
|
189
|
+
return text;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Check one argument against its declared schema, raising a caller-safe error.
|
|
193
|
+
*
|
|
194
|
+
* For path parameters this is only half the defence — see `pathSegment`, which covers the
|
|
195
|
+
* string ids that carry no type constraint worth the name.
|
|
196
|
+
*/
|
|
197
|
+
export function coerce(value, param) {
|
|
198
|
+
const coerced = coerceType(value, param);
|
|
199
|
+
checkConstraints(coerced, param);
|
|
200
|
+
checkFields(coerced, param);
|
|
201
|
+
return coerced;
|
|
202
|
+
}
|
|
203
|
+
function coerceType(value, param, label = param.name) {
|
|
204
|
+
const expected = param.type;
|
|
205
|
+
if (expected === "object" || expected === "array") {
|
|
206
|
+
// An opaque body object is passed through as given: the spec does not describe its
|
|
207
|
+
// fields, so validating or reshaping it would mean inventing a contract.
|
|
208
|
+
if (expected === "object" &&
|
|
209
|
+
(typeof value !== "object" || value === null || Array.isArray(value))) {
|
|
210
|
+
throw new InvocationError(`'${label}' must be an object`);
|
|
211
|
+
}
|
|
212
|
+
if (expected === "array" && !Array.isArray(value)) {
|
|
213
|
+
throw new InvocationError(`'${label}' must be a list`);
|
|
214
|
+
}
|
|
215
|
+
return value;
|
|
216
|
+
}
|
|
217
|
+
if (expected === "integer" || expected === "number") {
|
|
218
|
+
// `Number("")` and `Number(" ")` are both 0, so a blank id silently became id 0 and the
|
|
219
|
+
// call went to whatever that resolves to. Refuse instead of inventing a value.
|
|
220
|
+
const raw = String(value).trim();
|
|
221
|
+
if (raw === "") {
|
|
222
|
+
throw new InvocationError(`'${label}' must be a number, and was empty`);
|
|
223
|
+
}
|
|
224
|
+
const parsed = Number(raw);
|
|
225
|
+
if (!Number.isFinite(parsed)) {
|
|
226
|
+
throw new InvocationError(`'${label}' must be a number`);
|
|
227
|
+
}
|
|
228
|
+
// `Math.trunc` turned "1.9" into 1 without a word. An id is either an integer or it is
|
|
229
|
+
// the wrong id, and quietly rounding it addresses the wrong record.
|
|
230
|
+
if (expected === "integer" && !Number.isInteger(parsed)) {
|
|
231
|
+
throw new InvocationError(`'${label}' must be a whole number, not ${JSON.stringify(raw)}`);
|
|
232
|
+
}
|
|
233
|
+
return parsed;
|
|
234
|
+
}
|
|
235
|
+
if (expected === "boolean") {
|
|
236
|
+
if (typeof value === "boolean")
|
|
237
|
+
return value;
|
|
238
|
+
const text = String(value).trim().toLowerCase();
|
|
239
|
+
if (["true", "1", "yes"].includes(text))
|
|
240
|
+
return true;
|
|
241
|
+
if (["false", "0", "no"].includes(text))
|
|
242
|
+
return false;
|
|
243
|
+
throw new InvocationError(`'${label}' must be true or false`);
|
|
244
|
+
}
|
|
245
|
+
// NO DECLARED TYPE: pass the value through as given.
|
|
246
|
+
//
|
|
247
|
+
// The fallthrough below is String(value), which is right for a declared string and
|
|
248
|
+
// silently destructive for anything the spec left untyped. create_report declares
|
|
249
|
+
// `mail_to` and `report_filters` with no type and the API wants objects for both, so a
|
|
250
|
+
// correct payload was serialised to the literal "[object Object]" and refused:
|
|
251
|
+
//
|
|
252
|
+
// The property '#/mail_to' of type string did not match the following type: object
|
|
253
|
+
//
|
|
254
|
+
// The caller sent the right thing, this layer broke it, and the server's message blames
|
|
255
|
+
// the caller for a string it never wrote. An untyped parameter means the spec declines to
|
|
256
|
+
// say what shape it is — which is a reason to leave it alone, not to guess "string".
|
|
257
|
+
const untyped = !expected;
|
|
258
|
+
if (untyped && (typeof value === "object" || typeof value === "boolean")) {
|
|
259
|
+
return value;
|
|
260
|
+
}
|
|
261
|
+
const text = String(value);
|
|
262
|
+
if (param.values && param.values.length > 0) {
|
|
263
|
+
const allowed = param.values.map((v) => String(v));
|
|
264
|
+
if (!allowed.includes(text)) {
|
|
265
|
+
throw new InvocationError(`'${label}' must be one of: ${allowed.join(", ")}`);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return text;
|
|
269
|
+
}
|
|
270
|
+
/** Place a value at a JSON-pointer-ish path, creating the objects on the way. */
|
|
271
|
+
function place(root, pointer, value) {
|
|
272
|
+
const segments = pointer.split("/").filter((segment) => segment !== "");
|
|
273
|
+
let cursor = root;
|
|
274
|
+
for (const segment of segments.slice(0, -1)) {
|
|
275
|
+
const next = cursor[segment];
|
|
276
|
+
if (typeof next !== "object" || next === null || Array.isArray(next)) {
|
|
277
|
+
cursor[segment] = {};
|
|
278
|
+
}
|
|
279
|
+
cursor = cursor[segment];
|
|
280
|
+
}
|
|
281
|
+
cursor[segments[segments.length - 1]] = value;
|
|
282
|
+
}
|
|
283
|
+
const GROUPS = [
|
|
284
|
+
{ group: "path_params", declared: "path_params" },
|
|
285
|
+
{ group: "query", declared: "query" },
|
|
286
|
+
{ group: "body", declared: "body" },
|
|
287
|
+
];
|
|
288
|
+
export function bind(capability, args) {
|
|
289
|
+
let path = capability.path;
|
|
290
|
+
const query = {};
|
|
291
|
+
const body = {};
|
|
292
|
+
for (const { group, declared } of GROUPS) {
|
|
293
|
+
const supplied = args[group] || {};
|
|
294
|
+
if (typeof supplied !== "object" ||
|
|
295
|
+
supplied === null ||
|
|
296
|
+
Array.isArray(supplied)) {
|
|
297
|
+
throw new InvocationError(`${group} must be an object of name -> value`);
|
|
298
|
+
}
|
|
299
|
+
const params = capability[declared] || [];
|
|
300
|
+
const byName = new Map(params.map((param) => [param.name, param]));
|
|
301
|
+
// Unknown arguments are an error rather than being dropped: silently ignoring a
|
|
302
|
+
// misspelled filter would return a larger result set that looks like a correct answer.
|
|
303
|
+
const unknown = Object.keys(supplied).filter((name) => !byName.has(name));
|
|
304
|
+
if (unknown.length > 0) {
|
|
305
|
+
// A CALLER WHO SENT THE WRAPPER WE BUILD DESERVES TO BE TOLD SO.
|
|
306
|
+
//
|
|
307
|
+
// Most of these endpoints want a nested body — Rails `params.require(:test_case)` —
|
|
308
|
+
// and the fields declare that as `json_path: /test_case/name`, so this layer
|
|
309
|
+
// assembles the wrapper itself and the caller sends the fields flat. A caller who
|
|
310
|
+
// wraps it by hand therefore sends a key that is never a declared param, and the
|
|
311
|
+
// bare message reads as "that field does not exist" when the truth is the opposite:
|
|
312
|
+
// it exists, and building it is our job.
|
|
313
|
+
//
|
|
314
|
+
// Worth special-casing because the guidance itself has been telling callers to wrap:
|
|
315
|
+
// 25 tm capabilities carried a line describing the WIRE format to a caller who never
|
|
316
|
+
// writes the wire. Those lines are being corrected, but an agent working from an
|
|
317
|
+
// older artifact, or reasoning from the Rails convention, lands here either way.
|
|
318
|
+
const wrappers = new Set(params
|
|
319
|
+
.map((param) => param.json_path)
|
|
320
|
+
.filter((path) => Boolean(path))
|
|
321
|
+
.map((path) => path.replace(/^\//, "").split("/")[0])
|
|
322
|
+
.filter((segment) => segment && !byName.has(segment)));
|
|
323
|
+
const sentAWrapper = unknown.filter((name) => wrappers.has(name));
|
|
324
|
+
throw new InvocationError(`unknown ${group}: ${unknown.sort().join(", ")}. accepted: ` +
|
|
325
|
+
`${[...byName.keys()].sort().join(", ") || "none"}` +
|
|
326
|
+
(sentAWrapper.length > 0
|
|
327
|
+
? `. ${sentAWrapper.sort().join(" and ")} ` +
|
|
328
|
+
`${sentAWrapper.length > 1 ? "are wrappers" : "is a wrapper"} this ` +
|
|
329
|
+
`surface builds for you from each field's json_path — send the fields ` +
|
|
330
|
+
`directly instead of nesting them`
|
|
331
|
+
: ""));
|
|
332
|
+
}
|
|
333
|
+
for (const [name, raw] of Object.entries(supplied)) {
|
|
334
|
+
const param = byName.get(name);
|
|
335
|
+
const value = coerce(raw, param);
|
|
336
|
+
if (group === "path_params") {
|
|
337
|
+
// Encode with nothing exempt: a `/` inside a path value would otherwise rewrite the
|
|
338
|
+
// route. Schema checking already stops this for integer ids; this covers strings.
|
|
339
|
+
path = path.replaceAll(`{${name}}`, encodeURIComponent(pathSegment(value, name)));
|
|
340
|
+
}
|
|
341
|
+
else if (group === "body") {
|
|
342
|
+
place(body, param.json_path || `/${name}`, value);
|
|
343
|
+
}
|
|
344
|
+
else {
|
|
345
|
+
query[name] = value;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
// `required` is enforced for BODY as well as path. It was path-only on the Python side at
|
|
350
|
+
// first, so a missing required body field passed silently and the product answered with a
|
|
351
|
+
// 4xx that read like the caller's fault.
|
|
352
|
+
const missing = [];
|
|
353
|
+
for (const { group, declared } of GROUPS) {
|
|
354
|
+
if (group === "query")
|
|
355
|
+
continue;
|
|
356
|
+
const supplied = args[group] || {};
|
|
357
|
+
for (const param of capability[declared] ||
|
|
358
|
+
[]) {
|
|
359
|
+
if (param.required && !(param.name in supplied))
|
|
360
|
+
missing.push(param.name);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
if (missing.length > 0) {
|
|
364
|
+
throw new InvocationError(`missing required parameter(s): ${missing.sort().join(", ")}`);
|
|
365
|
+
}
|
|
366
|
+
const leftover = path.match(/\{[a-z_]+\}/gi);
|
|
367
|
+
if (leftover) {
|
|
368
|
+
throw new InvocationError(`path placeholder(s) not supplied: ${leftover.join(", ")}`);
|
|
369
|
+
}
|
|
370
|
+
return { path, query, body: Object.keys(body).length > 0 ? body : undefined };
|
|
371
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where the index comes from, and where each product lives.
|
|
3
|
+
*
|
|
4
|
+
* A product may DECLARE its host in its own index file (`<product>.base_url`, authored in
|
|
5
|
+
* the harness's product.yaml and carried through by the export). That is the default, not
|
|
6
|
+
* the last word: config overrides it, and for a region-sharded product account discovery
|
|
7
|
+
* outranks it — see `resolveBaseUrl`. The same artifact therefore still ships to every
|
|
8
|
+
* environment, because anything environment- or account-specific is resolved here.
|
|
9
|
+
*/
|
|
10
|
+
import { BrowserStackConfig } from "../../lib/types.js";
|
|
11
|
+
import { HostSource } from "./discovery.js";
|
|
12
|
+
import { Transport } from "./egress.js";
|
|
13
|
+
/**
|
|
14
|
+
* The environment this DEPLOYMENT points at, e.g. "preprod".
|
|
15
|
+
*
|
|
16
|
+
* Process-level on purpose, and the distinction from region matters: an environment is a
|
|
17
|
+
* property of the deployment (this instance talks to preprod), whereas a REGION is a
|
|
18
|
+
* property of the account (this user's data lives in EU). That is why region discovery is
|
|
19
|
+
* per request and never cached under REMOTE_MCP, while the environment is read once here.
|
|
20
|
+
*/
|
|
21
|
+
export declare function selectedEnvironment(): string;
|
|
22
|
+
/** True when this product's host is account-specific and must be discovered per call. */
|
|
23
|
+
export declare function isRegionSharded(product: string, source?: HostSource): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Resolve a product's host.
|
|
26
|
+
*
|
|
27
|
+
* Atlas resolves: an explicit per-session override, then the host for the session's
|
|
28
|
+
* environment (`harness.extra_environments[env][product]`), then the profile's own
|
|
29
|
+
* `base_url`. The same rungs, in the same order, with one addition:
|
|
30
|
+
*
|
|
31
|
+
* 1. CAPABILITY_REGISTRY_BASE_URL_<PRODUCT> explicit, environment-agnostic
|
|
32
|
+
* 2. CAPABILITY_REGISTRY_BASE_URL_<PRODUCT>_<ENV> this environment's host
|
|
33
|
+
* 3. CAPABILITY_REGISTRY_BASE_URLS {product:{env:url}} the same, as one map
|
|
34
|
+
* 4. account discovery — the index's `base_urls`, probed
|
|
35
|
+
* 5. the single host declared in the index, `base_url`
|
|
36
|
+
* 6. refuse, by name
|
|
37
|
+
*
|
|
38
|
+
* DISCOVERY OUTRANKS THE DECLARED HOST, which is where this departs from Atlas. A declared
|
|
39
|
+
* host is one fixed origin, so for a region-sharded product it would send every account
|
|
40
|
+
* outside the default region to the wrong host — a failure that only shows up on those
|
|
41
|
+
* accounts, and so passes any test run from inside the default one. Discovery answers with
|
|
42
|
+
* the region the account is actually on; `base_url` is the fallback for when the probe
|
|
43
|
+
* cannot answer at all.
|
|
44
|
+
*
|
|
45
|
+
* Refusing rather than guessing is deliberate: a guessed host fails as a DNS error or a 404
|
|
46
|
+
* that reads like the caller's problem, when it is our missing configuration.
|
|
47
|
+
*/
|
|
48
|
+
export declare function resolveBaseUrl(product: string, config: BrowserStackConfig, source?: HostSource, transport?: Transport): Promise<string>;
|
|
49
|
+
/**
|
|
50
|
+
* Locate the artifact(s).
|
|
51
|
+
*
|
|
52
|
+
* ONE FILE PER PRODUCT, so this returns a list. Explicit env wins; otherwise look for a
|
|
53
|
+
* `capability/` directory beside the compiled module and at the package root, then fall
|
|
54
|
+
* back to a single `capability-index.json` for the pre-release layout. The package-root
|
|
55
|
+
* candidates exist because `tsc` compiles TS and does not copy JSON into `dist`.
|
|
56
|
+
*
|
|
57
|
+
* Returns an empty list when nothing is found — the caller logs that and registers no
|
|
58
|
+
* tools, rather than throwing.
|
|
59
|
+
*/
|
|
60
|
+
export declare function indexPaths(): string[];
|
|
61
|
+
/** Off by default is wrong for a shipped feature, but a kill switch is not. */
|
|
62
|
+
export declare function isEnabled(): boolean;
|