@browserstack/mcp-server 1.5.0-beta.21 → 1.5.0-beta.23
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 +32 -48
- package/capability/loadtesting.capability-index.json +1876 -0
- package/capability/tm.capability-index.json +50104 -0
- package/dist/config.d.ts +1 -4
- package/dist/config.js +2 -23
- package/dist/index.d.ts +1 -2
- package/dist/index.js +3 -7
- package/dist/lib/constants.d.ts +0 -3
- package/dist/lib/constants.js +0 -3
- package/dist/lib/instrumentation.d.ts +2 -14
- package/dist/lib/instrumentation.js +29 -104
- package/dist/lib/upload-validator.d.ts +2 -3
- package/dist/lib/upload-validator.js +15 -18
- package/dist/lib/utils.js +3 -10
- package/dist/lib/version-resolver.d.ts +5 -0
- package/dist/lib/version-resolver.js +2 -8
- package/dist/logger.js +10 -1
- package/dist/server-factory.js +6 -10
- package/dist/tools/accessibility.js +5 -9
- package/dist/tools/accessiblity-utils/accessibility-rag.js +1 -3
- package/dist/tools/appautomate-utils/native-execution/constants.js +2 -4
- package/dist/tools/appautomate.js +1 -3
- package/dist/tools/applive.js +1 -1
- package/dist/tools/automate.d.ts +0 -7
- package/dist/tools/automate.js +1 -104
- package/dist/tools/bstack-sdk.js +2 -2
- package/dist/tools/build-insights.js +2 -41
- package/dist/tools/capability-registry/bind.d.ts +29 -0
- package/dist/tools/capability-registry/bind.js +329 -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 +133 -0
- package/dist/tools/capability-registry/index-loader.js +404 -0
- package/dist/tools/capability-registry/register.d.ts +48 -0
- package/dist/tools/capability-registry/register.js +604 -0
- package/dist/tools/capability-registry/resolve.d.ts +38 -0
- package/dist/tools/capability-registry/resolve.js +45 -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 +282 -0
- package/dist/tools/capability-registry/types.js +33 -0
- package/dist/tools/failurelogs-utils/app-automate.js +3 -4
- package/dist/tools/failurelogs-utils/automate.js +4 -5
- package/dist/tools/get-failure-logs.js +11 -30
- package/dist/tools/observability.js +1 -3
- package/dist/tools/percy-sdk.js +9 -9
- package/dist/tools/rca-agent-utils/format-rca.js +4 -5
- package/dist/tools/rca-agent-utils/get-failed-test-id.js +0 -12
- package/dist/tools/rca-agent-utils/types.d.ts +0 -1
- package/dist/tools/rca-agent.js +2 -5
- package/dist/tools/review-agent.js +1 -2
- package/dist/tools/sdk-utils/common/constants.d.ts +1 -1
- package/dist/tools/sdk-utils/common/constants.js +1 -2
- package/dist/tools/selfheal.js +3 -6
- package/dist/tools/testmanagement-utils/TCG-utils/api.js +7 -26
- package/dist/tools/testmanagement-utils/TCG-utils/config.d.ts +0 -2
- package/dist/tools/testmanagement-utils/TCG-utils/config.js +0 -5
- package/dist/tools/testmanagement-utils/testcase-from-file.js +1 -2
- package/dist/tools/testmanagement-utils/upload-file.js +2 -16
- package/dist/tools/testmanagement.js +17 -39
- package/package.json +11 -5
- package/dist/lib/node-version-notice.d.ts +0 -1
- package/dist/lib/node-version-notice.js +0 -9
- package/dist/lib/tool-latency.d.ts +0 -4
- package/dist/lib/tool-latency.js +0 -16
- package/dist/lib/untrusted-content.d.ts +0 -15
- package/dist/lib/untrusted-content.js +0 -24
- package/dist/tools/ask-browserstack/central-oauth.d.ts +0 -120
- package/dist/tools/ask-browserstack/central-oauth.js +0 -277
- package/dist/tools/ask-browserstack/config.d.ts +0 -102
- package/dist/tools/ask-browserstack/config.js +0 -140
- package/dist/tools/ask-browserstack/egress.d.ts +0 -34
- package/dist/tools/ask-browserstack/egress.js +0 -31
- package/dist/tools/ask-browserstack/register.d.ts +0 -61
- package/dist/tools/ask-browserstack/register.js +0 -416
- package/dist/tools/ask-browserstack/relay.d.ts +0 -201
- package/dist/tools/ask-browserstack/relay.js +0 -577
- package/dist/tools/ask-browserstack/stream.d.ts +0 -116
- package/dist/tools/ask-browserstack/stream.js +0 -236
- package/dist/tools/ask-browserstack/types.d.ts +0 -196
- package/dist/tools/ask-browserstack/types.js +0 -14
- package/dist/tools/automate-utils/list-session-ids.d.ts +0 -28
- package/dist/tools/automate-utils/list-session-ids.js +0 -87
- package/dist/tools/automate-utils/resolve-hashed-build-id.d.ts +0 -30
- package/dist/tools/automate-utils/resolve-hashed-build-id.js +0 -124
- package/dist/tools/failurelogs-utils/resolve-app-build-id.d.ts +0 -2
- package/dist/tools/failurelogs-utils/resolve-app-build-id.js +0 -5
- package/dist/tools/failurelogs-utils/video.d.ts +0 -3
- package/dist/tools/failurelogs-utils/video.js +0 -25
- package/dist/tools/tool-handoff.d.ts +0 -66
- package/dist/tools/tool-handoff.js +0 -78
|
@@ -0,0 +1,604 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The tool surface: four discovery tools plus ONE invoke tool.
|
|
3
|
+
*
|
|
4
|
+
* Discovery is deliberately two steps. `searchCapability` returns a shortlist — enough to
|
|
5
|
+
* CHOOSE — and `describeCapability` returns the contract for the one chosen. Parameters and
|
|
6
|
+
* response shapes are 86% of a full record and are needed once, not eight times: measured
|
|
7
|
+
* over eight queries, ~8.6k tokens a search becomes ~1.1k, and even describing every result
|
|
8
|
+
* still costs less than the single fat call did.
|
|
9
|
+
*
|
|
10
|
+
* ONE invoke tool means one set of MCP annotations, so they describe the whole surface
|
|
11
|
+
* honestly: it can write (not read-only) and it can never delete, because destructive
|
|
12
|
+
* endpoints are refused before binding. Write consent therefore rests on `user_permission`
|
|
13
|
+
* enforced HERE rather than on a client-side hint — which is the one thing a separate
|
|
14
|
+
* read/write tool pair was buying.
|
|
15
|
+
*/
|
|
16
|
+
import { z } from "zod";
|
|
17
|
+
import logger from "../../logger.js";
|
|
18
|
+
import { trackMCP } from "../../lib/instrumentation.js";
|
|
19
|
+
import { indexPaths, isEnabled, resolveBaseUrl } from "./config.js";
|
|
20
|
+
import { fetchTransport } from "./egress.js";
|
|
21
|
+
import { CapabilityRegistry, InvocationError, resolveResponses, } from "./index-loader.js";
|
|
22
|
+
import { invoke } from "./resolve.js";
|
|
23
|
+
import { ambiguousProducts, isBrowseQuery, searchCapabilities, singular, terms, vocabularyOf, } from "./search.js";
|
|
24
|
+
export const PERMISSION_VALUES = ["not_asked", "granted", "denied"];
|
|
25
|
+
/**
|
|
26
|
+
* The search-side twin of `user_permission`: did a human choose the product, or did you?
|
|
27
|
+
*
|
|
28
|
+
* No `denied`. A refused write is a thing the caller must not do; a refused product choice
|
|
29
|
+
* is not a state — the user either named one, in which case you search it, or has not been
|
|
30
|
+
* asked, in which case there is nothing to search yet.
|
|
31
|
+
*/
|
|
32
|
+
export const PRODUCT_CHOICE_VALUES = ["not_asked", "user_confirmed"];
|
|
33
|
+
/**
|
|
34
|
+
* The tool-adder the server factory calls.
|
|
35
|
+
*
|
|
36
|
+
* Registers NOTHING when the artifact is absent or unreadable, rather than throwing: a
|
|
37
|
+
* missing index is a packaging problem, and taking the whole MCP server down with it would
|
|
38
|
+
* remove every other product's tools too. The reason is logged so it is not silent.
|
|
39
|
+
*/
|
|
40
|
+
export function addCapabilityRegistryToolsFromConfig(server, config) {
|
|
41
|
+
if (!isEnabled()) {
|
|
42
|
+
logger.info("capability registry disabled by CAPABILITY_REGISTRY_DISABLED");
|
|
43
|
+
return {};
|
|
44
|
+
}
|
|
45
|
+
const files = indexPaths();
|
|
46
|
+
if (files.length === 0) {
|
|
47
|
+
logger.warn("capability registry index not found; its tools are not registered. Set " +
|
|
48
|
+
"CAPABILITY_REGISTRY_INDEX_DIR, or ship capabilities/<product>.capability-index.json " +
|
|
49
|
+
"at the package root.");
|
|
50
|
+
return {};
|
|
51
|
+
}
|
|
52
|
+
let registry;
|
|
53
|
+
try {
|
|
54
|
+
registry = CapabilityRegistry.fromFiles(files);
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
// One unreadable file fails the whole load on purpose (see `fromFiles`), so this is
|
|
58
|
+
// the only place that decides the surface is absent, and it says why.
|
|
59
|
+
logger.error("capability registry index unusable (%s): %s", files.join(", "), error instanceof Error ? error.message : String(error));
|
|
60
|
+
return {};
|
|
61
|
+
}
|
|
62
|
+
const loaded = registry.buildInfo();
|
|
63
|
+
logger.info("capability registry loaded: %d product(s) — %s", registry.productNames().length, registry
|
|
64
|
+
.productNames()
|
|
65
|
+
.map((name) => `${name} build ${loaded[name]?.build_id || "?"}` +
|
|
66
|
+
(loaded[name]?.version ? ` v${loaded[name].version}` : ""))
|
|
67
|
+
.join(", "));
|
|
68
|
+
return addCapabilityRegistryTools(server, {
|
|
69
|
+
registry,
|
|
70
|
+
// The whole product bundle, not just its host: a region-sharded product declares
|
|
71
|
+
// several candidates and the probe needs an endpoint from its own capabilities.
|
|
72
|
+
baseUrlFor: (product) => resolveBaseUrl(product, config, registry.index.products[product]),
|
|
73
|
+
// Read per call, not captured: the remote server rebuilds config per session, so a
|
|
74
|
+
// captured credential would outlive the session it belongs to.
|
|
75
|
+
credentialsFor: () => ({
|
|
76
|
+
username: config["browserstack-username"],
|
|
77
|
+
accessKey: config["browserstack-access-key"],
|
|
78
|
+
}),
|
|
79
|
+
}, config);
|
|
80
|
+
}
|
|
81
|
+
function ok(payload) {
|
|
82
|
+
return { content: [{ type: "text", text: JSON.stringify(payload) }] };
|
|
83
|
+
}
|
|
84
|
+
function failed(message, extra) {
|
|
85
|
+
return {
|
|
86
|
+
content: [
|
|
87
|
+
{
|
|
88
|
+
type: "text",
|
|
89
|
+
text: JSON.stringify({ ok: false, error: message, ...extra }),
|
|
90
|
+
},
|
|
91
|
+
],
|
|
92
|
+
isError: true,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
export function addCapabilityRegistryTools(server, deps, config) {
|
|
96
|
+
const { registry } = deps;
|
|
97
|
+
const productNames = registry.productNames();
|
|
98
|
+
/**
|
|
99
|
+
* The `product` argument, typed to what this build actually carries.
|
|
100
|
+
*
|
|
101
|
+
* An enum rather than a free string, so the accepted values travel in the SCHEMA the
|
|
102
|
+
* client validates against — the model sees them without spending a `listProducts` call,
|
|
103
|
+
* and a typo is rejected before the handler runs instead of coming back as a tool error.
|
|
104
|
+
* The set is fixed for the session because the index is read once at registration.
|
|
105
|
+
*/
|
|
106
|
+
const productArg = () => productNames.length > 0
|
|
107
|
+
? z.enum(productNames)
|
|
108
|
+
: z.string();
|
|
109
|
+
/** "tm, loadtesting" — for prose that has to name them. */
|
|
110
|
+
const productList = productNames.join(", ") || "none loaded";
|
|
111
|
+
// NO PRODUCT CATALOG IN ANY DESCRIPTION. listProducts used to restate every product's
|
|
112
|
+
// trimmed summary in its own description — duplicating, as static context on every
|
|
113
|
+
// single request, the exact thing the tool returns when called. Authored summaries are
|
|
114
|
+
// also unbounded (loadtesting's runs to 470 characters) and change with the artifact,
|
|
115
|
+
// so the prose went stale on its own. The names still travel in the `product` enum,
|
|
116
|
+
// which is where a client can actually validate them.
|
|
117
|
+
const transport = deps.transport || fetchTransport();
|
|
118
|
+
const tools = {};
|
|
119
|
+
/** Instrumentation in the house style, and never fatal to the call it wraps. */
|
|
120
|
+
const track = (name) => {
|
|
121
|
+
try {
|
|
122
|
+
trackMCP(name, server.server.getClientVersion(), undefined, config);
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
// Telemetry must not decide whether a tool call succeeds.
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
tools.listProducts = server.tool("listProducts", "List the BrowserStack products this surface can reach: what each one does, the " +
|
|
129
|
+
"entities it models, and a line saying what each entity is. START HERE — " +
|
|
130
|
+
"searchCapability needs a product, and this is what tells you which one. If two " +
|
|
131
|
+
"products could both fit the task, ask the user rather than choosing for them.", {}, {
|
|
132
|
+
title: "List Capability Products",
|
|
133
|
+
readOnlyHint: true,
|
|
134
|
+
destructiveHint: false,
|
|
135
|
+
idempotentHint: true,
|
|
136
|
+
openWorldHint: false,
|
|
137
|
+
}, async () => {
|
|
138
|
+
track("listProducts");
|
|
139
|
+
return ok({
|
|
140
|
+
products: registry.productNames().map((name) => ({
|
|
141
|
+
name,
|
|
142
|
+
summary: registry.index.products[name].summary,
|
|
143
|
+
// THE ENTITIES AND WHAT EACH ONE IS. Routing is this tool's whole job, and a
|
|
144
|
+
// product summary alone does not do it: "add a tag to xyz test" reads as
|
|
145
|
+
// either product until you can see that `tag` exists in one and not the other.
|
|
146
|
+
// The one-line description is what makes each name mean something — `version`
|
|
147
|
+
// alone does not say whether it versions a test case or a project — so an
|
|
148
|
+
// agent can choose here instead of calling describeEntity once per entity to
|
|
149
|
+
// find out, which for tm is 19 calls at ~1.4KB apiece.
|
|
150
|
+
//
|
|
151
|
+
// NAME AND DESCRIPTION ONLY. The aliases used to travel here too, and they are
|
|
152
|
+
// the wrong half for this job: they answer "what else is this called", which
|
|
153
|
+
// matters when a search has already failed on vocabulary, not when choosing a
|
|
154
|
+
// product. They still reach the caller at exactly that moment, in
|
|
155
|
+
// searchCapability's weak-match block, where the full vocabulary is the answer
|
|
156
|
+
// rather than 2.5KB of speculative context on every routing call.
|
|
157
|
+
entities: (vocabularyOf(registry.index.products, name)[name] ?? []).map(({ entity, description }) => ({
|
|
158
|
+
entity,
|
|
159
|
+
...(description ? { description } : {}),
|
|
160
|
+
})),
|
|
161
|
+
// NO build_id OR version. They are provenance — for our logs and for cache
|
|
162
|
+
// busting — and capability resolution must never depend on them, which means
|
|
163
|
+
// no caller has anything to do with them. They rode on the one call an agent
|
|
164
|
+
// makes before it knows anything, costing context to say nothing actionable.
|
|
165
|
+
// Still on searchCapability's response, where a support question about which
|
|
166
|
+
// index answered can actually be traced to a result.
|
|
167
|
+
})),
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
// listEntities WAS HERE, and is gone. It answered `{product, entities: [names]}` — a
|
|
171
|
+
// strict subset of what listProducts now returns for every product, and with the
|
|
172
|
+
// aliases missing. Keeping it would have meant two tools for one question, and a tool
|
|
173
|
+
// definition costs context on every request whether or not it is called, which is the
|
|
174
|
+
// entire reason this surface is five generic tools instead of 173 specific ones.
|
|
175
|
+
//
|
|
176
|
+
// One consequence to watch: listProducts now carries every product's vocabulary rather
|
|
177
|
+
// than one product's on request, so it grows with the number of products — roughly 1KB
|
|
178
|
+
// each. That is the right trade while routing is the problem it solves (you cannot
|
|
179
|
+
// choose between products by looking at one of them), but past a dozen products it may
|
|
180
|
+
// need a names-only default with aliases on request.
|
|
181
|
+
tools.describeEntity = server.tool("describeEntity", "Describe one entity: what it is, what identifies it, what it relates to, and the " +
|
|
182
|
+
"vocabulary the product uses for it. Read this before filtering or writing, because " +
|
|
183
|
+
"ids and field values usually have to be resolved first.", {
|
|
184
|
+
product: productArg().describe(`Which product the entity belongs to: ${productList}.`),
|
|
185
|
+
entity: z.string().describe("Entity name, as listProducts returns it."),
|
|
186
|
+
}, {
|
|
187
|
+
title: "Describe Entity",
|
|
188
|
+
readOnlyHint: true,
|
|
189
|
+
destructiveHint: false,
|
|
190
|
+
idempotentHint: true,
|
|
191
|
+
openWorldHint: false,
|
|
192
|
+
}, async ({ product, entity }) => {
|
|
193
|
+
track("describeEntity");
|
|
194
|
+
const bundle = registry.index.products[product];
|
|
195
|
+
if (!bundle)
|
|
196
|
+
return failed(`unknown product '${product}'`);
|
|
197
|
+
const doc = bundle.entities[entity];
|
|
198
|
+
if (!doc) {
|
|
199
|
+
return failed(`unknown entity '${entity}' in ${product}; known: ${Object.keys(bundle.entities).sort().join(", ")}`);
|
|
200
|
+
}
|
|
201
|
+
return ok({ product, entity, ...doc });
|
|
202
|
+
});
|
|
203
|
+
tools.searchCapability = server.tool("searchCapability", "Find endpoints this surface can call, by plain language, within ONE product. " +
|
|
204
|
+
`You must say which: ${productList}. If the task does not name it unambiguously, ` +
|
|
205
|
+
"call listProducts first — it returns what each product does, the entities each " +
|
|
206
|
+
"models, and what every entity means, which is what settles the choice. Where two " +
|
|
207
|
+
"products use the same word for different things, ask the user rather than picking. " +
|
|
208
|
+
"Search matches the product's OWN words, not synonyms. When your words are not the " +
|
|
209
|
+
"product's, the response says so: `weak_match: true` with a `suggested_vocabulary` " +
|
|
210
|
+
"map of the product's entities and their aliases. Results are still returned, but " +
|
|
211
|
+
"treat them as unconfirmed — pick the closest entity from that map and search again " +
|
|
212
|
+
"using its vocabulary, or call describeEntity on it for the fuller picture. That one " +
|
|
213
|
+
"extra round trip is far cheaper than invoking the wrong capability. " +
|
|
214
|
+
"Narrowing further with `entity` sharpens results. " +
|
|
215
|
+
"THIS IS A SHORTLIST, NOT A CONTRACT. Each result carries only what you need to " +
|
|
216
|
+
"CHOOSE: `name` (the handle), `product`, `mode` (whether it writes), `intent` and " +
|
|
217
|
+
"`guidance` (what it does and what goes wrong), and `method`/`path` for products " +
|
|
218
|
+
"that publish no name yet. " +
|
|
219
|
+
"It does NOT carry parameters or response shapes. Once you have picked one, call " +
|
|
220
|
+
"describeCapability for its full contract, then invokeCapability. Fetching the " +
|
|
221
|
+
"contract only for the one you chose is the difference between ~1k and ~8.6k tokens " +
|
|
222
|
+
"a search. Results are ranked and capped. " +
|
|
223
|
+
"TO SEE EVERYTHING rather than search, pass query='*' — that lists the product " +
|
|
224
|
+
"(honouring `entity` and `mode`) in a stable order instead of ranking it, which is " +
|
|
225
|
+
"what you want when the task is 'what can this product do' rather than a lookup. " +
|
|
226
|
+
"TO PAGE, send `offset`: every response carries `offset` and, when more matched, " +
|
|
227
|
+
"`next_offset` — resend the SAME query with that value. `next_offset` is absent on " +
|
|
228
|
+
"the last page, so walk until it stops rather than comparing counts.", {
|
|
229
|
+
query: z
|
|
230
|
+
.string()
|
|
231
|
+
.describe("What you are trying to do, in plain language. Pass '*' (or 'all') to BROWSE " +
|
|
232
|
+
"instead of search: every capability in the product, in a stable order, " +
|
|
233
|
+
"paginated — use it when you want to see the surface rather than find one thing."),
|
|
234
|
+
entity: z
|
|
235
|
+
.string()
|
|
236
|
+
.optional()
|
|
237
|
+
.describe("Restrict to one entity (listProducts names them)."),
|
|
238
|
+
// REQUIRED, and the reason is the tool that is NOT being called. listProducts
|
|
239
|
+
// carries the routing data — each product's purpose, its entities, and what every
|
|
240
|
+
// entity means — but nothing obliged an agent to read it: a search that worked
|
|
241
|
+
// without naming a product meant the routing step could always be skipped, and an
|
|
242
|
+
// optional step in front of a working one is a step that does not happen. Requiring
|
|
243
|
+
// the argument makes the ordering structural instead of advisory.
|
|
244
|
+
//
|
|
245
|
+
// It costs one listProducts call on queries that were already unambiguous — 139 of
|
|
246
|
+
// 147 vocabulary terms resolve to a single product — and buys the eight that are
|
|
247
|
+
// not (run, project, report, result, folder, workspace, execution, history), where
|
|
248
|
+
// the old behaviour was to silently pick whichever product ranked higher. A wasted
|
|
249
|
+
// round trip against a silent wrong-product answer is not a close trade.
|
|
250
|
+
product: productArg().describe(`Which product to search: ${productList}. Call listProducts if the task does ` +
|
|
251
|
+
`not make it obvious, and ask the user when two products could both fit.`),
|
|
252
|
+
// REQUIRED, and self-reported, exactly like `user_permission` on a write. The
|
|
253
|
+
// server cannot see whether you asked anyone; what it can do is refuse to answer a
|
|
254
|
+
// question only the user can settle, and make claiming otherwise an explicit act
|
|
255
|
+
// rather than an omission.
|
|
256
|
+
product_choice: z
|
|
257
|
+
.enum(PRODUCT_CHOICE_VALUES)
|
|
258
|
+
.describe("'user_confirmed' only when the user named the product, or the task names it " +
|
|
259
|
+
"unmistakably. 'not_asked' otherwise — then a query that could mean either " +
|
|
260
|
+
"product is refused and told what to ask, instead of being answered for the " +
|
|
261
|
+
"wrong one. Never search each product in turn and merge the results: that is " +
|
|
262
|
+
"the guess this argument exists to prevent."),
|
|
263
|
+
mode: z
|
|
264
|
+
.enum(["read", "write", "destructive"])
|
|
265
|
+
.optional()
|
|
266
|
+
.describe("Restrict to reads or writes. Omit to let the query decide."),
|
|
267
|
+
limit: z.number().optional().describe("Max results (default 8)."),
|
|
268
|
+
offset: z
|
|
269
|
+
.number()
|
|
270
|
+
.optional()
|
|
271
|
+
.describe("Skip this many results — send the `next_offset` from the previous response " +
|
|
272
|
+
"with the SAME query to get the next page. Omit for the first page."),
|
|
273
|
+
}, {
|
|
274
|
+
title: "Search Capabilities",
|
|
275
|
+
readOnlyHint: true,
|
|
276
|
+
destructiveHint: false,
|
|
277
|
+
idempotentHint: true,
|
|
278
|
+
openWorldHint: false,
|
|
279
|
+
}, async ({ query, entity, product, product_choice, mode, limit, offset }) => {
|
|
280
|
+
track("searchCapability");
|
|
281
|
+
// THE GATE. Nothing here can tell whether a human was asked — same as the write
|
|
282
|
+
// gate, which also takes the caller's word. What it can do is refuse to answer a
|
|
283
|
+
// question that is genuinely the user's, so that answering it anyway takes a
|
|
284
|
+
// deliberate claim instead of silence.
|
|
285
|
+
//
|
|
286
|
+
// Only when the query itself cannot settle the choice: every recognised word is one
|
|
287
|
+
// both products claim. 27 of the 198 eval queries, against 135 if constituent words
|
|
288
|
+
// counted. `entity` is an explicit narrowing, so a caller that named one has already
|
|
289
|
+
// been specific enough and is not asked again.
|
|
290
|
+
// The ambiguity gate asks the USER which product a word belongs to. A browse query
|
|
291
|
+
// contains no word to be ambiguous about — `*` names nothing — so there is nothing
|
|
292
|
+
// to ask, and asking anyway would block the one query whose whole purpose is to
|
|
293
|
+
// show what a product contains.
|
|
294
|
+
if (product_choice !== "user_confirmed" &&
|
|
295
|
+
!entity &&
|
|
296
|
+
!isBrowseQuery(query)) {
|
|
297
|
+
const ambiguity = ambiguousProducts(registry.index.products, query);
|
|
298
|
+
if (ambiguity.products.length > 1) {
|
|
299
|
+
// EVERYTHING NEEDED TO ASK, IN THE REFUSAL. Telling the agent to go and call
|
|
300
|
+
// listProducts costs a round trip and still leaves it composing a question out
|
|
301
|
+
// of nothing — so it tends to guess instead, which is the behaviour being
|
|
302
|
+
// stopped. What makes the choice answerable is what each product calls the
|
|
303
|
+
// shared word and what it means THERE: tm's project owns folders and test
|
|
304
|
+
// cases, Load Testing's does not.
|
|
305
|
+
const options = ambiguity.products.map((name) => {
|
|
306
|
+
const entities = registry.index.products[name]?.entities ?? {};
|
|
307
|
+
const senses = ambiguity.terms
|
|
308
|
+
.map((term) => {
|
|
309
|
+
const key = Object.keys(entities).find((candidate) => [
|
|
310
|
+
candidate,
|
|
311
|
+
...(entities[candidate].aliases ?? []),
|
|
312
|
+
].some((word) => terms(word).map(singular).join(" ") === term));
|
|
313
|
+
const doc = key ? entities[key] : undefined;
|
|
314
|
+
return doc
|
|
315
|
+
? { term, entity: key, means: doc.description }
|
|
316
|
+
: undefined;
|
|
317
|
+
})
|
|
318
|
+
.filter(Boolean);
|
|
319
|
+
return {
|
|
320
|
+
product: name,
|
|
321
|
+
summary: registry.index.products[name]?.summary,
|
|
322
|
+
...(senses.length ? { shared_terms: senses } : {}),
|
|
323
|
+
};
|
|
324
|
+
});
|
|
325
|
+
return failed(`'${query}' could mean ${ambiguity.products.join(" or ")} — ` +
|
|
326
|
+
`${ambiguity.terms.map((t) => `'${t}'`).join(", ")} ` +
|
|
327
|
+
`${ambiguity.terms.length === 1 ? "belongs" : "belong"} to both. ` +
|
|
328
|
+
"Put the choice in `clarify` to the USER in their own terms, wait for an " +
|
|
329
|
+
"answer, then resend with product_choice='user_confirmed'. Do NOT search " +
|
|
330
|
+
"each product in turn and merge the results — that answers the question " +
|
|
331
|
+
"instead of asking it.", {
|
|
332
|
+
clarify: {
|
|
333
|
+
question: `Which product do you mean — ${ambiguity.products.join(" or ")}?`,
|
|
334
|
+
shared: ambiguity.terms,
|
|
335
|
+
options,
|
|
336
|
+
},
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
const { hits, weak, top_matched, ...rest } = searchCapabilities(registry.index.products, query, {
|
|
341
|
+
entity,
|
|
342
|
+
product,
|
|
343
|
+
mode: mode,
|
|
344
|
+
limit,
|
|
345
|
+
offset,
|
|
346
|
+
});
|
|
347
|
+
return ok({
|
|
348
|
+
// NO build_id. It is provenance — for our logs and for cache busting — and
|
|
349
|
+
// resolution must never depend on it, which is exactly why no caller has anything
|
|
350
|
+
// to do with it. It was also the WHOLE registry's id, so a search scoped to one
|
|
351
|
+
// product still announced every other product's build: metadata about builds the
|
|
352
|
+
// caller did not ask about and cannot act on. The startup log already records
|
|
353
|
+
// what loaded, which is where a question about a stale index gets answered.
|
|
354
|
+
//
|
|
355
|
+
// A SHORTLIST: only what choosing requires. Parameters and response shapes are 86%
|
|
356
|
+
// of a full record and are needed for exactly ONE of the eight — the one the caller
|
|
357
|
+
// picks — so they move to describeCapability. Measured over eight queries: 8.6k
|
|
358
|
+
// tokens a search becomes ~1k, and even describing all eight results still costs
|
|
359
|
+
// slightly less than today.
|
|
360
|
+
//
|
|
361
|
+
// NO ROUTE, AND NO PRODUCT. Both were justified and both justifications expired.
|
|
362
|
+
//
|
|
363
|
+
// `method`/`path` were unconditional because Load Testing published no names, so
|
|
364
|
+
// its rows would otherwise have been unaddressable. It now names all 20, as tm
|
|
365
|
+
// names all 173 — every row on this surface is reachable by name. Publishing the
|
|
366
|
+
// route anyway contradicts the premise the whole registry rests on: an agent
|
|
367
|
+
// addresses a capability by a handle that outlives the route. They remain as a
|
|
368
|
+
// fallback for a product that ships unnamed capabilities, emitted only for the
|
|
369
|
+
// rows that actually need them, which today is none.
|
|
370
|
+
//
|
|
371
|
+
// `product` was here because results could span products. They cannot: `product`
|
|
372
|
+
// is a required argument, so every row is the product the caller named.
|
|
373
|
+
capabilities: hits.map(({ capability }) => ({
|
|
374
|
+
...(capability.name
|
|
375
|
+
? { name: capability.name }
|
|
376
|
+
: { method: capability.method, path: capability.path }),
|
|
377
|
+
mode: capability.mode,
|
|
378
|
+
entity: capability.entity,
|
|
379
|
+
...(capability.intent ? { intent: capability.intent } : {}),
|
|
380
|
+
...(capability.guidance?.length
|
|
381
|
+
? { guidance: capability.guidance }
|
|
382
|
+
: {}),
|
|
383
|
+
})),
|
|
384
|
+
...rest,
|
|
385
|
+
// WHEN THE MATCH IS WEAK, HAND OVER THE VOCABULARY.
|
|
386
|
+
//
|
|
387
|
+
// A vocabulary miss returns a full page of confident-looking hits — nothing in the
|
|
388
|
+
// shape of the response says the caller's word does not exist in this product. This
|
|
389
|
+
// is that signal, plus the fix in the same round trip: the caller is a model, and it
|
|
390
|
+
// maps "bucket" to "folder" instantly once it can see the entity list.
|
|
391
|
+
//
|
|
392
|
+
// Additive, never a replacement — the results are still there, and the caller is
|
|
393
|
+
// told to re-search rather than to trust this. That is what makes a generous
|
|
394
|
+
// threshold safe.
|
|
395
|
+
...(weak
|
|
396
|
+
? {
|
|
397
|
+
weak_match: true,
|
|
398
|
+
suggested_vocabulary: vocabularyOf(registry.index.products, product),
|
|
399
|
+
hint: "Nothing matched the product's own words strongly (best term score " +
|
|
400
|
+
`${top_matched.toFixed(1)}). The results below may not answer the ` +
|
|
401
|
+
"question. Re-search using a term from suggested_vocabulary, or call " +
|
|
402
|
+
"describeEntity on the closest entity for its full vocabulary.",
|
|
403
|
+
}
|
|
404
|
+
: {}),
|
|
405
|
+
});
|
|
406
|
+
});
|
|
407
|
+
tools.describeCapability = server.tool("describeCapability", "The full contract for ONE capability you picked from searchCapability: its " +
|
|
408
|
+
"parameters grouped into path_params / query / body under the spec's own names, " +
|
|
409
|
+
"what it returns, and its declared response shapes fully expanded. Call this after " +
|
|
410
|
+
"search and before invokeCapability — search deliberately omits all of it, because " +
|
|
411
|
+
"it is only needed for the one capability you actually call. " +
|
|
412
|
+
"Identify it by `name`, exactly as search returned it; only when a result carries " +
|
|
413
|
+
"no `name` (some products publish none yet) pass `method` and `path` instead. " +
|
|
414
|
+
"Every parameter lists its type, whether it is required, its allowed values where " +
|
|
415
|
+
"the set is closed, and any limits the product declares — obey those before calling " +
|
|
416
|
+
"rather than discovering them from a rejected request.", {
|
|
417
|
+
name: z
|
|
418
|
+
.string()
|
|
419
|
+
.optional()
|
|
420
|
+
.describe("The capability's published name, exactly as searchCapability returned it."),
|
|
421
|
+
method: z
|
|
422
|
+
.string()
|
|
423
|
+
.optional()
|
|
424
|
+
.describe("HTTP method — only for capabilities returned without a `name`."),
|
|
425
|
+
path: z
|
|
426
|
+
.string()
|
|
427
|
+
.optional()
|
|
428
|
+
.describe("Path with {placeholders} intact — only for capabilities returned without a `name`."),
|
|
429
|
+
product: productArg()
|
|
430
|
+
.optional()
|
|
431
|
+
.describe(`Which product owns it (${productList}). searchCapability returns it on every ` +
|
|
432
|
+
"result; required only when two products share a name or a path."),
|
|
433
|
+
include_responses: z
|
|
434
|
+
.enum(["success", "all", "none"])
|
|
435
|
+
.optional()
|
|
436
|
+
.describe("Which declared responses to expand: 'success' (default, the 2xx shape), 'all' " +
|
|
437
|
+
"(adds the error shapes — several times larger, and near-identical across " +
|
|
438
|
+
"endpoints), or 'none'."),
|
|
439
|
+
}, {
|
|
440
|
+
title: "Describe Capability",
|
|
441
|
+
readOnlyHint: true,
|
|
442
|
+
destructiveHint: false,
|
|
443
|
+
idempotentHint: true,
|
|
444
|
+
openWorldHint: false,
|
|
445
|
+
}, async (input) => {
|
|
446
|
+
track("describeCapability");
|
|
447
|
+
try {
|
|
448
|
+
// The same two handles as invokeCapability, resolved the same way, so a name that
|
|
449
|
+
// describes is a name that invokes. Divergence here would be its own bug class.
|
|
450
|
+
if (!input.name && !(input.method && input.path)) {
|
|
451
|
+
return failed("pass `name` — or, for a capability returned without one, both `method` and " +
|
|
452
|
+
"`path`, exactly as searchCapability returned them");
|
|
453
|
+
}
|
|
454
|
+
const { product: owner, capability } = input.name
|
|
455
|
+
? registry.byNameLookup(input.name, input.product)
|
|
456
|
+
: registry.byEndpointLookup(input.method, input.path, input.product);
|
|
457
|
+
const selection = (input.include_responses ||
|
|
458
|
+
"success");
|
|
459
|
+
const responses = resolveResponses(registry.index.products[owner], capability, selection);
|
|
460
|
+
// Dropped rather than overwritten: the raw field holds `{"$response": …}` pointers,
|
|
461
|
+
// and spreading the capability would leak them through whenever the resolved value
|
|
462
|
+
// is absent.
|
|
463
|
+
const { responses: unresolved, method, path, ...contract } = capability;
|
|
464
|
+
void unresolved;
|
|
465
|
+
return ok({
|
|
466
|
+
// No build_id here either, same reason. `product` stays: describeCapability
|
|
467
|
+
// resolves by NAME and its product argument is optional, so the answer has to
|
|
468
|
+
// say whose contract came back.
|
|
469
|
+
product: owner,
|
|
470
|
+
// NO ROUTE, for the same reason the shortlist has none. A named capability is
|
|
471
|
+
// invoked by its name; the route is how WE reach the product, not something
|
|
472
|
+
// the caller acts on, and a contract that shows both invites the caller to
|
|
473
|
+
// hold the half that breaks when `/edit` becomes `/edit-v2`. The parameters
|
|
474
|
+
// below still carry `path_params`, so the caller knows what to supply — it
|
|
475
|
+
// just never sees the template they are substituted into.
|
|
476
|
+
//
|
|
477
|
+
// Emitted only when there is no name to use instead, which is what
|
|
478
|
+
// invokeCapability falls back to for a product that publishes none. No shipped
|
|
479
|
+
// product is in that state today.
|
|
480
|
+
...(capability.name ? {} : { method, path }),
|
|
481
|
+
...contract,
|
|
482
|
+
...(responses ? { responses } : {}),
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
catch (error) {
|
|
486
|
+
if (error instanceof InvocationError)
|
|
487
|
+
return failed(error.message);
|
|
488
|
+
logger.error("describeCapability failed: %s", error instanceof Error ? error.message : String(error));
|
|
489
|
+
return failed("that capability could not be described");
|
|
490
|
+
}
|
|
491
|
+
});
|
|
492
|
+
tools.invokeCapability = server.tool("invokeCapability", "Call a capability whose contract you have from describeCapability. Pass `name` exactly " +
|
|
493
|
+
"as given — that is the handle. Only when a result carries no `name` (some products " +
|
|
494
|
+
"do not publish them yet) pass `method` and `path` instead, exactly as returned. " +
|
|
495
|
+
"Arguments go in path_params / query / body under the spec's own names. One call " +
|
|
496
|
+
"makes exactly one request and returns the product's own response untouched; when " +
|
|
497
|
+
"`completed` is false there is another page, which you fetch by sending the " +
|
|
498
|
+
"capability's own page parameter. If the mode is 'write' you MUST ask the user " +
|
|
499
|
+
"first, then resend with user_permission='granted' and a change_summary; both are " +
|
|
500
|
+
"recorded. Capabilities whose mode is 'destructive' (deletes) are refused outright — " +
|
|
501
|
+
"archiving, closing and merging are ordinary writes and DO run, so read the mode and " +
|
|
502
|
+
"intent before confirming with the user.", {
|
|
503
|
+
name: z
|
|
504
|
+
.string()
|
|
505
|
+
.optional()
|
|
506
|
+
.describe("The capability's published name, exactly as returned (e.g. 'create_test_run_v1'). " +
|
|
507
|
+
"Preferred over method/path."),
|
|
508
|
+
method: z
|
|
509
|
+
.string()
|
|
510
|
+
.optional()
|
|
511
|
+
.describe("HTTP method — only for capabilities returned without a `name`."),
|
|
512
|
+
path: z
|
|
513
|
+
.string()
|
|
514
|
+
.optional()
|
|
515
|
+
.describe("Path with {placeholders} intact — only for capabilities returned without a `name`."),
|
|
516
|
+
path_params: z
|
|
517
|
+
.record(z.string(), z.any())
|
|
518
|
+
.optional()
|
|
519
|
+
.describe("Values for the {placeholders}."),
|
|
520
|
+
query: z
|
|
521
|
+
.record(z.string(), z.any())
|
|
522
|
+
.optional()
|
|
523
|
+
.describe("Query parameters."),
|
|
524
|
+
body: z
|
|
525
|
+
.record(z.string(), z.any())
|
|
526
|
+
.optional()
|
|
527
|
+
.describe("Body fields, under the spec's names."),
|
|
528
|
+
product: productArg()
|
|
529
|
+
.optional()
|
|
530
|
+
.describe(`Which product owns the capability (${productList}). searchCapability returns it ` +
|
|
531
|
+
"on every result; required only when two products share a name or a path."),
|
|
532
|
+
user_permission: z
|
|
533
|
+
.enum(PERMISSION_VALUES)
|
|
534
|
+
.optional()
|
|
535
|
+
.describe("Set to 'granted' only after the user has confirmed a write."),
|
|
536
|
+
change_summary: z
|
|
537
|
+
.string()
|
|
538
|
+
.optional()
|
|
539
|
+
.describe("What will change. Required for writes."),
|
|
540
|
+
}, {
|
|
541
|
+
title: "Invoke Capability",
|
|
542
|
+
// Not read-only: this is the one tool that writes. Never destructive, because
|
|
543
|
+
// destructive endpoints are refused before binding — the refusal is enforced here,
|
|
544
|
+
// not merely hinted at. Not idempotent: it creates, clones and starts runs. Closed
|
|
545
|
+
// world: it reaches BrowserStack products the index describes, nothing else.
|
|
546
|
+
readOnlyHint: false,
|
|
547
|
+
destructiveHint: false,
|
|
548
|
+
idempotentHint: false,
|
|
549
|
+
openWorldHint: false,
|
|
550
|
+
}, async (input) => {
|
|
551
|
+
track("invokeCapability");
|
|
552
|
+
try {
|
|
553
|
+
// Either handle resolves to the same capability. `name` wins when both are sent,
|
|
554
|
+
// rather than cross-checking them: a caller pasting a stale path alongside a good
|
|
555
|
+
// name should still reach the right operation, which is the point of naming.
|
|
556
|
+
if (!input.name && !(input.method && input.path)) {
|
|
557
|
+
return failed("pass `name` — or, for a capability returned without one, both `method` and " +
|
|
558
|
+
"`path`, exactly as searchCapability returned them");
|
|
559
|
+
}
|
|
560
|
+
const { product, capability } = input.name
|
|
561
|
+
? registry.byNameLookup(input.name, input.product)
|
|
562
|
+
: registry.byEndpointLookup(input.method, input.path, input.product);
|
|
563
|
+
/** What to call it in errors — the handle the caller actually used. */
|
|
564
|
+
const handle = capability.name || `${capability.method} ${capability.path}`;
|
|
565
|
+
const args = {
|
|
566
|
+
path_params: input.path_params,
|
|
567
|
+
query: input.query,
|
|
568
|
+
body: input.body,
|
|
569
|
+
};
|
|
570
|
+
if (capability.mode === "destructive") {
|
|
571
|
+
// Refused before binding, so consent is never sought for something that cannot run.
|
|
572
|
+
return failed(`${handle} is a destructive operation and is not available through this surface`);
|
|
573
|
+
}
|
|
574
|
+
if (capability.mode === "write") {
|
|
575
|
+
const permission = input.user_permission || "not_asked";
|
|
576
|
+
// PARAMETERS ARE VALIDATED BEFORE PERMISSION IS DEMANDED. The gate used to run
|
|
577
|
+
// first, so a caller with a typo'd parameter was told "ask the user to confirm this
|
|
578
|
+
// change", went back to the human for approval, and only then learned the parameter
|
|
579
|
+
// was wrong. A dry bind costs nothing and cannot mutate.
|
|
580
|
+
const { bind } = await import("./bind.js");
|
|
581
|
+
bind(capability, args);
|
|
582
|
+
if (permission !== "granted") {
|
|
583
|
+
// Catches the careless path, not the adversarial one: the model fills this field
|
|
584
|
+
// in, so it is an audit record and a speed bump, never authorisation.
|
|
585
|
+
return failed("refused: this endpoint changes data — ask the user to confirm, then retry " +
|
|
586
|
+
"with user_permission='granted' and a change_summary");
|
|
587
|
+
}
|
|
588
|
+
if (!(input.change_summary || "").trim()) {
|
|
589
|
+
return failed("change_summary is required: state what will change");
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
const result = await invoke(capability, args, await deps.baseUrlFor(product), deps.credentialsFor(), transport, registry.index.products[product]?.auth);
|
|
593
|
+
return ok(result);
|
|
594
|
+
}
|
|
595
|
+
catch (error) {
|
|
596
|
+
if (error instanceof InvocationError)
|
|
597
|
+
return failed(error.message);
|
|
598
|
+
logger.error("invokeCapability failed: %s", error instanceof Error ? error.message : String(error));
|
|
599
|
+
return failed("that capability could not be invoked");
|
|
600
|
+
}
|
|
601
|
+
});
|
|
602
|
+
return tools;
|
|
603
|
+
}
|
|
604
|
+
export default addCapabilityRegistryToolsFromConfig;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Invoke one endpoint and hand back what the product said.
|
|
3
|
+
*
|
|
4
|
+
* NO POST-PROCESSING, BY DECISION. There used to be row extraction by shape, a `returns`
|
|
5
|
+
* allowlist, a scalars-only filter for undeclared schemas, item counting, ordering, trimming,
|
|
6
|
+
* and guards that reported an empty projection as a registration defect. Every one of them
|
|
7
|
+
* was a place where we could be wrong ABOUT a correct answer — and each time we were, the
|
|
8
|
+
* caller saw a confident empty result rather than an error. The product's response is the
|
|
9
|
+
* answer; this module's job is to get it and return it.
|
|
10
|
+
*
|
|
11
|
+
* ONE REQUEST, ONE RESPONSE. Paging is therefore the caller's, which is why `p` and the
|
|
12
|
+
* page-size parameter are published for paginated endpoints (see `project.py::_is_public`).
|
|
13
|
+
* Hiding them made sense only while this module walked the pages itself.
|
|
14
|
+
*/
|
|
15
|
+
import { GroupedArguments } from "./bind.js";
|
|
16
|
+
import { Credentials, Transport } from "./egress.js";
|
|
17
|
+
import { AuthScheme } from "./types.js";
|
|
18
|
+
import { Capability } from "./types.js";
|
|
19
|
+
export interface InvokeResult {
|
|
20
|
+
/** The product answered 2xx. Nothing else decides this. */
|
|
21
|
+
ok: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Whether this response is the whole answer.
|
|
24
|
+
*
|
|
25
|
+
* False when the envelope itself says there is another page (`info.next`), so a caller
|
|
26
|
+
* knows to ask for one rather than assuming it has everything. This is a peek at one
|
|
27
|
+
* declared field, not a reshaping of the body.
|
|
28
|
+
*/
|
|
29
|
+
completed: boolean;
|
|
30
|
+
/** The product's status, and its body exactly as sent. */
|
|
31
|
+
http_response: {
|
|
32
|
+
status: number;
|
|
33
|
+
body: unknown;
|
|
34
|
+
/** Only when there was no response at all to speak for itself. */
|
|
35
|
+
error?: string;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export declare function invoke(capability: Capability, args: GroupedArguments, baseUrl: string, credentials: Credentials, transport: Transport, auth?: AuthScheme): Promise<InvokeResult>;
|