@shelby-protocol/mcp 0.1.2-dogfood.4 → 0.1.2
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 +105 -0
- package/dist/config.js +8 -15
- package/dist/server.js +3 -70
- package/dist/services/ShelbyAccount.js +2 -11
- package/dist/services/ShelbyAdmin.js +4 -36
- package/dist/services/ShelbyObjects.js +15 -6
- package/dist/services/agentKey.js +9 -70
- package/dist/services/catalog.js +7 -7
- package/dist/tools/buckets.js +26 -63
- package/dist/tools/index.js +2 -55
- package/dist/tools/objects.js +3 -2
- package/dist/tools/run.js +15 -38
- package/dist/tools/schemas.js +0 -3
- package/dist/utils/paths.js +8 -3
- package/package.json +4 -10
- package/dist/memoryConfig.js +0 -41
- package/dist/resources/setup-atlas.py +0 -56
- package/dist/services/DataVault.js +0 -251
- package/dist/services/intentContext.js +0 -15
- package/dist/services/liveVaultCatalog.js +0 -336
- package/dist/services/memorySelection.js +0 -27
- package/dist/services/memorySummaries.js +0 -114
- package/dist/services/memoryUsage.js +0 -78
- package/dist/services/vaultCatalog.js +0 -15
- package/dist/services/vaultNames.js +0 -17
- package/dist/tools/vaultSchemas.js +0 -65
- package/dist/tools/vaults.js +0 -193
- package/dist/utils/diagnostics.js +0 -179
- package/dist/utils/download.js +0 -17
package/README.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# Shelby MCP
|
|
2
|
+
|
|
3
|
+
An MCP server that lets agents manage Shelby buckets and read and write their objects. It authenticates with an agent key, which you can limit to an organization, some projects, or specific buckets, and make read-only.
|
|
4
|
+
|
|
5
|
+
## Requirements
|
|
6
|
+
|
|
7
|
+
- Node.js `>= 22.12.0` (see `.nvmrc`)
|
|
8
|
+
- A Shelby agent key
|
|
9
|
+
|
|
10
|
+
### Get an agent key
|
|
11
|
+
|
|
12
|
+
1. Go to [https://console.shelby.xyz/](https://console.shelby.xyz/) and open **Integrations**.
|
|
13
|
+
2. Create an agent key. Choose what it can reach (the organization, some projects, or specific buckets), whether it's read-only, and when it expires (30, 60, 90 or 180 days).
|
|
14
|
+
3. Copy the key. It's shown once.
|
|
15
|
+
|
|
16
|
+
In the last 7 days before the key expires, every tool result carries a second text block warning about it, so the agent can tell you to create a new key and update `SHELBY_AGENT_KEY`.
|
|
17
|
+
|
|
18
|
+
Give the key the least it needs. A key limited to specific buckets can read and write those buckets but can't create new ones.
|
|
19
|
+
|
|
20
|
+
## Setup
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
npm install
|
|
24
|
+
npm run build
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
The server reads the key from, in order:
|
|
28
|
+
|
|
29
|
+
1. `SHELBY_AGENT_KEY` in the environment, or in a `.env` file in this directory (`.env` is gitignored).
|
|
30
|
+
2. `SHELBY_BOT_KEY`, the older name.
|
|
31
|
+
3. The OS keychain, under service `shelby-mcp` and account `SHELBY_AGENT_KEY`:
|
|
32
|
+
- macOS: `security add-generic-password -s shelby-mcp -a SHELBY_AGENT_KEY -w`
|
|
33
|
+
- Linux (libsecret): `secret-tool store --label "Shelby agent key" service shelby-mcp account SHELBY_AGENT_KEY`
|
|
34
|
+
|
|
35
|
+
Uploads read and downloads write only inside the allowed directories. Pass `--allow-dir <dir>`, with more directories after it or repeated; without it, only the directory the server starts in is allowed. A bare `--allow-dir` with no directory, which Claude Desktop sends when its Allowed directories list is empty, allows none: the bucket tools still work, and uploads and downloads say where to add a directory. Paths are checked after resolving symlinks, so neither `..` nor a link can reach outside.
|
|
36
|
+
|
|
37
|
+
`SHELBY_ENV` selects the backend. It defaults to `beta`; `staging` points at staging. `SHELBY_ADMIN_URL`, `SHELBY_S3_ADMIN_URL` and `SHELBY_S3_ENDPOINT` override single endpoints.
|
|
38
|
+
|
|
39
|
+
## Use with Cursor
|
|
40
|
+
|
|
41
|
+
Add this to your user-level `~/.cursor/mcp.json`, and set `SHELBY_AGENT_KEY` in your shell profile or keychain rather than in the file:
|
|
42
|
+
|
|
43
|
+
```json
|
|
44
|
+
{
|
|
45
|
+
"mcpServers": {
|
|
46
|
+
"shelby": {
|
|
47
|
+
"command": "node",
|
|
48
|
+
"args": [
|
|
49
|
+
"<path-to-shelby-mcp>/dist/server.js",
|
|
50
|
+
"--allow-dir",
|
|
51
|
+
"<directory-with-your-files>"
|
|
52
|
+
],
|
|
53
|
+
"env": {
|
|
54
|
+
"SHELBY_AGENT_KEY": "${env:SHELBY_AGENT_KEY}"
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Don't put the key itself in a project-level `.cursor/mcp.json` or `.mcp.json`; those are easy to commit.
|
|
62
|
+
|
|
63
|
+
The server uses stdio. Restart the MCP client after changing the config.
|
|
64
|
+
|
|
65
|
+
## Use with Claude Desktop
|
|
66
|
+
|
|
67
|
+
`npm run bundle` builds two [MCP bundles](https://github.com/modelcontextprotocol/mcpb): `build/shelby.mcpb` for beta and `build/shelby-staging.mcpb` for staging. Open one to install it in Claude Desktop, which asks for the agent key (masked and stored securely) and the allowed directories.
|
|
68
|
+
|
|
69
|
+
## Tools
|
|
70
|
+
|
|
71
|
+
Tools take bucket and project **names**. The project is only needed when two reachable buckets share a name.
|
|
72
|
+
|
|
73
|
+
| Tool | What it does |
|
|
74
|
+
| --- | --- |
|
|
75
|
+
| `get_shelby_agent_key_info` | The key's name, expiry, permission, what it reaches, and the allowed directories. Never the key itself |
|
|
76
|
+
| `list_shelby_buckets` | Buckets the key reaches, with each one's S3 bucket name and account address, and where new buckets can be created |
|
|
77
|
+
| `get_shelby_bucket` | One bucket by name |
|
|
78
|
+
| `create_shelby_bucket` | Create a bucket and the managed account that owns its data |
|
|
79
|
+
| `list_shelby_objects` | List objects, optionally under a prefix, a page at a time |
|
|
80
|
+
| `upload_shelby_object` | Upload a local file, streamed, with a multipart upload above 16 MiB |
|
|
81
|
+
| `download_shelby_object` | Download an object to a local file. Won't replace an existing file unless asked |
|
|
82
|
+
| `get_shelby_s3_connection_guide` | Sample code (boto3, AWS SDK v3, AWS CLI) for S3 clients |
|
|
83
|
+
| `get_mcp_version` | Return this server's version |
|
|
84
|
+
|
|
85
|
+
Buckets can only be deleted in the Shelby console: the gateway doesn't let agent or bot keys delete them.
|
|
86
|
+
|
|
87
|
+
A bucket's name is also its S3 bucket name, so it must be 3–32 characters of lowercase letters, numbers, and dashes, start and end with a letter or number, and contain at least one letter. It also can't look like an account address (`0x` followed by hex) or use an AWS-reserved prefix or suffix, and `health`, `ready`, `metrics` and `internal` are reserved. API key names must be 3–32 characters: lowercase letters, numbers, dashes, and underscores.
|
|
88
|
+
|
|
89
|
+
S3 clients use the bucket's `s3_bucket`: the name it was created with, or its `account_address` for older buckets whose names aren't valid S3 bucket names. The account address always works too.
|
|
90
|
+
|
|
91
|
+
## Unscoped bot keys
|
|
92
|
+
|
|
93
|
+
Bot keys created before agent keys aren't limited to anything. They still work for managing buckets: `create_shelby_bucket` then also creates an API key, the bucket's S3 credential, and `list_shelby_buckets` returns API keys. They can't use the object tools; use an agent key for those.
|
|
94
|
+
|
|
95
|
+
## Scripts
|
|
96
|
+
|
|
97
|
+
| Script | What it does |
|
|
98
|
+
| --- | --- |
|
|
99
|
+
| `npm run build` | Compile TypeScript to `dist/` |
|
|
100
|
+
| `npm start` | Build and run the server |
|
|
101
|
+
| `npm run dev` | Build and run the FastMCP dev inspector |
|
|
102
|
+
| `npm test` | Build, then run the unit and smoke tests |
|
|
103
|
+
| `npm run lint` | Biome and `tsc --noEmit` |
|
|
104
|
+
| `npm run format` | Format with Biome |
|
|
105
|
+
| `npm run bundle` | Build the Claude Desktop bundles in `build/` |
|
package/dist/config.js
CHANGED
|
@@ -1,24 +1,19 @@
|
|
|
1
1
|
import { readFileSync } from "node:fs";
|
|
2
2
|
import { resolve } from "node:path";
|
|
3
3
|
import { loadEnvFromFile } from "./load-env.js";
|
|
4
|
-
import { loadMemoryConfig } from "./memoryConfig.js";
|
|
5
4
|
loadEnvFromFile();
|
|
6
5
|
const environments = {
|
|
7
6
|
beta: {
|
|
8
7
|
adminUrl: "https://admin.api.beta.shelby.xyz/api/rspc",
|
|
9
8
|
// JWT/bot-key authed control plane. Never send SigV4 requests here.
|
|
10
9
|
s3AdminUrl: "https://api.beta.shelby.xyz/s3/admin",
|
|
11
|
-
|
|
12
|
-
dataVaultArtifactApiUrl: undefined,
|
|
13
|
-
// S3 data plane accepts scoped agent keys.
|
|
10
|
+
// S3 data plane. Agent keys use it with a Bearer token, S3 clients with SigV4.
|
|
14
11
|
s3EndpointUrl: "https://s3.beta.shelby.xyz",
|
|
15
12
|
},
|
|
16
13
|
staging: {
|
|
17
14
|
adminUrl: "https://admin.api.staging.beta.shelby.xyz/api/rspc",
|
|
18
15
|
s3AdminUrl: "https://api.staging.beta.shelby.xyz/s3/admin",
|
|
19
|
-
dataVaultApiUrl: "https://api.staging.beta.shelby.xyz/data-vault",
|
|
20
16
|
s3EndpointUrl: "https://s3.staging.beta.shelby.xyz",
|
|
21
|
-
dataVaultArtifactApiUrl: "https://artifacts.staging.beta.shelby.xyz/data-vault",
|
|
22
17
|
},
|
|
23
18
|
};
|
|
24
19
|
function resolveEnvironment() {
|
|
@@ -31,38 +26,36 @@ function resolveEnvironment() {
|
|
|
31
26
|
/**
|
|
32
27
|
* Every `--allow-dir <dir>...` and `--allow-dir=<dir>` argument, resolved. `--allow-dir` takes
|
|
33
28
|
* each argument up to the next flag, since MCP bundle hosts expand a multi-directory picker into
|
|
34
|
-
* separate arguments.
|
|
29
|
+
* separate arguments. An empty picker leaves a bare `--allow-dir`, which allows no directory
|
|
30
|
+
* rather than falling back to the working directory. Only without any `--allow-dir` is the
|
|
31
|
+
* working directory allowed.
|
|
35
32
|
*/
|
|
36
33
|
export function parseAllowedDirs(argv, cwd) {
|
|
37
34
|
const dirs = [];
|
|
35
|
+
let flagged = false;
|
|
38
36
|
for (let i = 0; i < argv.length; i++) {
|
|
39
37
|
const arg = argv[i];
|
|
40
38
|
if (arg === "--allow-dir") {
|
|
41
|
-
|
|
39
|
+
flagged = true;
|
|
42
40
|
while (i + 1 < argv.length && !argv[i + 1].startsWith("--")) {
|
|
43
41
|
dirs.push(resolve(cwd, argv[++i]));
|
|
44
42
|
}
|
|
45
|
-
if (dirs.length === start)
|
|
46
|
-
throw new Error("--allow-dir needs a directory");
|
|
47
43
|
}
|
|
48
44
|
else if (arg.startsWith("--allow-dir=")) {
|
|
45
|
+
flagged = true;
|
|
49
46
|
dirs.push(resolve(cwd, arg.slice("--allow-dir=".length)));
|
|
50
47
|
}
|
|
51
48
|
}
|
|
52
|
-
return dirs.length > 0 ? dirs : [cwd];
|
|
49
|
+
return dirs.length > 0 || flagged ? dirs : [cwd];
|
|
53
50
|
}
|
|
54
51
|
const environment = resolveEnvironment();
|
|
55
52
|
const endpoints = environments[environment];
|
|
56
53
|
export const config = {
|
|
57
|
-
memories: loadMemoryConfig(),
|
|
58
54
|
environment,
|
|
59
55
|
shelby: {
|
|
60
56
|
adminUrl: process.env.SHELBY_ADMIN_URL ?? endpoints.adminUrl,
|
|
61
57
|
s3AdminUrl: process.env.SHELBY_S3_ADMIN_URL ?? endpoints.s3AdminUrl,
|
|
62
58
|
s3EndpointUrl: process.env.SHELBY_S3_ENDPOINT ?? endpoints.s3EndpointUrl,
|
|
63
|
-
dataVaultApiUrl: process.env.SHELBY_DATA_VAULT_API_URL ?? endpoints.dataVaultApiUrl,
|
|
64
|
-
dataVaultArtifactApiUrl: process.env.SHELBY_DATA_VAULT_ARTIFACT_API_URL ??
|
|
65
|
-
endpoints.dataVaultArtifactApiUrl,
|
|
66
59
|
// api-gateway chain name for Shelby; the only network buckets can be created on.
|
|
67
60
|
network: "shelby-beta",
|
|
68
61
|
// api-gateway service type for Managed Shelby Accounts (buckets).
|
package/dist/server.js
CHANGED
|
@@ -1,55 +1,14 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { spawnSync } from "node:child_process";
|
|
3
|
-
import { fileURLToPath } from "node:url";
|
|
4
2
|
import { FastMCP } from "fastmcp";
|
|
5
3
|
import { z } from "zod";
|
|
6
4
|
import { config } from "./config.js";
|
|
7
5
|
import { registerTools } from "./tools/index.js";
|
|
8
|
-
import { diagnosticLog, errorKind } from "./utils/diagnostics.js";
|
|
9
6
|
async function main() {
|
|
10
|
-
console.error(`Shelby MCP diagnostic log: ${diagnosticLog().location}`);
|
|
11
|
-
diagnosticLog().write({
|
|
12
|
-
event: "startup",
|
|
13
|
-
version: config.server.version,
|
|
14
|
-
environment: config.environment,
|
|
15
|
-
});
|
|
16
7
|
const server = new FastMCP({
|
|
17
|
-
logger: {
|
|
18
|
-
debug: () => { },
|
|
19
|
-
info: () => { },
|
|
20
|
-
warn: () => diagnosticLog().write({
|
|
21
|
-
event: "framework",
|
|
22
|
-
operation: "warning",
|
|
23
|
-
outcome: "error",
|
|
24
|
-
}),
|
|
25
|
-
error: () => diagnosticLog().write({
|
|
26
|
-
event: "framework",
|
|
27
|
-
operation: "error",
|
|
28
|
-
outcome: "error",
|
|
29
|
-
}),
|
|
30
|
-
log: () => { },
|
|
31
|
-
},
|
|
32
8
|
name: config.server.name,
|
|
33
9
|
version: config.server.version,
|
|
34
10
|
});
|
|
35
|
-
|
|
36
|
-
const shutdown = async () => {
|
|
37
|
-
await live.stop();
|
|
38
|
-
await server.stop();
|
|
39
|
-
};
|
|
40
|
-
const stop = () => {
|
|
41
|
-
void shutdown().then(() => process.exit(0), () => {
|
|
42
|
-
console.error("MCP shutdown failed");
|
|
43
|
-
process.exit(1);
|
|
44
|
-
});
|
|
45
|
-
};
|
|
46
|
-
process.once("SIGINT", stop);
|
|
47
|
-
process.once("SIGTERM", stop);
|
|
48
|
-
process.once("beforeExit", () => {
|
|
49
|
-
void live
|
|
50
|
-
.stop()
|
|
51
|
-
.catch(() => console.error("MCP discovery shutdown failed"));
|
|
52
|
-
});
|
|
11
|
+
registerTools(server);
|
|
53
12
|
server.addTool({
|
|
54
13
|
name: "get_mcp_version",
|
|
55
14
|
description: "Returns the version of the MCP server",
|
|
@@ -61,36 +20,10 @@ async function main() {
|
|
|
61
20
|
});
|
|
62
21
|
try {
|
|
63
22
|
await server.start({ transportType: "stdio" });
|
|
64
|
-
// Transport/session readiness never waits for credentials or catalog I/O.
|
|
65
|
-
live.startInitialDiscovery();
|
|
66
23
|
}
|
|
67
24
|
catch (error) {
|
|
68
|
-
|
|
69
|
-
event: "startup",
|
|
70
|
-
outcome: "error",
|
|
71
|
-
...errorKind(error),
|
|
72
|
-
});
|
|
73
|
-
await shutdown();
|
|
74
|
-
console.error("Error starting MCP server");
|
|
25
|
+
console.error("Error starting server:", error);
|
|
75
26
|
process.exit(1);
|
|
76
27
|
}
|
|
77
28
|
}
|
|
78
|
-
|
|
79
|
-
const result = spawnSync("python3", [
|
|
80
|
-
fileURLToPath(new URL("./resources/setup-atlas.py", import.meta.url)),
|
|
81
|
-
...process.argv.slice(3),
|
|
82
|
-
], { stdio: "inherit" });
|
|
83
|
-
if (result.error)
|
|
84
|
-
console.error("Atlas setup requires Python 3 on PATH");
|
|
85
|
-
process.exitCode = result.status ?? 1;
|
|
86
|
-
}
|
|
87
|
-
else
|
|
88
|
-
void main().catch((error) => {
|
|
89
|
-
diagnosticLog().write({
|
|
90
|
-
event: "startup",
|
|
91
|
-
outcome: "error",
|
|
92
|
-
...errorKind(error),
|
|
93
|
-
});
|
|
94
|
-
console.error("MCP startup failed");
|
|
95
|
-
process.exitCode = 1;
|
|
96
|
-
});
|
|
29
|
+
main();
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { config } from "../config.js";
|
|
2
|
-
import { diagnosticFetch } from "../utils/diagnostics.js";
|
|
3
2
|
import { explainAgentKeyRejection } from "../utils/errors.js";
|
|
4
3
|
import { agentKeyHeaders } from "./agentKey.js";
|
|
5
4
|
/**
|
|
@@ -16,14 +15,8 @@ export class ShelbyAccount {
|
|
|
16
15
|
async getAccount(scope) {
|
|
17
16
|
return this.request("GET", scope, "getAccount");
|
|
18
17
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
await this.request("DELETE", scope, "deleteAccount", undefined, {
|
|
22
|
-
treat404AsSuccess: true,
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
async request(method, scope, label, body, { treat404AsSuccess = false } = {}) {
|
|
26
|
-
const res = await diagnosticFetch("s3_admin", `${config.shelby.s3AdminUrl}/account`, {
|
|
18
|
+
async request(method, scope, label, body) {
|
|
19
|
+
const res = await fetch(`${config.shelby.s3AdminUrl}/account`, {
|
|
27
20
|
method,
|
|
28
21
|
headers: {
|
|
29
22
|
...this.headers,
|
|
@@ -35,8 +28,6 @@ export class ShelbyAccount {
|
|
|
35
28
|
body: body === undefined ? undefined : JSON.stringify(body),
|
|
36
29
|
});
|
|
37
30
|
if (res.status === 404) {
|
|
38
|
-
if (treat404AsSuccess)
|
|
39
|
-
return undefined;
|
|
40
31
|
throw new Error(`S3 gateway admin ${label}: not found`);
|
|
41
32
|
}
|
|
42
33
|
if (!res.ok) {
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { createAdminApiClient, } from "@aptos-labs/api-gateway-admin-api-client";
|
|
2
2
|
import { config } from "../config.js";
|
|
3
|
-
import { diagnosticFetch } from "../utils/diagnostics.js";
|
|
4
3
|
import { explainAgentKeyRejection, formatError } from "../utils/errors.js";
|
|
5
4
|
import { agentKeyHeaders } from "./agentKey.js";
|
|
6
5
|
function describe(error) {
|
|
@@ -12,12 +11,7 @@ function describe(error) {
|
|
|
12
11
|
* application with service type "S3Gateway".
|
|
13
12
|
*/
|
|
14
13
|
export class ShelbyAdmin {
|
|
15
|
-
|
|
16
|
-
headers;
|
|
17
|
-
constructor(signal, botKey) {
|
|
18
|
-
this.signal = signal;
|
|
19
|
-
this.headers = agentKeyHeaders(botKey);
|
|
20
|
-
}
|
|
14
|
+
headers = agentKeyHeaders();
|
|
21
15
|
/** What the key reaches. `scope` is null for an unscoped (legacy) bot key. */
|
|
22
16
|
async getBotApiKeySelf() {
|
|
23
17
|
try {
|
|
@@ -38,7 +32,7 @@ export class ShelbyAdmin {
|
|
|
38
32
|
throw new Error(`Failed to get organizations: ${describe(error)}`);
|
|
39
33
|
}
|
|
40
34
|
}
|
|
41
|
-
async createBucketApplication({ name, organization_id, project_id,
|
|
35
|
+
async createBucketApplication({ name, organization_id, project_id, }) {
|
|
42
36
|
try {
|
|
43
37
|
return await this.createApiClient({
|
|
44
38
|
organization_id,
|
|
@@ -49,7 +43,7 @@ export class ShelbyAdmin {
|
|
|
49
43
|
name,
|
|
50
44
|
network: config.shelby.network,
|
|
51
45
|
description: null,
|
|
52
|
-
service_type:
|
|
46
|
+
service_type: config.shelby.serviceType,
|
|
53
47
|
},
|
|
54
48
|
]);
|
|
55
49
|
}
|
|
@@ -69,28 +63,6 @@ export class ShelbyAdmin {
|
|
|
69
63
|
throw new Error(`Failed to create api key: ${describe(error)}`);
|
|
70
64
|
}
|
|
71
65
|
}
|
|
72
|
-
async setApplicationName({ new_name, ...scope }) {
|
|
73
|
-
try {
|
|
74
|
-
await this.createApiClient(scope).mutation([
|
|
75
|
-
"setApplicationNameV2",
|
|
76
|
-
{ new_application_name: new_name },
|
|
77
|
-
]);
|
|
78
|
-
}
|
|
79
|
-
catch (error) {
|
|
80
|
-
throw new Error(`Failed to rename application: ${describe(error)}`);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
async deleteApplication(scope) {
|
|
84
|
-
try {
|
|
85
|
-
await this.createApiClient(scope).mutation([
|
|
86
|
-
"deleteApplicationV2",
|
|
87
|
-
{ _dummy: "" },
|
|
88
|
-
]);
|
|
89
|
-
}
|
|
90
|
-
catch (error) {
|
|
91
|
-
throw new Error(`Failed to delete application: ${describe(error)}`);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
66
|
createApiClient(scope = {}) {
|
|
95
67
|
const scopeHeaders = {
|
|
96
68
|
"x-jwt-organization-id": scope.organization_id,
|
|
@@ -108,11 +80,7 @@ export class ShelbyAdmin {
|
|
|
108
80
|
if (value)
|
|
109
81
|
headers.set(key, value);
|
|
110
82
|
}
|
|
111
|
-
return
|
|
112
|
-
...init,
|
|
113
|
-
headers,
|
|
114
|
-
signal: this.signal ?? init?.signal,
|
|
115
|
-
});
|
|
83
|
+
return fetch(input, { ...init, headers });
|
|
116
84
|
},
|
|
117
85
|
});
|
|
118
86
|
}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { createReadStream, createWriteStream } from "node:fs";
|
|
3
|
+
import { rename, stat, unlink } from "node:fs/promises";
|
|
4
|
+
import { basename, dirname, join } from "node:path";
|
|
3
5
|
import { Readable } from "node:stream";
|
|
6
|
+
import { pipeline } from "node:stream/promises";
|
|
4
7
|
import { config } from "../config.js";
|
|
5
|
-
import { diagnosticFetch } from "../utils/diagnostics.js";
|
|
6
|
-
import { streamDownload } from "../utils/download.js";
|
|
7
8
|
import { explainAgentKeyRejection } from "../utils/errors.js";
|
|
8
9
|
import { agentKeyHeaders } from "./agentKey.js";
|
|
9
10
|
/** Comfortably under the gateway's default 32 MiB request limit. */
|
|
@@ -129,7 +130,15 @@ export class ShelbyObjects {
|
|
|
129
130
|
const res = await this.send(target, "GET", key);
|
|
130
131
|
if (!res.body)
|
|
131
132
|
throw new Error("The S3 gateway returned no body");
|
|
132
|
-
|
|
133
|
+
const temp = join(dirname(destPath), `.${basename(destPath)}.shelby-${randomBytes(6).toString("hex")}`);
|
|
134
|
+
try {
|
|
135
|
+
await pipeline(Readable.fromWeb(res.body), createWriteStream(temp, { flags: "wx" }));
|
|
136
|
+
await rename(temp, destPath);
|
|
137
|
+
}
|
|
138
|
+
catch (error) {
|
|
139
|
+
await unlink(temp).catch(() => undefined);
|
|
140
|
+
throw error;
|
|
141
|
+
}
|
|
133
142
|
return { key, size: (await stat(destPath)).size };
|
|
134
143
|
}
|
|
135
144
|
fileBody(filePath, start, end) {
|
|
@@ -140,7 +149,7 @@ export class ShelbyObjects {
|
|
|
140
149
|
async send(target, method, key, query, { body, headers, } = {}) {
|
|
141
150
|
const path = key ? `/${encodeKey(key)}` : "";
|
|
142
151
|
const search = query ? `?${query.toString()}` : "";
|
|
143
|
-
const res = await
|
|
152
|
+
const res = await fetch(`${this.endpoint}/${target.bucketAddress}${path}${search}`, {
|
|
144
153
|
method,
|
|
145
154
|
headers: {
|
|
146
155
|
...this.headers,
|
|
@@ -1,11 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { readFileSync } from "node:fs";
|
|
3
|
-
import { readFile } from "node:fs/promises";
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
4
2
|
import { MISSING_AGENT_KEY_MESSAGE } from "../utils/errors.js";
|
|
5
3
|
/** The keychain entry `shelby-mcp keychain` style setups store the key under. */
|
|
6
4
|
export const KEYCHAIN_SERVICE = "shelby-mcp";
|
|
7
5
|
export const KEYCHAIN_ACCOUNT = "SHELBY_AGENT_KEY";
|
|
8
|
-
function
|
|
6
|
+
function readKeychain() {
|
|
9
7
|
const commands = {
|
|
10
8
|
darwin: [
|
|
11
9
|
"security",
|
|
@@ -23,10 +21,7 @@ function keychainCommand() {
|
|
|
23
21
|
["lookup", "service", KEYCHAIN_SERVICE, "account", KEYCHAIN_ACCOUNT],
|
|
24
22
|
],
|
|
25
23
|
};
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
function readKeychain() {
|
|
29
|
-
const command = keychainCommand();
|
|
24
|
+
const command = commands[process.platform];
|
|
30
25
|
if (!command)
|
|
31
26
|
return undefined;
|
|
32
27
|
try {
|
|
@@ -42,83 +37,27 @@ function readKeychain() {
|
|
|
42
37
|
}
|
|
43
38
|
}
|
|
44
39
|
let cached;
|
|
45
|
-
function keyFileValue(content) {
|
|
46
|
-
const value = content.trim();
|
|
47
|
-
if (!value || Buffer.byteLength(value) > 8192 || /\s/.test(value)) {
|
|
48
|
-
throw new Error("Invalid SHELBY_AGENT_KEY_FILE; expected one agent key");
|
|
49
|
-
}
|
|
50
|
-
return value;
|
|
51
|
-
}
|
|
52
|
-
function readKeyFile(path) {
|
|
53
|
-
try {
|
|
54
|
-
return keyFileValue(readFileSync(path, "utf8"));
|
|
55
|
-
}
|
|
56
|
-
catch {
|
|
57
|
-
throw new Error("Cannot read SHELBY_AGENT_KEY_FILE; check its path, permissions and contents");
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
40
|
/**
|
|
61
|
-
* The agent key, from SHELBY_AGENT_KEY
|
|
62
|
-
*
|
|
41
|
+
* The agent key, from `SHELBY_AGENT_KEY`, the older `SHELBY_BOT_KEY`, or the OS
|
|
42
|
+
* keychain, in that order.
|
|
63
43
|
*/
|
|
64
44
|
export function getAgentKey() {
|
|
65
45
|
cached ??=
|
|
66
46
|
process.env.SHELBY_AGENT_KEY ||
|
|
67
47
|
process.env.SHELBY_BOT_KEY ||
|
|
68
|
-
(process.env.SHELBY_AGENT_KEY_FILE
|
|
69
|
-
? readKeyFile(process.env.SHELBY_AGENT_KEY_FILE)
|
|
70
|
-
: undefined) ||
|
|
71
48
|
readKeychain();
|
|
72
49
|
if (!cached)
|
|
73
50
|
throw new Error(MISSING_AGENT_KEY_MESSAGE);
|
|
74
51
|
return cached;
|
|
75
52
|
}
|
|
76
|
-
/**
|
|
77
|
-
* Fresh credentials for live discovery. Never reads or updates the legacy cache.
|
|
78
|
-
* Config's .env loader populates the same environment used by getAgentKey().
|
|
79
|
-
* A caller's discovery signal bounds both the keychain process and network reads.
|
|
80
|
-
*/
|
|
81
|
-
export async function getAgentKeyAsync(signal, timeoutMs = 5_000) {
|
|
82
|
-
signal?.throwIfAborted();
|
|
83
|
-
const envKey = process.env.SHELBY_AGENT_KEY || process.env.SHELBY_BOT_KEY;
|
|
84
|
-
if (envKey)
|
|
85
|
-
return envKey;
|
|
86
|
-
if (process.env.SHELBY_AGENT_KEY_FILE) {
|
|
87
|
-
try {
|
|
88
|
-
return keyFileValue(await readFile(process.env.SHELBY_AGENT_KEY_FILE, {
|
|
89
|
-
encoding: "utf8",
|
|
90
|
-
signal,
|
|
91
|
-
}));
|
|
92
|
-
}
|
|
93
|
-
catch {
|
|
94
|
-
signal?.throwIfAborted();
|
|
95
|
-
throw new Error("Cannot read SHELBY_AGENT_KEY_FILE; check its path, permissions and contents");
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
const command = keychainCommand();
|
|
99
|
-
const key = command
|
|
100
|
-
? await new Promise((resolve) => {
|
|
101
|
-
execFile(command[0], command[1], {
|
|
102
|
-
encoding: "utf8",
|
|
103
|
-
timeout: Math.max(1, Math.min(5_000, timeoutMs)),
|
|
104
|
-
signal,
|
|
105
|
-
}, (error, stdout) => resolve(error ? undefined : stdout.trim() || undefined));
|
|
106
|
-
})
|
|
107
|
-
: undefined;
|
|
108
|
-
signal?.throwIfAborted();
|
|
109
|
-
if (!key)
|
|
110
|
-
throw new Error(MISSING_AGENT_KEY_MESSAGE);
|
|
111
|
-
return key;
|
|
112
|
-
}
|
|
113
53
|
/** Replace the agent key anywhere in `text`, so no tool output can carry it. */
|
|
114
|
-
export function redactAgentKey(text
|
|
115
|
-
|
|
116
|
-
return cached ? redacted.replaceAll(cached, "<agent-key>") : redacted;
|
|
54
|
+
export function redactAgentKey(text) {
|
|
55
|
+
return cached ? text.replaceAll(cached, "<agent-key>") : text;
|
|
117
56
|
}
|
|
118
57
|
/** Headers that authenticate a request as the agent key. */
|
|
119
|
-
export function agentKeyHeaders(
|
|
58
|
+
export function agentKeyHeaders() {
|
|
120
59
|
return {
|
|
121
|
-
Authorization: `Bearer ${
|
|
60
|
+
Authorization: `Bearer ${getAgentKey()}`,
|
|
122
61
|
"x-is-aptos-bot": "true",
|
|
123
62
|
};
|
|
124
63
|
}
|
package/dist/services/catalog.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { config } from "../config.js";
|
|
2
|
-
export function catalogFromSelf(self
|
|
2
|
+
export function catalogFromSelf(self) {
|
|
3
3
|
const scope = self.scope;
|
|
4
4
|
if (!scope) {
|
|
5
5
|
return {
|
|
@@ -21,7 +21,7 @@ export function catalogFromSelf(self, serviceType = config.shelby.serviceType) {
|
|
|
21
21
|
readOnly: scope.permission === "ReadOnly",
|
|
22
22
|
key: self,
|
|
23
23
|
buckets: scope.applications
|
|
24
|
-
.filter((app) => app.service_type === serviceType)
|
|
24
|
+
.filter((app) => app.service_type === config.shelby.serviceType)
|
|
25
25
|
.map((app) => ({
|
|
26
26
|
...project(app.project_id),
|
|
27
27
|
application_id: app.id,
|
|
@@ -74,7 +74,7 @@ export function expiryWarning(key = lastSelf, now = new Date()) {
|
|
|
74
74
|
const days = Math.ceil(msLeft / ONE_DAY_MS);
|
|
75
75
|
return `⚠️ This Shelby agent key expires in ${days === 1 ? "less than a day" : `${days} days`} (${key.expires_at}). Tell the user to create a new one in the Shelby console and update SHELBY_AGENT_KEY before then.`;
|
|
76
76
|
}
|
|
77
|
-
export async function loadCatalog(admin
|
|
77
|
+
export async function loadCatalog(admin) {
|
|
78
78
|
let self;
|
|
79
79
|
let selfError;
|
|
80
80
|
try {
|
|
@@ -86,7 +86,7 @@ export async function loadCatalog(admin, serviceType = config.shelby.serviceType
|
|
|
86
86
|
selfError = error;
|
|
87
87
|
}
|
|
88
88
|
if (self?.scope)
|
|
89
|
-
return catalogFromSelf(self
|
|
89
|
+
return catalogFromSelf(self);
|
|
90
90
|
try {
|
|
91
91
|
return catalogFromOrganizations(await admin.getOrganizationsRecursively(), self);
|
|
92
92
|
}
|
|
@@ -107,14 +107,14 @@ function describePlace(record) {
|
|
|
107
107
|
? `project "${record.project_name}" in organization "${record.organization_name}"`
|
|
108
108
|
: `project "${record.project_name}"`;
|
|
109
109
|
}
|
|
110
|
-
export function findBucket(catalog, name, placement = {}
|
|
110
|
+
export function findBucket(catalog, name, placement = {}) {
|
|
111
111
|
const candidates = catalog.buckets.filter((bucket) => bucket.name === name && matches(bucket, placement));
|
|
112
112
|
if (candidates.length === 1)
|
|
113
113
|
return candidates[0];
|
|
114
114
|
if (candidates.length === 0) {
|
|
115
|
-
throw new Error(`No
|
|
115
|
+
throw new Error(`No bucket named "${name}" that this key can reach. Use list_shelby_buckets to see them.`);
|
|
116
116
|
}
|
|
117
|
-
throw new Error(`Several
|
|
117
|
+
throw new Error(`Several buckets are named "${name}": in ${candidates.map(describePlace).join(", ")}. Pass the project (and organization if needed).`);
|
|
118
118
|
}
|
|
119
119
|
export function findCreatableProject(catalog, placement = {}) {
|
|
120
120
|
if (catalog.readOnly) {
|