@edgegap/mcp 0.9.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/tools.js CHANGED
@@ -159,9 +159,24 @@ const NOT_FOUND = {
159
159
  'before it ran. If it is still running, check the request_id with edgegap_list_deployments.',
160
160
  relay: 'no relay session with that session_id. It may already have been deleted.',
161
161
  };
162
+ /**
163
+ * What a 403 means, per kind of resource. The API's own text is a bare "You
164
+ * are not authorized to access this resource", which reads like a bad token
165
+ * and sends agents to ask for a new one. A rejected token is a 401; a 403 is
166
+ * a token that works but may not use this feature.
167
+ */
168
+ const FORBIDDEN = {
169
+ relay: 'the token is valid, but this organization cannot use relays. Relays are not enabled on ' +
170
+ 'every plan or account; check the Relays page in the dashboard (https://app.edgegap.com) ' +
171
+ 'or ask Edgegap support. A new token will not help. Consider a dedicated server ' +
172
+ '(edgegap_deploy) instead.',
173
+ generic: 'the token is valid, but this organization may not use this feature or resource. It may ' +
174
+ 'not be enabled on the plan, or the resource belongs to another organization. A new token ' +
175
+ 'will not help; check the feature in the dashboard (https://app.edgegap.com) or ask the developer.',
176
+ };
162
177
  /** Wraps a handler so API errors come back as readable, self-correctable text
163
178
  * rather than a protocol-level exception the agent cannot act on. */
164
- function guard(auth, fn, notFound) {
179
+ function guard(auth, fn, notFound, forbidden) {
165
180
  const handled = fn().catch((err) => {
166
181
  if (err instanceof TokenUnavailableError) {
167
182
  // Not a failure to correct — a decision the human made. Say so plainly
@@ -169,7 +184,7 @@ function guard(auth, fn, notFound) {
169
184
  return fail(err.message);
170
185
  }
171
186
  if (err instanceof EdgegapApiError) {
172
- const hint = errorHint(err, notFound);
187
+ const hint = errorHint(err, notFound, forbidden);
173
188
  return fail(`${err.message}${hint ? `\n\nLikely cause: ${hint}` : ''}`);
174
189
  }
175
190
  return fail(redact(err.message ?? String(err), auth.current));
@@ -190,9 +205,9 @@ function guard(auth, fn, notFound) {
190
205
  }
191
206
  /** guard() with the 404 hint first, so it reads at the call site. */
192
207
  function guarded(auth, notFound, fn) {
193
- return guard(auth, fn, notFound);
208
+ return guard(auth, fn, notFound, notFound === NOT_FOUND.relay ? FORBIDDEN.relay : undefined);
194
209
  }
195
- function errorHint(err, notFound) {
210
+ function errorHint(err, notFound, forbidden) {
196
211
  switch (err.status) {
197
212
  case 401:
198
213
  // Deliberately does not promise a re-prompt: the local server asks again
@@ -207,6 +222,8 @@ function errorHint(err, notFound) {
207
222
  // Request ID provided", or a generic "could not understand" for logs),
208
223
  // not 404. Their only input is the request_id, so a 400 means that.
209
224
  return notFound === NOT_FOUND.deployment || notFound === NOT_FOUND.logs ? notFound : undefined;
225
+ case 403:
226
+ return forbidden ?? FORBIDDEN.generic;
210
227
  case 404:
211
228
  // Only the tool knows what was not found; no hint beats a wrong one.
212
229
  return notFound;
@@ -244,6 +261,56 @@ function pageInfo(totalCount, returned, page) {
244
261
  };
245
262
  }
246
263
  /** Trims a deployment status down to what an agent needs to act. */
264
+ /** The last `cap` characters of a deployment's container logs, or undefined
265
+ * if there are none or they cannot be read. Never throws. */
266
+ async function logTail(client, requestId, cap) {
267
+ try {
268
+ const res = await client.getDeploymentLogs(requestId);
269
+ const text = [res.logs, res.crash_logs].filter(Boolean).join('\n');
270
+ if (!text.trim())
271
+ return undefined;
272
+ return text.length > cap ? `...[truncated]...\n${text.slice(-cap)}` : text;
273
+ }
274
+ catch {
275
+ return undefined;
276
+ }
277
+ }
278
+ /**
279
+ * Stop is idempotent: stopping a deployment that has already stopped, or that
280
+ * Edgegap no longer knows, leaves nothing running, which is what was asked.
281
+ * Returns the success payload in that case, or undefined to report the error.
282
+ * An unknown request_id cannot be told apart from a cleaned-up one (both are a
283
+ * 400 or 404), so the result says so instead of claiming it was stopped.
284
+ */
285
+ async function alreadyStopped(client, requestId, err) {
286
+ if (!(err instanceof EdgegapApiError) || ![400, 404, 409, 410].includes(err.status))
287
+ return undefined;
288
+ let status;
289
+ try {
290
+ status = await client.getDeployment(requestId);
291
+ }
292
+ catch (statusErr) {
293
+ if (statusErr instanceof EdgegapApiError && [400, 404].includes(statusErr.status)) {
294
+ return {
295
+ request_id: requestId,
296
+ result: 'not_running',
297
+ note: 'Edgegap has no running deployment with this request_id: it already stopped and was ' +
298
+ 'cleaned up, or the request_id is wrong. Nothing is left to stop. If you expected it ' +
299
+ 'to be running, check edgegap_list_deployments.',
300
+ };
301
+ }
302
+ return undefined;
303
+ }
304
+ const current = (status.current_status ?? '').toUpperCase();
305
+ if (status.running && !current.includes('TERMINAT'))
306
+ return undefined;
307
+ return {
308
+ request_id: requestId,
309
+ result: 'already_stopped',
310
+ status: status.current_status,
311
+ note: 'This deployment was already stopped or stopping. Nothing more to do.',
312
+ };
313
+ }
247
314
  function compactDeployment(d) {
248
315
  const ports = Object.entries(d.ports ?? {}).map(([key, p]) => ({
249
316
  name: p.name ?? key,
@@ -1091,10 +1158,16 @@ export function registerTools(server, client, config, auth, options = {}) {
1091
1158
  last = await client.getDeployment(request_id);
1092
1159
  const status = (last.current_status ?? '').toUpperCase();
1093
1160
  if (last.error || TERMINAL_BAD.some((s) => status.includes(s))) {
1161
+ // Fetched now, not left to the agent: a failed deployment is
1162
+ // cleaned up quickly and its logs go with it, so a separate
1163
+ // edgegap_get_deployment_logs call often finds nothing.
1164
+ const logs = await logTail(client, request_id, 3000);
1094
1165
  return fail(`Deployment ${request_id} failed with status ${last.current_status}.\n` +
1095
1166
  `${last.error_detail ?? 'No error detail returned.'}\n\n` +
1096
- `Call edgegap_get_deployment_logs for container output, then fix the image or ` +
1097
- `port configuration before redeploying.`);
1167
+ (logs
1168
+ ? `Container output (tail):\n${logs}\n\nFix the image or port configuration before redeploying.`
1169
+ : `No container logs could be read (the container may never have started, or it ` +
1170
+ `was already cleaned up). Check the image, tag and ports before redeploying.`));
1098
1171
  }
1099
1172
  if (last.running || TERMINAL_OK.some((s) => status.includes(s))) {
1100
1173
  return ok({
@@ -1105,9 +1178,15 @@ export function registerTools(server, client, config, auth, options = {}) {
1105
1178
  await new Promise((r) => setTimeout(r, intervalMs));
1106
1179
  intervalMs = Math.min(intervalMs * 1.5, 10_000);
1107
1180
  }
1108
- return fail(`Deployment ${request_id} did not become ready within ${timeout_seconds ?? 180}s. ` +
1109
- `Last status: ${last?.current_status ?? 'unknown'}. It may still be starting — ` +
1110
- `call edgegap_get_deployment to check again, or edgegap_stop_deployment to clean up.`);
1181
+ // Not an error: the wait ran out, the deployment did not fail. It is
1182
+ // usually still starting (large images pull slowly on a cold host).
1183
+ return ok({
1184
+ ...(last ? compactDeployment(last) : { request_id }),
1185
+ ready: false,
1186
+ waited_seconds: Math.round((Date.now() - startedAt) / 1000),
1187
+ next_step: `Not ready within ${timeout_seconds ?? 180}s, and not failed: it may still be starting. ` +
1188
+ `Call edgegap_wait_for_deployment again, or edgegap_stop_deployment if it should not keep running.`,
1189
+ });
1111
1190
  }));
1112
1191
  // ---------------------------------------------------------------- 8 ----
1113
1192
  tool('edgegap_list_deployments', {
@@ -1198,7 +1277,16 @@ export function registerTools(server, client, config, auth, options = {}) {
1198
1277
  await dry.client.stopDeployment(request_id);
1199
1278
  return dryRunResult(dry.requests);
1200
1279
  }
1201
- const res = await client.stopDeployment(request_id);
1280
+ let res;
1281
+ try {
1282
+ res = await client.stopDeployment(request_id);
1283
+ }
1284
+ catch (err) {
1285
+ const gone = await alreadyStopped(client, request_id, err);
1286
+ if (gone)
1287
+ return ok(gone);
1288
+ throw err;
1289
+ }
1202
1290
  return ok({ request_id, result: res.message ?? 'stop requested' });
1203
1291
  }));
1204
1292
  }
@@ -1465,7 +1553,7 @@ export function registerTools(server, client, config, auth, options = {}) {
1465
1553
  // missing registry, so it must not trigger provisioning (which only
1466
1554
  // fails with a second, more confusing 403). Only a 404 — a
1467
1555
  // quick-start token whose project is not provisioned yet — does.
1468
- const unavailable = () => fail(registryCredentialsUnavailable(image_name, tag));
1556
+ const unavailable = () => manualStepsResult(registryCredentialsUnavailable(image_name, tag));
1469
1557
  let creds;
1470
1558
  try {
1471
1559
  creds = await client.getRegistryCredentials();
@@ -1705,7 +1793,7 @@ export function registerTools(server, client, config, auth, options = {}) {
1705
1793
  `(status ${last.status ?? 'unknown'}). Call edgegap_get_relay_session to check again.`);
1706
1794
  }
1707
1795
  return ok({ ...compactRelay(last), waited_seconds: Math.round((Date.now() - startedAt) / 1000) });
1708
- }));
1796
+ }, undefined, FORBIDDEN.relay));
1709
1797
  }
1710
1798
  // --------------------------------------------------------------- 15 ----
1711
1799
  tool('edgegap_get_relay_session', {
@@ -1737,7 +1825,7 @@ export function registerTools(server, client, config, auth, options = {}) {
1737
1825
  error: r.error || undefined,
1738
1826
  }));
1739
1827
  return ok({ ...pageInfo(res.total_count, sessions.length, 1), sessions });
1740
- }));
1828
+ }, undefined, FORBIDDEN.relay));
1741
1829
  if (mutating) {
1742
1830
  // ------------------------------------------------------------- 16 ----
1743
1831
  tool('edgegap_authorize_relay_user', {
@@ -2187,6 +2275,18 @@ function registerSkills(server, tool, config, options) {
2187
2275
  * registry login to this token. Written as the manual path that works today,
2188
2276
  * so the agent asks the developer instead of retrying or guessing.
2189
2277
  */
2278
+ /**
2279
+ * The registry tool's answer for a regular API token. Not an error: the tool
2280
+ * did what it can for that token type, and the steps are the next action.
2281
+ * Reported as a failure it counted every regular-token developer as a broken
2282
+ * call, and some agents retried it.
2283
+ */
2284
+ function manualStepsResult(steps) {
2285
+ return {
2286
+ content: [{ type: 'text', text: `status: manual_steps_required (no credentials returned)\n\n${steps}` }],
2287
+ structuredContent: { status: 'manual_steps_required', credentials_returned: false, steps },
2288
+ };
2289
+ }
2190
2290
  function registryCredentialsUnavailable(imageName, tag) {
2191
2291
  const image = imageName ?? '<image-name>';
2192
2292
  const buildTag = tag ?? '<unique-build-tag>';
package/dist/version.js CHANGED
@@ -3,4 +3,4 @@
3
3
  * call, and by edgegap_context. Keep in step with package.json and server.json;
4
4
  * test/package.mjs fails the build when they disagree.
5
5
  */
6
- export const VERSION = '0.9.0';
6
+ export const VERSION = '0.10.0';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@edgegap/mcp",
3
3
  "mcpName": "dev.edgegap/mcp",
4
- "version": "0.9.0",
4
+ "version": "0.10.0",
5
5
  "description": "Check, push, and deploy game servers on Edgegap from your coding agent, with relays for P2P games. Runs locally; your API token never leaves your machine.",
6
6
  "scripts": {
7
7
  "prebuild": "rm -rf dist",