teapot-coding-agent 0.37.33 → 0.37.34

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.
@@ -86,6 +86,37 @@ export class ServerAuth {
86
86
  response_types: ["code"],
87
87
  };
88
88
  }
89
+ /**
90
+ * Does a recorded redirect URI describe THIS loopback listener?
91
+ *
92
+ * The callback binds an EPHEMERAL port (`server.listen(0)`), so `redirectUrl` carries a different
93
+ * port on every attempt while the registration is bound to the one it was created with. Comparing
94
+ * them literally means "never matches", which re-registers on every flow — and re-registering
95
+ * issues a NEW client_id, which `saveClientInformation` correctly treats as invalidating the
96
+ * grant. MEASURED: Canva's store had `clientId`/`clientInformation` and NO `accessToken`/
97
+ * `refreshToken`, i.e. the authorization kept working and the token was thrown away by this loop.
98
+ *
99
+ * A loopback port is an implementation detail of the listener, not an identity of the client: the
100
+ * authorization server redirects to whatever port the request named, and RFC 8252 §7.3 explicitly
101
+ * allows a loopback redirect to use any port. So the comparison ignores it, and only the
102
+ * scheme/host/path have to agree.
103
+ */
104
+ redirectMatches(recorded) {
105
+ if (recorded === this.redirectUrl)
106
+ return true;
107
+ try {
108
+ const a = new URL(recorded);
109
+ const b = new URL(this.redirectUrl);
110
+ const loopback = (h) => h === "127.0.0.1" || h === "::1" || h === "localhost";
111
+ if (!loopback(a.hostname) || !loopback(b.hostname))
112
+ return false;
113
+ // any loopback host is interchangeable with any other, and the port is the listener's
114
+ return a.protocol === b.protocol && a.pathname === b.pathname;
115
+ }
116
+ catch {
117
+ return false; // unparseable record: cannot claim a match
118
+ }
119
+ }
89
120
  clientInformation() {
90
121
  if (!this.store.clientId)
91
122
  return undefined;
@@ -122,7 +153,7 @@ export class ServerAuth {
122
153
  */
123
154
  if (uris.length === 0)
124
155
  return stored;
125
- if (uris.includes(this.redirectUrl))
156
+ if (uris.some((u) => this.redirectMatches(String(u))))
126
157
  return stored;
127
158
  // a recorded URI that cannot match this run: re-register rather than send one it never saw
128
159
  return undefined;
@@ -138,29 +169,25 @@ export class ServerAuth {
138
169
  * True when a redirect URI was recorded and it is not the one this flow listens on — the same
139
170
  * condition `clientInformation()` acts on, factored out so `tokens()` cannot disagree with it.
140
171
  */
172
+ /**
173
+ * Is the STORED registration unusable for this run?
174
+ *
175
+ * The SAME comparison `clientInformation()` uses (`redirectMatches`), factored out so `tokens()`
176
+ * cannot disagree with it about one record. Everything below used to be re-derived here, and the
177
+ * two definitions drifted twice — once reporting stale where the other reused, once the reverse.
178
+ *
179
+ * - no recorded list → not stale. "Unknown" is not "wrong": some registration responses omit
180
+ * `redirect_uris`, and treating absence as a mismatch re-registers on every flow.
181
+ * - a loopback record → matches whatever port this run listens on (see `redirectMatches`), which
182
+ * is what stops the re-register → new client_id → grant-dropped loop.
183
+ * - anything else → stale: a record naming a different host cannot be reused.
184
+ */
141
185
  registrationIsStale() {
142
186
  const stored = this.store.clientInformation;
143
187
  const uris = Array.isArray(stored?.redirect_uris) ? stored.redirect_uris : [];
144
188
  if (uris.length === 0)
145
- return false; // unknown is not wrong: older records omit the list
146
- /**
147
- * A PLACEHOLDER redirect says NOTHING either way — so it must not be treated as stale.
148
- *
149
- * `authFor()` builds every reconnect's provider with `http://127.0.0.1/callback`, no port,
150
- * because none exists until a flow opens a listener. An earlier version called that
151
- * unconditionally stale, which made `tokens()` DROP the grant on every reconnect: a server that
152
- * had been authorized minutes earlier reported "needs a grant" again (MEASURED: Notion and
153
- * Canva both lost their access token after a restart, with `teapot.theme`-style certainty that
154
- * the authorization itself had worked).
155
- *
156
- * A placeholder can neither confirm nor refute a registration, so the honest answer is "I do not
157
- * know" — and an unknown registration keeps its grant. A real mismatch (a listener-backed URL
158
- * the record does not list) is still reported, which is the case that actually produced
159
- * `Client ID mismatch`.
160
- */
161
- if (!/:\d+\//.test(this.redirectUrl))
162
- return false;
163
- return !uris.includes(this.redirectUrl);
189
+ return false; // unknown is not wrong
190
+ return !uris.some((u) => this.redirectMatches(String(u)));
164
191
  }
165
192
  async saveClientInformation(info, _ctx) {
166
193
  const i = info;
@@ -144,6 +144,29 @@ export class McpRegistry {
144
144
  this.auths.set(name, auth);
145
145
  return auth;
146
146
  }
147
+ /**
148
+ * Try a configuration WITHOUT disturbing the saved connection, using the SAVED credentials.
149
+ *
150
+ * `test` used `connectMcp(cfg)` with no auth provider "on purpose", so an authorized server
151
+ * answered 401 and the row said "needs a grant (🔑 authorize)" while the same server was
152
+ * `● connected` with 65 tools two lines above — reported as "Notionは 🔑 authorized なのに
153
+ * Error POSTing to endpoint · needs a grant". A test that ignores the token it is testing cannot
154
+ * answer the question the button asks.
155
+ *
156
+ * The connection is throwaway (closed by the caller) and the registry entry is only touched on
157
+ * success, so a test against an unsaved edit cannot invent or corrupt an entry.
158
+ */
159
+ async testConfig(cfg) {
160
+ const auth = await this.authFor(cfg.name);
161
+ const authArg = auth ? { provider: auth } : undefined;
162
+ const conn = await connectMcp({ ...cfg, enabled: true }, authArg);
163
+ try {
164
+ return { tools: await conn.tools() };
165
+ }
166
+ finally {
167
+ await conn.close();
168
+ }
169
+ }
147
170
  /** connect one server (idempotent) and discover its tools */
148
171
  async connect(name) {
149
172
  const st = this.servers.get(name);
@@ -57,7 +57,6 @@ import path from "node:path";
57
57
  import { bus } from "../bus.js";
58
58
  import { readEvents, readEventsTail, filterByBranch } from "../log/events.js";
59
59
  import { mcp } from "../agent/mcp-registry.js";
60
- import { connectMcp } from "../agent/mcp.js";
61
60
  /** media categories the file tree can preview inline */
62
61
  const IMAGE_EXT = new Set(["png", "jpg", "jpeg", "gif", "webp", "svg", "bmp", "ico", "avif"]);
63
62
  const VIDEO_EXT = new Set(["mp4", "webm", "mov", "mkv", "avi", "m4v"]);
@@ -1141,12 +1140,16 @@ export function buildApp(master) {
1141
1140
  if (!body?.name)
1142
1141
  return c.json({ error: "a server needs a name" }, 400);
1143
1142
  try {
1144
- // no OAuth provider here on purpose: `test` answers "is this reachable and what
1145
- // does it expose", which an unauthorized server can still answer with a clear 401.
1146
- // Starting a browser flow from a test button would be a surprise.
1147
- const conn = await connectMcp({ ...body, enabled: true });
1148
- try {
1149
- const tools = await conn.tools();
1143
+ /**
1144
+ * Uses the SAVED credentials, and still never starts a browser flow.
1145
+ *
1146
+ * The distinction matters: the point of `test` is "does this server work AS CONFIGURED", so it
1147
+ * must present the grant the operator already obtained. It used to connect anonymously "on
1148
+ * purpose" and report 401 — the row then said "needs a grant" for a server that was already
1149
+ * `● connected`.
1150
+ */
1151
+ {
1152
+ const { tools } = await mcp.testConfig(body);
1150
1153
  /**
1151
1154
  * A successful test CLEARS the row's `error`.
1152
1155
  *
@@ -1164,9 +1167,6 @@ export function buildApp(master) {
1164
1167
  tools: tools.map((t) => ({ name: t.name, description: t.description })),
1165
1168
  });
1166
1169
  }
1167
- finally {
1168
- await conn.close();
1169
- }
1170
1170
  }
1171
1171
  catch (err) {
1172
1172
  const message = err.message;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "teapot-coding-agent",
3
- "version": "0.37.33",
3
+ "version": "0.37.34",
4
4
  "description": "A lightweight, always-on multi-agent harness for AI coding agents",
5
5
  "type": "module",
6
6
  "license": "AGPL-3.0-or-later",