@carbonaitech/ottofiles-mcp 0.1.0 → 1.1.0-rc.1
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 +132 -12
- package/dist/config.js +18 -2
- package/dist/config.js.map +1 -1
- package/dist/elicit.js +15 -0
- package/dist/elicit.js.map +1 -0
- package/dist/gateway.js +24 -2
- package/dist/gateway.js.map +1 -1
- package/dist/guard.js +102 -0
- package/dist/guard.js.map +1 -0
- package/dist/index.js +326 -0
- package/dist/index.js.map +1 -1
- package/dist/oauth.js +10 -0
- package/dist/oauth.js.map +1 -1
- package/dist/tools/addWorkspaceDocuments.js +172 -0
- package/dist/tools/addWorkspaceDocuments.js.map +1 -0
- package/dist/tools/addWorkspaceMember.js +139 -0
- package/dist/tools/addWorkspaceMember.js.map +1 -0
- package/dist/tools/createDocument.js +478 -0
- package/dist/tools/createDocument.js.map +1 -0
- package/dist/tools/createWorkspace.js +203 -0
- package/dist/tools/createWorkspace.js.map +1 -0
- package/dist/tools/deleteDocument.js +98 -0
- package/dist/tools/deleteDocument.js.map +1 -0
- package/dist/tools/deleteWorkspace.js +81 -0
- package/dist/tools/deleteWorkspace.js.map +1 -0
- package/dist/tools/editMetadata.js +248 -0
- package/dist/tools/editMetadata.js.map +1 -0
- package/dist/tools/getDocumentHistory.js +69 -0
- package/dist/tools/getDocumentHistory.js.map +1 -0
- package/dist/tools/guarded.js +136 -0
- package/dist/tools/guarded.js.map +1 -0
- package/dist/tools/listCompanies.js +18 -0
- package/dist/tools/listCompanies.js.map +1 -0
- package/dist/tools/lockDocument.js +101 -0
- package/dist/tools/lockDocument.js.map +1 -0
- package/dist/tools/manageAccess.js +353 -0
- package/dist/tools/manageAccess.js.map +1 -0
- package/dist/tools/myAccess.js +48 -0
- package/dist/tools/myAccess.js.map +1 -0
- package/dist/tools/reactivateWorkspace.js +105 -0
- package/dist/tools/reactivateWorkspace.js.map +1 -0
- package/dist/tools/removeWorkspaceDocument.js +82 -0
- package/dist/tools/removeWorkspaceDocument.js.map +1 -0
- package/dist/tools/removeWorkspaceMember.js +85 -0
- package/dist/tools/removeWorkspaceMember.js.map +1 -0
- package/dist/tools/restoreDocument.js +101 -0
- package/dist/tools/restoreDocument.js.map +1 -0
- package/dist/tools/rosterCounts.js +12 -0
- package/dist/tools/rosterCounts.js.map +1 -0
- package/dist/tools/searchContent.js +70 -0
- package/dist/tools/searchContent.js.map +1 -0
- package/dist/tools/setStatus.js +133 -0
- package/dist/tools/setStatus.js.map +1 -0
- package/dist/tools/shareDocument.js +187 -0
- package/dist/tools/shareDocument.js.map +1 -0
- package/dist/tools/shareHelpers.js +50 -0
- package/dist/tools/shareHelpers.js.map +1 -0
- package/dist/tools/switchCompany.js +35 -0
- package/dist/tools/switchCompany.js.map +1 -0
- package/dist/tools/unlockDocument.js +155 -0
- package/dist/tools/unlockDocument.js.map +1 -0
- package/dist/tools/unshareDocument.js +154 -0
- package/dist/tools/unshareDocument.js.map +1 -0
- package/dist/tools/uploadVersion.js +403 -0
- package/dist/tools/uploadVersion.js.map +1 -0
- package/dist/tools/workspaceHelpers.js +153 -0
- package/dist/tools/workspaceHelpers.js.map +1 -0
- package/dist/ttl.js +54 -0
- package/dist/ttl.js.map +1 -0
- package/package.json +7 -6
package/README.md
CHANGED
|
@@ -8,26 +8,117 @@ security tier and ACLs: exactly what you'd see signed into OttoFiles.
|
|
|
8
8
|
|
|
9
9
|
## Install
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
Published on public npm as `@carbonaitech/ottofiles-mcp` — no registry auth or
|
|
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).
|
|
12
18
|
|
|
13
|
-
**
|
|
19
|
+
Requires **Node 18+** on your PATH. The three `OTTOFILES_*` values below are the
|
|
20
|
+
**dev** environment and are identical for every user; pin the version
|
|
21
|
+
(`@1.1.0`) so a client always spawns a known build.
|
|
22
|
+
|
|
23
|
+
### Claude Code
|
|
14
24
|
|
|
15
25
|
```bash
|
|
16
|
-
# Claude Code
|
|
17
26
|
claude mcp add ottofiles \
|
|
18
27
|
--env OTTOFILES_ISSUER=https://id.dev.carbonai.ca \
|
|
19
28
|
--env OTTOFILES_GATEWAY_URL=https://gateways.ottofiles.dev.carbonai.ca \
|
|
20
29
|
--env OTTOFILES_CLIENT_ID=ottofiles_mcp_cli \
|
|
21
|
-
-- npx -y @carbonaitech/ottofiles-mcp
|
|
30
|
+
-- npx -y @carbonaitech/ottofiles-mcp@1.1.0
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Claude Desktop
|
|
34
|
+
|
|
35
|
+
Claude Desktop's **"Add custom connector"** dialog only accepts a **URL** (a
|
|
36
|
+
remote server) — it can't add this local package, so don't use that dialog. Use
|
|
37
|
+
one of these two local routes instead:
|
|
38
|
+
|
|
39
|
+
**A. One-click extension (`.mcpb`) — easiest for a tester.** Download
|
|
40
|
+
`ottofiles.mcpb` (see [desktop-extension/](desktop-extension/)) and double-click
|
|
41
|
+
it; Claude Desktop opens an install dialog, you click **Install**, and it
|
|
42
|
+
appears under **Settings → Extensions**. The dev URLs are baked in — nothing to
|
|
43
|
+
type.
|
|
44
|
+
|
|
45
|
+
**B. Config file.** Edit `claude_desktop_config.json` (Settings → Developer →
|
|
46
|
+
Edit Config):
|
|
47
|
+
|
|
48
|
+
```json
|
|
49
|
+
{
|
|
50
|
+
"mcpServers": {
|
|
51
|
+
"ottofiles": {
|
|
52
|
+
"command": "npx",
|
|
53
|
+
"args": ["-y", "@carbonaitech/ottofiles-mcp@1.1.0"],
|
|
54
|
+
"env": {
|
|
55
|
+
"OTTOFILES_ISSUER": "https://id.dev.carbonai.ca",
|
|
56
|
+
"OTTOFILES_GATEWAY_URL": "https://gateways.ottofiles.dev.carbonai.ca",
|
|
57
|
+
"OTTOFILES_CLIENT_ID": "ottofiles_mcp_cli"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
On **Windows**, if `npx` isn't found, set `"command": "npx.cmd"` (or give the
|
|
65
|
+
full path to `npx.cmd`). Node must be installed and on the PATH of the account
|
|
66
|
+
that runs Claude Desktop. Restart Claude Desktop after editing.
|
|
67
|
+
|
|
68
|
+
See [desktop-extension/README.md](desktop-extension/README.md) for the full
|
|
69
|
+
Desktop guide and how the `.mcpb` is built and distributed.
|
|
70
|
+
|
|
71
|
+
### Cursor / VS Code / Windsurf
|
|
72
|
+
|
|
73
|
+
Add an entry with the same shape to the client's MCP config (`.cursor/mcp.json`,
|
|
74
|
+
VS Code's `mcp.json`, or Windsurf's `mcp_config.json`):
|
|
75
|
+
|
|
76
|
+
```json
|
|
77
|
+
{
|
|
78
|
+
"mcpServers": {
|
|
79
|
+
"ottofiles": {
|
|
80
|
+
"command": "npx",
|
|
81
|
+
"args": ["-y", "@carbonaitech/ottofiles-mcp@1.1.0"],
|
|
82
|
+
"env": {
|
|
83
|
+
"OTTOFILES_ISSUER": "https://id.dev.carbonai.ca",
|
|
84
|
+
"OTTOFILES_GATEWAY_URL": "https://gateways.ottofiles.dev.carbonai.ca",
|
|
85
|
+
"OTTOFILES_CLIENT_ID": "ottofiles_mcp_cli"
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
22
90
|
```
|
|
23
91
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
92
|
+
### Codex CLI / Gemini CLI
|
|
93
|
+
|
|
94
|
+
Both take a stdio MCP server as `command` + `args` + `env` in their own config
|
|
95
|
+
(Codex: `~/.codex/config.toml` `[mcp_servers.ottofiles]`; Gemini:
|
|
96
|
+
`~/.gemini/settings.json` `mcpServers`). Use `npx -y
|
|
97
|
+
@carbonaitech/ottofiles-mcp@1.1.0` as the command with the three `OTTOFILES_*`
|
|
98
|
+
env values above.
|
|
99
|
+
|
|
100
|
+
### Cloud clients (not supported by this package)
|
|
101
|
+
|
|
102
|
+
**claude.ai (web + mobile), ChatGPT, and Microsoft Copilot** can only add a
|
|
103
|
+
**remote HTTPS** MCP server with OAuth; they cannot spawn a local stdio process.
|
|
104
|
+
This package is stdio-only, so those clients cannot use it. A hosted
|
|
105
|
+
Streamable-HTTP server is required for them and is **not built yet**.
|
|
106
|
+
|
|
107
|
+
### Signing in (loopback OAuth)
|
|
108
|
+
|
|
109
|
+
On the **first tool call** (not when the client connects), the server opens your
|
|
110
|
+
browser to sign in with your own OttoFiles account; the token (with a refresh
|
|
111
|
+
token) is cached under `~/.ottofiles-mcp/` and stays valid ~7 days. Sign-in uses
|
|
112
|
+
a loopback redirect (`http://127.0.0.1:8765-8767/callback`, RFC 8252), so the
|
|
113
|
+
server and your browser must be on the **same machine** — a remote/SSH session
|
|
114
|
+
with no local browser can't complete the interactive login. Set only
|
|
115
|
+
`OTTOFILES_CLIENT_ID` (the public PKCE client `ottofiles_mcp_cli`); there is no
|
|
116
|
+
secret to paste. Everything you see is scoped to your own security tier and
|
|
117
|
+
ACLs, exactly like a web session.
|
|
27
118
|
|
|
28
119
|
## Tools
|
|
29
120
|
|
|
30
|
-
All read-only, all scoped to the signed-in user's security tier and ACLs.
|
|
121
|
+
All read-only except `switch_company` and the write tools below, all scoped to the signed-in user's security tier and ACLs.
|
|
31
122
|
|
|
32
123
|
| Tool | Wraps | Purpose |
|
|
33
124
|
|------|-------|---------|
|
|
@@ -40,6 +131,25 @@ All read-only, all scoped to the signed-in user's security tier and ACLs.
|
|
|
40
131
|
| `get_document_versions` | `GET /documents/{id}/versions` | Every version — increment, label, file, upload status, who and when. |
|
|
41
132
|
| `document_access_roster` | `GET /api/documents/{id}/access-roster` | Who can access a document and who is denied, with effective tier rank. |
|
|
42
133
|
| `list_workspaces` | `GET /api/workspaces` | External share spaces — title, document and member counts, expiry state. |
|
|
134
|
+
| `search_content` | `POST /api/search/documents` (`ContentSearch: true`) | Search inside extracted file-body text, with a snippet and `matchedInContent` flag per hit. |
|
|
135
|
+
| `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}`. |
|
|
136
|
+
| `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. |
|
|
137
|
+
| `list_companies` | `GET /profile/companies` | The companies (id, name) the signed-in user belongs to. Optional `showInactive`. |
|
|
138
|
+
| `switch_company` | `POST /profile/switch/companies/{id}`, token refresh, `GET /profile` | **State-changing.** Switch the active company for the whole MCP session (shared token cache, so other instances are affected) and return the new one. Interactive OAuth only; errors in headless mode. |
|
|
139
|
+
|
|
140
|
+
## Write tools
|
|
141
|
+
|
|
142
|
+
`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.
|
|
143
|
+
|
|
144
|
+
`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.
|
|
145
|
+
|
|
146
|
+
**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.
|
|
147
|
+
|
|
148
|
+
- **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).
|
|
149
|
+
- **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.
|
|
150
|
+
- **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.
|
|
151
|
+
- **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.
|
|
152
|
+
- Duplicate-submission guards are per process and are lost when the server restarts.
|
|
43
153
|
|
|
44
154
|
## Authentication
|
|
45
155
|
|
|
@@ -84,11 +194,21 @@ npm run inspect # Inspector UI over the built server
|
|
|
84
194
|
```
|
|
85
195
|
|
|
86
196
|
Config is read from the environment, or from a `.env` in the package root (see
|
|
87
|
-
`.env.example`) — never from the current working directory.
|
|
88
|
-
|
|
197
|
+
`.env.example`) — never from the current working directory. Explicitly passed
|
|
198
|
+
environment variables always win; `.env` only fills the gaps (`src/config.ts`
|
|
199
|
+
skips any key already set). `provision/` holds the credential scripts and their
|
|
200
|
+
own README.
|
|
201
|
+
|
|
202
|
+
> **Clone-only gotcha.** Auth mode is chosen by whether a client **secret** is
|
|
203
|
+
> present (`OTTOFILES_CLIENT_SECRET` ⇒ headless `client_credentials`; absent ⇒
|
|
204
|
+
> interactive browser login). If your package-root `.env` carries a
|
|
205
|
+
> `OTTOFILES_CLIENT_SECRET`, it fills that gap even when you pass the interactive
|
|
206
|
+
> dev URLs on the command line, so the server comes up in `client_credentials`
|
|
207
|
+
> mode. To force interactive from a clone, set `OTTOFILES_AUTH_MODE=interactive`
|
|
208
|
+
> (or remove the secret from `.env`). This never affects `npx`/extension
|
|
209
|
+
> installs — they have no `.env`.
|
|
89
210
|
|
|
90
211
|
## Roadmap
|
|
91
212
|
|
|
92
|
-
-
|
|
93
|
-
confirmation story and tighter scoping.
|
|
213
|
+
- Server-side detection of agent-originated writes (client discriminator, out-of-band confirmation, velocity limits) — a backend track.
|
|
94
214
|
- Blob / download streaming for document bytes.
|
package/dist/config.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { readFileSync, existsSync } from "node:fs";
|
|
2
|
-
import { dirname, join } from "node:path";
|
|
2
|
+
import path, { dirname, join } from "node:path";
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
4
|
// Minimal .env loader (no dependency). Values already present in the real
|
|
5
5
|
// environment win — so an MCP client that injects env via its JSON config
|
|
@@ -42,7 +42,7 @@ function loadDotEnv() {
|
|
|
42
42
|
// (mirrors CreatePortalCredential in Services.Workspace). Search only strictly
|
|
43
43
|
// needs lukla_search_api, but the wider set keeps the same credential usable as
|
|
44
44
|
// more read tools are added.
|
|
45
|
-
const DEFAULT_SCOPES = "lukla_search_api lukla_document_api lukla_bin_api lukla_workspace_api lukla_permission_api lukla_storage_api";
|
|
45
|
+
const DEFAULT_SCOPES = "lukla_search_api lukla_document_api lukla_bin_api lukla_workspace_api lukla_permission_api lukla_storage_api lukla_profile_api lukla_auditing_api";
|
|
46
46
|
function required(name) {
|
|
47
47
|
const value = process.env[name];
|
|
48
48
|
if (!value || value.trim() === "") {
|
|
@@ -74,8 +74,24 @@ export function loadConfig() {
|
|
|
74
74
|
scopes: (process.env.OTTOFILES_SCOPES ?? DEFAULT_SCOPES).trim(),
|
|
75
75
|
mode,
|
|
76
76
|
redirectPort: parseRedirectPort(process.env.OTTOFILES_REDIRECT_PORT),
|
|
77
|
+
uploadRoots: parseUploadRoots(process.env.OTTOFILES_UPLOAD_ROOTS),
|
|
77
78
|
};
|
|
78
79
|
}
|
|
80
|
+
export function parseUploadRoots(raw) {
|
|
81
|
+
if (!raw || raw.trim() === "")
|
|
82
|
+
return [];
|
|
83
|
+
const roots = [];
|
|
84
|
+
for (const part of raw.split(new RegExp(`[${path.delimiter},]`))) {
|
|
85
|
+
const v = part.trim();
|
|
86
|
+
if (!v)
|
|
87
|
+
continue;
|
|
88
|
+
if (!path.isAbsolute(v)) {
|
|
89
|
+
throw new Error(`OTTOFILES_UPLOAD_ROOTS entries must be absolute directories (got "${v}").`);
|
|
90
|
+
}
|
|
91
|
+
roots.push(path.resolve(v));
|
|
92
|
+
}
|
|
93
|
+
return roots;
|
|
94
|
+
}
|
|
79
95
|
function parseRedirectPort(raw) {
|
|
80
96
|
if (raw === undefined || raw.trim() === "")
|
|
81
97
|
return 8765;
|
package/dist/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACnD,OAAO,IAAI,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAiBzC,0EAA0E;AAC1E,0EAA0E;AAC1E,8EAA8E;AAC9E,6EAA6E;AAC7E,kBAAkB;AAClB,SAAS,UAAU;IACjB,M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B,CAAC,GAAuB;IAChD,IAAI,GAAG,KAAK,SAAS,IAAI,GAAG,CAAC,IAAI,EAAE,KAAK,EAAE;QAAE,OAAO,IAAI,CAAC;IACxD,MAAM,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;IACtB,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,KAAK,EAAE,CAAC;QAC/C,MAAM,IAAI,KAAK,CACb,4DAA4D,GAAG,mDAAmD,CACnH,CAAC;IACJ,CAAC;IACD,OAAO,CAAC,CAAC;AACX,CAAC"}
|
package/dist/elicit.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
let elicitor = null;
|
|
2
|
+
export function setElicitor(fn) {
|
|
3
|
+
elicitor = fn;
|
|
4
|
+
}
|
|
5
|
+
export async function askHuman(message) {
|
|
6
|
+
if (!elicitor)
|
|
7
|
+
return "unsupported";
|
|
8
|
+
try {
|
|
9
|
+
return await elicitor(message);
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return "declined";
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=elicit.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"elicit.js","sourceRoot":"","sources":["../src/elicit.ts"],"names":[],"mappings":"AAGA,IAAI,QAAQ,GAAoB,IAAI,CAAC;AAErC,MAAM,UAAU,WAAW,CAAC,EAAmB;IAC7C,QAAQ,GAAG,EAAE,CAAC;AAChB,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,OAAe;IAC5C,IAAI,CAAC,QAAQ;QAAE,OAAO,aAAa,CAAC;IACpC,IAAI,CAAC;QACH,OAAO,MAAM,QAAQ,CAAC,OAAO,CAAC,CAAC;IACjC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,UAAU,CAAC;IACpB,CAAC;AACH,CAAC"}
|
package/dist/gateway.js
CHANGED
|
@@ -23,6 +23,8 @@ export class GatewayClient {
|
|
|
23
23
|
queue = [];
|
|
24
24
|
// Cap on 429 retries per request before giving up.
|
|
25
25
|
static MAX_429_RETRIES = 4;
|
|
26
|
+
// Per-request timeout so a stalled socket cannot hang past the upload session.
|
|
27
|
+
static REQUEST_TIMEOUT_MS = 120_000;
|
|
26
28
|
constructor(config, tokens) {
|
|
27
29
|
this.config = config;
|
|
28
30
|
this.tokens = tokens;
|
|
@@ -30,6 +32,16 @@ export class GatewayClient {
|
|
|
30
32
|
async postJson(path, payload) {
|
|
31
33
|
return this.gate(() => this.request("POST", path, payload));
|
|
32
34
|
}
|
|
35
|
+
async putJson(path, payload) {
|
|
36
|
+
return this.gate(() => this.request("PUT", path, payload));
|
|
37
|
+
}
|
|
38
|
+
/** 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));
|
|
41
|
+
}
|
|
42
|
+
async deleteJson(path) {
|
|
43
|
+
return this.gate(() => this.request("DELETE", path));
|
|
44
|
+
}
|
|
33
45
|
async getJson(path) {
|
|
34
46
|
return this.gate(() => this.request("GET", path));
|
|
35
47
|
}
|
|
@@ -53,16 +65,26 @@ export class GatewayClient {
|
|
|
53
65
|
async request(method, path, payload, isRetry = false, retry429 = 0) {
|
|
54
66
|
const token = await this.tokens.getToken();
|
|
55
67
|
const url = `${this.config.gatewayUrl}${path}`;
|
|
68
|
+
const isBytes = payload instanceof Uint8Array;
|
|
56
69
|
const res = await fetch(url, {
|
|
70
|
+
signal: AbortSignal.timeout(GatewayClient.REQUEST_TIMEOUT_MS),
|
|
57
71
|
method,
|
|
58
72
|
headers: {
|
|
59
73
|
Authorization: `Bearer ${token}`,
|
|
60
74
|
Accept: "application/json",
|
|
61
75
|
...(payload !== undefined
|
|
62
|
-
? {
|
|
76
|
+
? {
|
|
77
|
+
"Content-Type": isBytes
|
|
78
|
+
? "application/octet-stream"
|
|
79
|
+
: "application/json",
|
|
80
|
+
}
|
|
63
81
|
: {}),
|
|
64
82
|
},
|
|
65
|
-
body: payload
|
|
83
|
+
body: payload === undefined
|
|
84
|
+
? undefined
|
|
85
|
+
: isBytes
|
|
86
|
+
? payload
|
|
87
|
+
: JSON.stringify(payload),
|
|
66
88
|
});
|
|
67
89
|
// Respect server-side rate limiting: honour Retry-After when present, else
|
|
68
90
|
// exponential backoff, up to a cap. Cooperates with any gateway throttle.
|
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;;;;GAIG;AACH,MAAM,OAAO,aAAa;
|
|
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;;;;GAIG;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,G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,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC;YAC9C,MAAM,IAAI,YAAY,CACpB,qBAAqB,MAAM,IAAI,IAAI,YAAY,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,UAAU,EAAE,EAC7E,GAAG,CAAC,MAAM,EACV,IAAI,CACL,CAAC;QACJ,CAAC;QAED,oBAAoB;QACpB,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC;QAC9B,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAM,CAAC;IACpD,CAAC"}
|
package/dist/guard.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { createHmac, randomBytes, timingSafeEqual } from "node:crypto";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
// Preview tokens bind a confirm call to the preview that preceded it: the token is an
|
|
4
|
+
// HMAC over the tool name, the canonical effect-defining arguments, and an issue/expiry
|
|
5
|
+
// time. The key is minted per process and never leaves it. This stops preview-skipping and
|
|
6
|
+
// arg drift between preview and confirm. It is NOT a human gate: an agent that controls
|
|
7
|
+
// both calls can still obtain and present a token. Human gating is elicitation plus
|
|
8
|
+
// leaving these tools on per-call client approval.
|
|
9
|
+
const KEY = randomBytes(32);
|
|
10
|
+
// Tokens are intentionally replayable within the TTL (not single-use): a confirm whose outcome
|
|
11
|
+
// is ambiguous must be retryable with the same token; the writes themselves are idempotent or guarded.
|
|
12
|
+
const DEFAULT_TTL_SECONDS = 600;
|
|
13
|
+
const MIN_TTL_SECONDS = 30;
|
|
14
|
+
const MAX_TTL_SECONDS = 3600;
|
|
15
|
+
let clock = () => Date.now();
|
|
16
|
+
export function setClockForTests(fn) {
|
|
17
|
+
clock = fn ?? (() => Date.now());
|
|
18
|
+
}
|
|
19
|
+
export const previewTokenField = z
|
|
20
|
+
.string()
|
|
21
|
+
.trim()
|
|
22
|
+
.max(512)
|
|
23
|
+
.optional()
|
|
24
|
+
.describe("Required when confirm is true: the previewToken returned by the preview call with identical arguments. Expires after a few minutes; re-run the preview if the arguments change or it expires.");
|
|
25
|
+
export const TOKEN_REFUSAL = "REFUSED: re-run preview; args changed or token expired. confirm:true requires the previewToken from a preview call with identical arguments.";
|
|
26
|
+
function ttlMs() {
|
|
27
|
+
const raw = Number(process.env.OTTOFILES_PREVIEW_TTL_SECONDS);
|
|
28
|
+
const s = Number.isFinite(raw) && raw > 0 ? raw : DEFAULT_TTL_SECONDS;
|
|
29
|
+
return Math.min(MAX_TTL_SECONDS, Math.max(MIN_TTL_SECONDS, s)) * 1000;
|
|
30
|
+
}
|
|
31
|
+
export function canonicalJson(value) {
|
|
32
|
+
if (value === null || typeof value !== "object")
|
|
33
|
+
return JSON.stringify(value) ?? "null";
|
|
34
|
+
if (Array.isArray(value))
|
|
35
|
+
return `[${value.map((v) => canonicalJson(v === undefined ? null : v)).join(",")}]`;
|
|
36
|
+
const obj = value;
|
|
37
|
+
const keys = Object.keys(obj)
|
|
38
|
+
.filter((k) => obj[k] !== undefined)
|
|
39
|
+
.sort();
|
|
40
|
+
return `{${keys.map((k) => `${JSON.stringify(k)}:${canonicalJson(obj[k])}`).join(",")}}`;
|
|
41
|
+
}
|
|
42
|
+
function mac(tool, payload, iat, exp, file) {
|
|
43
|
+
return createHmac("sha256", KEY)
|
|
44
|
+
.update(canonicalJson({ tool, payload, iat, exp, file }))
|
|
45
|
+
.digest();
|
|
46
|
+
}
|
|
47
|
+
export function issuePreviewToken(tool, payload, file) {
|
|
48
|
+
const iat = clock();
|
|
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
|
+
}
|
|
56
|
+
const token = `${iat}.${exp}.${mac(tool, payload, iat, exp).toString("base64url")}`;
|
|
57
|
+
return { token, expiresAt: new Date(exp).toISOString() };
|
|
58
|
+
}
|
|
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) {
|
|
74
|
+
if (typeof token !== "string")
|
|
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
|
+
}
|
|
90
|
+
const m = /^(\d{1,15})\.(\d{1,15})\.([A-Za-z0-9_-]{43})$/.exec(token.trim());
|
|
91
|
+
if (!m)
|
|
92
|
+
throw new Error(TOKEN_REFUSAL);
|
|
93
|
+
const iat = Number(m[1]);
|
|
94
|
+
const exp = Number(m[2]);
|
|
95
|
+
if (!timeValid(iat, exp))
|
|
96
|
+
throw new Error(TOKEN_REFUSAL);
|
|
97
|
+
const given = Buffer.from(m[3], "base64url");
|
|
98
|
+
const want = mac(tool, payload, iat, exp);
|
|
99
|
+
if (given.length !== want.length || !timingSafeEqual(given, want))
|
|
100
|
+
throw new Error(TOKEN_REFUSAL);
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"guard.js","sourceRoot":"","sources":["../src/guard.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACvE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,sFAAsF;AACtF,wFAAwF;AACxF,2FAA2F;AAC3F,wFAAwF;AACxF,oFAAoF;AACpF,mDAAmD;AACnD,MAAM,GAAG,GAAG,WAAW,CAAC,EAAE,CAAC,CAAC;AAC5B,+FAA+F;AAC/F,uGAAuG;AAEvG,MAAM,mBAAmB,GAAG,GAAG,CAAC;AAChC,MAAM,eAAe,GAAG,EAAE,CAAC;AAC3B,MAAM,eAAe,GAAG,IAAI,CAAC;AAE7B,IAAI,KAAK,GAAiB,GAAG,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC;AAE3C,MAAM,UAAU,gBAAgB,CAAC,EAAyB;IACxD,KAAK,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;AACnC,CAAC;AAED,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC;KAC/B,MAAM,EAAE;KACR,IA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