@maviapi/mcp 0.2.4 → 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/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 all = words(q).filter((t) => t.length > 1);
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
- let hit = 0;
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/config.js CHANGED
@@ -1,4 +1,4 @@
1
- export const VERSION = "0.2.4";
1
+ export const VERSION = "0.2.5";
2
2
  export const DEFAULT_BASE_URL = "https://api.maviapi.com";
3
3
  export const SITE_URL = "https://maviapi.com";
4
4
  /**
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";
@@ -161,11 +161,8 @@ export class Toolset {
161
161
  const query = stringArg(args, "query");
162
162
  const apis = await this.client.listApis();
163
163
  const hits = searchApis(apis, query);
164
- if (!hits.length) {
165
- return text(`No maviapi API covers "${query}". None of the ${apis.length} APIs in the catalog is about it, so do not use an unrelated one instead. ` +
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
- }
164
+ if (!hits.length)
165
+ return text(noFullMatch(apis, query));
169
166
  const header = query ? `${hits.length} API(s) match "${query}":` : `${apis.length} APIs in the maviapi catalog:`;
170
167
  const lines = hits.map((api) => {
171
168
  const host = hostOf(api.target_url);
@@ -461,3 +458,31 @@ export class Toolset {
461
458
  return tools;
462
459
  }
463
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.4",
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",