@soft-boy/agentschema 0.3.10 → 0.3.11

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/oauth.js +24 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@soft-boy/agentschema",
3
- "version": "0.3.10",
3
+ "version": "0.3.11",
4
4
  "description": "Markdown based AI agent schema",
5
5
  "type": "module",
6
6
  "bin": {
package/src/oauth.js CHANGED
@@ -18,8 +18,12 @@ export async function discover(serverUrl, { fetch = globalThis.fetch } = {}) {
18
18
  const url = new URL(serverUrl);
19
19
  const path = url.pathname.replace(/\/$/, "");
20
20
 
21
- // Protected resource metadata (RFC 9728): which authorization server the MCP server trusts.
21
+ // Protected resource metadata (RFC 9728): which authorization server the MCP server trusts. The
22
+ // server can say where that is when it turns away a request with no token, and some put it where
23
+ // the well-known addresses below wouldn't find it, so its word comes first.
24
+ const hinted = await resourceMetadataHint(serverUrl, fetch);
22
25
  const resourceMeta =
26
+ (hinted && (await getJSON(hinted, fetch))) ||
23
27
  (path && (await getJSON(`${url.origin}/.well-known/oauth-protected-resource${path}`, fetch))) ||
24
28
  (await getJSON(`${url.origin}/.well-known/oauth-protected-resource`, fetch));
25
29
  const issuer = resourceMeta?.authorization_servers?.[0] ?? url.origin;
@@ -53,6 +57,25 @@ export async function discover(serverUrl, { fetch = globalThis.fetch } = {}) {
53
57
  };
54
58
  }
55
59
 
60
+ // Where the MCP server's 401 says its protected resource metadata is: the resource_metadata
61
+ // parameter of its WWW-Authenticate header. Returns null when it doesn't say.
62
+ async function resourceMetadataHint(serverUrl, fetch) {
63
+ try {
64
+ const res = await fetch(serverUrl, {
65
+ method: "POST",
66
+ headers: { "content-type": "application/json", accept: "application/json, text/event-stream" },
67
+ body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "initialize", params: { protocolVersion: "2025-06-18", capabilities: {}, clientInfo: { name: "agentschema", version: "0" } } }),
68
+ signal: AbortSignal.timeout(10_000),
69
+ });
70
+ if (res.status !== 401) return null;
71
+ const match = /\bresource_metadata\s*=\s*(?:"([^"]*)"|([^\s,]+))/i.exec(res.headers.get("www-authenticate") ?? "");
72
+ const hint = match?.[1] ?? match?.[2];
73
+ return hint && URL.canParse(hint) ? hint : null;
74
+ } catch {
75
+ return null;
76
+ }
77
+ }
78
+
56
79
  async function getJSON(url, fetch) {
57
80
  try {
58
81
  const res = await fetch(url, { headers: { accept: "application/json" } });