@maviapi/mcp 0.2.3 → 0.2.5
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 +3 -3
- package/dist/catalog.js +101 -25
- package/dist/config.js +1 -1
- package/dist/requests.js +27 -18
- package/dist/result.js +88 -0
- package/dist/server.js +1 -0
- package/dist/tools.js +47 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -99,7 +99,7 @@ Leave out `env` to try it with the demo key.
|
|
|
99
99
|
|------|--------------|
|
|
100
100
|
| `search_apis` | Find an API for a website or topic ("flights", "turkish news", "booking.com"). Empty query lists the whole catalog. |
|
|
101
101
|
| `get_api` | An API's endpoints, their parameters, and the fields each one returns. |
|
|
102
|
-
| `call_api` | Call an endpoint and get live JSON back. List endpoints return 10 results unless you pass `limit`. |
|
|
102
|
+
| `call_api` | Call an endpoint and get live JSON back. List endpoints return 10 results unless you pass `limit`. An answer over 50,000 characters is cut to whole rows and flagged `truncated` (rows returned, rows asked); the HTTP API itself never cuts. |
|
|
103
103
|
| `create_api` | No API for the site you need? Starts a build from a URL and a plain-language description, and returns a `request_id`. |
|
|
104
104
|
| `get_build_status` | Where a build is: its phase, each endpoint with its test results or what went wrong, and what the build said last. `wait_seconds` returns as soon as something changes. A finished build is not public until it is published. |
|
|
105
105
|
| `request_endpoint` | Add one endpoint to an API that exists, or a changed version of one (it ships beside the old one, e.g. `/search-v2`). Previews first; `confirm: true` starts the build. |
|
|
@@ -109,8 +109,8 @@ Leave out `env` to try it with the demo key.
|
|
|
109
109
|
| `publish_request` | Make a finished request live. Needs a key with the `requests:publish` permission. |
|
|
110
110
|
| `cancel_request` | Stop the build running for a request. |
|
|
111
111
|
| `report_endpoint_issue` | Report an endpoint that fails, comes back empty or repeats the same page. maviapi replays the call and checks the endpoint itself: a fault its own check confirms is repaired in place, the rest go to a person. You can report an API you have called with your own key. With no arguments it lists your reports. |
|
|
112
|
-
| `get_key_info` | What the configured key may do: permissions, plan, remaining credits, limits. |
|
|
113
|
-
| `get_changelog` |
|
|
112
|
+
| `get_key_info` | What the configured key may do: permissions, plan, remaining credits, limits. Works with any key of your own, with or without permissions. |
|
|
113
|
+
| `get_changelog` | What changed in an API, newest first: the date of each change, which one is live, and what it changed. |
|
|
114
114
|
|
|
115
115
|
The request tools need your own API key with request permissions. A key only calls data APIs unless you turn permissions on when you create it on the [keys page](https://maviapi.com/dashboard/keys): "Start and manage builds" for asking, steering, cancelling and reporting, and "Publish builds" for `publish_request`. `create_api` works with any key. Every rule (who may publish, one build per API at a time with up to 3 requests in line, the daily limits) is enforced by maviapi, not by this server.
|
|
116
116
|
|
package/dist/catalog.js
CHANGED
|
@@ -197,38 +197,104 @@ export function findEndpoint(endpoints, ref) {
|
|
|
197
197
|
endpoints.find((e) => e.slug.toLowerCase() === r) ??
|
|
198
198
|
endpoints.find((e) => e.path.toLowerCase().replace(/^\/+/, "") === r));
|
|
199
199
|
}
|
|
200
|
-
/**
|
|
201
|
-
|
|
200
|
+
/** Query words that say nothing about which API is meant. */
|
|
201
|
+
const STOP_WORDS = new Set([
|
|
202
|
+
"a", "an", "the", "of", "in", "on", "at", "for", "to", "from", "by", "with", "and", "or", "near",
|
|
203
|
+
"api", "apis", "data", "site", "website", "com", "www",
|
|
204
|
+
]);
|
|
205
|
+
/**
|
|
206
|
+
* A score an API reaches only through its slug, name or site (not its
|
|
207
|
+
* description alone). With it an API is kept even when some query words miss.
|
|
208
|
+
*/
|
|
209
|
+
export const STRONG_MATCH = 4;
|
|
210
|
+
/** Lowercase words of a text: letters and digits in any script, split at everything else. */
|
|
211
|
+
function words(text) {
|
|
212
|
+
return text.toLowerCase().split(/[^\p{L}\p{N}]+/u).filter(Boolean).map(singular);
|
|
213
|
+
}
|
|
214
|
+
/** The query words that count: stopwords and one-letter words dropped (unless nothing else is left). */
|
|
215
|
+
function queryTokens(q) {
|
|
216
|
+
const all = words(q).filter((t) => t.length > 1);
|
|
217
|
+
const meaningful = all.filter((t) => !STOP_WORDS.has(t));
|
|
218
|
+
return meaningful.length ? meaningful : all;
|
|
219
|
+
}
|
|
220
|
+
/** How strongly one query word matches an API: 0 when it does not. */
|
|
221
|
+
function tokenHit(api, token) {
|
|
222
|
+
const slug = api.slug.toLowerCase();
|
|
223
|
+
const host = hostOf(api.target_url) ?? "";
|
|
224
|
+
let hit = 0;
|
|
225
|
+
if (slug === token)
|
|
226
|
+
hit += 10;
|
|
227
|
+
else if (words(slug).includes(token) || (token.length >= 4 && slug.includes(token)))
|
|
228
|
+
hit += 5;
|
|
229
|
+
if (words(host).includes(token) || (token.length >= 4 && host.includes(token)))
|
|
230
|
+
hit += 5;
|
|
231
|
+
if (words(api.name).includes(token))
|
|
232
|
+
hit += 4;
|
|
233
|
+
if (words(api.description ?? "").includes(token))
|
|
234
|
+
hit += 1;
|
|
235
|
+
return hit;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Rank catalog APIs against a free-text query. An empty query lists them all.
|
|
239
|
+
*
|
|
240
|
+
* Query words match whole words (not parts of words, so "out" does not match
|
|
241
|
+
* "without"), and an API is kept only when every query word matches it
|
|
242
|
+
* somewhere, or one matches its slug, name or site. A query no API answers
|
|
243
|
+
* returns nothing, rather than every API that shares a word with it; see
|
|
244
|
+
* partialMatches for the closest APIs then.
|
|
245
|
+
* `loose` keeps any API that matches one word: for "did you mean" hints.
|
|
246
|
+
*/
|
|
247
|
+
export function searchApis(apis, query, opts = {}) {
|
|
202
248
|
const q = (query ?? "").trim().toLowerCase();
|
|
203
249
|
if (!q)
|
|
204
250
|
return [...apis];
|
|
205
|
-
const tokens = q.split(/\s+/).filter((t) => t.length > 1);
|
|
206
251
|
const queryHost = hostOf(q);
|
|
252
|
+
const tokens = queryTokens(q);
|
|
207
253
|
const scored = apis.map((api) => {
|
|
208
|
-
const slug = api.slug.toLowerCase();
|
|
209
|
-
const name = api.name.toLowerCase();
|
|
210
254
|
const host = hostOf(api.target_url) ?? "";
|
|
211
|
-
const description = (api.description ?? "").toLowerCase();
|
|
212
255
|
let score = queryHost && queryHost === host ? 20 : 0;
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
score += 5;
|
|
220
|
-
if (name.includes(token))
|
|
221
|
-
score += 4;
|
|
222
|
-
if (description.includes(token))
|
|
223
|
-
score += 1;
|
|
256
|
+
let matched = 0;
|
|
257
|
+
for (const token of tokens) {
|
|
258
|
+
const hit = tokenHit(api, token);
|
|
259
|
+
if (hit)
|
|
260
|
+
matched++;
|
|
261
|
+
score += hit;
|
|
224
262
|
}
|
|
225
|
-
|
|
263
|
+
const kept = opts.loose || score >= STRONG_MATCH || (tokens.length > 0 && matched === tokens.length);
|
|
264
|
+
return { api, score: kept ? score : 0, matched };
|
|
226
265
|
});
|
|
266
|
+
// An API that matches more of the query's words ranks first, then the higher score.
|
|
227
267
|
return scored
|
|
228
268
|
.filter((s) => s.score > 0)
|
|
229
|
-
.sort((a, b) => b.score - a.score)
|
|
269
|
+
.sort((a, b) => b.matched - a.matched || b.score - a.score)
|
|
230
270
|
.map((s) => s.api);
|
|
231
271
|
}
|
|
272
|
+
/** Most partial matches an answer lists. */
|
|
273
|
+
export const MAX_PARTIAL = 5;
|
|
274
|
+
/**
|
|
275
|
+
* The APIs that match at least one query word (after stopwords) as a whole
|
|
276
|
+
* word in name, slug, site or description, best first, at most MAX_PARTIAL.
|
|
277
|
+
* For when no API matches every word: "events istanbul" has no API about both,
|
|
278
|
+
* but event APIs exist and take the city as a parameter.
|
|
279
|
+
*/
|
|
280
|
+
export function partialMatches(apis, query) {
|
|
281
|
+
const tokens = queryTokens((query ?? "").trim().toLowerCase());
|
|
282
|
+
const hits = apis
|
|
283
|
+
.map((api) => {
|
|
284
|
+
const scores = tokens.map((token) => tokenHit(api, token));
|
|
285
|
+
return {
|
|
286
|
+
api,
|
|
287
|
+
score: scores.reduce((a, b) => a + b, 0),
|
|
288
|
+
matched: tokens.filter((_, i) => scores[i]),
|
|
289
|
+
missed: tokens.filter((_, i) => !scores[i]),
|
|
290
|
+
};
|
|
291
|
+
})
|
|
292
|
+
.filter((s) => s.matched.length > 0)
|
|
293
|
+
.sort((a, b) => b.matched.length - a.matched.length || b.score - a.score)
|
|
294
|
+
.slice(0, MAX_PARTIAL)
|
|
295
|
+
.map(({ api, matched, missed }) => ({ api, matched, missed }));
|
|
296
|
+
return { words: tokens, hits };
|
|
297
|
+
}
|
|
232
298
|
/** "hotels" -> "hotel", so a plural query still matches a singular description. Leaves "news" and "class" alone. */
|
|
233
299
|
function singular(token) {
|
|
234
300
|
return token.length > 4 && token.endsWith("s") && !token.endsWith("ss") ? token.slice(0, -1) : token;
|
|
@@ -259,15 +325,16 @@ export function describeParams(schema) {
|
|
|
259
325
|
/**
|
|
260
326
|
* Flatten a response schema into `path: type` lines (`data.items[].price:
|
|
261
327
|
* number`), so an agent knows which fields come back without reading a full
|
|
262
|
-
* JSON Schema document.
|
|
328
|
+
* JSON Schema document. Past `maxLines`, one last line says how many fields
|
|
329
|
+
* were left out.
|
|
263
330
|
*/
|
|
264
|
-
export function outlineOutput(schema, maxLines =
|
|
331
|
+
export function outlineOutput(schema, maxLines = OUTLINE_MAX_LINES, maxDepth = 5) {
|
|
265
332
|
if (!schema)
|
|
266
333
|
return [];
|
|
267
334
|
const lines = [];
|
|
268
335
|
const walk = (node, prefix, depth) => {
|
|
269
336
|
for (const [key, child] of Object.entries(node.properties ?? {})) {
|
|
270
|
-
if (lines.length
|
|
337
|
+
if (lines.length > OUTLINE_HARD_CAP)
|
|
271
338
|
return;
|
|
272
339
|
const path = prefix ? `${prefix}.${key}` : key;
|
|
273
340
|
if (child.properties && depth < maxDepth) {
|
|
@@ -282,10 +349,19 @@ export function outlineOutput(schema, maxLines = 40, maxDepth = 5) {
|
|
|
282
349
|
}
|
|
283
350
|
};
|
|
284
351
|
walk(schema, "", 0);
|
|
285
|
-
if (lines.length
|
|
286
|
-
lines
|
|
287
|
-
|
|
352
|
+
if (lines.length <= maxLines)
|
|
353
|
+
return lines;
|
|
354
|
+
const left = lines.length - maxLines;
|
|
355
|
+
const more = lines.length > OUTLINE_HARD_CAP ? `${left}+` : String(left);
|
|
356
|
+
return [...lines.slice(0, maxLines), `… and ${more} more field${left === 1 ? "" : "s"}`];
|
|
288
357
|
}
|
|
358
|
+
/**
|
|
359
|
+
* Response fields get_api lists per endpoint. Endpoints return a few dozen
|
|
360
|
+
* fields; a full list keeps an agent from guessing at the ones it cannot see.
|
|
361
|
+
*/
|
|
362
|
+
export const OUTLINE_MAX_LINES = 200;
|
|
363
|
+
/** Stop walking a schema past this many fields. */
|
|
364
|
+
const OUTLINE_HARD_CAP = 1000;
|
|
289
365
|
/** MCP tool names allow [A-Za-z0-9_-], up to 64 characters. */
|
|
290
366
|
export function toolName(apiSlug, endpointSlug) {
|
|
291
367
|
return `${apiSlug}_${endpointSlug}`.replace(/[^A-Za-z0-9_-]/g, "_").slice(0, 64);
|
package/dist/config.js
CHANGED
package/dist/requests.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { findEndpoint, truncate } from "./catalog.js";
|
|
2
2
|
import { siteLink } from "./config.js";
|
|
3
|
-
import {
|
|
3
|
+
import { UserError, failure, fitAnswer, humanDuration, objectArg, stringArg, text } from "./result.js";
|
|
4
4
|
/**
|
|
5
5
|
* The request tools: ask for an endpoint on an API that exists, follow its
|
|
6
6
|
* build, try the draft, publish it, cancel it. They drive the same routes the
|
|
@@ -157,15 +157,15 @@ export const REQUEST_TOOLS = [
|
|
|
157
157
|
name: "get_key_info",
|
|
158
158
|
title: "Show this maviapi key",
|
|
159
159
|
description: "What the configured API key may do: its permissions, the account's plan and remaining credits, and the limits that apply. " +
|
|
160
|
-
"Check it before starting something that needs a permission or uses credits.
|
|
160
|
+
"Check it before starting something that needs a permission or uses credits. Works with any API key of your own.",
|
|
161
161
|
inputSchema: { type: "object", properties: {} },
|
|
162
162
|
annotations: READ,
|
|
163
163
|
},
|
|
164
164
|
{
|
|
165
165
|
name: "get_changelog",
|
|
166
166
|
title: "Show a maviapi API's versions",
|
|
167
|
-
description: "
|
|
168
|
-
"Use it when an endpoint behaves differently than before
|
|
167
|
+
description: "What changed in an API, newest first: the date of each change, which one is live, and what it changed (endpoints added, fields changed, fixes). " +
|
|
168
|
+
"Use it when an endpoint behaves differently than before.",
|
|
169
169
|
inputSchema: {
|
|
170
170
|
type: "object",
|
|
171
171
|
properties: {
|
|
@@ -224,6 +224,20 @@ export function describeDiff(diff) {
|
|
|
224
224
|
}
|
|
225
225
|
return parts.join("; ");
|
|
226
226
|
}
|
|
227
|
+
/** One changelog entry as a line, with its listed changes under it. */
|
|
228
|
+
export function describeChange(e) {
|
|
229
|
+
const live = e.live ?? e.state === "active";
|
|
230
|
+
const tags = [live ? "live" : "", e.compat === "breaking" ? "breaking" : ""].filter(Boolean);
|
|
231
|
+
const when = (e.date ?? e.id ?? "").slice(0, 10);
|
|
232
|
+
const label = [when, typeof e.version === "number" ? `build ${e.version}` : ""].filter(Boolean).join(", ") || "undated";
|
|
233
|
+
const headline = e.title?.trim() || e.notes?.summary?.trim() || (e.hotfix_of ? `Repair of build ${e.hotfix_of}.` : describeDiff(e.diff)) || "No details.";
|
|
234
|
+
const out = [`- ${label}${tags.length ? ` (${tags.join(", ")})` : ""}: ${headline}`];
|
|
235
|
+
for (const change of (e.notes?.changes ?? []).slice(0, 8)) {
|
|
236
|
+
if (change.text)
|
|
237
|
+
out.push(` - ${change.kind ? `${change.kind}: ` : ""}${change.text}`);
|
|
238
|
+
}
|
|
239
|
+
return out;
|
|
240
|
+
}
|
|
227
241
|
export class RequestTools {
|
|
228
242
|
client;
|
|
229
243
|
config;
|
|
@@ -574,11 +588,12 @@ export class RequestTools {
|
|
|
574
588
|
filled[p.name] = p.example;
|
|
575
589
|
}
|
|
576
590
|
const result = await this.client.callDraft(status.draft.id, shortPath(endpoint.path, status.slug), filled);
|
|
591
|
+
if (result.ok) {
|
|
592
|
+
const asked = Number(filled.limit);
|
|
593
|
+
return fitAnswer(result.body, { asked: Number.isFinite(asked) ? asked : undefined });
|
|
594
|
+
}
|
|
577
595
|
const body = typeof result.body === "string" ? result.body : JSON.stringify(result.body);
|
|
578
|
-
|
|
579
|
-
return result.ok
|
|
580
|
-
? text(out)
|
|
581
|
-
: failure(`The draft answered ${result.status}: ${truncate(body, 2000)}`);
|
|
596
|
+
return failure(`The draft answered ${result.status}: ${truncate(body, 2000)}`);
|
|
582
597
|
}
|
|
583
598
|
async publish(args) {
|
|
584
599
|
const id = stringArg(args, "request_id", true);
|
|
@@ -649,19 +664,13 @@ export class RequestTools {
|
|
|
649
664
|
return failure(this.deps.describeFailure(result));
|
|
650
665
|
const entries = (result.body.entries ?? []);
|
|
651
666
|
if (!entries.length)
|
|
652
|
-
return text(`${api.name} (${api.slug}) has no recorded
|
|
653
|
-
const lines = entries.slice(0, 30).
|
|
654
|
-
const tags = [e.state === "active" ? "live" : e.state, e.compat === "breaking" ? "breaking" : ""]
|
|
655
|
-
.filter(Boolean)
|
|
656
|
-
.join(", ");
|
|
657
|
-
const changes = e.hotfix_of ? `repair of v${e.hotfix_of}` : describeDiff(e.diff);
|
|
658
|
-
return `- v${e.version} (${tags}${e.date ? `, ${e.date.slice(0, 10)}` : ""})${changes ? `: ${changes}` : ""}`;
|
|
659
|
-
});
|
|
667
|
+
return text(`${api.name} (${api.slug}) has no recorded changes yet.`);
|
|
668
|
+
const lines = entries.slice(0, 30).flatMap(describeChange);
|
|
660
669
|
return text([
|
|
661
|
-
`${api.name} (${api.slug})
|
|
670
|
+
`${api.name} (${api.slug}) changes, newest first:`,
|
|
662
671
|
...lines,
|
|
663
672
|
"",
|
|
664
|
-
|
|
673
|
+
"call_api always serves the live build.",
|
|
665
674
|
].join("\n"));
|
|
666
675
|
}
|
|
667
676
|
}
|
package/dist/result.js
CHANGED
|
@@ -3,6 +3,94 @@ export const MAX_RESULT_CHARS = 50_000;
|
|
|
3
3
|
/** A failure worth showing the agent verbatim: bad input, unknown names. */
|
|
4
4
|
export class UserError extends Error {
|
|
5
5
|
}
|
|
6
|
+
const META_KEY = "maviapi/truncation";
|
|
7
|
+
/** The path to the longest list in a JSON answer, two levels deep at most: where its rows are. */
|
|
8
|
+
function rowsPath(value, depth = 0) {
|
|
9
|
+
if (Array.isArray(value))
|
|
10
|
+
return [];
|
|
11
|
+
if (!value || typeof value !== "object" || depth > 1)
|
|
12
|
+
return null;
|
|
13
|
+
let best = null;
|
|
14
|
+
for (const [key, child] of Object.entries(value)) {
|
|
15
|
+
const sub = rowsPath(child, depth + 1);
|
|
16
|
+
if (!sub)
|
|
17
|
+
continue;
|
|
18
|
+
const size = JSON.stringify(child).length;
|
|
19
|
+
if (!best || size > best.size)
|
|
20
|
+
best = { path: [key, ...sub], size };
|
|
21
|
+
}
|
|
22
|
+
return best?.path ?? null;
|
|
23
|
+
}
|
|
24
|
+
function getAt(value, path) {
|
|
25
|
+
return path.reduce((node, key) => node[key], value);
|
|
26
|
+
}
|
|
27
|
+
/** A copy of `value` whose list at `path` keeps only its first `n` rows. */
|
|
28
|
+
function withRows(value, path, n) {
|
|
29
|
+
if (!path.length)
|
|
30
|
+
return value.slice(0, n);
|
|
31
|
+
const [key, ...rest] = path;
|
|
32
|
+
return { ...value, [key]: withRows(value[key], rest, n) };
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* An endpoint's answer as tool text, cut to `max` characters when it is
|
|
36
|
+
* longer. A JSON answer is cut at whole rows of its longest list, so what is
|
|
37
|
+
* left still parses; anything else is cut at `max` characters. The result
|
|
38
|
+
* says what was left out, and that the HTTP API itself never cuts.
|
|
39
|
+
*/
|
|
40
|
+
export function fitAnswer(body, opts = {}) {
|
|
41
|
+
const max = opts.max ?? MAX_RESULT_CHARS;
|
|
42
|
+
const full = typeof body === "string" ? body : JSON.stringify(body);
|
|
43
|
+
if (full.length <= max)
|
|
44
|
+
return text(full);
|
|
45
|
+
const where = opts.url ? ` (${opts.url})` : "";
|
|
46
|
+
const http = `The HTTP API${where} does not cut answers.`;
|
|
47
|
+
let kept = null;
|
|
48
|
+
let truncation = null;
|
|
49
|
+
const path = typeof body === "string" ? null : rowsPath(body);
|
|
50
|
+
if (path) {
|
|
51
|
+
const rows = getAt(body, path);
|
|
52
|
+
// The most rows that still fit: whole rows only, so the JSON stays valid.
|
|
53
|
+
let lo = 0;
|
|
54
|
+
let hi = rows.length;
|
|
55
|
+
while (lo < hi) {
|
|
56
|
+
const mid = Math.ceil((lo + hi) / 2);
|
|
57
|
+
if (JSON.stringify(withRows(body, path, mid)).length <= max)
|
|
58
|
+
lo = mid;
|
|
59
|
+
else
|
|
60
|
+
hi = mid - 1;
|
|
61
|
+
}
|
|
62
|
+
const candidate = JSON.stringify(withRows(body, path, lo));
|
|
63
|
+
if (lo > 0 && candidate.length <= max) {
|
|
64
|
+
kept = candidate;
|
|
65
|
+
truncation = {
|
|
66
|
+
truncated: true,
|
|
67
|
+
rows_returned: lo,
|
|
68
|
+
rows_in_answer: rows.length,
|
|
69
|
+
...(opts.asked !== undefined && { rows_asked: opts.asked }),
|
|
70
|
+
max_chars: max,
|
|
71
|
+
note: `This tool cut the answer to ${lo} of its ${rows.length} rows to stay under ${max.toLocaleString("en-US")} characters. ` +
|
|
72
|
+
`${http} Pass a smaller limit and page with offset to read the rest here.`,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (!truncation) {
|
|
77
|
+
kept = full.slice(0, max);
|
|
78
|
+
truncation = {
|
|
79
|
+
truncated: true,
|
|
80
|
+
...(opts.asked !== undefined && { rows_asked: opts.asked }),
|
|
81
|
+
max_chars: max,
|
|
82
|
+
note: `This tool cut the answer at ${max.toLocaleString("en-US")} of its ${full.length.toLocaleString("en-US")} characters, so the text above is incomplete. ` +
|
|
83
|
+
`${http} Pass a smaller limit and page with offset to read the rest here.`,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
content: [
|
|
88
|
+
{ type: "text", text: kept ?? "" },
|
|
89
|
+
{ type: "text", text: JSON.stringify(truncation) },
|
|
90
|
+
],
|
|
91
|
+
_meta: { [META_KEY]: truncation },
|
|
92
|
+
};
|
|
93
|
+
}
|
|
6
94
|
export function text(body) {
|
|
7
95
|
return { content: [{ type: "text", text: body }] };
|
|
8
96
|
}
|
package/dist/server.js
CHANGED
|
@@ -8,6 +8,7 @@ Workflow: search_apis to find an API for a site or topic, get_api to read its en
|
|
|
8
8
|
If no API covers the site you need, create_api builds one (about half an hour; follow it with get_build_status).
|
|
9
9
|
If an API exists but lacks an endpoint, or you need one to behave differently, request_endpoint adds one: preview it, let the user agree, start it with confirm: true, follow it with get_build_status (wait_seconds), try it with call_draft, and publish_request when the user wants it live.
|
|
10
10
|
If an endpoint is simply broken (it fails, comes back empty, or repeats the same page), report_endpoint_issue with the params of the failing call: maviapi replays it and checks the endpoint itself. A fault its own check confirms is repaired in place; the rest are read by a person.
|
|
11
|
+
The request tools need a key of your own with permissions: request_endpoint, send_request_message, cancel_request and report_endpoint_issue need "Start and manage builds" (requests:write), list_requests and call_draft either permission, and publish_request "Publish builds" (requests:publish). Without them, the user publishes a finished build on its page in the dashboard (https://maviapi.com/dashboard/requests), and creates a key with permissions at https://maviapi.com/dashboard/keys. get_key_info shows what the configured key may do, its credits and its limits.
|
|
11
12
|
Starting a build and publishing change things for the user: do them when the user asked, not on your own.`;
|
|
12
13
|
/**
|
|
13
14
|
* App directories (Claude's among them) read a tool's display name from
|
package/dist/tools.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { DEFAULT_LIMIT, MissingParamsError, describeParams, findApi, findEndpoint, hostOf, outlineOutput, searchApis, toolName, truncate, } from "./catalog.js";
|
|
1
|
+
import { DEFAULT_LIMIT, MissingParamsError, buildQuery, describeParams, fillPath, findApi, findEndpoint, hostOf, outlineOutput, partialMatches, searchApis, toolName, truncate, } from "./catalog.js";
|
|
2
2
|
import { MaviapiError } from "./client.js";
|
|
3
3
|
import { MAX_WAIT_SECONDS, REQUEST_TOOLS, RequestTools } from "./requests.js";
|
|
4
|
-
import { MAX_RESULT_CHARS, UserError, failure, humanDuration, objectArg, stringArg, text } from "./result.js";
|
|
4
|
+
import { MAX_RESULT_CHARS, UserError, failure, fitAnswer, humanDuration, objectArg, stringArg, text } from "./result.js";
|
|
5
5
|
export { humanDuration };
|
|
6
6
|
import { PLAYGROUND_DAILY_LIMIT, PLAYGROUND_FREE_BUILDS, PLAYGROUND_RATE_LIMIT, siteLink, } from "./config.js";
|
|
7
7
|
const READ_ONLY = { readOnlyHint: true, destructiveHint: false, idempotentHint: true };
|
|
@@ -41,7 +41,8 @@ const GENERIC_TOOLS = [
|
|
|
41
41
|
title: "Call a maviapi endpoint",
|
|
42
42
|
description: "Fetch live, structured JSON from a website through a maviapi endpoint. " +
|
|
43
43
|
"`api` is the API slug (or domain), `endpoint` the endpoint name or path from get_api, and `params` its parameters, path parameters such as {id} included. " +
|
|
44
|
-
`List endpoints return ${DEFAULT_LIMIT} results unless you pass \`limit\`; page with \`offset\`. Each successful call uses one maviapi credit
|
|
44
|
+
`List endpoints return ${DEFAULT_LIMIT} results unless you pass \`limit\`; page with \`offset\`. Each successful call uses one maviapi credit. ` +
|
|
45
|
+
`An answer over ${MAX_RESULT_CHARS.toLocaleString("en-US")} characters is cut to whole rows here and flagged \`truncated\` (the HTTP API returns it in full).`,
|
|
45
46
|
inputSchema: {
|
|
46
47
|
type: "object",
|
|
47
48
|
properties: {
|
|
@@ -54,7 +55,7 @@ const GENERIC_TOOLS = [
|
|
|
54
55
|
},
|
|
55
56
|
version: {
|
|
56
57
|
type: "integer",
|
|
57
|
-
description: "Call this
|
|
58
|
+
description: "Call this build of the API instead of the live one. Only the API's owner can pick a build; anyone else gets the live one. Rarely needed.",
|
|
58
59
|
},
|
|
59
60
|
},
|
|
60
61
|
required: ["api", "endpoint"],
|
|
@@ -160,10 +161,8 @@ export class Toolset {
|
|
|
160
161
|
const query = stringArg(args, "query");
|
|
161
162
|
const apis = await this.client.listApis();
|
|
162
163
|
const hits = searchApis(apis, query);
|
|
163
|
-
if (!hits.length)
|
|
164
|
-
return text(
|
|
165
|
-
"If the site you need is missing, create_api can build an API for it.");
|
|
166
|
-
}
|
|
164
|
+
if (!hits.length)
|
|
165
|
+
return text(noFullMatch(apis, query));
|
|
167
166
|
const header = query ? `${hits.length} API(s) match "${query}":` : `${apis.length} APIs in the maviapi catalog:`;
|
|
168
167
|
const lines = hits.map((api) => {
|
|
169
168
|
const host = hostOf(api.target_url);
|
|
@@ -179,7 +178,7 @@ export class Toolset {
|
|
|
179
178
|
const apis = await this.client.listApis();
|
|
180
179
|
const api = findApi(apis, ref);
|
|
181
180
|
if (!api) {
|
|
182
|
-
const suggestions = searchApis(apis, ref).slice(0, 3).map((a) => a.slug);
|
|
181
|
+
const suggestions = searchApis(apis, ref, { loose: true }).slice(0, 3).map((a) => a.slug);
|
|
183
182
|
throw new UserError(`No maviapi API named "${ref}".` +
|
|
184
183
|
(suggestions.length ? ` Did you mean: ${suggestions.join(", ")}?` : " Use search_apis to find one.") +
|
|
185
184
|
" If the site is not in the catalog, create_api can build one.");
|
|
@@ -217,7 +216,7 @@ export class Toolset {
|
|
|
217
216
|
const params = describeParams(ep.inputSchema);
|
|
218
217
|
out.push(params.length ? "Parameters:" : "Parameters: none");
|
|
219
218
|
out.push(...params);
|
|
220
|
-
const fields = outlineOutput(ep.outputSchema
|
|
219
|
+
const fields = outlineOutput(ep.outputSchema);
|
|
221
220
|
if (fields.length)
|
|
222
221
|
out.push("Returns:", ...fields.map((f) => ` ${f}`));
|
|
223
222
|
}
|
|
@@ -246,7 +245,7 @@ export class Toolset {
|
|
|
246
245
|
}
|
|
247
246
|
const version = args.version === undefined || args.version === null ? undefined : Number(args.version);
|
|
248
247
|
if (version !== undefined && (!Number.isInteger(version) || version < 1)) {
|
|
249
|
-
throw new UserError("`version` must be a
|
|
248
|
+
throw new UserError("`version` must be a build number of this API, e.g. 3. Leave it out to call the live build.");
|
|
250
249
|
}
|
|
251
250
|
return this.run(api.slug, endpoint, params, version);
|
|
252
251
|
}
|
|
@@ -262,15 +261,20 @@ export class Toolset {
|
|
|
262
261
|
const result = await this.client.call(slug, endpoint, params, version);
|
|
263
262
|
if (!result.ok)
|
|
264
263
|
return failure(this.describeFailure(result));
|
|
265
|
-
const
|
|
266
|
-
|
|
264
|
+
const asked = Number(params.limit);
|
|
265
|
+
const filled = fillPath(endpoint.path, params);
|
|
266
|
+
const answer = fitAnswer(result.body, {
|
|
267
|
+
asked: Number.isFinite(asked) ? asked : undefined,
|
|
268
|
+
url: `${this.config.baseUrl}/v1/sites/${encodeURIComponent(slug)}${filled.path}${buildQuery(filled.query)}`,
|
|
269
|
+
});
|
|
267
270
|
if (this.config.usingPlaygroundKey && !this.nudged) {
|
|
268
271
|
this.nudged = true;
|
|
269
|
-
|
|
272
|
+
const first = answer.content[0];
|
|
273
|
+
first.text +=
|
|
270
274
|
`\n\n(Served with maviapi's shared demo key, limited to ${PLAYGROUND_RATE_LIMIT} requests a minute. ` +
|
|
271
275
|
`For a higher limit, create a free key at ${siteLink("/sign-up", "demo_key_notice")} and set MAVIAPI_API_KEY.)`;
|
|
272
276
|
}
|
|
273
|
-
return
|
|
277
|
+
return answer;
|
|
274
278
|
}
|
|
275
279
|
describeFailure({ status, body, retryAfter }) {
|
|
276
280
|
const payload = (body && typeof body === "object" ? body : {});
|
|
@@ -454,3 +458,31 @@ export class Toolset {
|
|
|
454
458
|
return tools;
|
|
455
459
|
}
|
|
456
460
|
}
|
|
461
|
+
/**
|
|
462
|
+
* The answer when no API matches every word of the query: the closest partial
|
|
463
|
+
* matches, labelled as partial, or (when no word matches anything) a plain no.
|
|
464
|
+
* A place in the query is usually a parameter, not part of an API's name.
|
|
465
|
+
*/
|
|
466
|
+
function noFullMatch(apis, query) {
|
|
467
|
+
const { words, hits } = partialMatches(apis, query);
|
|
468
|
+
if (!hits.length) {
|
|
469
|
+
return (`No maviapi API covers "${query}". None of the ${apis.length} APIs in the catalog matches it. ` +
|
|
470
|
+
"If the query names a site, create_api can build an API for it. For a topic, try one plain word (e.g. \"hotels\"), " +
|
|
471
|
+
"or call search_apis with no query to list the whole catalog.");
|
|
472
|
+
}
|
|
473
|
+
const quoted = (list) => list.map((w) => `"${w}"`).join(", ");
|
|
474
|
+
const best = hits[0].matched;
|
|
475
|
+
const lines = hits.map(({ api, matched, missed }) => {
|
|
476
|
+
const host = hostOf(api.target_url);
|
|
477
|
+
const description = truncate((api.description ?? "").trim(), 220);
|
|
478
|
+
const note = missed.length ? ` [matches ${quoted(matched)}, not ${quoted(missed)}]` : "";
|
|
479
|
+
return `- ${api.slug}: ${api.name}${host ? ` (${host})` : ""}${note}. ${description}`;
|
|
480
|
+
});
|
|
481
|
+
return [
|
|
482
|
+
`No API matches every word of "${query}" (${quoted(words)}). Closest, matching ${quoted(best)} (partial matches):`,
|
|
483
|
+
...lines,
|
|
484
|
+
"",
|
|
485
|
+
"A city or place is usually a parameter of an API (a location, city or geo input), not part of its name: " +
|
|
486
|
+
"check a partial match with get_api, and use it only if it fits. If none fits, create_api can build an API for a site.",
|
|
487
|
+
].join("\n");
|
|
488
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@maviapi/mcp",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.5",
|
|
4
4
|
"description": "MCP server for maviapi, the unofficial API for any website: hosted, documented JSON APIs for sites with no official API, kept working when the site changes.",
|
|
5
5
|
"mcpName": "io.github.kuzenlabs/maviapi-mcp",
|
|
6
6
|
"type": "module",
|