@introspection-ai/recipes 0.13.0 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -58
- package/dist/{agent-tool.d.ts → agents.d.ts} +4 -1
- package/dist/agents.d.ts.map +1 -0
- package/dist/{agent-tool.js → agents.js} +1 -1
- package/dist/agents.js.map +1 -0
- package/dist/api/extensions.d.ts +3 -0
- package/dist/api/extensions.d.ts.map +1 -0
- package/dist/api/extensions.js +2 -0
- package/dist/api/extensions.js.map +1 -0
- package/dist/api/mcp.d.ts +4 -0
- package/dist/api/mcp.d.ts.map +1 -0
- package/dist/api/mcp.js +2 -0
- package/dist/api/mcp.js.map +1 -0
- package/dist/api/session.d.ts +3 -0
- package/dist/api/session.d.ts.map +1 -0
- package/dist/api/session.js +2 -0
- package/dist/api/session.js.map +1 -0
- package/dist/child-agent.d.ts +5 -4
- package/dist/child-agent.d.ts.map +1 -1
- package/dist/child-agent.js +26 -117
- package/dist/child-agent.js.map +1 -1
- package/dist/child-session.d.ts +23 -0
- package/dist/child-session.d.ts.map +1 -0
- package/dist/child-session.js +48 -0
- package/dist/child-session.js.map +1 -0
- package/dist/extensions.d.ts +29 -0
- package/dist/extensions.d.ts.map +1 -0
- package/dist/extensions.js +119 -0
- package/dist/extensions.js.map +1 -0
- package/dist/index.d.ts +4 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -13
- package/dist/index.js.map +1 -1
- package/dist/inspect.d.ts +38 -5
- package/dist/inspect.d.ts.map +1 -1
- package/dist/inspect.js +92 -41
- package/dist/inspect.js.map +1 -1
- package/dist/interactions.d.ts +18 -2
- package/dist/interactions.d.ts.map +1 -1
- package/dist/interactions.js +41 -10
- package/dist/interactions.js.map +1 -1
- package/dist/mcp-catalog.d.ts +1 -0
- package/dist/mcp-catalog.d.ts.map +1 -1
- package/dist/mcp-catalog.js +14 -6
- package/dist/mcp-catalog.js.map +1 -1
- package/dist/mcp-chunks/auth-command-ZLGYIEUY.js +273 -0
- package/dist/mcp-chunks/call-arguments-LU3W6D3K.js +13 -0
- package/dist/mcp-chunks/call-command-LKTKSYTH.js +612 -0
- package/dist/mcp-chunks/chunk-2U4BSDC4.js +3951 -0
- package/dist/mcp-chunks/chunk-2XL7MG7R.js +272 -0
- package/dist/mcp-chunks/chunk-3BJA27PY.js +2321 -0
- package/dist/mcp-chunks/chunk-4SQKWYFF.js +15 -0
- package/dist/mcp-chunks/chunk-5QOSCSQB.js +290 -0
- package/dist/mcp-chunks/chunk-6W5QFASN.js +77 -0
- package/dist/mcp-chunks/chunk-6XCZGU3G.js +1004 -0
- package/dist/mcp-chunks/chunk-7FHEBT5G.js +149 -0
- package/dist/mcp-chunks/chunk-7KA2VJVZ.js +114 -0
- package/dist/mcp-chunks/chunk-7UDVSOOF.js +138 -0
- package/dist/mcp-chunks/chunk-BNV3TPE4.js +66 -0
- package/dist/mcp-chunks/chunk-BTLAWHTA.js +129 -0
- package/dist/mcp-chunks/chunk-BV56DXPW.js +13 -0
- package/dist/mcp-chunks/chunk-BWHIA4PC.js +90 -0
- package/dist/mcp-chunks/chunk-BZIQOWE6.js +267 -0
- package/dist/mcp-chunks/chunk-D3MRAZ6H.js +16140 -0
- package/dist/mcp-chunks/chunk-DHBE7UBH.js +750 -0
- package/dist/mcp-chunks/chunk-E22DYOW5.js +251 -0
- package/dist/mcp-chunks/chunk-ECIVB7P4.js +139 -0
- package/dist/mcp-chunks/chunk-HKRJ6O36.js +1076 -0
- package/dist/mcp-chunks/chunk-IABRYROW.js +44 -0
- package/dist/mcp-chunks/chunk-J6LYIZXN.js +45 -0
- package/dist/mcp-chunks/chunk-L7NTQVYN.js +161 -0
- package/dist/mcp-chunks/chunk-LEZSACDE.js +141 -0
- package/dist/mcp-chunks/chunk-LKIBAYKU.js +208 -0
- package/dist/mcp-chunks/chunk-LNH5LAJD.js +152 -0
- package/dist/mcp-chunks/chunk-LQQEYD76.js +124 -0
- package/dist/mcp-chunks/chunk-LRKZP7DJ.js +47 -0
- package/dist/mcp-chunks/chunk-LS7PMWSR.js +109 -0
- package/dist/mcp-chunks/chunk-M3TPODXI.js +95 -0
- package/dist/mcp-chunks/chunk-MZ2NARG7.js +400 -0
- package/dist/mcp-chunks/chunk-N46NEFAF.js +45 -0
- package/dist/mcp-chunks/chunk-NNOEGEUZ.js +17273 -0
- package/dist/mcp-chunks/chunk-NRAJX5E4.js +69 -0
- package/dist/mcp-chunks/chunk-P3PSC54P.js +869 -0
- package/dist/mcp-chunks/chunk-PIIYJR44.js +6341 -0
- package/dist/mcp-chunks/chunk-PWYFKGKG.js +385 -0
- package/dist/mcp-chunks/chunk-SJCCBNZ4.js +45 -0
- package/dist/mcp-chunks/chunk-TATVT5IT.js +194 -0
- package/dist/mcp-chunks/chunk-TEUKFFQL.js +29 -0
- package/dist/mcp-chunks/chunk-UKYNQGNE.js +150 -0
- package/dist/mcp-chunks/chunk-UNKB4SBV.js +37 -0
- package/dist/mcp-chunks/chunk-WD6BOD24.js +138 -0
- package/dist/mcp-chunks/chunk-WQJZMCJN.js +73 -0
- package/dist/mcp-chunks/chunk-XANPGTLI.js +364 -0
- package/dist/mcp-chunks/chunk-XQK2JSW4.js +35 -0
- package/dist/mcp-chunks/chunk-YHQUTU6L.js +571 -0
- package/dist/mcp-chunks/chunk-YVZKMV5H.js +57 -0
- package/dist/mcp-chunks/cli-UV4QJZZM.js +929 -0
- package/dist/mcp-chunks/client-S7RQUG5U.js +13 -0
- package/dist/mcp-chunks/config-command-NLAGKID5.js +1101 -0
- package/dist/mcp-chunks/daemon-command-U242Z6VY.js +26 -0
- package/dist/mcp-chunks/dist-LK3SHAMX.js +6478 -0
- package/dist/mcp-chunks/emit-ts-command-VBXVXO6V.js +386 -0
- package/dist/mcp-chunks/generate-cli-runner-6CAZZA3P.js +1426 -0
- package/dist/mcp-chunks/inspect-cli-command-3ZORTOR7.js +111 -0
- package/dist/mcp-chunks/launch-YKRXHXJI.js +10 -0
- package/dist/mcp-chunks/lifecycle-XVKRHPH3.js +16 -0
- package/dist/mcp-chunks/list-command-2MEGGNFQ.js +4190 -0
- package/dist/mcp-chunks/output-utils-NKESLAJ6.js +12 -0
- package/dist/mcp-chunks/prompt-B1Yc1NPt-VGOTWJMN.js +918 -0
- package/dist/mcp-chunks/record-command-YXHTTTT5.js +17 -0
- package/dist/mcp-chunks/replay-command-25HJ6AQU.js +85 -0
- package/dist/mcp-chunks/resource-command-OO4NYF5C.js +97 -0
- package/dist/mcp-chunks/result-utils-MOHHLNM7.js +15 -0
- package/dist/mcp-chunks/runtime-5WJ3G25Y.js +33 -0
- package/dist/mcp-chunks/runtime-wrapper-RPWDIDBF.js +10 -0
- package/dist/mcp-chunks/serve-command-DHXJZMMN.js +3302 -0
- package/dist/mcp-chunks/timeouts-W2XWCCXR.js +18 -0
- package/dist/mcp-chunks/vault-command-PIYIUEZL.js +154 -0
- package/dist/mcp-daemon-client.d.ts +6 -1
- package/dist/mcp-daemon-client.d.ts.map +1 -1
- package/dist/mcp-daemon-client.js +119 -2
- package/dist/mcp-daemon-client.js.map +1 -1
- package/dist/mcp-daemon-protocol.d.ts +18 -1
- package/dist/mcp-daemon-protocol.d.ts.map +1 -1
- package/dist/mcp-daemon-protocol.js +31 -0
- package/dist/mcp-daemon-protocol.js.map +1 -1
- package/dist/mcp-daemon.js +169 -76602
- package/dist/mcp-daemon.js.map +1 -1
- package/dist/mcp-policy.d.ts +11 -0
- package/dist/mcp-policy.d.ts.map +1 -0
- package/dist/mcp-policy.js +27 -0
- package/dist/mcp-policy.js.map +1 -0
- package/dist/mcp-run-worker.js +38 -76540
- package/dist/mcp-tools.d.ts +39 -0
- package/dist/mcp-tools.d.ts.map +1 -0
- package/dist/mcp-tools.js +493 -0
- package/dist/mcp-tools.js.map +1 -0
- package/dist/mcp.d.ts +21 -9
- package/dist/mcp.d.ts.map +1 -1
- package/dist/mcp.js +101 -31
- package/dist/mcp.js.map +1 -1
- package/dist/model-binding.d.ts +39 -0
- package/dist/model-binding.d.ts.map +1 -0
- package/dist/model-binding.js +103 -0
- package/dist/model-binding.js.map +1 -0
- package/dist/pi-extension.d.ts +2 -22
- package/dist/pi-extension.d.ts.map +1 -1
- package/dist/pi-extension.js +296 -71
- package/dist/pi-extension.js.map +1 -1
- package/dist/recipe/resolve.d.ts +43 -20
- package/dist/recipe/resolve.d.ts.map +1 -1
- package/dist/recipe/resolve.js +215 -70
- package/dist/recipe/resolve.js.map +1 -1
- package/dist/recipe-agent.d.ts +24 -27
- package/dist/recipe-agent.d.ts.map +1 -1
- package/dist/recipe-agent.js +278 -230
- package/dist/recipe-agent.js.map +1 -1
- package/dist/recipe-check.d.ts +17 -0
- package/dist/recipe-check.d.ts.map +1 -0
- package/dist/recipe-check.js +258 -0
- package/dist/recipe-check.js.map +1 -0
- package/dist/recipe-extensions.d.ts.map +1 -1
- package/dist/recipe-extensions.js +0 -3
- package/dist/recipe-extensions.js.map +1 -1
- package/dist/recipe-model.d.ts +3 -1
- package/dist/recipe-model.d.ts.map +1 -1
- package/dist/recipe-model.js +15 -12
- package/dist/recipe-model.js.map +1 -1
- package/dist/recipe-package.d.ts +6 -28
- package/dist/recipe-package.d.ts.map +1 -1
- package/dist/recipe-package.js +248 -191
- package/dist/recipe-package.js.map +1 -1
- package/dist/recipe-skills.d.ts.map +1 -1
- package/dist/recipe-skills.js +42 -29
- package/dist/recipe-skills.js.map +1 -1
- package/dist/run-controller.d.ts +10 -9
- package/dist/run-controller.d.ts.map +1 -1
- package/dist/run-controller.js +28 -21
- package/dist/run-controller.js.map +1 -1
- package/dist/session.d.ts +40 -53
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +312 -173
- package/dist/session.js.map +1 -1
- package/dist/test-utils.d.ts +7 -7
- package/dist/test-utils.d.ts.map +1 -1
- package/dist/test-utils.js +32 -15
- package/dist/test-utils.js.map +1 -1
- package/docs/agent-composition.md +41 -35
- package/docs/host-api.md +201 -0
- package/docs/index.md +8 -14
- package/docs/interactions.md +12 -11
- package/docs/mcp-auth.md +6 -6
- package/docs/mcp-configuration.md +72 -24
- package/docs/pi-extension.md +39 -11
- package/docs/recipe-flow.md +9 -9
- package/docs/recipe-format.md +90 -41
- package/package.json +23 -17
- package/vendor/mcp-client/darwin-arm64/mcp-client +0 -0
- package/vendor/mcp-client/darwin-x64/mcp-client +0 -0
- package/vendor/mcp-client/linux-arm64/mcp-client +0 -0
- package/vendor/recipe-check/darwin-arm64/recipe-check +0 -0
- package/vendor/recipe-check/darwin-x64/recipe-check +0 -0
- package/vendor/recipe-check/linux-arm64/recipe-check +0 -0
- package/vendor/recipe-check/linux-x64/recipe-check +0 -0
- package/vendor/recipe-check/linux-x64-musl/recipe-check +0 -0
- package/vendor/recipe-check/win32-x64/recipe-check.exe +0 -0
- package/dist/agent-tool.d.ts.map +0 -1
- package/dist/agent-tool.js.map +0 -1
- package/dist/run.d.ts +0 -28
- package/dist/run.d.ts.map +0 -1
- package/dist/run.js +0 -84
- package/dist/run.js.map +0 -1
- package/dist/testing.d.ts +0 -25
- package/dist/testing.d.ts.map +0 -1
- package/dist/testing.js +0 -96
- package/dist/testing.js.map +0 -1
- package/docs/deployment-configuration.md +0 -65
- package/docs/migration.md +0 -70
- package/docs/python-bindings-release.md +0 -47
- package/docs/recipe-evals.md +0 -70
- package/docs/recipe-judges.md +0 -152
- package/docs/runtime-library.md +0 -171
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { createRequire as __createRequire } from "node:module"; const require = __createRequire(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
analyzeConnectionError
|
|
4
|
+
} from "./chunk-UKYNQGNE.js";
|
|
5
|
+
import {
|
|
6
|
+
auth
|
|
7
|
+
} from "./chunk-P3PSC54P.js";
|
|
8
|
+
|
|
9
|
+
// node_modules/.pnpm/mcporter@0.12.3/node_modules/mcporter/dist/runtime-oauth-support.js
|
|
10
|
+
function maybeEnableOAuth(definition, logger) {
|
|
11
|
+
if (definition.auth === "oauth" || definition.auth === "refreshable_bearer") {
|
|
12
|
+
return void 0;
|
|
13
|
+
}
|
|
14
|
+
if (definition.command.kind !== "http") {
|
|
15
|
+
return void 0;
|
|
16
|
+
}
|
|
17
|
+
logger.info(`Detected OAuth requirement for '${definition.name}'. Launching browser flow...`);
|
|
18
|
+
return {
|
|
19
|
+
...definition,
|
|
20
|
+
auth: "oauth"
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function isUnauthorizedError(error) {
|
|
24
|
+
const issue = analyzeConnectionError(error);
|
|
25
|
+
return issue.kind === "auth";
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// node_modules/.pnpm/mcporter@0.12.3/node_modules/mcporter/dist/runtime/oauth.js
|
|
29
|
+
var DEFAULT_OAUTH_CODE_TIMEOUT_MS = 3e5;
|
|
30
|
+
var OAUTH_FLOW_ERROR = /* @__PURE__ */ Symbol("oauth-flow-error");
|
|
31
|
+
var POST_AUTH_CONNECT_ERROR = /* @__PURE__ */ Symbol("post-auth-connect-error");
|
|
32
|
+
var MAX_OAUTH_ERROR_DETAIL_LENGTH = 1200;
|
|
33
|
+
var PROACTIVE_TOKEN_SKEW_SECONDS = 60;
|
|
34
|
+
var OAuthTimeoutError = class extends Error {
|
|
35
|
+
timeoutMs;
|
|
36
|
+
serverName;
|
|
37
|
+
constructor(serverName, timeoutMs) {
|
|
38
|
+
const seconds = Math.round(timeoutMs / 1e3);
|
|
39
|
+
super(`OAuth authorization for '${serverName}' timed out after ${seconds}s; aborting.`);
|
|
40
|
+
this.name = "OAuthTimeoutError";
|
|
41
|
+
this.timeoutMs = timeoutMs;
|
|
42
|
+
this.serverName = serverName;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var OAuthAuthorizationNotStartedError = class extends Error {
|
|
46
|
+
serverName;
|
|
47
|
+
constructor(serverName, cause) {
|
|
48
|
+
const causeMessage = formatOAuthErrorDetail(cause);
|
|
49
|
+
const detail = causeMessage ? ` Last error: ${causeMessage}` : "";
|
|
50
|
+
super(`OAuth authorization for '${serverName}' did not produce an authorization URL; aborting instead of waiting for a browser callback.${detail}`);
|
|
51
|
+
this.name = "OAuthAuthorizationNotStartedError";
|
|
52
|
+
this.serverName = serverName;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
function formatOAuthErrorDetail(cause) {
|
|
56
|
+
if (!(cause instanceof Error) || !cause.message) {
|
|
57
|
+
return "";
|
|
58
|
+
}
|
|
59
|
+
return truncateOAuthErrorDetail(cause.message);
|
|
60
|
+
}
|
|
61
|
+
function truncateOAuthErrorDetail(message) {
|
|
62
|
+
if (message.length <= MAX_OAUTH_ERROR_DETAIL_LENGTH) {
|
|
63
|
+
return message;
|
|
64
|
+
}
|
|
65
|
+
const truncated = message.length - MAX_OAUTH_ERROR_DETAIL_LENGTH;
|
|
66
|
+
return `${message.slice(0, MAX_OAUTH_ERROR_DETAIL_LENGTH)}... [truncated ${truncated} chars]`;
|
|
67
|
+
}
|
|
68
|
+
function markOAuthFlowError(error) {
|
|
69
|
+
return markError(error, OAUTH_FLOW_ERROR);
|
|
70
|
+
}
|
|
71
|
+
function isOAuthFlowError(error) {
|
|
72
|
+
return hasErrorMarker(error, OAUTH_FLOW_ERROR);
|
|
73
|
+
}
|
|
74
|
+
function markPostAuthConnectError(error) {
|
|
75
|
+
return markError(error, POST_AUTH_CONNECT_ERROR);
|
|
76
|
+
}
|
|
77
|
+
function isPostAuthConnectError(error) {
|
|
78
|
+
return hasErrorMarker(error, POST_AUTH_CONNECT_ERROR);
|
|
79
|
+
}
|
|
80
|
+
function markError(error, marker) {
|
|
81
|
+
if (!error || typeof error !== "object" && typeof error !== "function") {
|
|
82
|
+
return error;
|
|
83
|
+
}
|
|
84
|
+
Object.defineProperty(error, marker, {
|
|
85
|
+
value: true,
|
|
86
|
+
enumerable: false,
|
|
87
|
+
configurable: true
|
|
88
|
+
});
|
|
89
|
+
return error;
|
|
90
|
+
}
|
|
91
|
+
function hasErrorMarker(error, marker) {
|
|
92
|
+
return !!error && (typeof error === "object" || typeof error === "function") && marker in error && Boolean(error[marker]);
|
|
93
|
+
}
|
|
94
|
+
function hasUsableCachedAccessToken(tokens) {
|
|
95
|
+
if (!tokens || typeof tokens.access_token !== "string" || tokens.access_token.trim().length === 0) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
const stored = tokens;
|
|
99
|
+
const expiresAt = typeof stored.expires_at === "number" ? stored.expires_at : stored.expiresAt;
|
|
100
|
+
return typeof expiresAt === "number" && expiresAt > Math.floor(Date.now() / 1e3) + PROACTIVE_TOKEN_SKEW_SECONDS;
|
|
101
|
+
}
|
|
102
|
+
async function connectWithAuth(client, transport, session, logger, options = {}) {
|
|
103
|
+
const { serverName, maxAttempts = 3, oauthTimeoutMs = DEFAULT_OAUTH_CODE_TIMEOUT_MS, recreateTransport } = options;
|
|
104
|
+
const state = {
|
|
105
|
+
activeTransport: transport,
|
|
106
|
+
attempt: 0,
|
|
107
|
+
hasCompletedAuthFlow: false
|
|
108
|
+
};
|
|
109
|
+
while (true) {
|
|
110
|
+
try {
|
|
111
|
+
await attemptTransportConnect(client, state);
|
|
112
|
+
if (session && !state.hasCompletedAuthFlow && options.serverUrl) {
|
|
113
|
+
await completeProactiveAuthorization(state.activeTransport, session, logger, {
|
|
114
|
+
serverName,
|
|
115
|
+
oauthTimeoutMs,
|
|
116
|
+
serverUrl: options.serverUrl,
|
|
117
|
+
fetchFn: options.fetchFn
|
|
118
|
+
});
|
|
119
|
+
state.hasCompletedAuthFlow = true;
|
|
120
|
+
}
|
|
121
|
+
if (session && state.hasCompletedAuthFlow) {
|
|
122
|
+
await session.close().catch(() => {
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
return state.activeTransport;
|
|
126
|
+
} catch (error) {
|
|
127
|
+
const unauthorized = isUnauthorizedError(error);
|
|
128
|
+
if (!shouldRetryAuthorization(state, unauthorized, session)) {
|
|
129
|
+
await closeReplacementTransport(transport, state.activeTransport);
|
|
130
|
+
throw state.hasCompletedAuthFlow && !unauthorized ? markPostAuthConnectError(error) : error;
|
|
131
|
+
}
|
|
132
|
+
state.attempt += 1;
|
|
133
|
+
if (state.attempt > maxAttempts) {
|
|
134
|
+
await closeReplacementTransport(transport, state.activeTransport);
|
|
135
|
+
throw state.hasCompletedAuthFlow ? markPostAuthConnectError(error) : error;
|
|
136
|
+
}
|
|
137
|
+
if (session.hasAuthorizationRedirectStarted?.() !== false) {
|
|
138
|
+
logger.warn(`OAuth authorization required for '${serverName ?? "unknown"}'. Waiting for browser approval...`);
|
|
139
|
+
}
|
|
140
|
+
try {
|
|
141
|
+
state.activeTransport = await completeAuthorizationChallenge(state.activeTransport, session, logger, error, {
|
|
142
|
+
serverName,
|
|
143
|
+
oauthTimeoutMs,
|
|
144
|
+
recreateTransport
|
|
145
|
+
});
|
|
146
|
+
state.hasCompletedAuthFlow = true;
|
|
147
|
+
logger.info("Authorization code accepted. Retrying connection...");
|
|
148
|
+
} catch (authError) {
|
|
149
|
+
const message = authError instanceof OAuthAuthorizationNotStartedError ? "OAuth authorization could not start." : "OAuth authorization failed while waiting for callback.";
|
|
150
|
+
logger.error(message, authError);
|
|
151
|
+
await closeReplacementTransport(transport, state.activeTransport);
|
|
152
|
+
throw markOAuthFlowError(authError);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
async function attemptTransportConnect(client, state) {
|
|
158
|
+
await client.connect(state.activeTransport);
|
|
159
|
+
return state.activeTransport;
|
|
160
|
+
}
|
|
161
|
+
function shouldRetryAuthorization(_state, unauthorized, session) {
|
|
162
|
+
if (!session || !unauthorized) {
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
async function closeReplacementTransport(originalTransport, activeTransport) {
|
|
168
|
+
if (activeTransport === originalTransport) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
await activeTransport.close().catch(() => {
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
async function completeAuthorizationChallenge(transport, session, logger, connectError, options) {
|
|
175
|
+
if (session.hasAuthorizationRedirectStarted?.() === false) {
|
|
176
|
+
throw new OAuthAuthorizationNotStartedError(options.serverName ?? "unknown", connectError);
|
|
177
|
+
}
|
|
178
|
+
const code = await waitForAuthorizationCodeWithTimeout(session, logger, options.serverName, options.oauthTimeoutMs ?? DEFAULT_OAUTH_CODE_TIMEOUT_MS);
|
|
179
|
+
if (typeof transport.finishAuth !== "function") {
|
|
180
|
+
logger.warn("Transport does not support finishAuth; cannot complete OAuth flow automatically.");
|
|
181
|
+
throw connectError;
|
|
182
|
+
}
|
|
183
|
+
await transport.finishAuth(code);
|
|
184
|
+
if (!options.recreateTransport) {
|
|
185
|
+
return transport;
|
|
186
|
+
}
|
|
187
|
+
const nextTransport = await options.recreateTransport(transport);
|
|
188
|
+
await transport.close().catch(() => {
|
|
189
|
+
});
|
|
190
|
+
return nextTransport;
|
|
191
|
+
}
|
|
192
|
+
async function completeProactiveAuthorization(transport, session, logger, options) {
|
|
193
|
+
if (!options.serverUrl) {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
try {
|
|
197
|
+
const cachedTokens = await session.provider.tokens?.();
|
|
198
|
+
if (hasUsableCachedAccessToken(cachedTokens)) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
const result = await auth(session.provider, {
|
|
202
|
+
serverUrl: options.serverUrl,
|
|
203
|
+
fetchFn: options.fetchFn
|
|
204
|
+
});
|
|
205
|
+
if (result !== "REDIRECT") {
|
|
206
|
+
await session.close().catch(() => {
|
|
207
|
+
});
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
if (session.hasAuthorizationRedirectStarted?.() === false) {
|
|
211
|
+
throw new OAuthAuthorizationNotStartedError(options.serverName ?? "unknown");
|
|
212
|
+
}
|
|
213
|
+
logger.warn(`OAuth authorization required for '${options.serverName ?? "unknown"}'. Waiting for browser approval...`);
|
|
214
|
+
if (typeof transport.finishAuth !== "function") {
|
|
215
|
+
throw new Error("Transport does not support finishAuth; cannot complete OAuth flow automatically.");
|
|
216
|
+
}
|
|
217
|
+
const code = await waitForAuthorizationCodeWithTimeout(session, logger, options.serverName, options.oauthTimeoutMs ?? DEFAULT_OAUTH_CODE_TIMEOUT_MS);
|
|
218
|
+
await transport.finishAuth(code);
|
|
219
|
+
await session.close().catch(() => {
|
|
220
|
+
});
|
|
221
|
+
} catch (error) {
|
|
222
|
+
throw markOAuthFlowError(error);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
function waitForAuthorizationCodeWithTimeout(session, logger, serverName, timeoutMs = DEFAULT_OAUTH_CODE_TIMEOUT_MS) {
|
|
226
|
+
if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {
|
|
227
|
+
return session.waitForAuthorizationCode();
|
|
228
|
+
}
|
|
229
|
+
const displayName = serverName ?? "unknown";
|
|
230
|
+
return new Promise((resolve, reject) => {
|
|
231
|
+
const timer = setTimeout(() => {
|
|
232
|
+
const error = new OAuthTimeoutError(displayName, timeoutMs);
|
|
233
|
+
logger.warn(error.message);
|
|
234
|
+
reject(error);
|
|
235
|
+
}, timeoutMs);
|
|
236
|
+
session.waitForAuthorizationCode().then((code) => {
|
|
237
|
+
clearTimeout(timer);
|
|
238
|
+
resolve(code);
|
|
239
|
+
}, (error) => {
|
|
240
|
+
clearTimeout(timer);
|
|
241
|
+
reject(error);
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
function parseOAuthTimeout(raw) {
|
|
246
|
+
if (!raw) {
|
|
247
|
+
return DEFAULT_OAUTH_CODE_TIMEOUT_MS;
|
|
248
|
+
}
|
|
249
|
+
const parsed = Number.parseInt(raw, 10);
|
|
250
|
+
if (!Number.isFinite(parsed) || parsed <= 0) {
|
|
251
|
+
return DEFAULT_OAUTH_CODE_TIMEOUT_MS;
|
|
252
|
+
}
|
|
253
|
+
return parsed;
|
|
254
|
+
}
|
|
255
|
+
function resolveOAuthTimeoutFromEnv() {
|
|
256
|
+
return parseOAuthTimeout(process.env.MCPORTER_OAUTH_TIMEOUT_MS ?? process.env.MCPORTER_OAUTH_TIMEOUT);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
export {
|
|
260
|
+
maybeEnableOAuth,
|
|
261
|
+
isUnauthorizedError,
|
|
262
|
+
OAuthTimeoutError,
|
|
263
|
+
isOAuthFlowError,
|
|
264
|
+
isPostAuthConnectError,
|
|
265
|
+
connectWithAuth,
|
|
266
|
+
resolveOAuthTimeoutFromEnv
|
|
267
|
+
};
|