@browserstack/mcp-server 1.3.1-beta.1 → 1.4.0-beta.1
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/dist/index.js +5 -2
- package/dist/server-factory.js +5 -0
- package/dist/tools/accessibility.js +5 -2
- package/dist/tools/ask-browserstack/central-oauth.d.ts +114 -0
- package/dist/tools/ask-browserstack/central-oauth.js +271 -0
- package/dist/tools/ask-browserstack/config.d.ts +96 -0
- package/dist/tools/ask-browserstack/config.js +134 -0
- package/dist/tools/ask-browserstack/egress.d.ts +34 -0
- package/dist/tools/ask-browserstack/egress.js +31 -0
- package/dist/tools/ask-browserstack/register.d.ts +61 -0
- package/dist/tools/ask-browserstack/register.js +394 -0
- package/dist/tools/ask-browserstack/relay.d.ts +201 -0
- package/dist/tools/ask-browserstack/relay.js +575 -0
- package/dist/tools/ask-browserstack/stream.d.ts +116 -0
- package/dist/tools/ask-browserstack/stream.js +237 -0
- package/dist/tools/ask-browserstack/types.d.ts +196 -0
- package/dist/tools/ask-browserstack/types.js +10 -0
- package/dist/tools/get-failure-logs.js +3 -1
- package/dist/tools/rca-agent.js +5 -2
- package/dist/tools/selfheal.js +5 -2
- package/dist/tools/testmanagement.js +33 -15
- package/dist/tools/tool-handoff.d.ts +37 -0
- package/dist/tools/tool-handoff.js +47 -0
- package/package.json +1 -1
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import logger from "../../logger.js";
|
|
2
|
+
/**
|
|
3
|
+
* Where Atlas lives, and the timeout ladder.
|
|
4
|
+
*
|
|
5
|
+
* The host IS compiled in, matching every other tool here — `TM_BASE_URLS`, the
|
|
6
|
+
* instrumentation endpoint — so an install needs no configuration to work. One env var
|
|
7
|
+
* overrides it. See the warning on `DEFAULT_ATLAS_URL`: the compiled-in value is currently
|
|
8
|
+
* STAGING and is a deliberate placeholder.
|
|
9
|
+
*/
|
|
10
|
+
/**
|
|
11
|
+
* CONTRACT §4 — the timeout ladder, outermost first. EACH LAYER MUST EXCEED THE ONE INSIDE
|
|
12
|
+
* IT, or a layer dies before the layer it is waiting on can answer:
|
|
13
|
+
*
|
|
14
|
+
* MCP client -> tool call longest, client-side, not ours
|
|
15
|
+
* POST /agent HTTP request 330s <- here
|
|
16
|
+
* Atlas gate -> stream ask 300s Atlas's `permission_relay_timeout`
|
|
17
|
+
* elicitInput 270s <- here
|
|
18
|
+
*
|
|
19
|
+
* 300s is the browser path's existing PERMISSION_TIMEOUT, which also auto-rejects.
|
|
20
|
+
*/
|
|
21
|
+
export const AGENT_TIMEOUT_MS = 330_000;
|
|
22
|
+
export const ELICITATION_TIMEOUT_MS = 270_000;
|
|
23
|
+
/** Thrown for anything this tool refuses to attempt. Never carries a credential. */
|
|
24
|
+
export class AskError extends Error {
|
|
25
|
+
}
|
|
26
|
+
/** Off by default is wrong for a shipped feature, but a kill switch is not. */
|
|
27
|
+
export function isEnabled() {
|
|
28
|
+
return (process.env.ASK_BROWSERSTACK_DISABLED || "").toLowerCase() !== "true";
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* May the relay be offered in the hosted (`REMOTE_MCP`) deployment?
|
|
32
|
+
*
|
|
33
|
+
* OFF BY DEFAULT, because it depends on something outside this package: the host has to
|
|
34
|
+
* keep one `McpServer` alive per session. Stateless hosts build a fresh server per POST,
|
|
35
|
+
* and an elicitation answer — which arrives as a SEPARATE POST — then reaches an instance
|
|
36
|
+
* that never asked anything, leaving the real one suspended until it times out. So this
|
|
37
|
+
* must stay opt-in per deployment rather than become a default that silently hangs.
|
|
38
|
+
*
|
|
39
|
+
* Turning it on does NOT force the relay on: `relayMode` still asks whether THIS client
|
|
40
|
+
* declared the `elicitation` capability, and a client that did not still gets a read-only
|
|
41
|
+
* run. This flag only removes the blanket refusal.
|
|
42
|
+
*/
|
|
43
|
+
export function allowRemoteRelay() {
|
|
44
|
+
return ((process.env.ASK_BROWSERSTACK_ALLOW_REMOTE_RELAY || "").toLowerCase() ===
|
|
45
|
+
"true");
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* ============================================================================
|
|
49
|
+
* PRODUCTION DEFAULTS
|
|
50
|
+
* ============================================================================
|
|
51
|
+
*
|
|
52
|
+
* These hosts are PRODUCTION. They replace the interim staging placeholders that this
|
|
53
|
+
* package shipped with while the relay was being built ("for now lets hardcode the
|
|
54
|
+
* base_url to staging only then we will point this to prod url later") — that step is
|
|
55
|
+
* now done.
|
|
56
|
+
*
|
|
57
|
+
* `https://workflows.browserstack.com` was verified, not guessed: its `/api/profiles`
|
|
58
|
+
* answers `401 {"detail":"authentication required"}`, byte-identical to staging Atlas.
|
|
59
|
+
* The production auth endpoint is `https://auth.browserstack.com/oauth2/v2/token`.
|
|
60
|
+
*
|
|
61
|
+
* WHY THIS MATTERS: this package publishes to npm as `@browserstack/mcp-server`, so an
|
|
62
|
+
* install with no environment variables set now talks to PRODUCTION. That is correct for
|
|
63
|
+
* a production deployment, but it removes the old safety property — a misconfigured or
|
|
64
|
+
* test deployment that forgets `ASK_BROWSERSTACK_ATLAS_URL` no longer fails safe onto
|
|
65
|
+
* staging, it reads and writes REAL customer data. Non-production deployments MUST set
|
|
66
|
+
* that variable explicitly. The resolved host is logged at info on first use, naming
|
|
67
|
+
* whether it came from the env var or from here, so a deployment pointing at the wrong
|
|
68
|
+
* Atlas is visible in a log line rather than inferred later from confusing data.
|
|
69
|
+
*
|
|
70
|
+
* Staging hosts, for anyone setting the override:
|
|
71
|
+
* ASK_BROWSERSTACK_ATLAS_URL = https://ai-platform-service.bsstag.com
|
|
72
|
+
* ASK_BROWSERSTACK_AUTH_TOKEN_URL = https://auth-preprod.bsstag.com/oauth2/v2/token
|
|
73
|
+
*
|
|
74
|
+
* The tests assert these literals precisely so that repointing has to be deliberate
|
|
75
|
+
* rather than something that slips through.
|
|
76
|
+
*
|
|
77
|
+
* grep: DEFAULT-PROD-HOSTS
|
|
78
|
+
*/
|
|
79
|
+
export const DEFAULT_ATLAS_URL = "https://workflows.browserstack.com";
|
|
80
|
+
export const DEFAULT_AUTH_TOKEN_URL = "https://auth.browserstack.com/oauth2/v2/token";
|
|
81
|
+
/** An operator's override may carry a trailing slash; the constants above do not. */
|
|
82
|
+
function trimUrl(value) {
|
|
83
|
+
return value.trim().replace(/\/+$/, "");
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Announced ONCE per distinct resolution, not per tool call.
|
|
87
|
+
*
|
|
88
|
+
* The point is that a deployment talking to the wrong Atlas shows up in the log; repeating it
|
|
89
|
+
* on every call would only make it easier to scroll past.
|
|
90
|
+
*/
|
|
91
|
+
const announced = new Set();
|
|
92
|
+
/** For tests, and for anything that legitimately re-resolves. */
|
|
93
|
+
export function resetHostAnnouncements() {
|
|
94
|
+
announced.clear();
|
|
95
|
+
}
|
|
96
|
+
function announce(what, url, source) {
|
|
97
|
+
const line = `${what}|${url}|${source}`;
|
|
98
|
+
if (announced.has(line))
|
|
99
|
+
return;
|
|
100
|
+
announced.add(line);
|
|
101
|
+
logger.info("askBrowserstackAI: %s is %s (source: %s)", what, url, source);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Resolve Atlas's base URL:
|
|
105
|
+
*
|
|
106
|
+
* 1. ASK_BROWSERSTACK_ATLAS_URL explicit override
|
|
107
|
+
* 2. the built-in staging default (see the warning above)
|
|
108
|
+
*
|
|
109
|
+
* Matching every other tool here, which ships its host in the code and treats the env var as
|
|
110
|
+
* an override — `TM_BASE_URLS`, the instrumentation endpoint. There is no environment map and
|
|
111
|
+
* no selector: one default, one override.
|
|
112
|
+
*/
|
|
113
|
+
export function atlasBaseUrl() {
|
|
114
|
+
const explicit = process.env.ASK_BROWSERSTACK_ATLAS_URL;
|
|
115
|
+
const url = explicit && explicit.trim() ? trimUrl(explicit) : DEFAULT_ATLAS_URL;
|
|
116
|
+
announce("Atlas", url, explicit && explicit.trim() ? "env" : "default");
|
|
117
|
+
return url;
|
|
118
|
+
}
|
|
119
|
+
/** Resolved per call, never captured at construction. */
|
|
120
|
+
export function agentUrl() {
|
|
121
|
+
return `${atlasBaseUrl()}/agent`;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Where a central-OAuth JWT is minted (CONTRACT v1.2 §I, as amended by task 7).
|
|
125
|
+
*
|
|
126
|
+
* The shared `delegation.token` path is gone from Atlas, so a user-attested central JWT is
|
|
127
|
+
* the only way in. Same two rungs as the host, and the same staging default.
|
|
128
|
+
*/
|
|
129
|
+
export function authTokenUrl() {
|
|
130
|
+
const explicit = process.env.ASK_BROWSERSTACK_AUTH_TOKEN_URL;
|
|
131
|
+
const url = explicit && explicit.trim() ? trimUrl(explicit) : DEFAULT_AUTH_TOKEN_URL;
|
|
132
|
+
announce("auth token endpoint", url, explicit && explicit.trim() ? "env" : "default");
|
|
133
|
+
return url;
|
|
134
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pieces of the outbound `POST /agent` that are not the transport itself.
|
|
3
|
+
*
|
|
4
|
+
* The transport moved to `stream.ts` when A2 was removed: `/agent` is read as an event
|
|
5
|
+
* stream now, so a one-request-one-response `AgentTransport` has nothing left to describe.
|
|
6
|
+
* What stays here is what both halves always shared — the header set, the credential pair,
|
|
7
|
+
* and the response shape `relay.ts` reads to tell a refusal from an unreachable service
|
|
8
|
+
* apart, which the stream's JSON-degrade path still produces.
|
|
9
|
+
*
|
|
10
|
+
* AUTH HERE IS NOT THE PRODUCT-API AUTH. `/agent` accepts exactly two credentials, both in
|
|
11
|
+
* `Authorization`: the shared delegation token or a BrowserStack central JWT. There is no
|
|
12
|
+
* `Api-Token` path on this route (CONTRACT v1.2 §I), so sending one would not merely be
|
|
13
|
+
* useless — it would push the user's `access_key` across a trust boundary to an endpoint
|
|
14
|
+
* that has no use for it, and into every request log on the way. The capability registry's
|
|
15
|
+
* `authHeaders` remains right for PRODUCT calls; it is simply not the header set for this
|
|
16
|
+
* one, and is deliberately not imported here.
|
|
17
|
+
*/
|
|
18
|
+
export interface Credentials {
|
|
19
|
+
username: string;
|
|
20
|
+
accessKey: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The complete header set for `POST /agent` (CONTRACT v1.2 §4). Three headers, no more.
|
|
24
|
+
*
|
|
25
|
+
* The token is a secret and appears nowhere else: not in a log line, not in a result, not in
|
|
26
|
+
* an error message.
|
|
27
|
+
*/
|
|
28
|
+
export declare function agentHeaders(token: string): Record<string, string>;
|
|
29
|
+
export interface AgentResponse {
|
|
30
|
+
status: number;
|
|
31
|
+
body: unknown;
|
|
32
|
+
/** Only when there was no response at all to speak for itself. */
|
|
33
|
+
error?: string;
|
|
34
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The pieces of the outbound `POST /agent` that are not the transport itself.
|
|
3
|
+
*
|
|
4
|
+
* The transport moved to `stream.ts` when A2 was removed: `/agent` is read as an event
|
|
5
|
+
* stream now, so a one-request-one-response `AgentTransport` has nothing left to describe.
|
|
6
|
+
* What stays here is what both halves always shared — the header set, the credential pair,
|
|
7
|
+
* and the response shape `relay.ts` reads to tell a refusal from an unreachable service
|
|
8
|
+
* apart, which the stream's JSON-degrade path still produces.
|
|
9
|
+
*
|
|
10
|
+
* AUTH HERE IS NOT THE PRODUCT-API AUTH. `/agent` accepts exactly two credentials, both in
|
|
11
|
+
* `Authorization`: the shared delegation token or a BrowserStack central JWT. There is no
|
|
12
|
+
* `Api-Token` path on this route (CONTRACT v1.2 §I), so sending one would not merely be
|
|
13
|
+
* useless — it would push the user's `access_key` across a trust boundary to an endpoint
|
|
14
|
+
* that has no use for it, and into every request log on the way. The capability registry's
|
|
15
|
+
* `authHeaders` remains right for PRODUCT calls; it is simply not the header set for this
|
|
16
|
+
* one, and is deliberately not imported here.
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* The complete header set for `POST /agent` (CONTRACT v1.2 §4). Three headers, no more.
|
|
20
|
+
*
|
|
21
|
+
* The token is a secret and appears nowhere else: not in a log line, not in a result, not in
|
|
22
|
+
* an error message.
|
|
23
|
+
*/
|
|
24
|
+
export function agentHeaders(token) {
|
|
25
|
+
return {
|
|
26
|
+
Authorization: `Bearer ${token}`,
|
|
27
|
+
"Content-Type": "application/json",
|
|
28
|
+
// Attribution, so the downstream service can see the call came from an agent.
|
|
29
|
+
"request-source": "ai-chatbot",
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `askBrowserstackAI` — one tool call in, one tool result out, with a human's approval
|
|
3
|
+
* relayed through the middle of it.
|
|
4
|
+
*
|
|
5
|
+
* The shape, and why:
|
|
6
|
+
*
|
|
7
|
+
* 1. NEGOTIATE FIRST. `relayMode()` is consulted BEFORE Atlas is called, so Atlas learns
|
|
8
|
+
* whether a human is reachable before it starts rather than discovering it at the gate.
|
|
9
|
+
* Anything other than "offered" means `permission_relay` is omitted entirely and Atlas
|
|
10
|
+
* runs read-only — today's exact behaviour, and the path opencode and goose stay on.
|
|
11
|
+
* That also covers the hosted `REMOTE_MCP` deployment, where the relay cannot work at
|
|
12
|
+
* all; see `relayMode` for why. Nothing here depends on `sampling`, which Claude Code
|
|
13
|
+
* does not declare.
|
|
14
|
+
* 2. LISTEN ON LOOPBACK. Transport is A2, so Atlas calls US back; because it initiates,
|
|
15
|
+
* the decision returns on the same connection to the same pod and PLAN.md's affinity
|
|
16
|
+
* problem never arises for this stdio deployment.
|
|
17
|
+
* 3. ELICIT, ONCE. Atlas's `description` is the message, nothing is requested in the form,
|
|
18
|
+
* and the ACTION is mapped by CONTRACT §7 with no second chances.
|
|
19
|
+
* 4. RETURN THE TRAIL. `approvals` and `applied_before_stop` are what let a caller tell
|
|
20
|
+
* "nothing happened" from "some steps applied, then stopped".
|
|
21
|
+
*
|
|
22
|
+
* FAIL CLOSED THROUGHOUT. A decline, a cancel, a timeout, a bad token, a body we cannot
|
|
23
|
+
* parse, a handler that throws — every one of them denies. An unattended run cannot approve
|
|
24
|
+
* itself because a headless client returns `cancel`, which is a deny.
|
|
25
|
+
*/
|
|
26
|
+
import { McpServer, RegisteredTool } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
27
|
+
import { BrowserStackConfig } from "../../lib/types.js";
|
|
28
|
+
import { Credentials } from "./egress.js";
|
|
29
|
+
import type { AgentStreamTransport, DecisionTransport } from "./stream.js";
|
|
30
|
+
import { RelayMode } from "./types.js";
|
|
31
|
+
export interface AskDeps {
|
|
32
|
+
/** Resolved per call: a deployment's host is configuration, not a constructor argument. */
|
|
33
|
+
agentUrl: () => string;
|
|
34
|
+
/**
|
|
35
|
+
* Sign in and return a bearer for `POST /agent`. Cached behind this, not minted per call.
|
|
36
|
+
*
|
|
37
|
+
* A function rather than a value because it is resolved per call for the same reason the
|
|
38
|
+
* host is, and because it can fail in ways a caller needs told apart.
|
|
39
|
+
*/
|
|
40
|
+
mintToken: () => Promise<string>;
|
|
41
|
+
/**
|
|
42
|
+
* Read per call, not captured: the remote server rebuilds config per session, so a
|
|
43
|
+
* captured credential would outlive the session it belongs to.
|
|
44
|
+
*
|
|
45
|
+
* These are now THE AUTH CREDENTIAL, not merely attribution: the access key is exchanged
|
|
46
|
+
* for a user-attested central JWT, which is what lets Atlas run the approved product call
|
|
47
|
+
* as the human rather than as a shared service account.
|
|
48
|
+
*/
|
|
49
|
+
credentialsFor: () => Credentials;
|
|
50
|
+
/**
|
|
51
|
+
* The two transport seams, injectable so a test can drive a whole approval round trip
|
|
52
|
+
* — ask, elicit, decide, result — without a socket.
|
|
53
|
+
*/
|
|
54
|
+
streamTransport?: AgentStreamTransport;
|
|
55
|
+
decisionTransport?: DecisionTransport;
|
|
56
|
+
}
|
|
57
|
+
export declare function relayMode(server: McpServer): RelayMode;
|
|
58
|
+
export declare function addAskBrowserstackAITool(server: McpServer, deps: AskDeps, config?: BrowserStackConfig): Record<string, RegisteredTool>;
|
|
59
|
+
/** The tool-adder the server factory calls. */
|
|
60
|
+
export declare function addAskBrowserstackAIToolFromConfig(server: McpServer, config: BrowserStackConfig): Record<string, RegisteredTool>;
|
|
61
|
+
export default addAskBrowserstackAIToolFromConfig;
|
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `askBrowserstackAI` — one tool call in, one tool result out, with a human's approval
|
|
3
|
+
* relayed through the middle of it.
|
|
4
|
+
*
|
|
5
|
+
* The shape, and why:
|
|
6
|
+
*
|
|
7
|
+
* 1. NEGOTIATE FIRST. `relayMode()` is consulted BEFORE Atlas is called, so Atlas learns
|
|
8
|
+
* whether a human is reachable before it starts rather than discovering it at the gate.
|
|
9
|
+
* Anything other than "offered" means `permission_relay` is omitted entirely and Atlas
|
|
10
|
+
* runs read-only — today's exact behaviour, and the path opencode and goose stay on.
|
|
11
|
+
* That also covers the hosted `REMOTE_MCP` deployment, where the relay cannot work at
|
|
12
|
+
* all; see `relayMode` for why. Nothing here depends on `sampling`, which Claude Code
|
|
13
|
+
* does not declare.
|
|
14
|
+
* 2. LISTEN ON LOOPBACK. Transport is A2, so Atlas calls US back; because it initiates,
|
|
15
|
+
* the decision returns on the same connection to the same pod and PLAN.md's affinity
|
|
16
|
+
* problem never arises for this stdio deployment.
|
|
17
|
+
* 3. ELICIT, ONCE. Atlas's `description` is the message, nothing is requested in the form,
|
|
18
|
+
* and the ACTION is mapped by CONTRACT §7 with no second chances.
|
|
19
|
+
* 4. RETURN THE TRAIL. `approvals` and `applied_before_stop` are what let a caller tell
|
|
20
|
+
* "nothing happened" from "some steps applied, then stopped".
|
|
21
|
+
*
|
|
22
|
+
* FAIL CLOSED THROUGHOUT. A decline, a cancel, a timeout, a bad token, a body we cannot
|
|
23
|
+
* parse, a handler that throws — every one of them denies. An unattended run cannot approve
|
|
24
|
+
* itself because a headless client returns `cancel`, which is a deny.
|
|
25
|
+
*/
|
|
26
|
+
import { ErrorCode, McpError, } from "@modelcontextprotocol/sdk/types.js";
|
|
27
|
+
import { z } from "zod";
|
|
28
|
+
import appConfig from "../../config.js";
|
|
29
|
+
import { trackMCP } from "../../lib/instrumentation.js";
|
|
30
|
+
import logger from "../../logger.js";
|
|
31
|
+
import { AskError, ELICITATION_TIMEOUT_MS, agentUrl, allowRemoteRelay, authTokenUrl, isEnabled, } from "./config.js";
|
|
32
|
+
import { fetchTokenTransport, mintCentralToken } from "./central-oauth.js";
|
|
33
|
+
import { agentHeaders } from "./egress.js";
|
|
34
|
+
import { EVENT_PERMISSION, EVENT_RESULT, EVENT_RUN, decisionUrl, fetchAgentStreamTransport, fetchDecisionTransport, parseAsk, } from "./stream.js";
|
|
35
|
+
import { buildResult, decide, elicitationMessage, elicitationShape, errorResult, } from "./relay.js";
|
|
36
|
+
import { PRODUCTS, } from "./types.js";
|
|
37
|
+
/**
|
|
38
|
+
* THE FALLBACK POSITIONING IN THE FIRST SENTENCE IS LOAD-BEARING.
|
|
39
|
+
*
|
|
40
|
+
* This server registers 45 tools, most of them hand-written for one endpoint each. Those are
|
|
41
|
+
* faster, cheaper and more predictable than handing a task to an agent that has to work out
|
|
42
|
+
* its own API calls, so they should win whenever one of them actually fits. What this tool
|
|
43
|
+
* covers is the gap: a task nothing here has a tool for, or one where the specific tools were
|
|
44
|
+
* tried and did not get there.
|
|
45
|
+
*
|
|
46
|
+
* A description is the ONLY thing steering that choice — the client picks a tool from these
|
|
47
|
+
* words alone, before any call is made — so the ordering is deliberate: when to reach for it
|
|
48
|
+
* first, what it does second, and the consent behaviour last.
|
|
49
|
+
*/
|
|
50
|
+
const DESCRIPTION = "Use this when no other BrowserStack tool here fits the task, or when the ones you tried " +
|
|
51
|
+
"did not get you there. Prefer a specific tool whenever one fits: it is faster and more " +
|
|
52
|
+
"predictable than handing the job to an agent. " +
|
|
53
|
+
"Otherwise, describe what you want in plain language and BrowserStack's agent decides " +
|
|
54
|
+
"which calls to make, then returns its answer plus the steps it took. Anything that would " +
|
|
55
|
+
"change data pauses and asks you to confirm it first, in your own client; deletes are " +
|
|
56
|
+
"refused outright. If your client cannot show you a prompt, the run is read-only and " +
|
|
57
|
+
"everything it wanted to change comes back in `needs_approval` instead. One task per call.";
|
|
58
|
+
/**
|
|
59
|
+
* `isError` marks a call that FAILED, not one that was refused.
|
|
60
|
+
*
|
|
61
|
+
* A `blocked` run is the feature working: the agent asked, a human said no, and the trail
|
|
62
|
+
* says so. Flagging that as a tool error makes a client render a correct refusal in red and
|
|
63
|
+
* — worse — invites it to retry, which is exactly the "retry forever" loop the distinct
|
|
64
|
+
* `permission_relay` reasons exist to prevent. `ok` still means `status === "ok"`.
|
|
65
|
+
*/
|
|
66
|
+
function toResult(payload) {
|
|
67
|
+
const failed = payload.status === "error" || payload.status === "rate_limited";
|
|
68
|
+
return {
|
|
69
|
+
content: [{ type: "text", text: JSON.stringify(payload) }],
|
|
70
|
+
...(failed ? { isError: true } : {}),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function isTimeout(error) {
|
|
74
|
+
return error instanceof McpError && error.code === ErrorCode.RequestTimeout;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Relay one ask to the human and record what they said.
|
|
78
|
+
*
|
|
79
|
+
* The elicitation is NEVER retried. A client that timed out or errored has told us it
|
|
80
|
+
* cannot get an answer, and asking again only produces a second prompt for the same action.
|
|
81
|
+
*/
|
|
82
|
+
async function relayOneAsk(server, ask, approvals, relatedRequestId) {
|
|
83
|
+
let answer;
|
|
84
|
+
try {
|
|
85
|
+
// `relatedRequestId` IS LOAD-BEARING OVER HTTP, and its absence fails silently.
|
|
86
|
+
// Streamable HTTP routes a server->client message onto the stream of the request it
|
|
87
|
+
// relates to (`_requestToStreamMapping`). With no id the SDK falls back to the
|
|
88
|
+
// standalone SSE stream, and a host that answers GET /mcp with 405 has none — so the
|
|
89
|
+
// SDK drops the message with "Stream is disconnected", the tool waits out its 270s,
|
|
90
|
+
// and Atlas's gate expires into `reason: "timeout"`. The human is told they did not
|
|
91
|
+
// answer a question they were never shown.
|
|
92
|
+
//
|
|
93
|
+
// Measured exactly that way against the hosted server before this was threaded
|
|
94
|
+
// through. On stdio it is irrelevant — one pipe, nothing to route — which is why no
|
|
95
|
+
// local test could have caught it.
|
|
96
|
+
answer = await server.server.elicitInput({
|
|
97
|
+
mode: "form",
|
|
98
|
+
// Framed with the PRODUCT and nothing else (v1.1 §G): a bare sentence with no
|
|
99
|
+
// attribution is a worse prompt than a framed one, and `product` is all the
|
|
100
|
+
// ask carries — the route, method, path and op_key never reach this side by design,
|
|
101
|
+
// and that is the whole privacy boundary (the ask is four named fields). The
|
|
102
|
+
// description goes through VERBATIM: paraphrasing it would mean the human approves
|
|
103
|
+
// something other than what the model actually said. Atlas no longer rewrites it
|
|
104
|
+
// either — it used to replace a route-shaped one with a placeholder, which asked a
|
|
105
|
+
// person to approve a sentence they could not read; CONTRACT v2 §3 was amended and
|
|
106
|
+
// that guard removed. An older Atlas can still send the placeholder, which is why
|
|
107
|
+
// the framing is tested against it.
|
|
108
|
+
message: elicitationMessage(ask.product, ask.description),
|
|
109
|
+
requestedSchema: {
|
|
110
|
+
// NOTHING IS REQUESTED. The action IS the answer: `accept` already means the human
|
|
111
|
+
// approved, and `decline` already gives them an unambiguous refusal in the same
|
|
112
|
+
// dialog. A `confirm` boolean used to live here and produced a FALSE DENIAL — a
|
|
113
|
+
// user approved on preprod and was told they had refused, because a client renders
|
|
114
|
+
// a form field and submits its unset value. We cannot distinguish that from a
|
|
115
|
+
// deliberate untick, so the field is gone rather than guessed at.
|
|
116
|
+
//
|
|
117
|
+
// Fail-closed is untouched by this and never rested on the boolean: a headless
|
|
118
|
+
// client with nobody at the terminal returns `cancel` (measured, HANDOFF.md), and
|
|
119
|
+
// `cancel` is a deny. That is what stops an unattended run self-approving.
|
|
120
|
+
type: "object",
|
|
121
|
+
properties: {},
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
// The inner rung of CONTRACT §4's ladder, strictly shorter than Atlas's 300s gate.
|
|
125
|
+
{ timeout: ELICITATION_TIMEOUT_MS, relatedRequestId });
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
if (isTimeout(error)) {
|
|
129
|
+
approvals.push({
|
|
130
|
+
description: ask.description,
|
|
131
|
+
decision: "deny",
|
|
132
|
+
reason: "timeout",
|
|
133
|
+
});
|
|
134
|
+
return { perm_id: ask.perm_id, decision: "deny", reason: "timeout" };
|
|
135
|
+
}
|
|
136
|
+
// An unexpected failure has no honest `reason` in CONTRACT §2's vocabulary, so it is
|
|
137
|
+
// not given one on the wire: the throw says "this side broke" without claiming a human
|
|
138
|
+
// decided anything. `runStreamed` catches it and sends the explicit deny the throw
|
|
139
|
+
// implies — see the comment there for why A1 cannot let it escape.
|
|
140
|
+
approvals.push({
|
|
141
|
+
description: ask.description,
|
|
142
|
+
decision: "deny",
|
|
143
|
+
reason: "error",
|
|
144
|
+
});
|
|
145
|
+
throw error;
|
|
146
|
+
}
|
|
147
|
+
// The SHAPE of the answer only — a fixed action enum and a boolean, never the description
|
|
148
|
+
// or anything a user typed. Logged so that what a client actually submits can be read next
|
|
149
|
+
// time rather than inferred from a compiled binary.
|
|
150
|
+
logger.info("askBrowserstackAI: elicitation answered %s", elicitationShape(answer));
|
|
151
|
+
const { decision, reason } = decide(answer);
|
|
152
|
+
approvals.push({ description: ask.description, decision, reason });
|
|
153
|
+
return { perm_id: ask.perm_id, decision, reason };
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Decide whether to offer the approval channel at all.
|
|
157
|
+
*
|
|
158
|
+
* STDIO ALWAYS. HOSTED ONLY WHEN ITS OPERATOR OPTS IN — and the reason is a property of
|
|
159
|
+
* the HOST, not of this tool.
|
|
160
|
+
*
|
|
161
|
+
* Elicitation is a SERVER-INITIATED message whose answer arrives on a SEPARATE POST. A
|
|
162
|
+
* stateless host builds a fresh `McpServer` per POST, so that answer reaches an instance
|
|
163
|
+
* which never asked anything, while the one actually suspended on `await` waits out its
|
|
164
|
+
* timeout. Nothing in this package can fix that; what it holds is a live Promise resolver
|
|
165
|
+
* and a paused function in the host's heap, and a paused call cannot be moved.
|
|
166
|
+
*
|
|
167
|
+
* This is why `841c6358` was right to remove sessions from the hosted server on the
|
|
168
|
+
* grounds that "we use neither server-initiated messages nor subscriptions/sampling" —
|
|
169
|
+
* this feature is the exception that commit did not have to consider.
|
|
170
|
+
*
|
|
171
|
+
* MEASURED, not assumed: with the host keeping one server per session
|
|
172
|
+
* (browserstack/remote-mcp-server#96), a tool call and its elicitation answer were served
|
|
173
|
+
* by the same instance over hosted Streamable HTTP, and the relay completed. So the
|
|
174
|
+
* refusal below is now conditional rather than absolute.
|
|
175
|
+
*
|
|
176
|
+
* It stays OFF by default because it depends on a deployment property this package cannot
|
|
177
|
+
* observe. A hosted operator turns it on only once their host keeps sessions AND pins a
|
|
178
|
+
* session to a pod — sessions are per-process, so without affinity the answer POST can
|
|
179
|
+
* land on a replica that has never seen it. That failure is intermittent and reads like a
|
|
180
|
+
* client bug, which is exactly why it must not be the default.
|
|
181
|
+
*
|
|
182
|
+
* When refused, Atlas runs read-only — a supported path that already works — and
|
|
183
|
+
* `permission_relay.reason` says `remote_mode` so nobody mistakes it for a human's no.
|
|
184
|
+
*/
|
|
185
|
+
/**
|
|
186
|
+
* CONTRACT v2 (A1) — drive one run over the stream.
|
|
187
|
+
*
|
|
188
|
+
* The loop is the whole orchestration: read events, elicit on each `permission`, POST
|
|
189
|
+
* the decision, hand the `result` to `buildResult`. It decides nothing itself —
|
|
190
|
+
* `relayOneAsk` owns the elicitation and the allow/deny mapping, unchanged from the
|
|
191
|
+
* transport it replaced. That is deliberate: the transport changed, the judgement did
|
|
192
|
+
* not, and the judgement is the part that is dangerous to get wrong.
|
|
193
|
+
*
|
|
194
|
+
* Reading pauses while a human is being prompted, which is correct rather than merely
|
|
195
|
+
* tolerable: Atlas is blocked on that decision and will emit nothing but heartbeats
|
|
196
|
+
* until it arrives, and heartbeats are dropped by the parser.
|
|
197
|
+
*
|
|
198
|
+
* A run that ends with no `result` is an error, not an empty success. A stream that
|
|
199
|
+
* simply stops is indistinguishable from a network drop, and reporting it as a finished
|
|
200
|
+
* run with no answer would be the transport quietly speaking for the agent.
|
|
201
|
+
*/
|
|
202
|
+
async function runStreamed(server, streamTransport, decisionTransport, url, headers, body, approvals, mode, product, relatedRequestId) {
|
|
203
|
+
let runId = "";
|
|
204
|
+
let result;
|
|
205
|
+
let sawResult = false;
|
|
206
|
+
// 200 unless the reply was not a stream at all, in which case the transport carries
|
|
207
|
+
// the real status — `relay.ts` needs it to tell 401 from 403 from a plain failure.
|
|
208
|
+
let resultStatus = 200;
|
|
209
|
+
for await (const event of streamTransport(url, headers, body)) {
|
|
210
|
+
if (event.event === EVENT_RUN) {
|
|
211
|
+
runId = String(event.data?.run_id || "");
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
if (event.event === EVENT_RESULT) {
|
|
215
|
+
result = event.data;
|
|
216
|
+
sawResult = true;
|
|
217
|
+
if (typeof event.status === "number")
|
|
218
|
+
resultStatus = event.status;
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
if (event.event !== EVENT_PERMISSION)
|
|
222
|
+
continue;
|
|
223
|
+
// Validated, not cast: a frame missing a usable `perm_id` or carrying a blank
|
|
224
|
+
// description cannot produce an answerable prompt, so it must not produce a prompt.
|
|
225
|
+
const ask = parseAsk(event.data);
|
|
226
|
+
if (!ask) {
|
|
227
|
+
logger.error("askBrowserstackAI: unusable permission ask on the stream; ignoring");
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
if (!runId) {
|
|
231
|
+
// Atlas emits `run` before any ask precisely so this cannot happen. If it does,
|
|
232
|
+
// there is nowhere to send a decision — so do not prompt a human for an answer
|
|
233
|
+
// that could never be delivered.
|
|
234
|
+
logger.error("askBrowserstackAI: permission ask arrived before run_id; cannot answer");
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
// `relayOneAsk` RETHROWS on an unexpected elicitation failure. Under A2 that was
|
|
238
|
+
// load-bearing: the throw made the inbound callback answer 500, which Atlas's
|
|
239
|
+
// fail-closed rule read as a deny. Under A1 there is no inbound request to fail, so
|
|
240
|
+
// letting it escape would abandon the run and leave Atlas waiting out its full 300s
|
|
241
|
+
// gate — turning a client hiccup into a five-minute stall. So it is caught here and
|
|
242
|
+
// converted into the explicit deny the throw used to imply. `relayOneAsk` has
|
|
243
|
+
// already recorded the approvals entry, so only the wire decision is missing.
|
|
244
|
+
let decision;
|
|
245
|
+
try {
|
|
246
|
+
decision = await relayOneAsk(server, ask, approvals, relatedRequestId);
|
|
247
|
+
}
|
|
248
|
+
catch (error) {
|
|
249
|
+
logger.warn("askBrowserstackAI: elicitation failed, denying explicitly: %s", error instanceof Error ? error.message : String(error));
|
|
250
|
+
decision = { perm_id: ask.perm_id, decision: "deny", reason: "error" };
|
|
251
|
+
}
|
|
252
|
+
const status = await decisionTransport(decisionUrl(url, runId), headers, {
|
|
253
|
+
perm_id: decision.perm_id,
|
|
254
|
+
decision: decision.decision,
|
|
255
|
+
reason: decision.reason || "",
|
|
256
|
+
});
|
|
257
|
+
if (status !== 204) {
|
|
258
|
+
// Never fatal, and never re-sent. Atlas's gate is still waiting and denies on its
|
|
259
|
+
// own expiry, so a lost decision is safe — it can only cost an approval, never
|
|
260
|
+
// grant one. Retrying risks the opposite: a duplicate that 409s, or worse, an
|
|
261
|
+
// approval applied to a step the run has already moved past.
|
|
262
|
+
logger.warn("askBrowserstackAI: decision for %s was not accepted (HTTP %s)", decision.perm_id, status);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (!sawResult) {
|
|
266
|
+
return errorResult("BrowserStack AI ended the run without a result. Nothing was changed " +
|
|
267
|
+
"beyond any step you already approved.", approvals);
|
|
268
|
+
}
|
|
269
|
+
// Shaped as an `AgentResponse` so `buildResult` — written for the transport A1 replaced
|
|
270
|
+
// and unchanged — sees exactly what it always saw.
|
|
271
|
+
return buildResult({ status: resultStatus, body: result }, approvals, mode, product);
|
|
272
|
+
}
|
|
273
|
+
export function relayMode(server) {
|
|
274
|
+
// The hosted deployment refuses UNLESS its operator has opted in, because whether an
|
|
275
|
+
// elicitation can be answered there depends on the host keeping one server alive per
|
|
276
|
+
// session — see `allowRemoteRelay`. Verified working against the hosted Streamable
|
|
277
|
+
// HTTP server once it does (browserstack/remote-mcp-server#96).
|
|
278
|
+
if (appConfig.REMOTE_MCP && !allowRemoteRelay())
|
|
279
|
+
return "remote_mode";
|
|
280
|
+
// The real gate either way: can THIS client be asked? A client that never declared
|
|
281
|
+
// `elicitation` gets a read-only run whatever the deployment.
|
|
282
|
+
return server.server.getClientCapabilities()?.elicitation
|
|
283
|
+
? "offered"
|
|
284
|
+
: "no_human";
|
|
285
|
+
}
|
|
286
|
+
export function addAskBrowserstackAITool(server, deps, config) {
|
|
287
|
+
// A1 (CONTRACT v2) is the only path; A2 is gone. No version flag is needed to talk to
|
|
288
|
+
// an Atlas that predates the stream: such a server answers `POST /agent` with ordinary
|
|
289
|
+
// JSON, the parser sees no `text/event-stream`, and the run degrades to a read-only
|
|
290
|
+
// answer carrying that response's own status.
|
|
291
|
+
const streamTransport = deps.streamTransport || fetchAgentStreamTransport();
|
|
292
|
+
const decisionTransport = deps.decisionTransport || fetchDecisionTransport();
|
|
293
|
+
const tools = {};
|
|
294
|
+
/** Instrumentation in the house style, and never fatal to the call it wraps. */
|
|
295
|
+
const track = (name) => {
|
|
296
|
+
try {
|
|
297
|
+
trackMCP(name, server.server.getClientVersion(), undefined, config);
|
|
298
|
+
}
|
|
299
|
+
catch {
|
|
300
|
+
// Telemetry must not decide whether a tool call succeeds.
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
tools.askBrowserstackAI = server.tool("askBrowserstackAI", DESCRIPTION, {
|
|
304
|
+
product: z
|
|
305
|
+
.enum(PRODUCTS)
|
|
306
|
+
.describe("Which product to work in: tm (Test Management), a11y (Accessibility), " +
|
|
307
|
+
"tra (Test Reporting & Analytics)."),
|
|
308
|
+
query: z
|
|
309
|
+
.string()
|
|
310
|
+
.describe("What you want, in plain language. One thing per call."),
|
|
311
|
+
}, {
|
|
312
|
+
// It can write now, which is the whole point of the relay. Destructive operations
|
|
313
|
+
// stay refused, so `destructiveHint` is false for the same reason invokeEndpoint
|
|
314
|
+
// sets it false: consent is not a licence to delete.
|
|
315
|
+
readOnlyHint: false,
|
|
316
|
+
destructiveHint: false,
|
|
317
|
+
title: "Ask BrowserStack AI",
|
|
318
|
+
}, async ({ product, query }, extra) => {
|
|
319
|
+
track("askBrowserstackAI");
|
|
320
|
+
const approvals = [];
|
|
321
|
+
// Negotiated before anything else so the failure paths below report the mode they
|
|
322
|
+
// would have run in.
|
|
323
|
+
const mode = relayMode(server);
|
|
324
|
+
try {
|
|
325
|
+
const url = deps.agentUrl();
|
|
326
|
+
// Signed in BEFORE the listener is opened and before the run starts. Minting
|
|
327
|
+
// lazily mid-call would put a token round-trip inside the window where a human is
|
|
328
|
+
// being prompted, and a mint that failed there would strand an open port.
|
|
329
|
+
const headers = agentHeaders(await deps.mintToken());
|
|
330
|
+
const body = { task: query, product };
|
|
331
|
+
// Attribution, and now belt-and-braces rather than the source of truth: the minted
|
|
332
|
+
// JWT is user-attested, so Atlas sets `principal_verified=True` and takes the acting
|
|
333
|
+
// user from signed claims instead of this field. It is still sent because it is part
|
|
334
|
+
// of the frozen wire format (CONTRACT v1.2 §3) and dropping it would be a one-sided
|
|
335
|
+
// change — but nothing should trust it, and Atlas no longer does.
|
|
336
|
+
// Omitted ENTIRELY when unset, never sent as "".
|
|
337
|
+
const username = (deps.credentialsFor().username || "").trim();
|
|
338
|
+
if (username)
|
|
339
|
+
body.user_id = username;
|
|
340
|
+
// A1: asking for a stream costs nothing to set up — no port, no listener, no
|
|
341
|
+
// per-run bearer, because nothing dials in. Which is the whole point: the
|
|
342
|
+
// callback this replaces could never reach a laptop behind NAT, so the feature
|
|
343
|
+
// was read-only for every real user regardless of what was configured.
|
|
344
|
+
if (mode === "offered") {
|
|
345
|
+
body.permission_relay = { mode: "stream" };
|
|
346
|
+
}
|
|
347
|
+
else {
|
|
348
|
+
// Omitted ENTIRELY, not sent empty: its absence is what selects Atlas's
|
|
349
|
+
// read-only HeadlessGate.
|
|
350
|
+
logger.info("askBrowserstackAI: no permission relay (%s); running read-only", mode);
|
|
351
|
+
}
|
|
352
|
+
// `product` reaches the result so an entitlement refusal can name it: the flags
|
|
353
|
+
// are per product, and a bare "not enabled" sends the user to their admin
|
|
354
|
+
// asking about the wrong thing.
|
|
355
|
+
return toResult(await runStreamed(server, streamTransport, decisionTransport, url, headers, body, approvals, mode, product,
|
|
356
|
+
// The tool call's own id, so each elicitation is routed onto THIS request's
|
|
357
|
+
// stream. Over Streamable HTTP there is nowhere else for it to go.
|
|
358
|
+
extra?.requestId));
|
|
359
|
+
}
|
|
360
|
+
catch (error) {
|
|
361
|
+
const message = error instanceof AskError || error instanceof Error
|
|
362
|
+
? error.message
|
|
363
|
+
: String(error);
|
|
364
|
+
logger.error("askBrowserstackAI failed: %s", message);
|
|
365
|
+
// No `canElicit` argument: the request never left this process, so whether the
|
|
366
|
+
// client could have been prompted is not what the reader needs to know.
|
|
367
|
+
return toResult(errorResult(message, approvals));
|
|
368
|
+
}
|
|
369
|
+
// No teardown: A1 opens no port and binds nothing, so there is nothing that can
|
|
370
|
+
// leak across calls or survive an error. The stream is closed by its own
|
|
371
|
+
// iteration ending, and Atlas drops the run when the response completes.
|
|
372
|
+
});
|
|
373
|
+
return tools;
|
|
374
|
+
}
|
|
375
|
+
/** The tool-adder the server factory calls. */
|
|
376
|
+
export function addAskBrowserstackAIToolFromConfig(server, config) {
|
|
377
|
+
if (!isEnabled()) {
|
|
378
|
+
logger.info("askBrowserstackAI disabled by ASK_BROWSERSTACK_DISABLED");
|
|
379
|
+
return {};
|
|
380
|
+
}
|
|
381
|
+
const credentials = () => ({
|
|
382
|
+
username: config["browserstack-username"],
|
|
383
|
+
accessKey: config["browserstack-access-key"],
|
|
384
|
+
});
|
|
385
|
+
const tokenTransport = fetchTokenTransport();
|
|
386
|
+
return addAskBrowserstackAITool(server, {
|
|
387
|
+
// Both resolved per call. An unconfigured host surfaces as a named error from the
|
|
388
|
+
// tool rather than as a missing tool, so the cause is visible to whoever hits it.
|
|
389
|
+
agentUrl,
|
|
390
|
+
mintToken: () => mintCentralToken(authTokenUrl(), credentials(), tokenTransport),
|
|
391
|
+
credentialsFor: credentials,
|
|
392
|
+
}, config);
|
|
393
|
+
}
|
|
394
|
+
export default addAskBrowserstackAIToolFromConfig;
|