@livx.cc/agentx 0.99.136 → 0.99.137

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/cli.js CHANGED
@@ -14922,19 +14922,21 @@ var McpOAuth = class {
14922
14922
  const verifier = b64url(randomBytes2(32));
14923
14923
  const challenge = b64url(createHash2("sha256").update(verifier).digest());
14924
14924
  const state = b64url(randomBytes2(16));
14925
- const { code, redirectUri } = await this.captureCode(meta.authorization_endpoint, clientId, challenge, state);
14925
+ const { code, redirectUri } = await this.captureCode(meta, clientId, challenge, state);
14926
14926
  const tok = await this.exchange(meta.token_endpoint, {
14927
14927
  grant_type: "authorization_code",
14928
14928
  code,
14929
14929
  redirect_uri: redirectUri,
14930
14930
  client_id: clientId,
14931
- code_verifier: verifier
14931
+ code_verifier: verifier,
14932
+ ...meta.resource ? { resource: meta.resource } : {}
14932
14933
  });
14933
14934
  const bundle = {
14934
14935
  access_token: tok.access_token,
14935
14936
  refresh_token: tok.refresh_token,
14936
14937
  expires_at: tok.expires_in ? this.options.now() + tok.expires_in * 1e3 : void 0,
14937
14938
  client_id: clientId,
14939
+ resource: meta.resource,
14938
14940
  token_endpoint: meta.token_endpoint
14939
14941
  };
14940
14942
  const store = this.load();
@@ -14955,7 +14957,8 @@ var McpOAuth = class {
14955
14957
  const tok = await this.exchange(b.token_endpoint, {
14956
14958
  grant_type: "refresh_token",
14957
14959
  refresh_token: b.refresh_token,
14958
- client_id: b.client_id ?? ""
14960
+ client_id: b.client_id ?? "",
14961
+ ...b.resource ? { resource: b.resource } : {}
14959
14962
  });
14960
14963
  b.access_token = tok.access_token;
14961
14964
  if (tok.refresh_token) b.refresh_token = tok.refresh_token;
@@ -14971,20 +14974,42 @@ var McpOAuth = class {
14971
14974
  return !!this.load()[serverUrl];
14972
14975
  }
14973
14976
  // -- internals ----------------------------------------------------------------
14974
- /** Discover the Authorization Server metadata from the server's origin (RFC 8414 well-known endpoints). */
14977
+ /** Discover the Authorization Server: RFC 9728 Protected Resource Metadata first, then the server's origin. */
14975
14978
  async discover(serverUrl) {
14976
- const origin = new URL(serverUrl).origin;
14977
- const candidates = [`${origin}/.well-known/oauth-authorization-server`, `${origin}/.well-known/openid-configuration`];
14978
- for (const u of candidates) {
14979
+ const { origin, pathname } = new URL(serverUrl);
14980
+ const path = pathname === "/" ? "" : pathname;
14981
+ const prm = await this.firstJson(
14982
+ [`${origin}/.well-known/oauth-protected-resource${path}`, `${origin}/.well-known/oauth-protected-resource`],
14983
+ (m) => m?.authorization_servers?.length
14984
+ );
14985
+ const issuer = prm?.authorization_servers?.[0] ?? origin;
14986
+ const { origin: asOrigin, pathname: asPathname } = new URL(issuer);
14987
+ const asPath = asPathname === "/" ? "" : asPathname.replace(/\/$/, "");
14988
+ const candidates = [.../* @__PURE__ */ new Set([
14989
+ `${asOrigin}/.well-known/oauth-authorization-server${asPath}`,
14990
+ // RFC 8414 path insertion
14991
+ `${asOrigin}${asPath}/.well-known/oauth-authorization-server`,
14992
+ // path-suffix servers
14993
+ `${asOrigin}${asPath}/.well-known/openid-configuration`,
14994
+ `${asOrigin}/.well-known/oauth-authorization-server`,
14995
+ `${asOrigin}/.well-known/openid-configuration`
14996
+ ])];
14997
+ const meta = await this.firstJson(candidates, (m) => m?.authorization_endpoint && m?.token_endpoint);
14998
+ if (!meta) throw new Error(`could not discover OAuth metadata for ${serverUrl} (tried ${candidates.join(", ")})`);
14999
+ return { ...meta, resource: prm?.resource, scopes: prm?.scopes_supported };
15000
+ }
15001
+ /** First URL whose JSON body passes `valid`; a failed candidate just moves on to the next. */
15002
+ async firstJson(urls, valid) {
15003
+ for (const u of urls) {
14979
15004
  try {
14980
15005
  const r = await this.options.fetchImpl(u);
14981
15006
  if (!r.ok) continue;
14982
15007
  const m = await r.json();
14983
- if (m?.authorization_endpoint && m?.token_endpoint) return m;
15008
+ if (valid(m)) return m;
14984
15009
  } catch {
14985
15010
  }
14986
15011
  }
14987
- throw new Error(`could not discover OAuth metadata for ${serverUrl} (tried ${candidates.join(", ")})`);
15012
+ return void 0;
14988
15013
  }
14989
15014
  /** Dynamic Client Registration (RFC 7591) when no client_id was configured and the server supports it. */
14990
15015
  async registerClient(meta, serverUrl) {
@@ -15002,7 +15027,7 @@ var McpOAuth = class {
15002
15027
  return j.client_id;
15003
15028
  }
15004
15029
  /** Start a one-route loopback server, open the browser, and resolve with the returned auth code. */
15005
- captureCode(authEndpoint, clientId, challenge, state) {
15030
+ captureCode(meta, clientId, challenge, state) {
15006
15031
  return new Promise((resolve6, reject) => {
15007
15032
  const server = createServer((req, res) => {
15008
15033
  const url = new URL(req.url ?? "/", "http://127.0.0.1");
@@ -15027,14 +15052,16 @@ var McpOAuth = class {
15027
15052
  server.listen(0, "127.0.0.1", async () => {
15028
15053
  const port = server.address().port;
15029
15054
  redirectUri = `http://127.0.0.1:${port}/callback`;
15030
- const auth = new URL(authEndpoint);
15055
+ const auth = new URL(meta.authorization_endpoint);
15031
15056
  auth.searchParams.set("response_type", "code");
15032
15057
  auth.searchParams.set("client_id", clientId);
15033
15058
  auth.searchParams.set("redirect_uri", redirectUri);
15034
15059
  auth.searchParams.set("code_challenge", challenge);
15035
15060
  auth.searchParams.set("code_challenge_method", "S256");
15036
15061
  auth.searchParams.set("state", state);
15037
- if (this.options.scope) auth.searchParams.set("scope", this.options.scope);
15062
+ const scope = this.options.scope ?? meta.scopes?.join(" ");
15063
+ if (scope) auth.searchParams.set("scope", scope);
15064
+ if (meta.resource) auth.searchParams.set("resource", meta.resource);
15038
15065
  try {
15039
15066
  await this.options.openBrowser(auth.toString());
15040
15067
  } catch (e) {