@carbonaitech/ottofiles-mcp 1.0.0 → 1.1.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 +161 -16
- package/dist/content.js +22 -0
- package/dist/content.js.map +1 -0
- package/dist/gateway.js +45 -17
- package/dist/gateway.js.map +1 -1
- package/dist/guard.js +39 -6
- package/dist/guard.js.map +1 -1
- package/dist/http.js +548 -0
- package/dist/http.js.map +1 -0
- package/dist/index.js +53 -225
- package/dist/index.js.map +1 -1
- package/dist/introspect.js +285 -0
- package/dist/introspect.js.map +1 -0
- package/dist/readTools.js +130 -0
- package/dist/readTools.js.map +1 -0
- package/dist/tools/addWorkspaceDocuments.js +4 -4
- package/dist/tools/addWorkspaceDocuments.js.map +1 -1
- package/dist/tools/addWorkspaceMember.js +3 -3
- package/dist/tools/addWorkspaceMember.js.map +1 -1
- package/dist/tools/createDocument.js +478 -0
- package/dist/tools/createDocument.js.map +1 -0
- package/dist/tools/createWorkspace.js +27 -9
- package/dist/tools/createWorkspace.js.map +1 -1
- package/dist/tools/deleteWorkspace.js +3 -5
- package/dist/tools/deleteWorkspace.js.map +1 -1
- package/dist/tools/documentAccessRoster.js +25 -10
- package/dist/tools/documentAccessRoster.js.map +1 -1
- package/dist/tools/editMetadata.js +2 -2
- package/dist/tools/editMetadata.js.map +1 -1
- package/dist/tools/getDocument.js +3 -3
- package/dist/tools/getDocument.js.map +1 -1
- package/dist/tools/getDocumentHistory.js +5 -5
- package/dist/tools/getDocumentHistory.js.map +1 -1
- package/dist/tools/getDocumentVersions.js +3 -3
- package/dist/tools/getDocumentVersions.js.map +1 -1
- package/dist/tools/getPolicyStats.js +4 -4
- package/dist/tools/getPolicyStats.js.map +1 -1
- package/dist/tools/groupDocuments.js +35 -14
- package/dist/tools/groupDocuments.js.map +1 -1
- package/dist/tools/guarded.js +49 -6
- package/dist/tools/guarded.js.map +1 -1
- package/dist/tools/listCompanies.js +2 -2
- package/dist/tools/listCompanies.js.map +1 -1
- package/dist/tools/listPolicies.js +3 -3
- package/dist/tools/listPolicies.js.map +1 -1
- package/dist/tools/listWorkspaces.js +18 -4
- package/dist/tools/listWorkspaces.js.map +1 -1
- package/dist/tools/lockDocument.js +1 -1
- package/dist/tools/lockDocument.js.map +1 -1
- package/dist/tools/manageAccess.js +60 -101
- package/dist/tools/manageAccess.js.map +1 -1
- package/dist/tools/myAccess.js +5 -5
- package/dist/tools/myAccess.js.map +1 -1
- package/dist/tools/policyAccessRoster.js +23 -9
- package/dist/tools/policyAccessRoster.js.map +1 -1
- package/dist/tools/reactivateWorkspace.js +2 -2
- package/dist/tools/reactivateWorkspace.js.map +1 -1
- package/dist/tools/removeWorkspaceDocument.js +3 -7
- package/dist/tools/removeWorkspaceDocument.js.map +1 -1
- package/dist/tools/removeWorkspaceMember.js +4 -4
- package/dist/tools/removeWorkspaceMember.js.map +1 -1
- package/dist/tools/rosterCounts.js +12 -0
- package/dist/tools/rosterCounts.js.map +1 -0
- package/dist/tools/searchContent.js +14 -8
- package/dist/tools/searchContent.js.map +1 -1
- package/dist/tools/searchDocuments.js +20 -14
- package/dist/tools/searchDocuments.js.map +1 -1
- package/dist/tools/setStatus.js +3 -3
- package/dist/tools/setStatus.js.map +1 -1
- package/dist/tools/shareDocument.js +20 -16
- package/dist/tools/shareDocument.js.map +1 -1
- package/dist/tools/switchCompany.js +1 -2
- package/dist/tools/switchCompany.js.map +1 -1
- package/dist/tools/unlockDocument.js +2 -2
- package/dist/tools/unlockDocument.js.map +1 -1
- package/dist/tools/unshareDocument.js +7 -7
- package/dist/tools/unshareDocument.js.map +1 -1
- package/dist/tools/uploadVersion.js +18 -10
- package/dist/tools/uploadVersion.js.map +1 -1
- package/package.json +7 -5
- package/dist/tools/deleteDocument.js +0 -98
- package/dist/tools/deleteDocument.js.map +0 -1
- package/dist/tools/restoreDocument.js +0 -101
- package/dist/tools/restoreDocument.js.map +0 -1
package/README.md
CHANGED
|
@@ -9,22 +9,153 @@ security tier and ACLs: exactly what you'd see signed into OttoFiles.
|
|
|
9
9
|
## Install
|
|
10
10
|
|
|
11
11
|
Published on public npm as `@carbonaitech/ottofiles-mcp` — no registry auth or
|
|
12
|
-
token needed to install.
|
|
12
|
+
token needed to install. It is a **local stdio server** (a Node process your AI
|
|
13
|
+
client spawns), so it works with clients that support local/stdio MCP servers:
|
|
14
|
+
**Claude Code, Claude Desktop, Cursor, VS Code, Windsurf, Codex CLI, Gemini
|
|
15
|
+
CLI**. Cloud-only clients (**claude.ai web/mobile, ChatGPT, Microsoft Copilot**)
|
|
16
|
+
can connect only to a remote HTTPS MCP server and **cannot use this package** —
|
|
17
|
+
see [Cloud clients](#cloud-clients-not-supported-by-this-package).
|
|
13
18
|
|
|
14
|
-
**
|
|
19
|
+
Requires **Node 20.3+** on your PATH (the gateway path uses `AbortSignal.any`).
|
|
20
|
+
The three `OTTOFILES_*` values below are the
|
|
21
|
+
**dev** environment and are identical for every user; pin the version
|
|
22
|
+
(`@1.1.0`) so a client always spawns a known build.
|
|
23
|
+
|
|
24
|
+
### Claude Code
|
|
15
25
|
|
|
16
26
|
```bash
|
|
17
|
-
# Claude Code
|
|
18
27
|
claude mcp add ottofiles \
|
|
19
|
-
--env OTTOFILES_ISSUER=https://
|
|
28
|
+
--env OTTOFILES_ISSUER=https://identity.dev.lukla.com \
|
|
20
29
|
--env OTTOFILES_GATEWAY_URL=https://gateways.ottofiles.dev.carbonai.ca \
|
|
21
30
|
--env OTTOFILES_CLIENT_ID=ottofiles_mcp_cli \
|
|
22
|
-
-- npx -y @carbonaitech/ottofiles-mcp
|
|
31
|
+
-- npx -y @carbonaitech/ottofiles-mcp@1.1.0
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### Claude Desktop
|
|
35
|
+
|
|
36
|
+
Claude Desktop's **"Add custom connector"** dialog only accepts a **URL** (a
|
|
37
|
+
remote server) — it can't add this local package, so don't use that dialog. Use
|
|
38
|
+
one of these two local routes instead:
|
|
39
|
+
|
|
40
|
+
**A. One-click extension (`.mcpb`) — easiest for a tester.** Download
|
|
41
|
+
`ottofiles.mcpb` (see [desktop-extension/](desktop-extension/)) and double-click
|
|
42
|
+
it; Claude Desktop opens an install dialog, you click **Install**, and it
|
|
43
|
+
appears under **Settings → Extensions**. The dev URLs are baked in — nothing to
|
|
44
|
+
type.
|
|
45
|
+
|
|
46
|
+
**B. Config file.** Edit `claude_desktop_config.json` (Settings → Developer →
|
|
47
|
+
Edit Config):
|
|
48
|
+
|
|
49
|
+
```json
|
|
50
|
+
{
|
|
51
|
+
"mcpServers": {
|
|
52
|
+
"ottofiles": {
|
|
53
|
+
"command": "npx",
|
|
54
|
+
"args": ["-y", "@carbonaitech/ottofiles-mcp@1.1.0"],
|
|
55
|
+
"env": {
|
|
56
|
+
"OTTOFILES_ISSUER": "https://identity.dev.lukla.com",
|
|
57
|
+
"OTTOFILES_GATEWAY_URL": "https://gateways.ottofiles.dev.carbonai.ca",
|
|
58
|
+
"OTTOFILES_CLIENT_ID": "ottofiles_mcp_cli"
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
23
63
|
```
|
|
24
64
|
|
|
25
|
-
On
|
|
26
|
-
|
|
27
|
-
|
|
65
|
+
On **Windows**, if `npx` isn't found, set `"command": "npx.cmd"` (or give the
|
|
66
|
+
full path to `npx.cmd`). Node must be installed and on the PATH of the account
|
|
67
|
+
that runs Claude Desktop. Restart Claude Desktop after editing.
|
|
68
|
+
|
|
69
|
+
See [desktop-extension/README.md](desktop-extension/README.md) for the full
|
|
70
|
+
Desktop guide and how the `.mcpb` is built and distributed.
|
|
71
|
+
|
|
72
|
+
### Cursor / VS Code / Windsurf
|
|
73
|
+
|
|
74
|
+
Add an entry with the same shape to the client's MCP config (`.cursor/mcp.json`,
|
|
75
|
+
VS Code's `mcp.json`, or Windsurf's `mcp_config.json`):
|
|
76
|
+
|
|
77
|
+
```json
|
|
78
|
+
{
|
|
79
|
+
"mcpServers": {
|
|
80
|
+
"ottofiles": {
|
|
81
|
+
"command": "npx",
|
|
82
|
+
"args": ["-y", "@carbonaitech/ottofiles-mcp@1.1.0"],
|
|
83
|
+
"env": {
|
|
84
|
+
"OTTOFILES_ISSUER": "https://identity.dev.lukla.com",
|
|
85
|
+
"OTTOFILES_GATEWAY_URL": "https://gateways.ottofiles.dev.carbonai.ca",
|
|
86
|
+
"OTTOFILES_CLIENT_ID": "ottofiles_mcp_cli"
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Codex CLI / Gemini CLI
|
|
94
|
+
|
|
95
|
+
Both take a stdio MCP server as `command` + `args` + `env` in their own config
|
|
96
|
+
(Codex: `~/.codex/config.toml` `[mcp_servers.ottofiles]`; Gemini:
|
|
97
|
+
`~/.gemini/settings.json` `mcpServers`). Use `npx -y
|
|
98
|
+
@carbonaitech/ottofiles-mcp@1.1.0` as the command with the three `OTTOFILES_*`
|
|
99
|
+
env values above.
|
|
100
|
+
|
|
101
|
+
### Cloud clients (not supported by this package)
|
|
102
|
+
|
|
103
|
+
**claude.ai (web + mobile), ChatGPT, and Microsoft Copilot** can only add a
|
|
104
|
+
**remote HTTPS** MCP server with OAuth; they cannot spawn a local stdio process.
|
|
105
|
+
This npm package is stdio-only, so those clients install it only via the hosted
|
|
106
|
+
transport below.
|
|
107
|
+
|
|
108
|
+
### Hosted mode (preview)
|
|
109
|
+
|
|
110
|
+
A hosted **Streamable HTTP** transport runs the same server for cloud clients at
|
|
111
|
+
`https://mcp.ottofiles.dev.carbonai.ca/mcp` (dev). It exposes the **read-only
|
|
112
|
+
tools only** — the write tools stay on stdio for now, because their preview-token
|
|
113
|
+
key is per-process and can't survive multiple stateless replicas. Add that URL as
|
|
114
|
+
a remote MCP server; the client runs OAuth against `https://identity.dev.lukla.com`
|
|
115
|
+
(the server advertises Protected Resource Metadata and requires the
|
|
116
|
+
`lukla_mcp_api` scope). Each request carries your bearer, which the server
|
|
117
|
+
introspects for audience and then forwards to the gateway, so results are scoped
|
|
118
|
+
to your own tier and ACLs exactly as on stdio.
|
|
119
|
+
|
|
120
|
+
Run it locally with `npm run start:http` (or the `ottofiles-mcp-http` bin); it
|
|
121
|
+
needs `OTTOFILES_INTROSPECTION_CLIENT_ID` / `OTTOFILES_INTROSPECTION_CLIENT_SECRET`
|
|
122
|
+
to validate incoming tokens, and listens on `PORT` (default 8080) with a
|
|
123
|
+
`/healthz` probe. The POST body is capped at 256 KB (`OTTOFILES_MAX_BODY_BYTES`);
|
|
124
|
+
JSON-RPC batches (a top-level array) are rejected 400, and each request is
|
|
125
|
+
admission-controlled by a **two-tier** limiter keyed per user by the token's
|
|
126
|
+
`sub` (so a user's reconnect/refresh tokens share one budget). Tier 1 is a
|
|
127
|
+
per-user flood token bucket over all message types, checked before the body is
|
|
128
|
+
read (`OTTOFILES_FLOOD_RPS` 20 / `OTTOFILES_FLOOD_BURST` 40 → 429). Tier 2
|
|
129
|
+
applies only to `tools/call`: a global in-flight ceiling (`OTTOFILES_MAX_INFLIGHT`
|
|
130
|
+
24 → 503), a per-user in-flight cap (`OTTOFILES_USER_MAX_INFLIGHT` 6) and a
|
|
131
|
+
per-user token bucket (`OTTOFILES_USER_RPS` 10 / `OTTOFILES_USER_BURST` 20 → 429);
|
|
132
|
+
all with `Retry-After`. Concurrent **live** introspections (cache misses) are
|
|
133
|
+
bounded by a semaphore inside the introspector (`OTTOFILES_MAX_INTROSPECTIONS`
|
|
134
|
+
16); a cache **hit** takes no slot, so a junk-bearer flood can't turn away a
|
|
135
|
+
signed-in user whose token is cached. On saturation a miss waits ~1s for a slot,
|
|
136
|
+
then → 503 `introspection_busy`. Under a sustained junk-bearer flood a **first**
|
|
137
|
+
login may get a 503 `introspection_busy` (`Retry-After: 1`) and a signed-in user
|
|
138
|
+
may see at most one 503 per ~45s cache expiry — acceptable and self-recovering.
|
|
139
|
+
A client disconnect cancels the in-flight
|
|
140
|
+
gateway work for a **read** tool call (the tool-call abort signal is forwarded to
|
|
141
|
+
the gateway fetch) and releases the slot. The `/mcp` response is byte-capped at
|
|
142
|
+
1 MB (`OTTOFILES_MAX_MCP_RESPONSE_BYTES`), a backstop well above any legitimate
|
|
143
|
+
result. The HTTP server sets `headersTimeout` 20s and `requestTimeout` 120s. The
|
|
144
|
+
container (`Dockerfile`) deploys to dev AKS via `.helm-dev/`. Write tools are
|
|
145
|
+
stdio-only and run to completion (not exposed over HTTP); a client cancel never
|
|
146
|
+
tears a write midway.
|
|
147
|
+
|
|
148
|
+
### Signing in (loopback OAuth)
|
|
149
|
+
|
|
150
|
+
On the **first tool call** (not when the client connects), the server opens your
|
|
151
|
+
browser to sign in with your own OttoFiles account; the token (with a refresh
|
|
152
|
+
token) is cached under `~/.ottofiles-mcp/` and stays valid ~7 days. Sign-in uses
|
|
153
|
+
a loopback redirect (`http://127.0.0.1:8765-8767/callback`, RFC 8252), so the
|
|
154
|
+
server and your browser must be on the **same machine** — a remote/SSH session
|
|
155
|
+
with no local browser can't complete the interactive login. Set only
|
|
156
|
+
`OTTOFILES_CLIENT_ID` (the public PKCE client `ottofiles_mcp_cli`); there is no
|
|
157
|
+
secret to paste. Everything you see is scoped to your own security tier and
|
|
158
|
+
ACLs, exactly like a web session.
|
|
28
159
|
|
|
29
160
|
## Tools
|
|
30
161
|
|
|
@@ -33,14 +164,14 @@ All read-only except `switch_company` and the write tools below, all scoped to t
|
|
|
33
164
|
| Tool | Wraps | Purpose |
|
|
34
165
|
|------|-------|---------|
|
|
35
166
|
| `search_documents` | `POST /api/search/documents` | Find documents by text, number, type, status, policy, tier, or date — paged and sorted. |
|
|
36
|
-
| `group_documents` | `POST /api/search/documents/group` | Per-bucket counts + last-modified over the whole tier-visible corpus (the "group summary"). |
|
|
167
|
+
| `group_documents` | `POST /api/search/documents/group` | Per-bucket counts + last-modified over the whole tier-visible corpus (the "group summary"). `statusCodeId` buckets also carry `policyId`/`policyName`. Capped at 200 buckets (`truncated` flag, real `total`). |
|
|
37
168
|
| `list_policies` | `GET /api/policies` | List accessible policies with code, name, state, and document count. |
|
|
38
169
|
| `get_policy_stats` | `GET /api/policies/{id}/stats` | Aggregate a policy: document count, storage, counts by type/status, top contributors. |
|
|
39
|
-
| `policy_access_roster` | `GET /api/policies/{id}/access-roster` | Who can access a policy and who is denied, with effective tier
|
|
170
|
+
| `policy_access_roster` | `GET /api/policies/{id}/access-roster` | Administrator only. Who can access a policy and who is denied, with each user's effective tier. Counts come on page 1 only. |
|
|
40
171
|
| `get_document` | `GET /documents/{id}` | Full detail for one document: status, version, file metadata, anchors, lock state. |
|
|
41
172
|
| `get_document_versions` | `GET /documents/{id}/versions` | Every version — increment, label, file, upload status, who and when. |
|
|
42
|
-
| `document_access_roster` | `GET /api/documents/{id}/access-roster` | Who can access a document and who
|
|
43
|
-
| `list_workspaces` | `GET /api/workspaces` | External share spaces — title, document and member counts, expiry state. |
|
|
173
|
+
| `document_access_roster` | `GET /api/documents/{id}/access-roster` | Who can access a document and who holds an explicit grant. Deny flags, the denied count, granted-by and tiers are shown to administrators only. Counts come on page 1 only. |
|
|
174
|
+
| `list_workspaces` | `GET /api/workspaces` | External share spaces — title, document and member counts, expiry state. Capped at 200 (`truncated` flag, real `total`). `includeRevoked`/`includeExpired` only widen the set. |
|
|
44
175
|
| `search_content` | `POST /api/search/documents` (`ContentSearch: true`) | Search inside extracted file-body text, with a snippet and `matchedInContent` flag per hit. |
|
|
45
176
|
| `get_document_history` | `GET /audits/streams/scope` (`Type=Document`) | A document's audit trail — who did what and when, with before/after values. Access-checked via `GET /documents/{id}`. |
|
|
46
177
|
| `my_access` | `GET /profile`, `GET /api/tiers/users/{id}`, `GET /api/tiers/users/{id}/policy-overrides`, `GET /api/policies` | The signed-in user's roles, security tier, policy tier overrides, and visible policies. |
|
|
@@ -49,13 +180,16 @@ All read-only except `switch_company` and the write tools below, all scoped to t
|
|
|
49
180
|
|
|
50
181
|
## Write tools
|
|
51
182
|
|
|
52
|
-
`set_status`, `edit_metadata`, `manage_access`, `upload_version`, `lock_document`, `unlock_document`, `share_document`, `unshare_document`, `create_workspace`, `delete_workspace`, `reactivate_workspace`, `add_workspace_member`, `remove_workspace_member`, `add_workspace_documents` and `remove_workspace_document` change data. All are interactive-OAuth only.
|
|
183
|
+
`create_document`, `set_status`, `edit_metadata`, `manage_access`, `upload_version`, `lock_document`, `unlock_document`, `share_document`, `unshare_document`, `create_workspace`, `delete_workspace`, `reactivate_workspace`, `add_workspace_member`, `remove_workspace_member`, `add_workspace_documents` and `remove_workspace_document` change data. All are interactive-OAuth only.
|
|
184
|
+
|
|
185
|
+
`create_document` creates a brand-new document in an explicit `policyId` (the policy must be Published; drafts are refused) and uploads a file as its first version. Every number-forming anchor (an anchor whose name is in the policy's document-number pattern) is required; tier and status default to the policy's defaults. The preview renders the expected document number before anything is written.
|
|
53
186
|
|
|
54
187
|
**Do NOT allowlist or auto-approve the write tools in your MCP client.** Leave them on per-call approval. The preview token and confirmation prompts below cannot create a human gate that the client has approved away; an agent that controls both the preview and the confirm call (for example through prompt injection in a document it read) can still satisfy the token.
|
|
55
188
|
|
|
56
189
|
- **Preview, then confirm.** Every write tool previews first (`confirm` omitted or `false`; nothing changes). The preview returns a `previewToken`. `confirm:true` is refused unless it carries a valid, unexpired `previewToken` from a preview with identical effect-defining arguments. The token is an HMAC signed with a per-process random key (not persisted; restarting the server invalidates outstanding tokens) and lives 10 minutes (`OTTOFILES_PREVIEW_TTL_SECONDS`, clamped to 30-3600).
|
|
57
190
|
- **External disclosure.** When a tool would expose documents to someone outside the company (`create_workspace`, `add_workspace_member`, `add_workspace_documents`, `reactivate_workspace`), the client is asked via MCP elicitation, naming the recipients. If the client does not support elicitation, the call is refused unless `confirmedExternalEmails` contains exactly the external emails from the preview. `manage_access` deny and revoke also ask via elicitation when available.
|
|
58
|
-
- **
|
|
191
|
+
- **Document access.** `manage_access` can only revoke, deny or undeny (Administrator role; revoke runs through `POST /api/permissions/bulk/assign` with `grant:false`, deny/undeny through `POST`/`DELETE /api/permissions/deny`). It cannot grant: grants come only from document shares and workspaces, so use `share_document` to give someone access.
|
|
192
|
+
- **Local file uploads.** `upload_version` and `create_document` read a file from this machine and are **disabled by default**. Set `OTTOFILES_UPLOAD_ROOTS` to the absolute directories they may read from (separated by `:` on Linux/macOS, `;` on Windows, or commas). Paths are resolved through symlinks and must sit under one of the roots; anything else is refused without revealing whether it exists.
|
|
59
193
|
- **Workspaces.** `expiresAt` must be within 90 days; `message` is capped at 500 characters, titles at 200, member names at 100. All three are sent verbatim in the invitation email and shown in the preview.
|
|
60
194
|
- Duplicate-submission guards are per process and are lost when the server restarts.
|
|
61
195
|
|
|
@@ -102,8 +236,19 @@ npm run inspect # Inspector UI over the built server
|
|
|
102
236
|
```
|
|
103
237
|
|
|
104
238
|
Config is read from the environment, or from a `.env` in the package root (see
|
|
105
|
-
`.env.example`) — never from the current working directory.
|
|
106
|
-
|
|
239
|
+
`.env.example`) — never from the current working directory. Explicitly passed
|
|
240
|
+
environment variables always win; `.env` only fills the gaps (`src/config.ts`
|
|
241
|
+
skips any key already set). `provision/` holds the credential scripts and their
|
|
242
|
+
own README.
|
|
243
|
+
|
|
244
|
+
> **Clone-only gotcha.** Auth mode is chosen by whether a client **secret** is
|
|
245
|
+
> present (`OTTOFILES_CLIENT_SECRET` ⇒ headless `client_credentials`; absent ⇒
|
|
246
|
+
> interactive browser login). If your package-root `.env` carries a
|
|
247
|
+
> `OTTOFILES_CLIENT_SECRET`, it fills that gap even when you pass the interactive
|
|
248
|
+
> dev URLs on the command line, so the server comes up in `client_credentials`
|
|
249
|
+
> mode. To force interactive from a clone, set `OTTOFILES_AUTH_MODE=interactive`
|
|
250
|
+
> (or remove the secret from `.env`). This never affects `npx`/extension
|
|
251
|
+
> installs — they have no `.env`.
|
|
107
252
|
|
|
108
253
|
## Roadmap
|
|
109
254
|
|
package/dist/content.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { GatewayError } from "./gateway.js";
|
|
2
|
+
export function jsonContent(data) {
|
|
3
|
+
return {
|
|
4
|
+
content: [{ type: "text", text: JSON.stringify(data, null, 2) }],
|
|
5
|
+
};
|
|
6
|
+
}
|
|
7
|
+
export function errorContent(err) {
|
|
8
|
+
let text;
|
|
9
|
+
if (err instanceof GatewayError) {
|
|
10
|
+
text =
|
|
11
|
+
`OttoFiles request failed (HTTP ${err.status}).\n${err.message}` +
|
|
12
|
+
(err.body ? `\nResponse: ${err.body.slice(0, 800)}` : "");
|
|
13
|
+
}
|
|
14
|
+
else if (err instanceof Error) {
|
|
15
|
+
text = err.message;
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
text = String(err);
|
|
19
|
+
}
|
|
20
|
+
return { isError: true, content: [{ type: "text", text }] };
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=content.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"content.js","sourceRoot":"","sources":["../src/content.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAE5C,MAAM,UAAU,WAAW,CAAC,IAAa;IACvC,OAAO;QACL,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAe,EAAE,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC;KAC1E,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,GAAY;IACvC,IAAI,IAAY,CAAC;IACjB,IAAI,GAAG,YAAY,YAAY,EAAE,CAAC;QAChC,IAAI;YACF,kCAAkC,GAAG,CAAC,MAAM,OAAO,GAAG,CAAC,OAAO,EAAE;gBAChE,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,eAAe,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IAC9D,CAAC;SAAM,IAAI,GAAG,YAAY,KAAK,EAAE,CAAC;QAChC,IAAI,GAAG,GAAG,CAAC,OAAO,CAAC;IACrB,CAAC;SAAM,CAAC;QACN,IAAI,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;IACrB,CAAC;IACD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAe,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC;AACvE,CAAC"}
|
package/dist/gateway.js
CHANGED
|
@@ -12,6 +12,11 @@ export class GatewayError extends Error {
|
|
|
12
12
|
* Thin authed HTTP client for the OttoFiles gateway. Attaches the bearer token
|
|
13
13
|
* and retries exactly once on a 401 with a freshly-minted token (covers a token
|
|
14
14
|
* that expired or was revoked between cache and call).
|
|
15
|
+
*
|
|
16
|
+
* A call can carry an `AbortSignal` (from a read tool's `extra.signal`), combined
|
|
17
|
+
* with the per-request timeout so a client disconnect cancels the in-flight gateway
|
|
18
|
+
* fetch instead of leaving it to run. Write tools pass no signal (they run to
|
|
19
|
+
* completion), so cancelling a read is harmless while a write is never torn midway.
|
|
15
20
|
*/
|
|
16
21
|
export class GatewayClient {
|
|
17
22
|
config;
|
|
@@ -29,21 +34,21 @@ export class GatewayClient {
|
|
|
29
34
|
this.config = config;
|
|
30
35
|
this.tokens = tokens;
|
|
31
36
|
}
|
|
32
|
-
async postJson(path, payload) {
|
|
33
|
-
return this.gate(() => this.request("POST", path, payload));
|
|
37
|
+
async postJson(path, payload, signal) {
|
|
38
|
+
return this.gate(() => this.request("POST", path, payload, false, 0, signal));
|
|
34
39
|
}
|
|
35
|
-
async putJson(path, payload) {
|
|
36
|
-
return this.gate(() => this.request("PUT", path, payload));
|
|
40
|
+
async putJson(path, payload, signal) {
|
|
41
|
+
return this.gate(() => this.request("PUT", path, payload, false, 0, signal));
|
|
37
42
|
}
|
|
38
43
|
/** PUT raw bytes (application/octet-stream), e.g. one upload chunk. */
|
|
39
|
-
async putBytes(path, bytes) {
|
|
40
|
-
return this.gate(() => this.request("PUT", path, bytes));
|
|
44
|
+
async putBytes(path, bytes, signal) {
|
|
45
|
+
return this.gate(() => this.request("PUT", path, bytes, false, 0, signal));
|
|
41
46
|
}
|
|
42
|
-
async deleteJson(path) {
|
|
43
|
-
return this.gate(() => this.request("DELETE", path));
|
|
47
|
+
async deleteJson(path, signal) {
|
|
48
|
+
return this.gate(() => this.request("DELETE", path, undefined, false, 0, signal));
|
|
44
49
|
}
|
|
45
|
-
async getJson(path) {
|
|
46
|
-
return this.gate(() => this.request("GET", path));
|
|
50
|
+
async getJson(path, signal) {
|
|
51
|
+
return this.gate(() => this.request("GET", path, undefined, false, 0, signal));
|
|
47
52
|
}
|
|
48
53
|
/** Run `fn` under the concurrency limit. */
|
|
49
54
|
async gate(fn) {
|
|
@@ -59,15 +64,38 @@ export class GatewayClient {
|
|
|
59
64
|
this.queue.shift()?.();
|
|
60
65
|
}
|
|
61
66
|
}
|
|
62
|
-
|
|
63
|
-
|
|
67
|
+
// Abortable sleep: resolves after `ms`, or rejects early if the per-call signal
|
|
68
|
+
// aborts, so a client disconnect cancels a 429 back-off wait instead of parking
|
|
69
|
+
// the request (and its concurrency slot) for up to the capped delay.
|
|
70
|
+
static sleep(ms, signal) {
|
|
71
|
+
return new Promise((resolve, reject) => {
|
|
72
|
+
if (signal?.aborted)
|
|
73
|
+
return reject(new DOMException("Aborted", "AbortError"));
|
|
74
|
+
const onAbort = () => {
|
|
75
|
+
clearTimeout(t);
|
|
76
|
+
reject(new DOMException("Aborted", "AbortError"));
|
|
77
|
+
};
|
|
78
|
+
const t = setTimeout(() => {
|
|
79
|
+
signal?.removeEventListener("abort", onAbort);
|
|
80
|
+
resolve();
|
|
81
|
+
}, ms);
|
|
82
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
// Combine the per-request timeout with the caller's per-call AbortSignal so a
|
|
86
|
+
// client disconnect cancels the fetch. Write tools pass none.
|
|
87
|
+
combinedSignal(perCall) {
|
|
88
|
+
const timeout = AbortSignal.timeout(GatewayClient.REQUEST_TIMEOUT_MS);
|
|
89
|
+
return perCall ? AbortSignal.any([timeout, perCall]) : timeout;
|
|
64
90
|
}
|
|
65
|
-
async request(method, path, payload, isRetry = false, retry429 = 0) {
|
|
91
|
+
async request(method, path, payload, isRetry = false, retry429 = 0, signal) {
|
|
66
92
|
const token = await this.tokens.getToken();
|
|
67
93
|
const url = `${this.config.gatewayUrl}${path}`;
|
|
68
94
|
const isBytes = payload instanceof Uint8Array;
|
|
69
95
|
const res = await fetch(url, {
|
|
70
|
-
signal:
|
|
96
|
+
signal: this.combinedSignal(signal),
|
|
97
|
+
// Never follow a redirect off the configured gateway origin.
|
|
98
|
+
redirect: "error",
|
|
71
99
|
method,
|
|
72
100
|
headers: {
|
|
73
101
|
Authorization: `Bearer ${token}`,
|
|
@@ -95,14 +123,14 @@ export class GatewayClient {
|
|
|
95
123
|
const waitMs = Number.isFinite(retryAfter) && retryAfter > 0
|
|
96
124
|
? Math.min(retryAfter * 1000, 30_000)
|
|
97
125
|
: Math.min(1000 * 2 ** retry429, 8000);
|
|
98
|
-
await GatewayClient.sleep(waitMs);
|
|
99
|
-
return this.request(method, path, payload, isRetry, retry429 + 1);
|
|
126
|
+
await GatewayClient.sleep(waitMs, signal);
|
|
127
|
+
return this.request(method, path, payload, isRetry, retry429 + 1, signal);
|
|
100
128
|
}
|
|
101
129
|
if (res.status === 401 && !isRetry) {
|
|
102
130
|
// Drop the cached token so getToken() actually re-acquires (refresh, or
|
|
103
131
|
// re-mint) rather than replaying the same rejected token, then retry once.
|
|
104
132
|
this.tokens.invalidate();
|
|
105
|
-
return this.request(method, path, payload, true);
|
|
133
|
+
return this.request(method, path, payload, true, 0, signal);
|
|
106
134
|
}
|
|
107
135
|
if (!res.ok) {
|
|
108
136
|
const body = await res.text().catch(() => "");
|
package/dist/gateway.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"gateway.js","sourceRoot":"","sources":["../src/gateway.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,YAAa,SAAQ,KAAK;IAG1B;IACA;IAHX,YACE,OAAe,EACN,MAAc,EACd,IAAY;QAErB,KAAK,CAAC,OAAO,CAAC,CAAC;QAHN,WAAM,GAAN,MAAM,CAAQ;QACd,SAAI,GAAJ,IAAI,CAAQ;QAGrB,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;IAC7B,CAAC;CACF;AAED
|
|
1
|
+
{"version":3,"file":"gateway.js","sourceRoot":"","sources":["../src/gateway.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,YAAa,SAAQ,KAAK;IAG1B;IACA;IAHX,YACE,OAAe,EACN,MAAc,EACd,IAAY;QAErB,KAAK,CAAC,OAAO,CAAC,CAAC;QAHN,WAAM,GAAN,MAAM,CAAQ;QACd,SAAI,GAAJ,IAAI,CAAQ;QAGrB,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;IAC7B,CAAC;CACF;AAED;;;;;;;;;GASG;AACH,MAAM,OAAO,aAAa;IAYL;IACA;IAZnB,2EAA2E;IAC3E,2DAA2D;IAC1C,aAAa,GAAG,CAAC,CAAC;IAC3B,MAAM,GAAG,CAAC,CAAC;IACF,KAAK,GAAsB,EAAE,CAAC;IAC/C,mDAAmD;IAC3C,MAAM,CAAU,eAAe,GAAG,CAAC,CAAC;IAC5C,+EAA+E;IACvE,MAAM,CAAU,kBAAkB,GAAG,OAAO,CAAC;IAErD,YACmB,MAAuB,EACvB,MAAmB;QADnB,WAAM,GAAN,MAAM,CAAiB;QACvB,WAAM,GAAN,MAAM,CAAa;IACnC,CAAC;IAEJ,KAAK,CAAC,QAAQ,CAAI,IAAY,EAAE,OAAgB,EAAE,MAAoB;QACpE,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,OAAO,CAAI,MAAM,EAAE,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,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AAC"}
|
package/dist/guard.js
CHANGED
|
@@ -39,27 +39,60 @@ export function canonicalJson(value) {
|
|
|
39
39
|
.sort();
|
|
40
40
|
return `{${keys.map((k) => `${JSON.stringify(k)}:${canonicalJson(obj[k])}`).join(",")}}`;
|
|
41
41
|
}
|
|
42
|
-
function mac(tool, payload, iat, exp) {
|
|
42
|
+
function mac(tool, payload, iat, exp, file) {
|
|
43
43
|
return createHmac("sha256", KEY)
|
|
44
|
-
.update(canonicalJson({ tool, payload, iat, exp }))
|
|
44
|
+
.update(canonicalJson({ tool, payload, iat, exp, file }))
|
|
45
45
|
.digest();
|
|
46
46
|
}
|
|
47
|
-
export function issuePreviewToken(tool, payload) {
|
|
47
|
+
export function issuePreviewToken(tool, payload, file) {
|
|
48
48
|
const iat = clock();
|
|
49
49
|
const exp = iat + ttlMs();
|
|
50
|
+
if (file) {
|
|
51
|
+
const fileSize = file.fileSize;
|
|
52
|
+
const mtimeMs = Math.round(file.mtimeMs);
|
|
53
|
+
const sig = mac(tool, payload, iat, exp, { fileSize, mtimeMs }).toString("base64url");
|
|
54
|
+
return { token: `${iat}.${exp}.${fileSize}.${mtimeMs}.${sig}`, expiresAt: new Date(exp).toISOString() };
|
|
55
|
+
}
|
|
50
56
|
const token = `${iat}.${exp}.${mac(tool, payload, iat, exp).toString("base64url")}`;
|
|
51
57
|
return { token, expiresAt: new Date(exp).toISOString() };
|
|
52
58
|
}
|
|
53
|
-
|
|
59
|
+
function timeValid(iat, exp) {
|
|
60
|
+
const now = clock();
|
|
61
|
+
return exp > now && iat <= now + 60_000 && exp - iat <= MAX_TTL_SECONDS * 1000;
|
|
62
|
+
}
|
|
63
|
+
export function readTokenFile(token) {
|
|
64
|
+
if (typeof token !== "string")
|
|
65
|
+
return null;
|
|
66
|
+
const m = /^(\d{1,15})\.(\d{1,15})\.(\d{1,15})\.(-?\d{1,15})\.([A-Za-z0-9_-]{43})$/.exec(token.trim());
|
|
67
|
+
if (!m)
|
|
68
|
+
return null;
|
|
69
|
+
if (!timeValid(Number(m[1]), Number(m[2])))
|
|
70
|
+
return null;
|
|
71
|
+
return { fileSize: Number(m[3]), mtimeMs: Number(m[4]) };
|
|
72
|
+
}
|
|
73
|
+
export function verifyPreviewToken(tool, payload, token, file) {
|
|
54
74
|
if (typeof token !== "string")
|
|
55
75
|
throw new Error(TOKEN_REFUSAL);
|
|
76
|
+
if (file) {
|
|
77
|
+
const m = /^(\d{1,15})\.(\d{1,15})\.(\d{1,15})\.(-?\d{1,15})\.([A-Za-z0-9_-]{43})$/.exec(token.trim());
|
|
78
|
+
if (!m)
|
|
79
|
+
throw new Error(TOKEN_REFUSAL);
|
|
80
|
+
const iat = Number(m[1]);
|
|
81
|
+
const exp = Number(m[2]);
|
|
82
|
+
if (!timeValid(iat, exp))
|
|
83
|
+
throw new Error(TOKEN_REFUSAL);
|
|
84
|
+
const given = Buffer.from(m[5], "base64url");
|
|
85
|
+
const want = mac(tool, payload, iat, exp, { fileSize: file.fileSize, mtimeMs: Math.round(file.mtimeMs) });
|
|
86
|
+
if (given.length !== want.length || !timingSafeEqual(given, want))
|
|
87
|
+
throw new Error(TOKEN_REFUSAL);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
56
90
|
const m = /^(\d{1,15})\.(\d{1,15})\.([A-Za-z0-9_-]{43})$/.exec(token.trim());
|
|
57
91
|
if (!m)
|
|
58
92
|
throw new Error(TOKEN_REFUSAL);
|
|
59
93
|
const iat = Number(m[1]);
|
|
60
94
|
const exp = Number(m[2]);
|
|
61
|
-
|
|
62
|
-
if (exp <= now || iat > now + 60_000 || exp - iat > MAX_TTL_SECONDS * 1000)
|
|
95
|
+
if (!timeValid(iat, exp))
|
|
63
96
|
throw new Error(TOKEN_REFUSAL);
|
|
64
97
|
const given = Buffer.from(m[3], "base64url");
|
|
65
98
|
const want = mac(tool, payload, iat, exp);
|
package/dist/guard.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"guard.js","sourceRoot":"","sources":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