@mindstone/mcp-server-runway 0.3.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/LICENSE +97 -0
- package/README.md +119 -0
- package/dist/auth.d.ts +21 -0
- package/dist/auth.js +29 -0
- package/dist/bridge.d.ts +16 -0
- package/dist/bridge.js +43 -0
- package/dist/client.d.ts +82 -0
- package/dist/client.js +562 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +30 -0
- package/dist/server.d.ts +3 -0
- package/dist/server.js +20 -0
- package/dist/tools/account.d.ts +3 -0
- package/dist/tools/account.js +71 -0
- package/dist/tools/audio.d.ts +3 -0
- package/dist/tools/audio.js +138 -0
- package/dist/tools/configure.d.ts +3 -0
- package/dist/tools/configure.js +46 -0
- package/dist/tools/image.d.ts +3 -0
- package/dist/tools/image.js +52 -0
- package/dist/tools/index.d.ts +8 -0
- package/dist/tools/index.js +8 -0
- package/dist/tools/tasks.d.ts +3 -0
- package/dist/tools/tasks.js +370 -0
- package/dist/tools/video.d.ts +3 -0
- package/dist/tools/video.js +168 -0
- package/dist/tools/voices.d.ts +3 -0
- package/dist/tools/voices.js +85 -0
- package/dist/types.d.ts +117 -0
- package/dist/types.js +123 -0
- package/dist/utils.d.ts +14 -0
- package/dist/utils.js +42 -0
- package/package.json +54 -0
package/dist/client.js
ADDED
|
@@ -0,0 +1,562 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runway API HTTP client.
|
|
3
|
+
*
|
|
4
|
+
* Centralises Bearer auth + X-Runway-Version header injection,
|
|
5
|
+
* error handling, rate-limit messaging, and timeout handling.
|
|
6
|
+
*
|
|
7
|
+
* Auth: Authorization: Bearer {key}, X-Runway-Version: 2024-11-06
|
|
8
|
+
* Base URL: https://api.dev.runwayml.com/v1
|
|
9
|
+
*/
|
|
10
|
+
import * as fs from 'fs';
|
|
11
|
+
import * as os from 'os';
|
|
12
|
+
import * as path from 'path';
|
|
13
|
+
import { RunwayError, getRequestTimeoutMs, getUploadTimeoutMs, RUNWAY_API_BASE, RUNWAY_API_VERSION, MIME_MAP, DATA_URI_BINARY_LIMITS, MAX_UPLOAD_BYTES, MIN_UPLOAD_BYTES, } from './types.js';
|
|
14
|
+
import { getApiKey } from './auth.js';
|
|
15
|
+
// ============================================================================
|
|
16
|
+
// Local-file sandbox
|
|
17
|
+
// ============================================================================
|
|
18
|
+
//
|
|
19
|
+
// `upload_media` and `resolveMediaInput` (used by every prompt_image / video /
|
|
20
|
+
// audio / character / reference_* / media / file_path argument) read local
|
|
21
|
+
// files supplied by an LLM-controlled tool input. Without a sandbox, a
|
|
22
|
+
// malicious or hallucinated argument can name an arbitrary path on the host —
|
|
23
|
+
// e.g. `~/.ssh/id_rsa` or `/etc/passwd` — and exfiltrate its bytes to the
|
|
24
|
+
// upstream Runway API.
|
|
25
|
+
//
|
|
26
|
+
// The sandbox restricts local-file reads to a single allow-listed root
|
|
27
|
+
// directory tree:
|
|
28
|
+
//
|
|
29
|
+
// * Configurable via `RUNWAY_ALLOWED_ROOT`.
|
|
30
|
+
// * Defaults to `os.tmpdir()` when the env var is unset / empty.
|
|
31
|
+
// * Both the configured root and the input path are canonicalised via
|
|
32
|
+
// `fs.realpathSync`, which catches symlink-escapes (a symlink inside the
|
|
33
|
+
// root that points OUTSIDE the root is rejected).
|
|
34
|
+
// * `..` traversal is caught because realpath resolves it to the actual
|
|
35
|
+
// destination (which then fails the allow-list check).
|
|
36
|
+
//
|
|
37
|
+
// HTTPS, `data:`, and `runway://` URIs bypass this branch entirely — they
|
|
38
|
+
// don't read disk at all.
|
|
39
|
+
const RUNWAY_ALLOWED_ROOT_ENV = 'RUNWAY_ALLOWED_ROOT';
|
|
40
|
+
function getAllowedRoot() {
|
|
41
|
+
const raw = process.env[RUNWAY_ALLOWED_ROOT_ENV];
|
|
42
|
+
const root = raw && raw.length > 0 ? raw : os.tmpdir();
|
|
43
|
+
// Canonicalise the root once so the prefix-check below is stable across
|
|
44
|
+
// platforms where the system tmpdir is itself reached through a symlink
|
|
45
|
+
// (e.g. macOS: /var/folders/... → /private/var/folders/...).
|
|
46
|
+
return fs.realpathSync(path.resolve(root));
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Validate that `input` is a local file path inside the allow-listed root,
|
|
50
|
+
* canonicalising via `fs.realpathSync` so that symlink-escape attempts are
|
|
51
|
+
* caught. Returns the canonicalised (or, for not-yet-existing files, the
|
|
52
|
+
* lexically-resolved) path.
|
|
53
|
+
*
|
|
54
|
+
* Throws a structured `RunwayError` with code `PATH_OUTSIDE_ALLOWED_ROOT`
|
|
55
|
+
* when the resolved path falls outside the allow-listed root. Does NOT
|
|
56
|
+
* throw for "file does not exist" — callers handle that via their existing
|
|
57
|
+
* `FILE_NOT_FOUND` paths so error attribution stays clean.
|
|
58
|
+
*/
|
|
59
|
+
export function assertPathInAllowedRoot(input) {
|
|
60
|
+
const root = getAllowedRoot();
|
|
61
|
+
const lexical = path.resolve(input);
|
|
62
|
+
const denyMessage = `File path is outside the RUNWAY_ALLOWED_ROOT sandbox: ${input}`;
|
|
63
|
+
const denyResolution = `Set ${RUNWAY_ALLOWED_ROOT_ENV} to a directory that contains the file, ` +
|
|
64
|
+
`or move the file under ${root}.`;
|
|
65
|
+
const isInsideRoot = (p) => p === root || p.startsWith(root + path.sep);
|
|
66
|
+
// Try to canonicalise the input. If it exists, this also follows any
|
|
67
|
+
// symlinks — catching the "symlink inside the root pointing outside"
|
|
68
|
+
// attack, since the resolved target is what we check.
|
|
69
|
+
try {
|
|
70
|
+
const resolved = fs.realpathSync(lexical);
|
|
71
|
+
if (!isInsideRoot(resolved)) {
|
|
72
|
+
throw new RunwayError(denyMessage, 'PATH_OUTSIDE_ALLOWED_ROOT', denyResolution);
|
|
73
|
+
}
|
|
74
|
+
return resolved;
|
|
75
|
+
}
|
|
76
|
+
catch (err) {
|
|
77
|
+
if (err instanceof RunwayError)
|
|
78
|
+
throw err;
|
|
79
|
+
// realpath failed (most likely ENOENT). Try canonicalising the parent
|
|
80
|
+
// dir so callers can still distinguish "outside the sandbox" from
|
|
81
|
+
// "doesn't exist" cases. If the parent canonicalises and is outside,
|
|
82
|
+
// refuse. Otherwise, fall through to a lexical-only comparison.
|
|
83
|
+
const parent = path.dirname(lexical);
|
|
84
|
+
try {
|
|
85
|
+
const resolvedParent = fs.realpathSync(parent);
|
|
86
|
+
if (!isInsideRoot(resolvedParent)) {
|
|
87
|
+
throw new RunwayError(denyMessage, 'PATH_OUTSIDE_ALLOWED_ROOT', denyResolution);
|
|
88
|
+
}
|
|
89
|
+
return path.join(resolvedParent, path.basename(lexical));
|
|
90
|
+
}
|
|
91
|
+
catch (err2) {
|
|
92
|
+
if (err2 instanceof RunwayError)
|
|
93
|
+
throw err2;
|
|
94
|
+
// Even the parent doesn't resolve; use lexical comparison as a
|
|
95
|
+
// last-resort defence. This is conservative: a non-existent path
|
|
96
|
+
// outside the lexical root is rejected; an inside-the-root path is
|
|
97
|
+
// returned so the caller's existing FILE_NOT_FOUND surfaces cleanly.
|
|
98
|
+
if (!isInsideRoot(lexical)) {
|
|
99
|
+
throw new RunwayError(denyMessage, 'PATH_OUTSIDE_ALLOWED_ROOT', denyResolution);
|
|
100
|
+
}
|
|
101
|
+
return lexical;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
// ============================================================================
|
|
106
|
+
// Local-file sandbox — `download_runway_output` write target
|
|
107
|
+
// ============================================================================
|
|
108
|
+
//
|
|
109
|
+
// `download_runway_output` writes a file to disk at an LLM-controlled path.
|
|
110
|
+
// Without a sandbox, an attacker who can influence the tool input can overwrite
|
|
111
|
+
// arbitrary files — `~/.zshrc`, `~/.ssh/authorized_keys`, `/etc/cron.daily/x`,
|
|
112
|
+
// etc. — turning a "download a video" tool into a turn-key code-execution
|
|
113
|
+
// path on the host.
|
|
114
|
+
//
|
|
115
|
+
// The output-write sandbox restricts where the connector may create files:
|
|
116
|
+
//
|
|
117
|
+
// * Configurable via `RUNWAY_DOWNLOAD_ROOT`.
|
|
118
|
+
// * Defaults to `~/Downloads/runway-mcp` when the env var is unset / empty.
|
|
119
|
+
// (NOT `os.tmpdir()` — downloaded files are user-facing artifacts.)
|
|
120
|
+
// * Auto-creates the root directory if missing so the default works
|
|
121
|
+
// out-of-the-box.
|
|
122
|
+
// * The PARENT DIR of the requested output_path is canonicalised via
|
|
123
|
+
// `fs.realpathSync` and checked against the canonicalised root, catching
|
|
124
|
+
// symlink-escape attacks (a writable symlink inside the root pointing
|
|
125
|
+
// outside it is rejected).
|
|
126
|
+
// * `..` traversal is caught lexically so a path that resolves outside the
|
|
127
|
+
// root cannot reach realpath (and never depends on whether the upper
|
|
128
|
+
// directory exists).
|
|
129
|
+
// * A static deny-list of sensitive globs (`~/.ssh/**`, `~/.aws/**`,
|
|
130
|
+
// `~/.bashrc`, `~/.zshrc`, `/etc/**`) is refused EVEN WHEN the configured
|
|
131
|
+
// root would otherwise allow it (e.g. an attacker setting
|
|
132
|
+
// `RUNWAY_DOWNLOAD_ROOT=$HOME` or `=/`). This is the canonical
|
|
133
|
+
// "sensitive paths" set.
|
|
134
|
+
// * The actual write opens with `flags: 'wx'` (atomic refuse-on-existing)
|
|
135
|
+
// unless the caller passes `overwrite: true` to clobber.
|
|
136
|
+
const RUNWAY_DOWNLOAD_ROOT_ENV = 'RUNWAY_DOWNLOAD_ROOT';
|
|
137
|
+
function getDownloadRoot() {
|
|
138
|
+
const raw = process.env[RUNWAY_DOWNLOAD_ROOT_ENV];
|
|
139
|
+
if (raw && raw.length > 0)
|
|
140
|
+
return path.resolve(raw);
|
|
141
|
+
return path.join(os.homedir(), 'Downloads', 'runway-mcp');
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Sensitive paths that are refused even when the caller's
|
|
145
|
+
* `RUNWAY_DOWNLOAD_ROOT` would otherwise allow them. Each entry is checked
|
|
146
|
+
* against the realpath-resolved output target.
|
|
147
|
+
*/
|
|
148
|
+
function isPathInDenyList(resolvedAbs) {
|
|
149
|
+
let home;
|
|
150
|
+
try {
|
|
151
|
+
home = fs.realpathSync(os.homedir());
|
|
152
|
+
}
|
|
153
|
+
catch {
|
|
154
|
+
home = os.homedir();
|
|
155
|
+
}
|
|
156
|
+
const sshDir = path.join(home, '.ssh');
|
|
157
|
+
const awsDir = path.join(home, '.aws');
|
|
158
|
+
const bashrc = path.join(home, '.bashrc');
|
|
159
|
+
const zshrc = path.join(home, '.zshrc');
|
|
160
|
+
const etcRoot = path.resolve('/etc');
|
|
161
|
+
const isUnder = (p, root) => p === root || p.startsWith(root + path.sep);
|
|
162
|
+
if (isUnder(resolvedAbs, sshDir))
|
|
163
|
+
return { hit: true, reason: '~/.ssh/** is sensitive' };
|
|
164
|
+
if (isUnder(resolvedAbs, awsDir))
|
|
165
|
+
return { hit: true, reason: '~/.aws/** is sensitive' };
|
|
166
|
+
if (resolvedAbs === bashrc)
|
|
167
|
+
return { hit: true, reason: '~/.bashrc is sensitive' };
|
|
168
|
+
if (resolvedAbs === zshrc)
|
|
169
|
+
return { hit: true, reason: '~/.zshrc is sensitive' };
|
|
170
|
+
if (isUnder(resolvedAbs, etcRoot))
|
|
171
|
+
return { hit: true, reason: '/etc/** is sensitive' };
|
|
172
|
+
return { hit: false };
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Validate that `outputPath` is a writable destination for
|
|
176
|
+
* `download_runway_output`:
|
|
177
|
+
* 1. Must lexically resolve under `RUNWAY_DOWNLOAD_ROOT` (catches `..`).
|
|
178
|
+
* 2. Parent dir must `realpathSync` cleanly into the same root (catches
|
|
179
|
+
* symlink-escape).
|
|
180
|
+
* 3. Resolved target must not match the static deny-list (catches the
|
|
181
|
+
* `RUNWAY_DOWNLOAD_ROOT=$HOME` / `=/` defeat path).
|
|
182
|
+
*
|
|
183
|
+
* Returns the realpath-resolved write target on success, or throws a
|
|
184
|
+
* structured `RunwayError` otherwise. Auto-creates the configured root
|
|
185
|
+
* directory if missing so the default `~/Downloads/runway-mcp` works
|
|
186
|
+
* out-of-the-box.
|
|
187
|
+
*/
|
|
188
|
+
export function assertDownloadPathInRoot(outputPath) {
|
|
189
|
+
const lexicalRoot = getDownloadRoot();
|
|
190
|
+
// Auto-create the configured root so the default just works. Tolerate
|
|
191
|
+
// failures here — they'll surface later when we try to canonicalise it.
|
|
192
|
+
try {
|
|
193
|
+
fs.mkdirSync(lexicalRoot, { recursive: true });
|
|
194
|
+
}
|
|
195
|
+
catch {
|
|
196
|
+
/* tolerate; realpath below will fail with a clear error */
|
|
197
|
+
}
|
|
198
|
+
const realRoot = fs.realpathSync(lexicalRoot);
|
|
199
|
+
const lexical = path.resolve(outputPath);
|
|
200
|
+
const lexicalParent = path.dirname(lexical);
|
|
201
|
+
const denyMessage = `Output path is outside the RUNWAY_DOWNLOAD_ROOT sandbox: ${outputPath}`;
|
|
202
|
+
const denyResolution = `Set ${RUNWAY_DOWNLOAD_ROOT_ENV} to a directory that contains the target file, ` +
|
|
203
|
+
`or pick an output_path inside ${realRoot}.`;
|
|
204
|
+
const isUnder = (p, root) => p === root || p.startsWith(root + path.sep);
|
|
205
|
+
// (1) Lexical check — catches `..` traversal and "completely outside"
|
|
206
|
+
// paths regardless of filesystem state.
|
|
207
|
+
if (!isUnder(lexicalParent, lexicalRoot) && !isUnder(lexicalParent, realRoot)) {
|
|
208
|
+
throw new RunwayError(denyMessage, 'OUTPUT_OUTSIDE_DOWNLOAD_ROOT', denyResolution);
|
|
209
|
+
}
|
|
210
|
+
// (2) Realpath check on the parent dir — catches symlink-escape.
|
|
211
|
+
let realParent;
|
|
212
|
+
try {
|
|
213
|
+
realParent = fs.realpathSync(lexicalParent);
|
|
214
|
+
}
|
|
215
|
+
catch {
|
|
216
|
+
throw new RunwayError(`Parent directory does not exist or is not accessible: ${lexicalParent}`, 'OUTPUT_PARENT_NOT_FOUND', `Create ${lexicalParent} first, or pick an output_path inside ${realRoot}.`);
|
|
217
|
+
}
|
|
218
|
+
if (!isUnder(realParent, realRoot)) {
|
|
219
|
+
throw new RunwayError(denyMessage, 'OUTPUT_OUTSIDE_DOWNLOAD_ROOT', denyResolution);
|
|
220
|
+
}
|
|
221
|
+
const resolved = path.join(realParent, path.basename(lexical));
|
|
222
|
+
// (3) Static deny-list — applies even when nominally inside the root.
|
|
223
|
+
const denied = isPathInDenyList(resolved);
|
|
224
|
+
if (denied.hit) {
|
|
225
|
+
throw new RunwayError(`Output path matches the sensitive deny-list and is refused: ${outputPath}`, 'OUTPUT_PATH_DENY_LISTED', `Refused: ${denied.reason}. Paths under ~/.ssh, ~/.aws, /etc, and shell rc files (~/.bashrc, ~/.zshrc) cannot be overwritten by Runway downloads even when ${RUNWAY_DOWNLOAD_ROOT_ENV} would otherwise permit it.`);
|
|
226
|
+
}
|
|
227
|
+
// (4) Refuse to write through a non-regular-file existing target.
|
|
228
|
+
//
|
|
229
|
+
// The parent-dir realpath check above does NOT inspect the terminal
|
|
230
|
+
// filename — that's appended via `path.basename(lexical)`. Without an
|
|
231
|
+
// explicit lstat here, `fs.openSync(resolved, 'w')` (under
|
|
232
|
+
// `overwrite: true`) would silently follow a pre-existing symlink at
|
|
233
|
+
// the target path and clobber whatever the symlink points at, even when
|
|
234
|
+
// the destination is outside the allow-listed root. This same gap also
|
|
235
|
+
// applies to other non-regular-file targets (directories, FIFOs,
|
|
236
|
+
// sockets, character/block devices) — overwriting them via `openSync`
|
|
237
|
+
// either fails noisily (EISDIR) or silently writes to an unintended
|
|
238
|
+
// sink. Refuse all of these here with structured error codes so the
|
|
239
|
+
// refusal is clean and pre-existing regular-file behaviour (governed
|
|
240
|
+
// by `overwrite` at the caller) is preserved.
|
|
241
|
+
//
|
|
242
|
+
// ENOENT (target does not yet exist) is the happy path: the caller's
|
|
243
|
+
// `flags: 'wx'` / `'w'` choice handles the create, and ENOENT here is
|
|
244
|
+
// not a sandbox failure.
|
|
245
|
+
try {
|
|
246
|
+
const lst = fs.lstatSync(resolved);
|
|
247
|
+
if (lst.isSymbolicLink()) {
|
|
248
|
+
throw new RunwayError(`Output path already exists as a symbolic link, refusing to write through it: ${outputPath}`, 'OUTPUT_PATH_IS_SYMLINK', 'Remove or rename the existing symlink before retrying. Runway downloads refuse to follow symlinks at the output target — even when overwrite=true — to prevent writing through a symlink to an unintended location.');
|
|
249
|
+
}
|
|
250
|
+
if (!lst.isFile()) {
|
|
251
|
+
throw new RunwayError(`Output path already exists and is not a regular file (directory, FIFO, socket, or other special file): ${outputPath}`, 'OUTPUT_PATH_NOT_REGULAR_FILE', 'Remove or rename the existing target before retrying, or pick a different output_path. Runway downloads only write to fresh paths or to existing regular files (with overwrite=true).');
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
catch (err) {
|
|
255
|
+
if (err instanceof RunwayError)
|
|
256
|
+
throw err;
|
|
257
|
+
const e = err;
|
|
258
|
+
// ENOENT: missing target — not a sandbox failure. Anything else is
|
|
259
|
+
// an unexpected lstat error and should propagate (e.g. EACCES).
|
|
260
|
+
if (e?.code !== 'ENOENT')
|
|
261
|
+
throw err;
|
|
262
|
+
}
|
|
263
|
+
return { resolved, root: realRoot };
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Make an authenticated JSON request to the Runway API.
|
|
267
|
+
*/
|
|
268
|
+
export async function runwayFetch(urlPath, options = {}) {
|
|
269
|
+
const apiKey = getApiKey();
|
|
270
|
+
if (!apiKey || apiKey.trim().length === 0) {
|
|
271
|
+
throw new RunwayError('Runway API key not configured', 'AUTH_REQUIRED', 'Configure your Runway API key in Settings. Get one at https://dev.runwayml.com/');
|
|
272
|
+
}
|
|
273
|
+
const url = `${RUNWAY_API_BASE}${urlPath}`;
|
|
274
|
+
console.error(`[Runway API] ${options.method || 'GET'} ${url}`);
|
|
275
|
+
let response;
|
|
276
|
+
const timeoutMs = getRequestTimeoutMs();
|
|
277
|
+
const timeoutSignal = AbortSignal.timeout(timeoutMs);
|
|
278
|
+
const callerSignal = options.signal ?? undefined;
|
|
279
|
+
const fetchSignal = callerSignal === undefined ? timeoutSignal : AbortSignal.any([callerSignal, timeoutSignal]);
|
|
280
|
+
try {
|
|
281
|
+
response = await fetch(url, {
|
|
282
|
+
...options,
|
|
283
|
+
signal: fetchSignal,
|
|
284
|
+
headers: {
|
|
285
|
+
'Content-Type': 'application/json',
|
|
286
|
+
'Authorization': `Bearer ${apiKey}`,
|
|
287
|
+
'X-Runway-Version': RUNWAY_API_VERSION,
|
|
288
|
+
...(options.headers || {}),
|
|
289
|
+
},
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
catch (error) {
|
|
293
|
+
// Attribute timeout to OUR signal only (not any caller-supplied TimeoutError):
|
|
294
|
+
// timeoutSignal.aborted goes true iff its timer actually expired. If the caller
|
|
295
|
+
// aborted first, their AbortError rethrows unchanged.
|
|
296
|
+
if (timeoutSignal.aborted) {
|
|
297
|
+
const timeoutSec = Math.round(timeoutMs / 1000);
|
|
298
|
+
throw new RunwayError(`Request to Runway API timed out after ${timeoutSec}s`, 'TIMEOUT', `The request took longer than ${timeoutSec}s. Set RUNWAY_REQUEST_TIMEOUT_MS to increase the timeout, or try again.`);
|
|
299
|
+
}
|
|
300
|
+
throw error;
|
|
301
|
+
}
|
|
302
|
+
if (response.status === 429) {
|
|
303
|
+
throw new RunwayError('Rate limited.', 'RATE_LIMITED', 'Wait a moment and try again.');
|
|
304
|
+
}
|
|
305
|
+
if (response.status === 401) {
|
|
306
|
+
throw new RunwayError('Authentication failed.', 'AUTH_FAILED', 'Check your Runway API key.');
|
|
307
|
+
}
|
|
308
|
+
if (response.status === 403) {
|
|
309
|
+
throw new RunwayError('Access forbidden.', 'AUTH_FAILED', 'Check your Runway API key and account permissions.');
|
|
310
|
+
}
|
|
311
|
+
if (response.status === 404) {
|
|
312
|
+
throw new RunwayError('Resource not found.', 'NOT_FOUND', 'The resource does not exist or was deleted.');
|
|
313
|
+
}
|
|
314
|
+
if (!response.ok) {
|
|
315
|
+
let detail = '';
|
|
316
|
+
try {
|
|
317
|
+
detail = (await response.json())?.error || '';
|
|
318
|
+
}
|
|
319
|
+
catch { /* empty */ }
|
|
320
|
+
throw new RunwayError(`Runway API error (HTTP ${response.status}): ${detail}`, `HTTP_${response.status}`, 'Try again or check https://dev.runwayml.com/');
|
|
321
|
+
}
|
|
322
|
+
return (await response.json());
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Make a raw authenticated fetch (for DELETE endpoints that return 204 with no body).
|
|
326
|
+
*/
|
|
327
|
+
export async function runwayRawFetch(urlPath, options = {}) {
|
|
328
|
+
const apiKey = getApiKey();
|
|
329
|
+
if (!apiKey || apiKey.trim().length === 0) {
|
|
330
|
+
throw new RunwayError('Runway API key not configured', 'AUTH_REQUIRED', 'Configure your Runway API key in Settings. Get one at https://dev.runwayml.com/');
|
|
331
|
+
}
|
|
332
|
+
const url = `${RUNWAY_API_BASE}${urlPath}`;
|
|
333
|
+
console.error(`[Runway API] ${options.method || 'GET'} ${url}`);
|
|
334
|
+
let response;
|
|
335
|
+
const timeoutMs = getRequestTimeoutMs();
|
|
336
|
+
const timeoutSignal = AbortSignal.timeout(timeoutMs);
|
|
337
|
+
const callerSignal = options.signal ?? undefined;
|
|
338
|
+
const fetchSignal = callerSignal === undefined ? timeoutSignal : AbortSignal.any([callerSignal, timeoutSignal]);
|
|
339
|
+
try {
|
|
340
|
+
response = await fetch(url, {
|
|
341
|
+
...options,
|
|
342
|
+
signal: fetchSignal,
|
|
343
|
+
headers: {
|
|
344
|
+
'Authorization': `Bearer ${apiKey}`,
|
|
345
|
+
'X-Runway-Version': RUNWAY_API_VERSION,
|
|
346
|
+
...(options.headers || {}),
|
|
347
|
+
},
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
catch (error) {
|
|
351
|
+
// Attribute timeout to OUR signal only (not any caller-supplied TimeoutError):
|
|
352
|
+
// timeoutSignal.aborted goes true iff its timer actually expired. If the caller
|
|
353
|
+
// aborted first, their AbortError rethrows unchanged.
|
|
354
|
+
if (timeoutSignal.aborted) {
|
|
355
|
+
const timeoutSec = Math.round(timeoutMs / 1000);
|
|
356
|
+
throw new RunwayError(`Request to Runway API timed out after ${timeoutSec}s`, 'TIMEOUT', `The request took longer than ${timeoutSec}s. Set RUNWAY_REQUEST_TIMEOUT_MS to increase the timeout, or try again.`);
|
|
357
|
+
}
|
|
358
|
+
throw error;
|
|
359
|
+
}
|
|
360
|
+
if (response.status === 429) {
|
|
361
|
+
throw new RunwayError('Rate limited.', 'RATE_LIMITED', 'Wait a moment and try again.');
|
|
362
|
+
}
|
|
363
|
+
if (response.status === 401) {
|
|
364
|
+
throw new RunwayError('Authentication failed.', 'AUTH_FAILED', 'Check your Runway API key.');
|
|
365
|
+
}
|
|
366
|
+
if (response.status === 403) {
|
|
367
|
+
throw new RunwayError('Access forbidden.', 'AUTH_FAILED', 'Check your Runway API key and account permissions.');
|
|
368
|
+
}
|
|
369
|
+
return response;
|
|
370
|
+
}
|
|
371
|
+
// ============================================================================
|
|
372
|
+
// File resolution & ephemeral upload helpers
|
|
373
|
+
// ============================================================================
|
|
374
|
+
/**
|
|
375
|
+
* Upload a local file to Runway's ephemeral storage.
|
|
376
|
+
* Returns a runway:// URI valid for 24 hours.
|
|
377
|
+
*
|
|
378
|
+
* SECURITY: refuses paths outside `RUNWAY_ALLOWED_ROOT` (default
|
|
379
|
+
* `os.tmpdir()`); see `assertPathInAllowedRoot`. The sandbox check runs
|
|
380
|
+
* BEFORE any file read or upstream API call, so a disallowed path never
|
|
381
|
+
* triggers an `/uploads` request and the file's bytes are never read.
|
|
382
|
+
*/
|
|
383
|
+
export async function uploadEphemeral(filePath) {
|
|
384
|
+
// Sandbox check first — refuse before reading the file or hitting /uploads.
|
|
385
|
+
const safePath = assertPathInAllowedRoot(filePath);
|
|
386
|
+
if (!fs.existsSync(safePath)) {
|
|
387
|
+
throw new RunwayError(`File not found: ${filePath}`, 'FILE_NOT_FOUND', 'Provide an accessible local file path.');
|
|
388
|
+
}
|
|
389
|
+
const stats = fs.statSync(safePath);
|
|
390
|
+
if (!stats.isFile()) {
|
|
391
|
+
throw new RunwayError(`Not a file: ${filePath}`, 'INVALID_INPUT', 'Provide a file path, not a directory.');
|
|
392
|
+
}
|
|
393
|
+
if (stats.size > MAX_UPLOAD_BYTES) {
|
|
394
|
+
throw new RunwayError('File exceeds 200MB upload limit.', 'FILE_TOO_LARGE', 'Reduce the file size or use a URL instead.');
|
|
395
|
+
}
|
|
396
|
+
if (stats.size < MIN_UPLOAD_BYTES) {
|
|
397
|
+
throw new RunwayError('File must be at least 512 bytes.', 'FILE_TOO_SMALL', 'Provide a valid media file.');
|
|
398
|
+
}
|
|
399
|
+
const filename = path.basename(safePath);
|
|
400
|
+
const uploadInfo = await runwayFetch('/uploads', {
|
|
401
|
+
method: 'POST',
|
|
402
|
+
body: JSON.stringify({ filename, type: 'ephemeral' }),
|
|
403
|
+
});
|
|
404
|
+
const fileBuffer = fs.readFileSync(safePath);
|
|
405
|
+
const formData = new FormData();
|
|
406
|
+
for (const [key, value] of Object.entries(uploadInfo.fields)) {
|
|
407
|
+
formData.append(key, value);
|
|
408
|
+
}
|
|
409
|
+
formData.append('file', new Blob([fileBuffer]), filename);
|
|
410
|
+
// Signed-URL upload to external storage (not the Runway API). Uses a
|
|
411
|
+
// workload-appropriate timeout — uploads can be up to 200MB, unlike the
|
|
412
|
+
// sub-second JSON calls covered by `getRequestTimeoutMs()`. No caller
|
|
413
|
+
// signal is plumbed here, so a bare `timeoutSignal.aborted` check is
|
|
414
|
+
// sufficient (same shape as napkin's downloadFile path).
|
|
415
|
+
const uploadTimeoutMs = getUploadTimeoutMs();
|
|
416
|
+
const uploadTimeoutSignal = AbortSignal.timeout(uploadTimeoutMs);
|
|
417
|
+
let uploadRes;
|
|
418
|
+
try {
|
|
419
|
+
uploadRes = await fetch(uploadInfo.uploadUrl, {
|
|
420
|
+
method: 'POST',
|
|
421
|
+
body: formData,
|
|
422
|
+
signal: uploadTimeoutSignal,
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
catch (error) {
|
|
426
|
+
if (uploadTimeoutSignal.aborted) {
|
|
427
|
+
const timeoutSec = Math.round(uploadTimeoutMs / 1000);
|
|
428
|
+
throw new RunwayError(`Runway upload timed out after ${timeoutSec}s`, 'TIMEOUT', `The signed-URL upload took longer than ${timeoutSec}s. Set RUNWAY_UPLOAD_TIMEOUT_MS to increase the timeout, or reduce the file size / use a faster connection.`);
|
|
429
|
+
}
|
|
430
|
+
throw error;
|
|
431
|
+
}
|
|
432
|
+
if (!uploadRes.ok && uploadRes.status !== 204) {
|
|
433
|
+
throw new RunwayError(`Upload failed (HTTP ${uploadRes.status})`, 'UPLOAD_FAILED', 'Try again. If the file is too large (max 200MB), reduce its size.');
|
|
434
|
+
}
|
|
435
|
+
return uploadInfo.runwayUri;
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Resolve a media input to a usable URI.
|
|
439
|
+
* - HTTPS/data/runway URIs are passed through.
|
|
440
|
+
* - Local files under the size limit are converted to data URIs.
|
|
441
|
+
* - Large local files are uploaded via ephemeral upload.
|
|
442
|
+
*
|
|
443
|
+
* SECURITY: local-file inputs are sandboxed under `RUNWAY_ALLOWED_ROOT`
|
|
444
|
+
* (default `os.tmpdir()`). Paths outside the sandbox — including symlinks
|
|
445
|
+
* pointing outside it — are refused before any file read.
|
|
446
|
+
*/
|
|
447
|
+
export async function resolveMediaInput(input, category) {
|
|
448
|
+
if (input.startsWith('https://') || input.startsWith('data:') || input.startsWith('runway://')) {
|
|
449
|
+
return input;
|
|
450
|
+
}
|
|
451
|
+
// Sandbox the local-file branch first so a disallowed path is never read.
|
|
452
|
+
const safePath = assertPathInAllowedRoot(input);
|
|
453
|
+
try {
|
|
454
|
+
const stats = fs.statSync(safePath);
|
|
455
|
+
if (!stats.isFile()) {
|
|
456
|
+
throw new RunwayError(`Not a file: ${input}`, 'INVALID_INPUT', 'Provide a file path, not a directory.');
|
|
457
|
+
}
|
|
458
|
+
const limit = DATA_URI_BINARY_LIMITS[category];
|
|
459
|
+
if (stats.size > limit) {
|
|
460
|
+
return await uploadEphemeral(safePath);
|
|
461
|
+
}
|
|
462
|
+
const buffer = fs.readFileSync(safePath);
|
|
463
|
+
const ext = input.split('.').pop()?.toLowerCase() || 'bin';
|
|
464
|
+
const fallback = category === 'image' ? 'image/png' : category === 'video' ? 'video/mp4' : 'audio/mpeg';
|
|
465
|
+
const mime = MIME_MAP[ext] || fallback;
|
|
466
|
+
return `data:${mime};base64,${buffer.toString('base64')}`;
|
|
467
|
+
}
|
|
468
|
+
catch (err) {
|
|
469
|
+
if (err instanceof RunwayError)
|
|
470
|
+
throw err;
|
|
471
|
+
throw new RunwayError(`Could not read file: ${input}`, 'FILE_NOT_FOUND', 'Provide a valid HTTPS URL, Runway URI, or accessible local file path.');
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
// ============================================================================
|
|
475
|
+
// SSRF / Host validation for download URLs
|
|
476
|
+
// ============================================================================
|
|
477
|
+
/**
|
|
478
|
+
* Check whether a hostname is private, localhost, or otherwise reserved.
|
|
479
|
+
* Matches the same patterns as Workday's SSRF prevention.
|
|
480
|
+
*/
|
|
481
|
+
function isPrivateOrReservedHost(hostname) {
|
|
482
|
+
const lower = hostname.toLowerCase();
|
|
483
|
+
// Localhost names
|
|
484
|
+
if (lower === 'localhost' || lower === '[::1]' || lower === '::1') {
|
|
485
|
+
return true;
|
|
486
|
+
}
|
|
487
|
+
// .local domains
|
|
488
|
+
if (lower.endsWith('.local')) {
|
|
489
|
+
return true;
|
|
490
|
+
}
|
|
491
|
+
// IPv4 private/reserved ranges
|
|
492
|
+
const ipMatch = hostname.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
|
|
493
|
+
if (ipMatch) {
|
|
494
|
+
const [, a, b] = ipMatch.map(Number);
|
|
495
|
+
if (a === 127)
|
|
496
|
+
return true; // 127.0.0.0/8 loopback
|
|
497
|
+
if (a === 10)
|
|
498
|
+
return true; // 10.0.0.0/8 private
|
|
499
|
+
if (a === 172 && b >= 16 && b <= 31)
|
|
500
|
+
return true; // 172.16.0.0/12 private
|
|
501
|
+
if (a === 192 && b === 168)
|
|
502
|
+
return true; // 192.168.0.0/16 private
|
|
503
|
+
if (a === 169 && b === 254)
|
|
504
|
+
return true; // 169.254.0.0/16 link-local
|
|
505
|
+
if (a === 0)
|
|
506
|
+
return true; // 0.0.0.0/8
|
|
507
|
+
}
|
|
508
|
+
// IPv6 private/loopback (bracket-wrapped from URL parsing)
|
|
509
|
+
if (lower.startsWith('[') && lower.endsWith(']')) {
|
|
510
|
+
const inner = lower.slice(1, -1);
|
|
511
|
+
if (inner === '::1' || inner === '::' || inner.startsWith('fe80:') || inner.startsWith('fc') || inner.startsWith('fd')) {
|
|
512
|
+
return true;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
return false;
|
|
516
|
+
}
|
|
517
|
+
/**
|
|
518
|
+
* Validate a download URL for SSRF safety.
|
|
519
|
+
* Returns an error message if the URL is unsafe, or null if OK.
|
|
520
|
+
*/
|
|
521
|
+
export function validateDownloadUrl(url) {
|
|
522
|
+
let parsed;
|
|
523
|
+
try {
|
|
524
|
+
parsed = new URL(url);
|
|
525
|
+
}
|
|
526
|
+
catch {
|
|
527
|
+
return 'Invalid URL.';
|
|
528
|
+
}
|
|
529
|
+
if (parsed.protocol !== 'https:') {
|
|
530
|
+
return 'Only HTTPS URLs are supported for download.';
|
|
531
|
+
}
|
|
532
|
+
if (isPrivateOrReservedHost(parsed.hostname)) {
|
|
533
|
+
return 'Cannot download from local/private network addresses.';
|
|
534
|
+
}
|
|
535
|
+
return null;
|
|
536
|
+
}
|
|
537
|
+
// ============================================================================
|
|
538
|
+
// Cost estimation helper
|
|
539
|
+
// ============================================================================
|
|
540
|
+
export function costEstimate(model, duration, audio) {
|
|
541
|
+
const rates = {
|
|
542
|
+
'gen4.5': 12,
|
|
543
|
+
gen4_turbo: 5,
|
|
544
|
+
gen3a_turbo: 5,
|
|
545
|
+
gen4_aleph: 15,
|
|
546
|
+
act_two: 5,
|
|
547
|
+
veo3: 40,
|
|
548
|
+
'veo3.1': audio === false ? 20 : 40,
|
|
549
|
+
'veo3.1_fast': audio === false ? 10 : 15,
|
|
550
|
+
};
|
|
551
|
+
const credits = (rates[model] || 5) * duration;
|
|
552
|
+
return { credits, usd: `$${(credits * 0.01).toFixed(2)}` };
|
|
553
|
+
}
|
|
554
|
+
/**
|
|
555
|
+
* Add content moderation to a request body if specified.
|
|
556
|
+
*/
|
|
557
|
+
export function addContentModeration(body, contentMod) {
|
|
558
|
+
if (contentMod === 'low') {
|
|
559
|
+
body.contentModeration = { publicFigureThreshold: 'low' };
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
//# sourceMappingURL=client.js.map
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Runway ML MCP Server
|
|
4
|
+
*
|
|
5
|
+
* Comprehensive AI media generation via Runway's API:
|
|
6
|
+
* - Video generation (text-to-video, image-to-video, video-to-video, character performance)
|
|
7
|
+
* - Image generation (text+reference-to-image)
|
|
8
|
+
* - Audio (TTS, sound effects, voice swap, dubbing, voice isolation)
|
|
9
|
+
* - Custom voices (list, create, preview, delete)
|
|
10
|
+
* - Task management (check, wait, cancel, download)
|
|
11
|
+
* - Account (balance, usage analytics, API key configuration)
|
|
12
|
+
*
|
|
13
|
+
* Environment variables:
|
|
14
|
+
* - RUNWAYML_API_SECRET: Runway API key (required)
|
|
15
|
+
* - MCP_HOST_BRIDGE_STATE: Path to host app bridge state file (primary, optional)
|
|
16
|
+
* - MINDSTONE_REBEL_BRIDGE_STATE: Legacy/deprecated bridge state path (optional)
|
|
17
|
+
*/
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Runway ML MCP Server
|
|
4
|
+
*
|
|
5
|
+
* Comprehensive AI media generation via Runway's API:
|
|
6
|
+
* - Video generation (text-to-video, image-to-video, video-to-video, character performance)
|
|
7
|
+
* - Image generation (text+reference-to-image)
|
|
8
|
+
* - Audio (TTS, sound effects, voice swap, dubbing, voice isolation)
|
|
9
|
+
* - Custom voices (list, create, preview, delete)
|
|
10
|
+
* - Task management (check, wait, cancel, download)
|
|
11
|
+
* - Account (balance, usage analytics, API key configuration)
|
|
12
|
+
*
|
|
13
|
+
* Environment variables:
|
|
14
|
+
* - RUNWAYML_API_SECRET: Runway API key (required)
|
|
15
|
+
* - MCP_HOST_BRIDGE_STATE: Path to host app bridge state file (primary, optional)
|
|
16
|
+
* - MINDSTONE_REBEL_BRIDGE_STATE: Legacy/deprecated bridge state path (optional)
|
|
17
|
+
*/
|
|
18
|
+
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
19
|
+
import { createServer } from './server.js';
|
|
20
|
+
async function main() {
|
|
21
|
+
const server = createServer();
|
|
22
|
+
const transport = new StdioServerTransport();
|
|
23
|
+
await server.connect(transport);
|
|
24
|
+
console.error('Runway MCP server running on stdio');
|
|
25
|
+
}
|
|
26
|
+
main().catch((error) => {
|
|
27
|
+
console.error('Fatal error:', error);
|
|
28
|
+
process.exit(1);
|
|
29
|
+
});
|
|
30
|
+
//# sourceMappingURL=index.js.map
|
package/dist/server.d.ts
ADDED
package/dist/server.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
3
|
+
import { registerConfigureTools, registerVideoTools, registerImageTools, registerAudioTools, registerVoiceTools, registerTaskTools, registerAccountTools, } from './tools/index.js';
|
|
4
|
+
const require = createRequire(import.meta.url);
|
|
5
|
+
const pkg = require('../package.json');
|
|
6
|
+
export function createServer() {
|
|
7
|
+
const server = new McpServer({
|
|
8
|
+
name: 'runway-mcp-server',
|
|
9
|
+
version: pkg.version,
|
|
10
|
+
});
|
|
11
|
+
registerConfigureTools(server);
|
|
12
|
+
registerVideoTools(server);
|
|
13
|
+
registerImageTools(server);
|
|
14
|
+
registerAudioTools(server);
|
|
15
|
+
registerVoiceTools(server);
|
|
16
|
+
registerTaskTools(server);
|
|
17
|
+
registerAccountTools(server);
|
|
18
|
+
return server;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=server.js.map
|