@tool-bridge/server 0.27.0 → 0.28.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/dist/admin.js CHANGED
@@ -2,7 +2,7 @@
2
2
  import {
3
3
  BootstrapStateStore,
4
4
  package_default
5
- } from "./chunk-TMO3OAV5.js";
5
+ } from "./chunk-A5VV3P2S.js";
6
6
  import {
7
7
  httpsReleaseUrl,
8
8
  pluginImageSchema
@@ -228,7 +228,7 @@ var BootstrapStateStore = class {
228
228
  // package.json
229
229
  var package_default = {
230
230
  name: "@tool-bridge/server",
231
- version: "0.27.0",
231
+ version: "0.28.0",
232
232
  description: "Self-hosted Tool Bridge Node server with PostgreSQL, S3 object storage and WebSocket device channels",
233
233
  type: "module",
234
234
  license: "MIT",
@@ -11,7 +11,7 @@ import {
11
11
  BootstrapLockBusyError,
12
12
  BootstrapStateStore,
13
13
  package_default
14
- } from "./chunk-TMO3OAV5.js";
14
+ } from "./chunk-A5VV3P2S.js";
15
15
  import {
16
16
  createGuardedFetch
17
17
  } from "./chunk-NXLE6ZEK.js";
@@ -20983,7 +20983,8 @@ import { z as z7 } from "zod";
20983
20983
  var MCP_OAUTH_CLIENT_ID = "https://chatgpt.com/oauth/client.json";
20984
20984
  var MCP_OAUTH_REDIRECT_URI = "https://chatgpt.com/connector_platform_oauth_redirect";
20985
20985
  var MCP_OAUTH_SCOPE = "mcp";
20986
- var ACCESS_TOKEN_LIFETIME = 3600;
20986
+ var MCP_OAUTH_ACCESS_TOKEN_LIFETIME = 3600;
20987
+ var MCP_OAUTH_REFRESH_TOKEN_LIFETIME = 90 * 24 * 3600;
20987
20988
  var RECORD_PREFIX = "mcp-inbound:";
20988
20989
  var grantRecord = z7.object({
20989
20990
  clientId: z7.literal(MCP_OAUTH_CLIENT_ID),
@@ -21003,8 +21004,9 @@ function mcpClient() {
21003
21004
  return {
21004
21005
  id: MCP_OAUTH_CLIENT_ID,
21005
21006
  redirectUris: [MCP_OAUTH_REDIRECT_URI],
21006
- grants: ["authorization_code"],
21007
- accessTokenLifetime: ACCESS_TOKEN_LIFETIME
21007
+ grants: ["authorization_code", "refresh_token"],
21008
+ accessTokenLifetime: MCP_OAUTH_ACCESS_TOKEN_LIFETIME,
21009
+ refreshTokenLifetime: MCP_OAUTH_REFRESH_TOKEN_LIFETIME
21008
21010
  };
21009
21011
  }
21010
21012
  function isMcpScope(scope) {
@@ -21037,8 +21039,8 @@ async function activeKey(state, keyId, now) {
21037
21039
  const key = await state.get(KEY_SK_HASH + hash2);
21038
21040
  return key?.id === keyId && key.hash === hash2 && isKeyActive(key, now) ? key : null;
21039
21041
  }
21040
- async function readAccessToken(state, issuer, token, now) {
21041
- const parsed = grantRecord.safeParse(await state.get(await recordKey("token", issuer, token)));
21042
+ async function readGrant(state, kind, issuer, value, now) {
21043
+ const parsed = grantRecord.safeParse(await state.get(await recordKey(kind, issuer, value)));
21042
21044
  if (!parsed.success || !isCurrentGrant(parsed.data, issuer, now)) return null;
21043
21045
  const key = await activeKey(state, parsed.data.keyId, now);
21044
21046
  return key && { record: parsed.data, key };
@@ -21097,15 +21099,34 @@ function createMcpOAuthModel(state, issuer) {
21097
21099
  async saveToken(token, client, user) {
21098
21100
  const now = /* @__PURE__ */ new Date();
21099
21101
  const keyId = user.id;
21100
- const { accessToken, accessTokenExpiresAt } = token;
21101
- if (client.id !== MCP_OAUTH_CLIENT_ID || !isMcpScope(token.scope) || !accessToken || !accessTokenExpiresAt || !(accessTokenExpiresAt > now) || typeof keyId !== "string" || !await activeKey(state, keyId, now.toISOString())) return null;
21102
- const record3 = newGrant(issuer, keyId, accessTokenExpiresAt);
21103
- const key = await recordKey("token", issuer, accessToken);
21104
- if (!await cas(key, null, record3)) return null;
21105
- return { accessToken, accessTokenExpiresAt, client: mcpClient(), scope: [MCP_OAUTH_SCOPE], user: { id: keyId } };
21102
+ const { accessToken, accessTokenExpiresAt, refreshToken, refreshTokenExpiresAt } = token;
21103
+ if (client.id !== MCP_OAUTH_CLIENT_ID || !isMcpScope(token.scope) || !accessToken || !accessTokenExpiresAt || !(accessTokenExpiresAt > now) || !refreshToken || !refreshTokenExpiresAt || !(refreshTokenExpiresAt > now) || typeof keyId !== "string" || !await activeKey(state, keyId, now.toISOString())) return null;
21104
+ const accessKey = await recordKey("token", issuer, accessToken);
21105
+ const refreshKey = await recordKey("refresh", issuer, refreshToken);
21106
+ if (!await cas(accessKey, null, newGrant(issuer, keyId, accessTokenExpiresAt))) return null;
21107
+ let savedRefresh = false;
21108
+ try {
21109
+ savedRefresh = await cas(refreshKey, null, newGrant(issuer, keyId, refreshTokenExpiresAt));
21110
+ } catch (err) {
21111
+ await cas(accessKey, 0, null).catch(() => false);
21112
+ throw err;
21113
+ }
21114
+ if (!savedRefresh) {
21115
+ await cas(accessKey, 0, null).catch(() => false);
21116
+ return null;
21117
+ }
21118
+ return {
21119
+ accessToken,
21120
+ accessTokenExpiresAt,
21121
+ refreshToken,
21122
+ refreshTokenExpiresAt,
21123
+ client: mcpClient(),
21124
+ scope: [MCP_OAUTH_SCOPE],
21125
+ user: { id: keyId }
21126
+ };
21106
21127
  },
21107
21128
  async getAccessToken(accessToken) {
21108
- const hit = await readAccessToken(state, issuer, accessToken, (/* @__PURE__ */ new Date()).toISOString());
21129
+ const hit = await readGrant(state, "token", issuer, accessToken, (/* @__PURE__ */ new Date()).toISOString());
21109
21130
  if (!hit) return null;
21110
21131
  return {
21111
21132
  accessToken,
@@ -21115,6 +21136,22 @@ function createMcpOAuthModel(state, issuer) {
21115
21136
  user: { id: hit.key.id }
21116
21137
  };
21117
21138
  },
21139
+ async getRefreshToken(refreshToken) {
21140
+ const hit = await readGrant(state, "refresh", issuer, refreshToken, (/* @__PURE__ */ new Date()).toISOString());
21141
+ if (!hit) return null;
21142
+ return {
21143
+ refreshToken,
21144
+ refreshTokenExpiresAt: new Date(hit.record.expiresAt),
21145
+ client: mcpClient(),
21146
+ scope: [hit.record.scope],
21147
+ user: { id: hit.key.id }
21148
+ };
21149
+ },
21150
+ async revokeToken(token) {
21151
+ if (!(token.refreshTokenExpiresAt && token.refreshTokenExpiresAt.getTime() > Date.now())) return false;
21152
+ const key = await recordKey("refresh", issuer, token.refreshToken);
21153
+ return cas(key, 0, null);
21154
+ },
21118
21155
  async verifyScope(token, scope) {
21119
21156
  return isMcpScope(token.scope) && isMcpScope(scope);
21120
21157
  }
@@ -21123,7 +21160,7 @@ function createMcpOAuthModel(state, issuer) {
21123
21160
  async function identifyMcpToken(state, issuer, bearer, now) {
21124
21161
  const token = bearer?.trim().match(/^Bearer +([^\s]+)$/i)?.[1];
21125
21162
  if (!token) return null;
21126
- const hit = await readAccessToken(state, issuer, token, now);
21163
+ const hit = await readGrant(state, "token", issuer, token, now);
21127
21164
  if (!hit) return null;
21128
21165
  const { key } = hit;
21129
21166
  return {
@@ -21137,7 +21174,13 @@ async function identifyMcpToken(state, issuer, bearer, now) {
21137
21174
 
21138
21175
  // src/mcpOAuthPage.ts
21139
21176
  import { html } from "hono/html";
21177
+ function hostLabel(url3) {
21178
+ return URL.parse(url3)?.host ?? "";
21179
+ }
21140
21180
  function consentPage(action, redirectUri, invalidKey = false) {
21181
+ const gatewayHost = hostLabel(action);
21182
+ const callbackHost = hostLabel(redirectUri) || "ChatGPT";
21183
+ const describedBy = invalidKey ? "sk-error sk-note" : "sk-note";
21141
21184
  const page = html`<!doctype html>
21142
21185
  <html lang="zh-CN">
21143
21186
  <head>
@@ -21145,26 +21188,471 @@ function consentPage(action, redirectUri, invalidKey = false) {
21145
21188
  <meta name="viewport" content="width=device-width, initial-scale=1">
21146
21189
  <title>授权 ChatGPT 访问 Tool Bridge</title>
21147
21190
  <style>
21148
- :root { color-scheme: light dark; font: 100%/1.6 system-ui, sans-serif; }
21149
- main { max-width: 30rem; margin: 12vh auto; padding: 0 1.25rem; }
21150
- h1 { font-size: 1.5rem; line-height: 1.35; }
21151
- label, input, button { display: block; }
21152
- input, button { box-sizing: border-box; font: inherit; padding: .5rem .75rem; }
21153
- input { width: 100%; margin: .25rem 0 1rem; }
21154
- button { display: inline-block; cursor: pointer; }
21191
+ :root {
21192
+ color-scheme: light dark;
21193
+ --bg: #F6F6F8;
21194
+ --surface: #FFFFFF;
21195
+ --surface-subtle: #F7F7F9;
21196
+ --fg: #09090B;
21197
+ --muted: #52525B;
21198
+ --subtle: #686872;
21199
+ --border: #E4E4E8;
21200
+ --border-strong: #D4D4DC;
21201
+ --primary: #09090B;
21202
+ --primary-hover: #27272A;
21203
+ --primary-fg: #FFFFFF;
21204
+ --secondary-hover: #EFEFF3;
21205
+ --input-bg: #FFFFFF;
21206
+ --ring: #8A5A00;
21207
+ --ring-soft: rgba(138, 90, 0, 0.16);
21208
+ --destructive: #B92534;
21209
+ --destructive-soft: #FFF0F2;
21210
+ --destructive-border: rgba(185, 37, 52, 0.28);
21211
+ --destructive-ring: rgba(185, 37, 52, 0.16);
21212
+ --shadow: 0 1px 2px rgba(9, 9, 11, 0.04), 0 10px 28px -16px rgba(9, 9, 11, 0.12);
21213
+ --font-sans: 'IBM Plex Sans', -apple-system, BlinkMacSystemFont, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', ui-sans-serif, system-ui, sans-serif;
21214
+ --font-mono: 'IBM Plex Mono', ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
21215
+ }
21216
+
21217
+ @media (prefers-color-scheme: dark) {
21218
+ :root {
21219
+ --bg: #09090B;
21220
+ --surface: #101013;
21221
+ --surface-subtle: #16161A;
21222
+ --fg: #F4F4F6;
21223
+ --muted: #B4B4BC;
21224
+ --subtle: #8A8A94;
21225
+ --border: #242429;
21226
+ --border-strong: #383842;
21227
+ --primary: #F4F4F6;
21228
+ --primary-hover: #E4E4E8;
21229
+ --primary-fg: #09090B;
21230
+ --secondary-hover: #1E1E24;
21231
+ --input-bg: #09090B;
21232
+ --ring: #F5C451;
21233
+ --ring-soft: rgba(245, 196, 81, 0.2);
21234
+ --destructive: #FF9DA6;
21235
+ --destructive-soft: #381D23;
21236
+ --destructive-border: rgba(255, 157, 166, 0.3);
21237
+ --destructive-ring: rgba(255, 157, 166, 0.2);
21238
+ --shadow: 0 1px 2px rgba(0, 0, 0, 0.35), 0 18px 38px -18px rgba(0, 0, 0, 0.7);
21239
+ }
21240
+ }
21241
+
21242
+ *, *::before, *::after { box-sizing: border-box; }
21243
+
21244
+ html, body { height: 100%; }
21245
+
21246
+ body {
21247
+ margin: 0;
21248
+ min-height: 100svh;
21249
+ display: grid;
21250
+ place-items: center;
21251
+ padding: 24px 16px;
21252
+ background: var(--bg);
21253
+ color: var(--fg);
21254
+ font-family: var(--font-sans);
21255
+ font-size: 14px;
21256
+ line-height: 1.5;
21257
+ -webkit-font-smoothing: antialiased;
21258
+ text-rendering: optimizeLegibility;
21259
+ }
21260
+
21261
+ .shell {
21262
+ width: 100%;
21263
+ max-width: 432px;
21264
+ }
21265
+
21266
+ .brand-bar {
21267
+ display: flex;
21268
+ align-items: center;
21269
+ justify-content: space-between;
21270
+ gap: 12px;
21271
+ margin-bottom: 14px;
21272
+ padding: 0 2px;
21273
+ }
21274
+
21275
+ .brand {
21276
+ display: inline-flex;
21277
+ align-items: center;
21278
+ gap: 9px;
21279
+ font-size: 14px;
21280
+ font-weight: 600;
21281
+ letter-spacing: -0.01em;
21282
+ color: var(--fg);
21283
+ flex-shrink: 0;
21284
+ }
21285
+
21286
+ .brand svg {
21287
+ width: 24px;
21288
+ height: 18px;
21289
+ display: block;
21290
+ }
21291
+
21292
+ .host {
21293
+ display: inline-flex;
21294
+ align-items: center;
21295
+ gap: 5px;
21296
+ min-width: 0;
21297
+ font-family: var(--font-mono);
21298
+ font-size: 12px;
21299
+ color: var(--muted);
21300
+ }
21301
+
21302
+ .host svg {
21303
+ width: 12px;
21304
+ height: 12px;
21305
+ flex-shrink: 0;
21306
+ color: var(--subtle);
21307
+ }
21308
+
21309
+ .host span {
21310
+ overflow: hidden;
21311
+ text-overflow: ellipsis;
21312
+ white-space: nowrap;
21313
+ }
21314
+
21315
+ .card {
21316
+ background: var(--surface);
21317
+ border: 1px solid var(--border);
21318
+ border-radius: 12px;
21319
+ box-shadow: var(--shadow);
21320
+ overflow: hidden;
21321
+ }
21322
+
21323
+ .card-head {
21324
+ padding: 24px 24px 20px;
21325
+ }
21326
+
21327
+ .eyebrow {
21328
+ margin: 0 0 8px;
21329
+ font-family: var(--font-mono);
21330
+ font-size: 11px;
21331
+ font-weight: 500;
21332
+ letter-spacing: 0.06em;
21333
+ text-transform: uppercase;
21334
+ color: var(--subtle);
21335
+ }
21336
+
21337
+ h1 {
21338
+ margin: 0 0 8px;
21339
+ font-size: 20px;
21340
+ font-weight: 600;
21341
+ line-height: 1.3;
21342
+ letter-spacing: -0.02em;
21343
+ }
21344
+
21345
+ .lead {
21346
+ margin: 0;
21347
+ font-size: 13.5px;
21348
+ line-height: 1.55;
21349
+ color: var(--muted);
21350
+ }
21351
+
21352
+ .facts {
21353
+ margin: 0;
21354
+ padding: 14px 24px;
21355
+ background: var(--surface-subtle);
21356
+ border-top: 1px solid var(--border);
21357
+ border-bottom: 1px solid var(--border);
21358
+ display: grid;
21359
+ gap: 10px;
21360
+ }
21361
+
21362
+ .fact {
21363
+ display: grid;
21364
+ grid-template-columns: 76px minmax(0, 1fr);
21365
+ gap: 12px;
21366
+ align-items: baseline;
21367
+ font-size: 13px;
21368
+ line-height: 1.45;
21369
+ }
21370
+
21371
+ .fact dt {
21372
+ color: var(--subtle);
21373
+ }
21374
+
21375
+ .fact dd {
21376
+ margin: 0;
21377
+ color: var(--fg);
21378
+ min-width: 0;
21379
+ overflow-wrap: anywhere;
21380
+ }
21381
+
21382
+ .mono {
21383
+ font-family: var(--font-mono);
21384
+ font-size: 12.5px;
21385
+ }
21386
+
21387
+ .dim {
21388
+ color: var(--subtle);
21389
+ }
21390
+
21391
+ .nowrap {
21392
+ white-space: nowrap;
21393
+ }
21394
+
21395
+ .card-body {
21396
+ padding: 22px 24px 24px;
21397
+ }
21398
+
21399
+ .field-head {
21400
+ display: flex;
21401
+ align-items: baseline;
21402
+ justify-content: space-between;
21403
+ gap: 8px;
21404
+ margin-bottom: 7px;
21405
+ }
21406
+
21407
+ label {
21408
+ font-size: 13px;
21409
+ font-weight: 500;
21410
+ color: var(--fg);
21411
+ }
21412
+
21413
+ .hint {
21414
+ font-size: 12px;
21415
+ color: var(--subtle);
21416
+ }
21417
+
21418
+ input[type="password"] {
21419
+ display: block;
21420
+ width: 100%;
21421
+ height: 42px;
21422
+ padding: 0 12px;
21423
+ border-radius: 8px;
21424
+ border: 1px solid var(--border-strong);
21425
+ background: var(--input-bg);
21426
+ color: var(--fg);
21427
+ font-family: var(--font-mono);
21428
+ font-size: 14px;
21429
+ line-height: 1.4;
21430
+ outline: none;
21431
+ transition: border-color 120ms ease, box-shadow 120ms ease;
21432
+ }
21433
+
21434
+ input[type="password"]::placeholder {
21435
+ color: var(--subtle);
21436
+ opacity: 0.75;
21437
+ }
21438
+
21439
+ input[type="password"]:focus {
21440
+ border-color: var(--ring);
21441
+ box-shadow: 0 0 0 3px var(--ring-soft);
21442
+ }
21443
+
21444
+ input[type="password"][aria-invalid="true"] {
21445
+ border-color: var(--destructive);
21446
+ }
21447
+
21448
+ input[type="password"][aria-invalid="true"]:focus {
21449
+ border-color: var(--destructive);
21450
+ box-shadow: 0 0 0 3px var(--destructive-ring);
21451
+ }
21452
+
21453
+ .alert {
21454
+ margin: 10px 0 0;
21455
+ padding: 10px 12px;
21456
+ border-radius: 8px;
21457
+ border: 1px solid var(--destructive-border);
21458
+ background: var(--destructive-soft);
21459
+ color: var(--destructive);
21460
+ font-size: 12.5px;
21461
+ line-height: 1.45;
21462
+ display: flex;
21463
+ align-items: flex-start;
21464
+ gap: 8px;
21465
+ }
21466
+
21467
+ .alert svg {
21468
+ width: 15px;
21469
+ height: 15px;
21470
+ flex-shrink: 0;
21471
+ margin-top: 2px;
21472
+ }
21473
+
21474
+ .actions {
21475
+ margin-top: 18px;
21476
+ display: flex;
21477
+ flex-direction: column;
21478
+ gap: 8px;
21479
+ }
21480
+
21481
+ .btn {
21482
+ width: 100%;
21483
+ height: 40px;
21484
+ padding: 0 16px;
21485
+ border-radius: 8px;
21486
+ font-family: var(--font-sans);
21487
+ font-size: 14px;
21488
+ font-weight: 500;
21489
+ cursor: pointer;
21490
+ display: inline-flex;
21491
+ align-items: center;
21492
+ justify-content: center;
21493
+ transition: background-color 120ms ease, border-color 120ms ease, color 120ms ease, opacity 120ms ease;
21494
+ }
21495
+
21496
+ .btn:focus-visible {
21497
+ outline: 2px solid var(--ring);
21498
+ outline-offset: 2px;
21499
+ }
21500
+
21501
+ .btn-primary {
21502
+ border: 1px solid transparent;
21503
+ background: var(--primary);
21504
+ color: var(--primary-fg);
21505
+ }
21506
+
21507
+ .btn-primary:hover {
21508
+ background: var(--primary-hover);
21509
+ }
21510
+
21511
+ .btn-secondary {
21512
+ border: 1px solid var(--border);
21513
+ background: transparent;
21514
+ color: var(--muted);
21515
+ }
21516
+
21517
+ .btn-secondary:hover {
21518
+ background: var(--secondary-hover);
21519
+ color: var(--fg);
21520
+ border-color: var(--border-strong);
21521
+ }
21522
+
21523
+ .footnote {
21524
+ margin: 14px 0 0;
21525
+ font-size: 12px;
21526
+ line-height: 1.5;
21527
+ color: var(--subtle);
21528
+ text-align: center;
21529
+ }
21530
+
21531
+ @media (pointer: coarse) {
21532
+ input[type="password"] {
21533
+ height: 46px;
21534
+ font-size: 16px;
21535
+ }
21536
+
21537
+ .btn {
21538
+ height: 46px;
21539
+ }
21540
+ }
21541
+
21542
+ @media (max-width: 440px) {
21543
+ body {
21544
+ padding: 16px 12px;
21545
+ }
21546
+
21547
+ .card-head {
21548
+ padding: 20px 18px 18px;
21549
+ }
21550
+
21551
+ .facts {
21552
+ padding: 14px 18px;
21553
+ }
21554
+
21555
+ .fact {
21556
+ grid-template-columns: 68px minmax(0, 1fr);
21557
+ gap: 10px;
21558
+ }
21559
+
21560
+ .card-body {
21561
+ padding: 18px;
21562
+ }
21563
+ }
21564
+
21565
+ @media (prefers-reduced-motion: reduce) {
21566
+ *, *::before, *::after {
21567
+ transition-duration: 0.01ms !important;
21568
+ }
21569
+ }
21155
21570
  </style>
21156
21571
  </head>
21157
- <body><main>
21158
- <h1>授权 ChatGPT 访问 Tool Bridge</h1>
21159
- <p>ChatGPT 将在一小时内通过 MCP 使用这把密钥允许的工具。密钥只提交到此网关。</p>
21160
- <form method="post" action="${action}">
21161
- <label for="sk">网关访问密钥(SK)</label>
21162
- <input id="sk" name="sk" type="password" autocomplete="off" required>
21163
- ${invalidKey ? html`<p role="alert">密钥无效或已过期,请重新输入。</p>` : ""}
21164
- <button type="submit">确认授权</button>
21165
- <button type="submit" name="decision" value="deny" formnovalidate>取消</button>
21166
- </form>
21167
- </main></body></html>`;
21572
+ <body>
21573
+ <main class="shell">
21574
+ <header class="brand-bar">
21575
+ <div class="brand">
21576
+ <svg viewBox="0 0 40 30" fill="none" stroke="currentColor" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
21577
+ <path d="M7.8 11.8Q20 1.4 32.2 11.8"/>
21578
+ <path d="M8.4 14.8H12.2V13Q12.2 11.8 13.6 11.7L16.6 11.5"/>
21579
+ <path d="M31.6 14.8H27.8V13Q27.8 11.8 26.4 11.7L23.4 11.5"/>
21580
+ <path d="M20 14.9V21.1"/>
21581
+ <circle cx="5" cy="14" r="3.4"/>
21582
+ <circle cx="35" cy="14" r="3.4"/>
21583
+ <circle cx="20" cy="11.5" r="3.4"/>
21584
+ <circle cx="20" cy="24.5" r="3.4"/>
21585
+ </svg>
21586
+ <span>Tool Bridge</span>
21587
+ </div>
21588
+ ${gatewayHost ? html`<div class="host" title="${gatewayHost}">
21589
+ <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
21590
+ <rect x="3.5" y="7.25" width="9" height="6.25" rx="1.5"/>
21591
+ <path d="M5.5 7.25V5.25a2.5 2.5 0 0 1 5 0v2"/>
21592
+ </svg>
21593
+ <span>${gatewayHost}</span>
21594
+ </div>` : ""}
21595
+ </header>
21596
+
21597
+ <section class="card" aria-labelledby="consent-title">
21598
+ <div class="card-head">
21599
+ <p class="eyebrow">MCP OAuth</p>
21600
+ <h1 id="consent-title">授权 ChatGPT 访问</h1>
21601
+ <p class="lead">使用访问密钥(SK)建立长期 MCP 连接。密钥仅提交至当前网关,不会传给 ChatGPT。</p>
21602
+ </div>
21603
+
21604
+ <dl class="facts">
21605
+ <div class="fact">
21606
+ <dt>客户端</dt>
21607
+ <dd>ChatGPT <span class="mono dim">· ${callbackHost}</span></dd>
21608
+ </div>
21609
+ <div class="fact">
21610
+ <dt>访问范围</dt>
21611
+ <dd>仅 <span class="mono">/~mcp</span>,权限与所填 SK 一致</dd>
21612
+ </div>
21613
+ <div class="fact">
21614
+ <dt>续期与撤销</dt>
21615
+ <dd>自动续期(90 天闲置过期);<span class="nowrap">禁删 SK 立即失效</span></dd>
21616
+ </div>
21617
+ </dl>
21618
+
21619
+ <div class="card-body">
21620
+ <form method="post" action="${action}">
21621
+ <div class="field-head">
21622
+ <label for="sk">网关访问密钥(SK)</label>
21623
+ <span class="hint">建议使用专用受限密钥</span>
21624
+ </div>
21625
+ <input
21626
+ id="sk"
21627
+ name="sk"
21628
+ type="password"
21629
+ autocomplete="off"
21630
+ spellcheck="false"
21631
+ placeholder="tbk_…"
21632
+ required
21633
+ autofocus
21634
+ aria-invalid="${invalidKey ? "true" : "false"}"
21635
+ aria-describedby="${describedBy}"
21636
+ >
21637
+ ${invalidKey ? html`<div id="sk-error" class="alert" role="alert">
21638
+ <svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
21639
+ <circle cx="8" cy="8" r="6.25"/>
21640
+ <path d="M8 5v3.5"/>
21641
+ <circle cx="8" cy="11.25" r="0.5" fill="currentColor" stroke="none"/>
21642
+ </svg>
21643
+ <span>密钥无效、已禁用或已过期,请核对后重新输入。</span>
21644
+ </div>` : ""}
21645
+ <div class="actions">
21646
+ <button type="submit" class="btn btn-primary">确认授权</button>
21647
+ <button type="submit" name="decision" value="deny" formnovalidate class="btn btn-secondary">取消</button>
21648
+ </div>
21649
+ <p id="sk-note" class="footnote">确认后将返回 <span class="mono">${callbackHost}</span> 完成连接。</p>
21650
+ </form>
21651
+ </div>
21652
+ </section>
21653
+ </main>
21654
+ </body>
21655
+ </html>`;
21168
21656
  return new Response(page.toString(), {
21169
21657
  status: invalidKey ? 401 : 200,
21170
21658
  headers: {
@@ -21183,10 +21671,11 @@ var MCP_AUTHORIZE_PATH = "/~oauth/mcp/authorize";
21183
21671
  var TOKEN = "/~oauth/mcp/token";
21184
21672
  var FORM_TYPE = "application/x-www-form-urlencoded";
21185
21673
  var MAX_REQUEST = 8192;
21186
- var ACCESS_SECONDS = 3600;
21187
21674
  var commonFields = ["client_id", "redirect_uri", "resource", "scope"];
21188
21675
  var authorizeFields = [...commonFields, "response_type", "state", "code_challenge", "code_challenge_method"];
21189
- var tokenFields = [...commonFields, "grant_type", "code", "code_verifier"];
21676
+ var authCodeTokenFields = [...commonFields, "grant_type", "code", "code_verifier"];
21677
+ var refreshTokenFields = ["grant_type", "client_id", "refresh_token", "resource", "scope"];
21678
+ var tokenFields = [.../* @__PURE__ */ new Set([...authCodeTokenFields, ...refreshTokenFields])];
21190
21679
  var noStore = { "cache-control": "no-store", "pragma": "no-cache" };
21191
21680
  var EndpointError = class extends Error {
21192
21681
  constructor(code, status = 400) {
@@ -21310,7 +21799,7 @@ function createMcpOAuth(state, canonicalOrigin, fetchClientMetadata) {
21310
21799
  token_endpoint: issuer + TOKEN,
21311
21800
  scopes_supported: [MCP_OAUTH_SCOPE],
21312
21801
  response_types_supported: ["code"],
21313
- grant_types_supported: ["authorization_code"],
21802
+ grant_types_supported: ["authorization_code", "refresh_token"],
21314
21803
  token_endpoint_auth_methods_supported: ["none"],
21315
21804
  code_challenge_methods_supported: ["S256"],
21316
21805
  authorization_response_iss_parameter_supported: true,
@@ -21319,11 +21808,13 @@ function createMcpOAuth(state, canonicalOrigin, fetchClientMetadata) {
21319
21808
  };
21320
21809
  const oauth = new OAuth2Server({
21321
21810
  model: createMcpOAuthModel(state, issuer),
21322
- accessTokenLifetime: ACCESS_SECONDS,
21811
+ accessTokenLifetime: MCP_OAUTH_ACCESS_TOKEN_LIFETIME,
21812
+ refreshTokenLifetime: MCP_OAUTH_REFRESH_TOKEN_LIFETIME,
21323
21813
  authorizationCodeLifetime: 300,
21324
21814
  requirePKCE: true,
21325
21815
  allowEmptyState: true,
21326
- requireClientAuthentication: { authorization_code: false }
21816
+ alwaysIssueNewRefreshToken: true,
21817
+ requireClientAuthentication: { authorization_code: false, refresh_token: false }
21327
21818
  });
21328
21819
  const routes = new Hono2();
21329
21820
  routes.onError((err) => {
@@ -21368,17 +21859,41 @@ function createMcpOAuth(state, canonicalOrigin, fetchClientMetadata) {
21368
21859
  if (request2.method !== "POST") return methodError("POST");
21369
21860
  if (new URL(request2.url).search || request2.headers.has("authorization")) throw new EndpointError("invalid_request");
21370
21861
  const params = await form(request2, tokenFields);
21371
- if (params.get("grant_type") !== "authorization_code") throw new EndpointError("unsupported_grant_type");
21372
- checkBinding(params, resourceServerUrl.href);
21373
- if (!params.has("code") || !/^[A-Za-z0-9._~-]{43,128}$/.test(params.get("code_verifier") ?? "")) {
21374
- throw new EndpointError("invalid_request");
21862
+ const grantType = params.get("grant_type");
21863
+ if (!grantType) throw new EndpointError("invalid_request");
21864
+ if (grantType === "authorization_code") {
21865
+ for (const name of params.keys()) {
21866
+ if (!authCodeTokenFields.includes(name)) throw new EndpointError("invalid_request");
21867
+ }
21868
+ checkBinding(params, resourceServerUrl.href);
21869
+ if (!params.has("code") || !/^[A-Za-z0-9._~-]{43,128}$/.test(params.get("code_verifier") ?? "")) {
21870
+ throw new EndpointError("invalid_request");
21871
+ }
21872
+ } else if (grantType === "refresh_token") {
21873
+ for (const name of params.keys()) {
21874
+ if (!refreshTokenFields.includes(name)) throw new EndpointError("invalid_request");
21875
+ }
21876
+ if (params.get("client_id") !== MCP_OAUTH_CLIENT_ID) throw new EndpointError("invalid_client");
21877
+ if (params.has("resource") && params.get("resource") !== resourceServerUrl.href) {
21878
+ throw new EndpointError("invalid_target");
21879
+ }
21880
+ if ((params.get("scope") ?? MCP_OAUTH_SCOPE) !== MCP_OAUTH_SCOPE) throw new EndpointError("invalid_scope");
21881
+ if (!params.has("refresh_token")) throw new EndpointError("invalid_request");
21882
+ } else {
21883
+ throw new EndpointError("unsupported_grant_type");
21375
21884
  }
21376
21885
  const headers = { "content-type": FORM_TYPE, "content-length": String(Buffer.byteLength(params.toString())) };
21377
21886
  const token = await oauth.token(
21378
21887
  new OAuth2Server.Request({ headers, method: "POST", query: {}, body: Object.fromEntries(params) }),
21379
21888
  new OAuth2Server.Response()
21380
21889
  );
21381
- return Response.json({ access_token: token.accessToken, token_type: "Bearer", expires_in: ACCESS_SECONDS, scope: MCP_OAUTH_SCOPE }, { headers: noStore });
21890
+ return Response.json({
21891
+ access_token: token.accessToken,
21892
+ token_type: "Bearer",
21893
+ expires_in: MCP_OAUTH_ACCESS_TOKEN_LIFETIME,
21894
+ ...token.refreshToken ? { refresh_token: token.refreshToken } : {},
21895
+ scope: MCP_OAUTH_SCOPE
21896
+ }, { headers: noStore });
21382
21897
  });
21383
21898
  return {
21384
21899
  resourceMetadataUrl: getOAuthProtectedResourceMetadataUrl(resourceServerUrl),
package/dist/index.js CHANGED
@@ -19,11 +19,11 @@ import {
19
19
  createTbServer,
20
20
  probeS3ObjectStore,
21
21
  runtimeAppSettings
22
- } from "./chunk-IZZ5C42L.js";
22
+ } from "./chunk-WR4IHUDC.js";
23
23
  import "./chunk-U7PTOA3U.js";
24
24
  import {
25
25
  BootstrapStateStore
26
- } from "./chunk-TMO3OAV5.js";
26
+ } from "./chunk-A5VV3P2S.js";
27
27
  import "./chunk-NXLE6ZEK.js";
28
28
  import "./chunk-6W5WJCKR.js";
29
29
  import "./chunk-4WDQGEOC.js";
package/dist/main.js CHANGED
@@ -1,11 +1,11 @@
1
1
  #!/usr/bin/env node
2
2
  import {
3
3
  createManagedServer
4
- } from "./chunk-IZZ5C42L.js";
4
+ } from "./chunk-WR4IHUDC.js";
5
5
  import "./chunk-U7PTOA3U.js";
6
6
  import {
7
7
  package_default
8
- } from "./chunk-TMO3OAV5.js";
8
+ } from "./chunk-A5VV3P2S.js";
9
9
  import "./chunk-NXLE6ZEK.js";
10
10
  import "./chunk-6W5WJCKR.js";
11
11
  import "./chunk-4WDQGEOC.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tool-bridge/server",
3
- "version": "0.27.0",
3
+ "version": "0.28.0",
4
4
  "description": "Self-hosted Tool Bridge Node server with PostgreSQL, S3 object storage and WebSocket device channels",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -60,9 +60,9 @@
60
60
  "@types/write-file-atomic": "^4.0.3",
61
61
  "@types/proper-lockfile": "^4.1.4",
62
62
  "@types/negotiator": "^0.6.4",
63
- "@tool-bridge/core": "0.1.0",
64
63
  "@tool-bridge/app": "0.25.0",
65
- "@tool-bridge/plugins": "0.1.0"
64
+ "@tool-bridge/plugins": "0.1.0",
65
+ "@tool-bridge/core": "0.1.0"
66
66
  },
67
67
  "repository": {
68
68
  "type": "git",