@empiricalrun/playwright-utils 0.67.1 → 0.68.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/CHANGELOG.md +19 -0
- package/dist/config/proxy.js +1 -1
- package/dist/generated/api-worker-openapi.d.ts +204 -152
- package/dist/generated/api-worker-openapi.d.ts.map +1 -1
- package/dist/mcp/index.d.ts +46 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +172 -0
- package/dist/mcp/mcp.spec.d.ts +2 -0
- package/dist/mcp/mcp.spec.d.ts.map +1 -0
- package/dist/mcp/mcp.spec.js +55 -0
- package/dist/mcp/test-server.d.ts +8 -0
- package/dist/mcp/test-server.d.ts.map +1 -0
- package/dist/mcp/test-server.js +166 -0
- package/package.json +10 -4
- package/playwright.config.ts +1 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { type AuthProvider, Client, type OAuthClientProvider, type OAuthTokens } from "@modelcontextprotocol/client";
|
|
2
|
+
import type { Fixtures, Page } from "@playwright/test";
|
|
3
|
+
export type McpAuthorizationRequest = {
|
|
4
|
+
authorizationUrl: URL;
|
|
5
|
+
clientName: string;
|
|
6
|
+
redirectUrl: string;
|
|
7
|
+
};
|
|
8
|
+
export type McpBrowserAuth = {
|
|
9
|
+
page: Page;
|
|
10
|
+
/**
|
|
11
|
+
* Drives the authorization server's own pages (sign-in, consent) after the
|
|
12
|
+
* page has navigated to `authorizationUrl`. Omit it when the server redirects
|
|
13
|
+
* back without user interaction.
|
|
14
|
+
*/
|
|
15
|
+
consent?: (page: Page, request: McpAuthorizationRequest) => Promise<void>;
|
|
16
|
+
scope?: string;
|
|
17
|
+
clientName?: string;
|
|
18
|
+
};
|
|
19
|
+
export type McpAuth = {
|
|
20
|
+
browser: McpBrowserAuth;
|
|
21
|
+
} | {
|
|
22
|
+
bearer: string;
|
|
23
|
+
} | {
|
|
24
|
+
provider: AuthProvider | OAuthClientProvider;
|
|
25
|
+
};
|
|
26
|
+
export type McpSessionOptions = {
|
|
27
|
+
url: string | URL;
|
|
28
|
+
clientInfo?: {
|
|
29
|
+
name: string;
|
|
30
|
+
version: string;
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
export type McpSession = {
|
|
34
|
+
client: Client;
|
|
35
|
+
connect: (auth?: McpAuth) => Promise<Client>;
|
|
36
|
+
/** Dynamically registered OAuth client id, set once browser authorization registers a client. */
|
|
37
|
+
readonly clientId: string | undefined;
|
|
38
|
+
readonly clientName: string | undefined;
|
|
39
|
+
readonly tokens: OAuthTokens | undefined;
|
|
40
|
+
close: () => Promise<void>;
|
|
41
|
+
};
|
|
42
|
+
export declare function createMcpSession(options: McpSessionOptions): McpSession;
|
|
43
|
+
export declare const mcpFixtures: Fixtures<{
|
|
44
|
+
mcp: (options: McpSessionOptions) => McpSession;
|
|
45
|
+
}>;
|
|
46
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/mcp/index.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,KAAK,YAAY,EAEjB,MAAM,EAEN,KAAK,mBAAmB,EAExB,KAAK,WAAW,EAEjB,MAAM,8BAA8B,CAAC;AACtC,OAAO,KAAK,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAIvD,MAAM,MAAM,uBAAuB,GAAG;IACpC,gBAAgB,EAAE,GAAG,CAAC;IACtB,UAAU,EAAE,MAAM,CAAC;IACnB,WAAW,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG;IAC3B,IAAI,EAAE,IAAI,CAAC;IACX;;;;OAIG;IACH,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,uBAAuB,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1E,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,OAAO,GACf;IAAE,OAAO,EAAE,cAAc,CAAA;CAAE,GAC3B;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,GAClB;IAAE,QAAQ,EAAE,YAAY,GAAG,mBAAmB,CAAA;CAAE,CAAC;AAErD,MAAM,MAAM,iBAAiB,GAAG;IAC9B,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;IAClB,UAAU,CAAC,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;CAChD,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,CAAC,IAAI,CAAC,EAAE,OAAO,KAAK,OAAO,CAAC,MAAM,CAAC,CAAC;IAC7C,iGAAiG;IACjG,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;IACtC,QAAQ,CAAC,UAAU,EAAE,MAAM,GAAG,SAAS,CAAC;IACxC,QAAQ,CAAC,MAAM,EAAE,WAAW,GAAG,SAAS,CAAC;IACzC,KAAK,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC5B,CAAC;AA6HF,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,iBAAiB,GAAG,UAAU,CA+DvE;AAED,eAAO,MAAM,WAAW,EAAE,QAAQ,CAAC;IACjC,GAAG,EAAE,CAAC,OAAO,EAAE,iBAAiB,KAAK,UAAU,CAAC;CACjD,CAUA,CAAC"}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mcpFixtures = void 0;
|
|
4
|
+
exports.createMcpSession = createMcpSession;
|
|
5
|
+
const node_crypto_1 = require("node:crypto");
|
|
6
|
+
const node_http_1 = require("node:http");
|
|
7
|
+
const client_1 = require("@modelcontextprotocol/client");
|
|
8
|
+
const CALLBACK_PATH = "/callback";
|
|
9
|
+
const DEFAULT_CLIENT_INFO = { name: "empirical-playwright", version: "1.0.0" };
|
|
10
|
+
function createInMemoryOAuthProvider({ clientName, redirectUrl, scope, }) {
|
|
11
|
+
const state = (0, node_crypto_1.randomUUID)();
|
|
12
|
+
let clientInformation;
|
|
13
|
+
let tokens;
|
|
14
|
+
let codeVerifier = "";
|
|
15
|
+
let discoveryState;
|
|
16
|
+
let authorizationUrl;
|
|
17
|
+
const provider = {
|
|
18
|
+
redirectUrl,
|
|
19
|
+
clientMetadata: {
|
|
20
|
+
client_name: clientName,
|
|
21
|
+
redirect_uris: [redirectUrl],
|
|
22
|
+
grant_types: ["authorization_code", "refresh_token"],
|
|
23
|
+
response_types: ["code"],
|
|
24
|
+
token_endpoint_auth_method: "none",
|
|
25
|
+
...(scope ? { scope } : {}),
|
|
26
|
+
},
|
|
27
|
+
state: () => state,
|
|
28
|
+
clientInformation: () => clientInformation,
|
|
29
|
+
saveClientInformation: (information) => {
|
|
30
|
+
clientInformation = information;
|
|
31
|
+
},
|
|
32
|
+
tokens: () => tokens,
|
|
33
|
+
saveTokens: (newTokens) => {
|
|
34
|
+
tokens = newTokens;
|
|
35
|
+
},
|
|
36
|
+
saveCodeVerifier: (verifier) => {
|
|
37
|
+
codeVerifier = verifier;
|
|
38
|
+
},
|
|
39
|
+
codeVerifier: () => codeVerifier,
|
|
40
|
+
saveDiscoveryState: (state) => {
|
|
41
|
+
discoveryState = state;
|
|
42
|
+
},
|
|
43
|
+
discoveryState: () => discoveryState,
|
|
44
|
+
redirectToAuthorization: (url) => {
|
|
45
|
+
authorizationUrl = url;
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
return {
|
|
49
|
+
provider,
|
|
50
|
+
state,
|
|
51
|
+
clientId: () => clientInformation?.client_id,
|
|
52
|
+
tokens: () => tokens,
|
|
53
|
+
authorizationUrl: () => authorizationUrl,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
async function startLoopbackCallback() {
|
|
57
|
+
let resolveCallback;
|
|
58
|
+
const callback = new Promise((resolve) => {
|
|
59
|
+
resolveCallback = resolve;
|
|
60
|
+
});
|
|
61
|
+
const server = (0, node_http_1.createServer)((req, res) => {
|
|
62
|
+
const url = new URL(req.url ?? "/", redirectUrl);
|
|
63
|
+
if (url.pathname !== CALLBACK_PATH) {
|
|
64
|
+
res.writeHead(404).end();
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
res.writeHead(200, { "content-type": "text/html" });
|
|
68
|
+
res.end("<h1>MCP client callback received</h1>");
|
|
69
|
+
resolveCallback(url);
|
|
70
|
+
});
|
|
71
|
+
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
|
|
72
|
+
const { port } = server.address();
|
|
73
|
+
const redirectUrl = `http://127.0.0.1:${port}${CALLBACK_PATH}`;
|
|
74
|
+
return {
|
|
75
|
+
redirectUrl,
|
|
76
|
+
callback,
|
|
77
|
+
close: () => new Promise((resolve) => server.close(resolve)),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
async function authorizeInBrowser(serverUrl, transport, oauth, options, callback) {
|
|
81
|
+
const result = await (0, client_1.auth)(oauth.provider, {
|
|
82
|
+
serverUrl,
|
|
83
|
+
scope: options.scope,
|
|
84
|
+
});
|
|
85
|
+
if (result === "AUTHORIZED") {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const authorizationUrl = oauth.authorizationUrl();
|
|
89
|
+
if (!authorizationUrl) {
|
|
90
|
+
throw new Error("MCP authorization did not produce an authorization URL");
|
|
91
|
+
}
|
|
92
|
+
const { page } = options;
|
|
93
|
+
await page.goto(authorizationUrl.href);
|
|
94
|
+
await options.consent?.(page, {
|
|
95
|
+
authorizationUrl,
|
|
96
|
+
clientName: options.clientName,
|
|
97
|
+
redirectUrl: options.redirectUrl,
|
|
98
|
+
});
|
|
99
|
+
const params = (await callback).searchParams;
|
|
100
|
+
if (params.get("state") !== oauth.state) {
|
|
101
|
+
throw new Error("MCP authorization callback has an unexpected state");
|
|
102
|
+
}
|
|
103
|
+
const error = params.get("error");
|
|
104
|
+
if (error) {
|
|
105
|
+
throw new Error(`MCP authorization was rejected: ${error} ${params.get("error_description") ?? ""}`.trim());
|
|
106
|
+
}
|
|
107
|
+
await transport.finishAuth(params);
|
|
108
|
+
}
|
|
109
|
+
function createMcpSession(options) {
|
|
110
|
+
const serverUrl = new URL(options.url);
|
|
111
|
+
const client = new client_1.Client(options.clientInfo ?? DEFAULT_CLIENT_INFO);
|
|
112
|
+
let oauth;
|
|
113
|
+
let clientName;
|
|
114
|
+
async function connect(authOption) {
|
|
115
|
+
if (authOption && "browser" in authOption) {
|
|
116
|
+
clientName =
|
|
117
|
+
authOption.browser.clientName ??
|
|
118
|
+
`Empirical Playwright MCP ${(0, node_crypto_1.randomUUID)()}`;
|
|
119
|
+
const loopback = await startLoopbackCallback();
|
|
120
|
+
oauth = createInMemoryOAuthProvider({
|
|
121
|
+
clientName,
|
|
122
|
+
redirectUrl: loopback.redirectUrl,
|
|
123
|
+
scope: authOption.browser.scope,
|
|
124
|
+
});
|
|
125
|
+
const transport = new client_1.StreamableHTTPClientTransport(serverUrl, {
|
|
126
|
+
authProvider: oauth.provider,
|
|
127
|
+
});
|
|
128
|
+
try {
|
|
129
|
+
await authorizeInBrowser(serverUrl, transport, oauth, {
|
|
130
|
+
...authOption.browser,
|
|
131
|
+
clientName,
|
|
132
|
+
redirectUrl: loopback.redirectUrl,
|
|
133
|
+
}, loopback.callback);
|
|
134
|
+
}
|
|
135
|
+
finally {
|
|
136
|
+
await loopback.close();
|
|
137
|
+
}
|
|
138
|
+
await client.connect(transport);
|
|
139
|
+
return client;
|
|
140
|
+
}
|
|
141
|
+
const authProvider = authOption && "bearer" in authOption
|
|
142
|
+
? { token: async () => authOption.bearer }
|
|
143
|
+
: authOption?.provider;
|
|
144
|
+
await client.connect(new client_1.StreamableHTTPClientTransport(serverUrl, { authProvider }));
|
|
145
|
+
return client;
|
|
146
|
+
}
|
|
147
|
+
return {
|
|
148
|
+
client,
|
|
149
|
+
connect,
|
|
150
|
+
get clientId() {
|
|
151
|
+
return oauth?.clientId();
|
|
152
|
+
},
|
|
153
|
+
get clientName() {
|
|
154
|
+
return clientName;
|
|
155
|
+
},
|
|
156
|
+
get tokens() {
|
|
157
|
+
return oauth?.tokens();
|
|
158
|
+
},
|
|
159
|
+
close: () => client.close(),
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
exports.mcpFixtures = {
|
|
163
|
+
mcp: async ({}, use) => {
|
|
164
|
+
const sessions = [];
|
|
165
|
+
await use((options) => {
|
|
166
|
+
const session = createMcpSession(options);
|
|
167
|
+
sessions.push(session);
|
|
168
|
+
return session;
|
|
169
|
+
});
|
|
170
|
+
await Promise.all(sessions.map((session) => session.close()));
|
|
171
|
+
},
|
|
172
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp.spec.d.ts","sourceRoot":"","sources":["../../src/mcp/mcp.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const test_1 = require("@playwright/test");
|
|
4
|
+
const index_1 = require("./index");
|
|
5
|
+
const test_server_1 = require("./test-server");
|
|
6
|
+
const test = test_1.test
|
|
7
|
+
.extend(index_1.mcpFixtures)
|
|
8
|
+
.extend({
|
|
9
|
+
server: [
|
|
10
|
+
async ({}, use) => {
|
|
11
|
+
const server = await (0, test_server_1.startTestMcpServer)();
|
|
12
|
+
await use(server);
|
|
13
|
+
await server.close();
|
|
14
|
+
},
|
|
15
|
+
{ scope: "worker" },
|
|
16
|
+
],
|
|
17
|
+
});
|
|
18
|
+
test("browser authorization drives the server's consent page and connects", async ({ page, mcp, server, }) => {
|
|
19
|
+
const session = mcp({ url: server.url });
|
|
20
|
+
await session.connect({
|
|
21
|
+
browser: {
|
|
22
|
+
page,
|
|
23
|
+
consent: async (page, { authorizationUrl, clientName }) => {
|
|
24
|
+
(0, test_1.expect)(authorizationUrl.searchParams.get("code_challenge_method")).toBe("S256");
|
|
25
|
+
await (0, test_1.expect)(page.getByRole("heading", { name: `Allow ${clientName}?` })).toBeVisible();
|
|
26
|
+
await page.getByRole("button", { name: "Authorize" }).click();
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
(0, test_1.expect)(server.registeredClientIds()).toContain(session.clientId);
|
|
31
|
+
(0, test_1.expect)(session.tokens?.access_token).toBeTruthy();
|
|
32
|
+
const { tools } = await session.client.listTools();
|
|
33
|
+
(0, test_1.expect)(tools.map((tool) => tool.name)).toEqual(["whoami"]);
|
|
34
|
+
const result = await session.client.callTool({ name: "whoami" });
|
|
35
|
+
(0, test_1.expect)(result.content).toEqual([{ type: "text", text: test_server_1.TEST_USER }]);
|
|
36
|
+
});
|
|
37
|
+
test("denied consent rejects and keeps the registered client id", async ({ page, mcp, server, }) => {
|
|
38
|
+
const session = mcp({ url: server.url });
|
|
39
|
+
await (0, test_1.expect)(session.connect({
|
|
40
|
+
browser: {
|
|
41
|
+
page,
|
|
42
|
+
consent: (page) => page.getByRole("button", { name: "Deny" }).click(),
|
|
43
|
+
},
|
|
44
|
+
})).rejects.toThrow("MCP authorization was rejected: access_denied");
|
|
45
|
+
(0, test_1.expect)(server.registeredClientIds()).toContain(session.clientId);
|
|
46
|
+
});
|
|
47
|
+
test("bearer auth connects with a pre-issued token", async ({ mcp, server, }) => {
|
|
48
|
+
const session = mcp({ url: server.url });
|
|
49
|
+
await session.connect({ bearer: server.staticToken });
|
|
50
|
+
const result = await session.client.callTool({ name: "whoami" });
|
|
51
|
+
(0, test_1.expect)(result.content).toEqual([{ type: "text", text: test_server_1.TEST_USER }]);
|
|
52
|
+
});
|
|
53
|
+
test("connecting without credentials is unauthorized", async ({ mcp, server, }) => {
|
|
54
|
+
await (0, test_1.expect)(mcp({ url: server.url }).connect()).rejects.toThrow();
|
|
55
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const TEST_USER = "ada@example.com";
|
|
2
|
+
export declare function startTestMcpServer(): Promise<{
|
|
3
|
+
url: string;
|
|
4
|
+
staticToken: `${string}-${string}-${string}-${string}-${string}`;
|
|
5
|
+
registeredClientIds: () => string[];
|
|
6
|
+
close: () => Promise<void>;
|
|
7
|
+
}>;
|
|
8
|
+
//# sourceMappingURL=test-server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test-server.d.ts","sourceRoot":"","sources":["../../src/mcp/test-server.ts"],"names":[],"mappings":"AASA,eAAO,MAAM,SAAS,oBAAoB,CAAC;AA+C3C,wBAAsB,kBAAkB;;;;;GAoJvC"}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TEST_USER = void 0;
|
|
4
|
+
exports.startTestMcpServer = startTestMcpServer;
|
|
5
|
+
const node_crypto_1 = require("node:crypto");
|
|
6
|
+
const node_http_1 = require("node:http");
|
|
7
|
+
const server_1 = require("@modelcontextprotocol/server");
|
|
8
|
+
exports.TEST_USER = "ada@example.com";
|
|
9
|
+
function json(body, status = 200, headers = {}) {
|
|
10
|
+
return new Response(JSON.stringify(body), {
|
|
11
|
+
status,
|
|
12
|
+
headers: { "content-type": "application/json", ...headers },
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
function escapeHtml(value) {
|
|
16
|
+
return value.replace(/[&<>"']/g, (char) => `&#${char.charCodeAt(0)};`);
|
|
17
|
+
}
|
|
18
|
+
function s256(verifier) {
|
|
19
|
+
return (0, node_crypto_1.createHash)("sha256").update(verifier).digest("base64url");
|
|
20
|
+
}
|
|
21
|
+
async function toWebRequest(req, origin) {
|
|
22
|
+
const chunks = [];
|
|
23
|
+
for await (const chunk of req) {
|
|
24
|
+
chunks.push(chunk);
|
|
25
|
+
}
|
|
26
|
+
const headers = new Headers();
|
|
27
|
+
for (const [name, value] of Object.entries(req.headers)) {
|
|
28
|
+
if (typeof value === "string") {
|
|
29
|
+
headers.set(name, value);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return new Request(new URL(req.url ?? "/", origin), {
|
|
33
|
+
method: req.method,
|
|
34
|
+
headers,
|
|
35
|
+
body: chunks.length ? Buffer.concat(chunks) : undefined,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
async function writeWebResponse(res, response) {
|
|
39
|
+
res.writeHead(response.status, Object.fromEntries(response.headers));
|
|
40
|
+
res.end(Buffer.from(await response.arrayBuffer()));
|
|
41
|
+
}
|
|
42
|
+
async function startTestMcpServer() {
|
|
43
|
+
const clients = new Map();
|
|
44
|
+
const pendingAuthorizations = new Map();
|
|
45
|
+
const codes = new Map();
|
|
46
|
+
const accessTokens = new Set();
|
|
47
|
+
const staticToken = (0, node_crypto_1.randomUUID)();
|
|
48
|
+
accessTokens.add(staticToken);
|
|
49
|
+
const mcp = (0, server_1.createMcpHandler)(() => {
|
|
50
|
+
const server = new server_1.McpServer({ name: "test-mcp", version: "1.0.0" });
|
|
51
|
+
server.registerTool("whoami", { description: "Returns the signed-in user" }, async () => ({ content: [{ type: "text", text: exports.TEST_USER }] }));
|
|
52
|
+
return server;
|
|
53
|
+
});
|
|
54
|
+
let origin = "";
|
|
55
|
+
const resourceMetadataUrl = () => `${origin}/.well-known/oauth-protected-resource/mcp`;
|
|
56
|
+
async function handle(request) {
|
|
57
|
+
const url = new URL(request.url);
|
|
58
|
+
switch (url.pathname) {
|
|
59
|
+
case "/.well-known/oauth-protected-resource/mcp":
|
|
60
|
+
return json({
|
|
61
|
+
resource: `${origin}/mcp`,
|
|
62
|
+
authorization_servers: [origin],
|
|
63
|
+
bearer_methods_supported: ["header"],
|
|
64
|
+
});
|
|
65
|
+
case "/.well-known/oauth-authorization-server":
|
|
66
|
+
return json({
|
|
67
|
+
issuer: origin,
|
|
68
|
+
authorization_endpoint: `${origin}/authorize`,
|
|
69
|
+
token_endpoint: `${origin}/token`,
|
|
70
|
+
registration_endpoint: `${origin}/register`,
|
|
71
|
+
response_types_supported: ["code"],
|
|
72
|
+
grant_types_supported: ["authorization_code", "refresh_token"],
|
|
73
|
+
code_challenge_methods_supported: ["S256"],
|
|
74
|
+
token_endpoint_auth_methods_supported: ["none"],
|
|
75
|
+
});
|
|
76
|
+
case "/register": {
|
|
77
|
+
const metadata = (await request.json());
|
|
78
|
+
const clientId = (0, node_crypto_1.randomUUID)();
|
|
79
|
+
clients.set(clientId, metadata);
|
|
80
|
+
return json({ ...metadata, client_id: clientId }, 201);
|
|
81
|
+
}
|
|
82
|
+
case "/authorize": {
|
|
83
|
+
const clientId = url.searchParams.get("client_id") ?? "";
|
|
84
|
+
const redirectUri = url.searchParams.get("redirect_uri") ?? "";
|
|
85
|
+
const client = clients.get(clientId);
|
|
86
|
+
if (!client?.redirect_uris.includes(redirectUri)) {
|
|
87
|
+
return new Response("Unknown client", { status: 400 });
|
|
88
|
+
}
|
|
89
|
+
const authorizationId = (0, node_crypto_1.randomUUID)();
|
|
90
|
+
pendingAuthorizations.set(authorizationId, {
|
|
91
|
+
clientId,
|
|
92
|
+
redirectUri,
|
|
93
|
+
state: url.searchParams.get("state") ?? "",
|
|
94
|
+
codeChallenge: url.searchParams.get("code_challenge") ?? "",
|
|
95
|
+
});
|
|
96
|
+
return new Response(`<h1>Allow ${escapeHtml(client.client_name)}?</h1>
|
|
97
|
+
<form action="/decide" method="get">
|
|
98
|
+
<input type="hidden" name="authorization_id" value="${authorizationId}">
|
|
99
|
+
<button name="decision" value="allow">Authorize</button>
|
|
100
|
+
<button name="decision" value="deny">Deny</button>
|
|
101
|
+
</form>`, { headers: { "content-type": "text/html" } });
|
|
102
|
+
}
|
|
103
|
+
case "/decide": {
|
|
104
|
+
const pending = pendingAuthorizations.get(url.searchParams.get("authorization_id") ?? "");
|
|
105
|
+
if (!pending) {
|
|
106
|
+
return new Response("Unknown authorization", { status: 400 });
|
|
107
|
+
}
|
|
108
|
+
const redirect = new URL(pending.redirectUri);
|
|
109
|
+
redirect.searchParams.set("state", pending.state);
|
|
110
|
+
if (url.searchParams.get("decision") === "allow") {
|
|
111
|
+
const code = (0, node_crypto_1.randomUUID)();
|
|
112
|
+
codes.set(code, pending);
|
|
113
|
+
redirect.searchParams.set("code", code);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
redirect.searchParams.set("error", "access_denied");
|
|
117
|
+
}
|
|
118
|
+
return Response.redirect(redirect.href, 302);
|
|
119
|
+
}
|
|
120
|
+
case "/token": {
|
|
121
|
+
const form = new URLSearchParams(await request.text());
|
|
122
|
+
const pending = codes.get(form.get("code") ?? "");
|
|
123
|
+
codes.delete(form.get("code") ?? "");
|
|
124
|
+
if (!pending ||
|
|
125
|
+
pending.clientId !== form.get("client_id") ||
|
|
126
|
+
pending.codeChallenge !== s256(form.get("code_verifier") ?? "")) {
|
|
127
|
+
return json({ error: "invalid_grant" }, 400);
|
|
128
|
+
}
|
|
129
|
+
const accessToken = (0, node_crypto_1.randomUUID)();
|
|
130
|
+
accessTokens.add(accessToken);
|
|
131
|
+
return json({
|
|
132
|
+
access_token: accessToken,
|
|
133
|
+
token_type: "Bearer",
|
|
134
|
+
expires_in: 3600,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
case "/mcp": {
|
|
138
|
+
const token = request.headers
|
|
139
|
+
.get("authorization")
|
|
140
|
+
?.replace(/^Bearer /, "");
|
|
141
|
+
if (!token || !accessTokens.has(token)) {
|
|
142
|
+
return json({ error: "invalid_token" }, 401, {
|
|
143
|
+
"www-authenticate": `Bearer resource_metadata="${resourceMetadataUrl()}"`,
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
return mcp.fetch(request);
|
|
147
|
+
}
|
|
148
|
+
default:
|
|
149
|
+
return new Response("Not found", { status: 404 });
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const httpServer = (0, node_http_1.createServer)(async (req, res) => {
|
|
153
|
+
await writeWebResponse(res, await handle(await toWebRequest(req, origin)));
|
|
154
|
+
});
|
|
155
|
+
await new Promise((resolve) => httpServer.listen(0, "127.0.0.1", resolve));
|
|
156
|
+
origin = `http://127.0.0.1:${httpServer.address().port}`;
|
|
157
|
+
return {
|
|
158
|
+
url: `${origin}/mcp`,
|
|
159
|
+
staticToken,
|
|
160
|
+
registeredClientIds: () => [...clients.keys()],
|
|
161
|
+
close: async () => {
|
|
162
|
+
await mcp.close();
|
|
163
|
+
await new Promise((resolve) => httpServer.close(resolve));
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@empiricalrun/playwright-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.68.0",
|
|
4
4
|
"engines": {
|
|
5
5
|
"node": ">=22"
|
|
6
6
|
},
|
|
@@ -25,6 +25,10 @@
|
|
|
25
25
|
"./test-case-sync-reporter": {
|
|
26
26
|
"types": "./dist/reporter/test-case-sync-reporter.d.ts",
|
|
27
27
|
"default": "./dist/reporter/test-case-sync-reporter.js"
|
|
28
|
+
},
|
|
29
|
+
"./mcp": {
|
|
30
|
+
"types": "./dist/mcp/index.d.ts",
|
|
31
|
+
"default": "./dist/mcp/index.js"
|
|
28
32
|
}
|
|
29
33
|
},
|
|
30
34
|
"repository": {
|
|
@@ -34,6 +38,7 @@
|
|
|
34
38
|
},
|
|
35
39
|
"author": "Empirical Team <hey@empirical.run>",
|
|
36
40
|
"devDependencies": {
|
|
41
|
+
"@modelcontextprotocol/server": "2.4.0",
|
|
37
42
|
"@playwright/test": "1.64.1",
|
|
38
43
|
"@types/async-retry": "^1.4.8",
|
|
39
44
|
"@types/authenticator": "^1.1.4",
|
|
@@ -45,9 +50,10 @@
|
|
|
45
50
|
"playwright-core": "1.64.1",
|
|
46
51
|
"serve-handler": "^6.1.6",
|
|
47
52
|
"typescript": "7.0.2",
|
|
48
|
-
"@empiricalrun/shared-types": "0.25.
|
|
53
|
+
"@empiricalrun/shared-types": "0.25.4"
|
|
49
54
|
},
|
|
50
55
|
"dependencies": {
|
|
56
|
+
"@modelcontextprotocol/client": "2.4.0",
|
|
51
57
|
"async-retry": "^1.3.3",
|
|
52
58
|
"authenticator": "^1.1.5",
|
|
53
59
|
"console-log-level": "^1.4.1",
|
|
@@ -56,9 +62,9 @@
|
|
|
56
62
|
"openapi-fetch": "0.14.1",
|
|
57
63
|
"puppeteer-extra-plugin-recaptcha": "^3.6.8",
|
|
58
64
|
"ts-morph": "^28.0.0",
|
|
59
|
-
"@empiricalrun/reporter": "^0.30.3",
|
|
60
65
|
"@empiricalrun/cua": "^0.6.0",
|
|
61
|
-
"@empiricalrun/r2-uploader": "^0.11.5"
|
|
66
|
+
"@empiricalrun/r2-uploader": "^0.11.5",
|
|
67
|
+
"@empiricalrun/reporter": "^0.30.3"
|
|
62
68
|
},
|
|
63
69
|
"scripts": {
|
|
64
70
|
"dev": "pnpm gen:api-worker-openapi && tsc --build --watch",
|