@maviapi/mcp 0.2.4 → 0.2.6
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 +1 -1
- package/dist/catalog.js +54 -21
- package/dist/client.js +3 -3
- package/dist/config.js +1 -1
- package/dist/requests.js +7 -7
- package/dist/tools.js +34 -17
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -102,7 +102,7 @@ Leave out `env` to try it with the demo key.
|
|
|
102
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
|
-
| `request_endpoint` | Add one endpoint to an API that exists, or a
|
|
105
|
+
| `request_endpoint` | Add one endpoint to an API that exists, or a change to one (it ships beside the old one, e.g. `/search-v2`). Previews first; `confirm: true` starts the build. |
|
|
106
106
|
| `list_requests` | Your API and endpoint requests, with their status. |
|
|
107
107
|
| `send_request_message` | Tell a build something in plain words: add detail, fix or drop a failed endpoint, try again. |
|
|
108
108
|
| `call_draft` | Call an endpoint of a draft before it is published. Costs no credits. |
|
package/dist/catalog.js
CHANGED
|
@@ -199,7 +199,7 @@ export function findEndpoint(endpoints, ref) {
|
|
|
199
199
|
}
|
|
200
200
|
/** Query words that say nothing about which API is meant. */
|
|
201
201
|
const STOP_WORDS = new Set([
|
|
202
|
-
"a", "an", "the", "of", "in", "on", "at", "for", "to", "from", "by", "with", "and", "or",
|
|
202
|
+
"a", "an", "the", "of", "in", "on", "at", "for", "to", "from", "by", "with", "and", "or", "near",
|
|
203
203
|
"api", "apis", "data", "site", "website", "com", "www",
|
|
204
204
|
]);
|
|
205
205
|
/**
|
|
@@ -211,13 +211,37 @@ export const STRONG_MATCH = 4;
|
|
|
211
211
|
function words(text) {
|
|
212
212
|
return text.toLowerCase().split(/[^\p{L}\p{N}]+/u).filter(Boolean).map(singular);
|
|
213
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
|
+
}
|
|
214
237
|
/**
|
|
215
238
|
* Rank catalog APIs against a free-text query. An empty query lists them all.
|
|
216
239
|
*
|
|
217
240
|
* Query words match whole words (not parts of words, so "out" does not match
|
|
218
241
|
* "without"), and an API is kept only when every query word matches it
|
|
219
242
|
* somewhere, or one matches its slug, name or site. A query no API answers
|
|
220
|
-
* returns nothing, rather than every API that shares a word with it
|
|
243
|
+
* returns nothing, rather than every API that shares a word with it; see
|
|
244
|
+
* partialMatches for the closest APIs then.
|
|
221
245
|
* `loose` keeps any API that matches one word: for "did you mean" hints.
|
|
222
246
|
*/
|
|
223
247
|
export function searchApis(apis, query, opts = {}) {
|
|
@@ -225,30 +249,13 @@ export function searchApis(apis, query, opts = {}) {
|
|
|
225
249
|
if (!q)
|
|
226
250
|
return [...apis];
|
|
227
251
|
const queryHost = hostOf(q);
|
|
228
|
-
const
|
|
229
|
-
const meaningful = all.filter((t) => !STOP_WORDS.has(t));
|
|
230
|
-
const tokens = meaningful.length ? meaningful : all;
|
|
252
|
+
const tokens = queryTokens(q);
|
|
231
253
|
const scored = apis.map((api) => {
|
|
232
|
-
const slug = api.slug.toLowerCase();
|
|
233
|
-
const slugWords = new Set(words(slug));
|
|
234
254
|
const host = hostOf(api.target_url) ?? "";
|
|
235
|
-
const hostWords = new Set(words(host));
|
|
236
|
-
const nameWords = new Set(words(api.name));
|
|
237
|
-
const descriptionWords = new Set(words(api.description ?? ""));
|
|
238
255
|
let score = queryHost && queryHost === host ? 20 : 0;
|
|
239
256
|
let matched = 0;
|
|
240
257
|
for (const token of tokens) {
|
|
241
|
-
|
|
242
|
-
if (slug === token)
|
|
243
|
-
hit += 10;
|
|
244
|
-
else if (slugWords.has(token) || (token.length >= 4 && slug.includes(token)))
|
|
245
|
-
hit += 5;
|
|
246
|
-
if (hostWords.has(token) || (token.length >= 4 && host.includes(token)))
|
|
247
|
-
hit += 5;
|
|
248
|
-
if (nameWords.has(token))
|
|
249
|
-
hit += 4;
|
|
250
|
-
if (descriptionWords.has(token))
|
|
251
|
-
hit += 1;
|
|
258
|
+
const hit = tokenHit(api, token);
|
|
252
259
|
if (hit)
|
|
253
260
|
matched++;
|
|
254
261
|
score += hit;
|
|
@@ -262,6 +269,32 @@ export function searchApis(apis, query, opts = {}) {
|
|
|
262
269
|
.sort((a, b) => b.matched - a.matched || b.score - a.score)
|
|
263
270
|
.map((s) => s.api);
|
|
264
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
|
+
}
|
|
265
298
|
/** "hotels" -> "hotel", so a plural query still matches a singular description. Leaves "news" and "class" alone. */
|
|
266
299
|
function singular(token) {
|
|
267
300
|
return token.length > 4 && token.endsWith("s") && !token.endsWith("ss") ? token.slice(0, -1) : token;
|
package/dist/client.js
CHANGED
|
@@ -52,10 +52,10 @@ export class MaviapiClient {
|
|
|
52
52
|
getBuild(id) {
|
|
53
53
|
return this.send("GET", `/v1/requests/${encodeURIComponent(id)}`);
|
|
54
54
|
}
|
|
55
|
-
/** Call one endpoint through the maviapi gateway, on the live build
|
|
56
|
-
async call(slug, endpoint, params
|
|
55
|
+
/** Call one endpoint through the maviapi gateway, on the live build. */
|
|
56
|
+
async call(slug, endpoint, params) {
|
|
57
57
|
const { path, query } = fillPath(endpoint.path, params);
|
|
58
|
-
return this.send(endpoint.method, `/v1/sites/${encodeURIComponent(slug)}${path}${buildQuery(query)}
|
|
58
|
+
return this.send(endpoint.method, `/v1/sites/${encodeURIComponent(slug)}${path}${buildQuery(query)}`);
|
|
59
59
|
}
|
|
60
60
|
/** Drop what is cached about one API, after a request changed it. */
|
|
61
61
|
invalidateApi(slug) {
|
package/dist/config.js
CHANGED
package/dist/requests.js
CHANGED
|
@@ -20,7 +20,7 @@ export const REQUEST_TOOLS = [
|
|
|
20
20
|
{
|
|
21
21
|
name: "request_endpoint",
|
|
22
22
|
title: "Ask for a new maviapi endpoint",
|
|
23
|
-
description: "Add one endpoint to an API that already exists, or a
|
|
23
|
+
description: "Add one endpoint to an API that already exists, or a change to one of its endpoints. " +
|
|
24
24
|
"Use it when get_api shows the API lacks the data, or an endpoint returns the wrong thing. " +
|
|
25
25
|
"Existing endpoints never change: a change ships as a new endpoint beside the old one (e.g. /search-v2). " +
|
|
26
26
|
"Without `confirm` nothing is built: the tool answers with the endpoint as it would be built, for the user to agree to. " +
|
|
@@ -42,7 +42,7 @@ export const REQUEST_TOOLS = [
|
|
|
42
42
|
},
|
|
43
43
|
change: {
|
|
44
44
|
type: "string",
|
|
45
|
-
description: 'For a
|
|
45
|
+
description: 'For a change to an existing endpoint: that endpoint\'s name or path, e.g. "/search". Use instead of `path`.',
|
|
46
46
|
},
|
|
47
47
|
confirm: { type: "boolean", description: "true starts the build. Leave it out to preview first." },
|
|
48
48
|
},
|
|
@@ -128,7 +128,7 @@ export const REQUEST_TOOLS = [
|
|
|
128
128
|
name: "report_endpoint_issue",
|
|
129
129
|
title: "Report a broken maviapi endpoint",
|
|
130
130
|
description: "Tell maviapi that an endpoint that exists returns the wrong thing: it fails, comes back empty, or repeats the same page. " +
|
|
131
|
-
"Use this instead of asking for a
|
|
131
|
+
"Use this instead of asking for a change when the endpoint is simply broken: the fix lands on the endpoint itself, for everyone. " +
|
|
132
132
|
"maviapi replays the call at once and then runs its own check of the endpoint. If that check fails too the report is confirmed and the endpoint is queued for repair; " +
|
|
133
133
|
"if not (a fault only certain parameters show, a repeated page, wrong values) a person on the team reads the report. " +
|
|
134
134
|
"A report's status is confirmed, fixed or received. " +
|
|
@@ -163,7 +163,7 @@ export const REQUEST_TOOLS = [
|
|
|
163
163
|
},
|
|
164
164
|
{
|
|
165
165
|
name: "get_changelog",
|
|
166
|
-
title: "Show a maviapi API
|
|
166
|
+
title: "Show what changed in a maviapi API",
|
|
167
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
168
|
"Use it when an endpoint behaves differently than before.",
|
|
169
169
|
inputSchema: {
|
|
@@ -320,7 +320,7 @@ export class RequestTools {
|
|
|
320
320
|
const path = stringArg(args, "path");
|
|
321
321
|
const change = stringArg(args, "change");
|
|
322
322
|
if (!path && !change) {
|
|
323
|
-
throw new UserError('Pass `path` for a new endpoint (e.g. "/reviews"), or `change` with an existing endpoint to get a
|
|
323
|
+
throw new UserError('Pass `path` for a new endpoint (e.g. "/reviews"), or `change` with an existing endpoint to get a change to it.');
|
|
324
324
|
}
|
|
325
325
|
if (path && change)
|
|
326
326
|
throw new UserError("Pass `path` or `change`, not both.");
|
|
@@ -355,7 +355,7 @@ export class RequestTools {
|
|
|
355
355
|
const endpoint = (body.endpoint ?? {});
|
|
356
356
|
const out = [
|
|
357
357
|
`Nothing has been built yet. This would add to ${api.name} (${api.slug}):`,
|
|
358
|
-
` GET ${endpoint.path}${endpoint.of ? ` (a
|
|
358
|
+
` GET ${endpoint.path}${endpoint.of ? ` (a change to GET ${endpoint.of}, which stays as it is)` : ""}`,
|
|
359
359
|
` ${endpoint.summary}`,
|
|
360
360
|
"",
|
|
361
361
|
"Show this to the user. If they agree, call request_endpoint again with the same arguments and confirm: true.",
|
|
@@ -425,7 +425,7 @@ export class RequestTools {
|
|
|
425
425
|
? "Nothing more to do: the endpoint keeps its path, so the same call works once it is repaired. " +
|
|
426
426
|
"get_api shows the endpoint's health; report_endpoint_issue with no arguments shows where this report stands."
|
|
427
427
|
: "If the fault only shows with certain parameters, they are in the report for the person who reads it. " +
|
|
428
|
-
"If you need different behavior rather than a fix, request_endpoint with `change` builds
|
|
428
|
+
"If you need different behavior rather than a fix, request_endpoint with `change` builds the change beside this one.",
|
|
429
429
|
].join("\n"));
|
|
430
430
|
}
|
|
431
431
|
async listReports() {
|
package/dist/tools.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DEFAULT_LIMIT, MissingParamsError, buildQuery, describeParams, fillPath, 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
4
|
import { MAX_RESULT_CHARS, UserError, failure, fitAnswer, humanDuration, objectArg, stringArg, text } from "./result.js";
|
|
@@ -53,10 +53,6 @@ const GENERIC_TOOLS = [
|
|
|
53
53
|
description: "Endpoint parameters, as listed by get_api.",
|
|
54
54
|
additionalProperties: true,
|
|
55
55
|
},
|
|
56
|
-
version: {
|
|
57
|
-
type: "integer",
|
|
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.",
|
|
59
|
-
},
|
|
60
56
|
},
|
|
61
57
|
required: ["api", "endpoint"],
|
|
62
58
|
},
|
|
@@ -161,11 +157,8 @@ export class Toolset {
|
|
|
161
157
|
const query = stringArg(args, "query");
|
|
162
158
|
const apis = await this.client.listApis();
|
|
163
159
|
const hits = searchApis(apis, query);
|
|
164
|
-
if (!hits.length)
|
|
165
|
-
return text(
|
|
166
|
-
"If the query names a site, create_api can build an API for it. For a topic, try one plain word (e.g. \"hotels\"), " +
|
|
167
|
-
"or call search_apis with no query to list the whole catalog.");
|
|
168
|
-
}
|
|
160
|
+
if (!hits.length)
|
|
161
|
+
return text(noFullMatch(apis, query));
|
|
169
162
|
const header = query ? `${hits.length} API(s) match "${query}":` : `${apis.length} APIs in the maviapi catalog:`;
|
|
170
163
|
const lines = hits.map((api) => {
|
|
171
164
|
const host = hostOf(api.target_url);
|
|
@@ -246,14 +239,10 @@ export class Toolset {
|
|
|
246
239
|
throw new UserError(`${api.slug} has no endpoint "${ref}". Available: ${endpoints.map((e) => e.name).join(", ")}. ` +
|
|
247
240
|
`get_api "${api.slug}" lists their parameters.`);
|
|
248
241
|
}
|
|
249
|
-
|
|
250
|
-
if (version !== undefined && (!Number.isInteger(version) || version < 1)) {
|
|
251
|
-
throw new UserError("`version` must be a build number of this API, e.g. 3. Leave it out to call the live build.");
|
|
252
|
-
}
|
|
253
|
-
return this.run(api.slug, endpoint, params, version);
|
|
242
|
+
return this.run(api.slug, endpoint, params);
|
|
254
243
|
}
|
|
255
244
|
/** Call an endpoint and turn the outcome into something an agent can act on. */
|
|
256
|
-
async run(slug, endpoint, input
|
|
245
|
+
async run(slug, endpoint, input) {
|
|
257
246
|
const params = { ...input };
|
|
258
247
|
if ("limit" in endpoint.inputSchema.properties && params.limit === undefined) {
|
|
259
248
|
params.limit = DEFAULT_LIMIT;
|
|
@@ -261,7 +250,7 @@ export class Toolset {
|
|
|
261
250
|
const missing = (endpoint.inputSchema.required ?? []).filter((name) => params[name] === undefined || params[name] === null || params[name] === "");
|
|
262
251
|
if (missing.length)
|
|
263
252
|
throw new MissingParamsError(missing);
|
|
264
|
-
const result = await this.client.call(slug, endpoint, params
|
|
253
|
+
const result = await this.client.call(slug, endpoint, params);
|
|
265
254
|
if (!result.ok)
|
|
266
255
|
return failure(this.describeFailure(result));
|
|
267
256
|
const asked = Number(params.limit);
|
|
@@ -461,3 +450,31 @@ export class Toolset {
|
|
|
461
450
|
return tools;
|
|
462
451
|
}
|
|
463
452
|
}
|
|
453
|
+
/**
|
|
454
|
+
* The answer when no API matches every word of the query: the closest partial
|
|
455
|
+
* matches, labelled as partial, or (when no word matches anything) a plain no.
|
|
456
|
+
* A place in the query is usually a parameter, not part of an API's name.
|
|
457
|
+
*/
|
|
458
|
+
function noFullMatch(apis, query) {
|
|
459
|
+
const { words, hits } = partialMatches(apis, query);
|
|
460
|
+
if (!hits.length) {
|
|
461
|
+
return (`No maviapi API covers "${query}". None of the ${apis.length} APIs in the catalog matches it. ` +
|
|
462
|
+
"If the query names a site, create_api can build an API for it. For a topic, try one plain word (e.g. \"hotels\"), " +
|
|
463
|
+
"or call search_apis with no query to list the whole catalog.");
|
|
464
|
+
}
|
|
465
|
+
const quoted = (list) => list.map((w) => `"${w}"`).join(", ");
|
|
466
|
+
const best = hits[0].matched;
|
|
467
|
+
const lines = hits.map(({ api, matched, missed }) => {
|
|
468
|
+
const host = hostOf(api.target_url);
|
|
469
|
+
const description = truncate((api.description ?? "").trim(), 220);
|
|
470
|
+
const note = missed.length ? ` [matches ${quoted(matched)}, not ${quoted(missed)}]` : "";
|
|
471
|
+
return `- ${api.slug}: ${api.name}${host ? ` (${host})` : ""}${note}. ${description}`;
|
|
472
|
+
});
|
|
473
|
+
return [
|
|
474
|
+
`No API matches every word of "${query}" (${quoted(words)}). Closest, matching ${quoted(best)} (partial matches):`,
|
|
475
|
+
...lines,
|
|
476
|
+
"",
|
|
477
|
+
"A city or place is usually a parameter of an API (a location, city or geo input), not part of its name: " +
|
|
478
|
+
"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.",
|
|
479
|
+
].join("\n");
|
|
480
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@maviapi/mcp",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
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",
|