anyapi-mcp-server 1.4.0 → 1.5.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/README.md +7 -3
- package/build/graphql-schema.js +59 -6
- package/build/index.js +33 -14
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -19,6 +19,7 @@ Works with services like **Datadog**, **PostHog**, **Metabase**, **Cloudflare**,
|
|
|
19
19
|
- **Multi-sample merging** — samples up to 10 array elements to build richer schemas that capture fields missing from individual items
|
|
20
20
|
- **Mutation support** — POST/PUT/DELETE/PATCH endpoints with OpenAPI request body schemas get GraphQL mutation types with typed inputs
|
|
21
21
|
- **Smart query suggestions** — `call_api` returns ready-to-use GraphQL queries based on the inferred schema
|
|
22
|
+
- **Shape-aware schema caching** — schemas are cached by response structure (not just endpoint), so the same path returning different shapes gets distinct schemas; `shapeHash` is returned for cache-aware workflows
|
|
22
23
|
- **Response caching** — 30-second TTL cache prevents duplicate HTTP calls across consecutive `call_api` → `query_api` flows
|
|
23
24
|
- **Retry with backoff** — automatic retries with exponential backoff and jitter for 429/5xx errors, honoring `Retry-After` headers
|
|
24
25
|
- **Multi-format responses** — parses JSON, XML, CSV, and plain text responses automatically
|
|
@@ -167,6 +168,8 @@ Inspect an API endpoint by making a real request and returning the inferred Grap
|
|
|
167
168
|
- Returns the full schema SDL showing all available fields and types
|
|
168
169
|
- Returns accepted parameters (name, location, required) from the API spec
|
|
169
170
|
- Returns `suggestedQueries` — ready-to-use GraphQL queries generated from the schema
|
|
171
|
+
- Returns `shapeHash` — a structural fingerprint of the response for cache-aware workflows
|
|
172
|
+
- Returns `bodyHash` for write operations when a request body is provided
|
|
170
173
|
- Accepts optional `headers` to override defaults for this request
|
|
171
174
|
- For write operations (POST/PUT/DELETE/PATCH) with request body schemas, the schema includes a Mutation type
|
|
172
175
|
|
|
@@ -207,6 +210,7 @@ Fetch data from an API endpoint, returning only the fields you select via GraphQ
|
|
|
207
210
|
- Supports `limit` and `offset` for client-side slicing of already-fetched data
|
|
208
211
|
- For API-level pagination, pass limit/offset inside `params` instead
|
|
209
212
|
- Accepts optional `headers` to override defaults for this request
|
|
213
|
+
- Response includes `_shapeHash` (and `_bodyHash` for writes) for tracking schema identity
|
|
210
214
|
|
|
211
215
|
### `explain_api`
|
|
212
216
|
|
|
@@ -225,7 +229,7 @@ Fetch data from multiple endpoints concurrently in a single tool call.
|
|
|
225
229
|
- Accepts an array of 1–10 requests, each with `method`, `path`, `params`, `body`, `query`, and optional `headers`
|
|
226
230
|
- All requests execute in parallel via `Promise.allSettled` — one failure does not affect the others
|
|
227
231
|
- Each request follows the `query_api` flow: HTTP fetch → schema inference → GraphQL field selection
|
|
228
|
-
- Returns an array of results: `{ method, path, data }` on success or `{ method, path, error }` on failure
|
|
232
|
+
- Returns an array of results: `{ method, path, data, shapeHash }` on success or `{ method, path, error }` on failure
|
|
229
233
|
- Run `call_api` first on each endpoint to discover the schema field names
|
|
230
234
|
|
|
231
235
|
## Workflow
|
|
@@ -260,8 +264,8 @@ OpenAPI/Postman spec
|
|
|
260
264
|
```
|
|
261
265
|
|
|
262
266
|
1. The spec is loaded at startup (from a local file or fetched from an HTTPS URL with filesystem caching) and parsed into an endpoint index with tags, paths, parameters, and request body schemas
|
|
263
|
-
2. `call_api` makes a real HTTP request, infers a GraphQL schema from the JSON response, and caches both the response (30s TTL) and the schema
|
|
264
|
-
3. `query_api` re-uses the cached response if called within 30s, executes your GraphQL field selection against the data, and returns only the fields you asked for
|
|
267
|
+
2. `call_api` makes a real HTTP request, infers a GraphQL schema from the JSON response, and caches both the response (30s TTL) and the schema. Schemas are keyed by endpoint + response shape, so the same path returning different structures gets distinct schemas
|
|
268
|
+
3. `query_api` re-uses the cached response if called within 30s, executes your GraphQL field selection against the data, and returns only the fields you asked for. Includes `_shapeHash` in the response for tracking schema identity
|
|
265
269
|
4. Write operations (POST/PUT/DELETE/PATCH) with OpenAPI request body schemas get a Mutation type with typed `GraphQLInputObjectType` inputs
|
|
266
270
|
|
|
267
271
|
## Supported Spec Formats
|
package/build/graphql-schema.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { GraphQLSchema, GraphQLObjectType, GraphQLInputObjectType, GraphQLScalarType, GraphQLString, GraphQLInt, GraphQLFloat, GraphQLBoolean, GraphQLList, GraphQLNonNull, graphql as executeGraphQL, printSchema, } from "graphql";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
2
3
|
const DEFAULT_ARRAY_LIMIT = 50;
|
|
3
4
|
const MAX_SAMPLE_SIZE = 30;
|
|
4
5
|
const MAX_INFER_DEPTH = 4;
|
|
@@ -28,8 +29,8 @@ export function truncateIfArray(data, limit, offset) {
|
|
|
28
29
|
const sliced = data.slice(off, off + lim);
|
|
29
30
|
return { data: sliced, truncated: sliced.length < total, total };
|
|
30
31
|
}
|
|
31
|
-
function cacheKey(method, pathTemplate) {
|
|
32
|
-
return `${method}:${pathTemplate}`;
|
|
32
|
+
function cacheKey(method, pathTemplate, hash) {
|
|
33
|
+
return `${method}:${pathTemplate}:${hash}`;
|
|
33
34
|
}
|
|
34
35
|
/**
|
|
35
36
|
* Sanitize a JSON key into a valid GraphQL field name.
|
|
@@ -155,6 +156,51 @@ function mergeArraySamples(arr) {
|
|
|
155
156
|
return null;
|
|
156
157
|
return mergeSamples(objectSamples);
|
|
157
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* Produce a structural fingerprint string from JSON data.
|
|
161
|
+
* Captures keys + recursive type structure but NOT values.
|
|
162
|
+
* Sorted keys ensure determinism regardless of object key order.
|
|
163
|
+
* Bounded by MAX_INFER_DEPTH to match inference behavior.
|
|
164
|
+
*/
|
|
165
|
+
function shapeFingerprint(data, depth = 0) {
|
|
166
|
+
if (data === null || data === undefined)
|
|
167
|
+
return "n";
|
|
168
|
+
if (depth >= MAX_INFER_DEPTH)
|
|
169
|
+
return "J";
|
|
170
|
+
if (Array.isArray(data)) {
|
|
171
|
+
if (data.length === 0)
|
|
172
|
+
return "[]";
|
|
173
|
+
if (hasMixedTypes(data))
|
|
174
|
+
return "[J]";
|
|
175
|
+
const mergeResult = mergeArraySamples(data);
|
|
176
|
+
if (mergeResult)
|
|
177
|
+
return `[${shapeFingerprint(mergeResult.merged, depth + 1)}]`;
|
|
178
|
+
return `[${shapeFingerprint(data[0], depth + 1)}]`;
|
|
179
|
+
}
|
|
180
|
+
if (typeof data === "object") {
|
|
181
|
+
const obj = data;
|
|
182
|
+
const keys = Object.keys(obj).sort();
|
|
183
|
+
if (keys.length === 0)
|
|
184
|
+
return "{}";
|
|
185
|
+
return `{${keys.map((k) => `${k}:${shapeFingerprint(obj[k], depth + 1)}`).join(",")}}`;
|
|
186
|
+
}
|
|
187
|
+
switch (typeof data) {
|
|
188
|
+
case "number":
|
|
189
|
+
return Number.isInteger(data) ? "i" : "f";
|
|
190
|
+
case "boolean":
|
|
191
|
+
return "b";
|
|
192
|
+
default:
|
|
193
|
+
return "s";
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Compute a truncated SHA-256 hash of the structural fingerprint of JSON data.
|
|
198
|
+
* Returns a 12-character hex string.
|
|
199
|
+
*/
|
|
200
|
+
export function computeShapeHash(data) {
|
|
201
|
+
const fp = shapeFingerprint(data);
|
|
202
|
+
return createHash("sha256").update(fp).digest("hex").slice(0, 12);
|
|
203
|
+
}
|
|
158
204
|
/**
|
|
159
205
|
* Recursively infer a GraphQL type from a JSON value.
|
|
160
206
|
* For objects, creates a named GraphQLObjectType with explicit resolvers
|
|
@@ -376,15 +422,22 @@ export function buildSchemaFromData(data, method, pathTemplate, requestBodySchem
|
|
|
376
422
|
}
|
|
377
423
|
/**
|
|
378
424
|
* Get a cached schema or build + cache a new one from the response data.
|
|
425
|
+
*
|
|
426
|
+
* @param cacheHash Optional hash to use as the cache key discriminator.
|
|
427
|
+
* If provided (e.g. body hash for mutations), it is used instead of the
|
|
428
|
+
* response shape hash for cache lookup. The shapeHash is always computed
|
|
429
|
+
* from the response data and returned regardless.
|
|
379
430
|
*/
|
|
380
|
-
export function getOrBuildSchema(data, method, pathTemplate, requestBodySchema) {
|
|
381
|
-
const
|
|
431
|
+
export function getOrBuildSchema(data, method, pathTemplate, requestBodySchema, cacheHash) {
|
|
432
|
+
const shapeHash = computeShapeHash(data);
|
|
433
|
+
const effectiveHash = cacheHash ?? shapeHash;
|
|
434
|
+
const key = cacheKey(method, pathTemplate, effectiveHash);
|
|
382
435
|
const cached = schemaCache.get(key);
|
|
383
436
|
if (cached)
|
|
384
|
-
return cached;
|
|
437
|
+
return { schema: cached, shapeHash };
|
|
385
438
|
const schema = buildSchemaFromData(data, method, pathTemplate, requestBodySchema);
|
|
386
439
|
schemaCache.set(key, schema);
|
|
387
|
-
return schema;
|
|
440
|
+
return { schema, shapeHash };
|
|
388
441
|
}
|
|
389
442
|
/**
|
|
390
443
|
* Convert a GraphQL schema to SDL string for display.
|
package/build/index.js
CHANGED
|
@@ -7,7 +7,8 @@ import { ApiIndex } from "./api-index.js";
|
|
|
7
7
|
import { callApi } from "./api-client.js";
|
|
8
8
|
import { initLogger } from "./logger.js";
|
|
9
9
|
import { generateSuggestions } from "./query-suggestions.js";
|
|
10
|
-
import { getOrBuildSchema, executeQuery, schemaToSDL, truncateIfArray, } from "./graphql-schema.js";
|
|
10
|
+
import { getOrBuildSchema, executeQuery, schemaToSDL, truncateIfArray, computeShapeHash, } from "./graphql-schema.js";
|
|
11
|
+
const WRITE_METHODS = new Set(["POST", "PUT", "DELETE", "PATCH"]);
|
|
11
12
|
const config = await loadConfig();
|
|
12
13
|
initLogger(config.logPath ?? null);
|
|
13
14
|
const apiIndex = new ApiIndex(config.spec);
|
|
@@ -74,7 +75,7 @@ server.tool("list_api", `List available ${config.name} API endpoints. ` +
|
|
|
74
75
|
? "{ items { method path summary tag parameters { name in required description } } _count }"
|
|
75
76
|
: "{ items { tag endpointCount } _count }";
|
|
76
77
|
const effectiveQuery = query ?? defaultQuery;
|
|
77
|
-
const schema = getOrBuildSchema(data, "LIST", category ?? search ?? "_categories");
|
|
78
|
+
const { schema } = getOrBuildSchema(data, "LIST", category ?? search ?? "_categories");
|
|
78
79
|
const { data: sliced, truncated, total } = truncateIfArray(data, limit ?? 20, offset);
|
|
79
80
|
const queryResult = await executeQuery(schema, sliced, effectiveQuery);
|
|
80
81
|
if (truncated && typeof queryResult === "object" && queryResult !== null) {
|
|
@@ -135,9 +136,12 @@ server.tool("call_api", `Inspect a ${config.name} API endpoint. Makes a real req
|
|
|
135
136
|
try {
|
|
136
137
|
const data = await callApi(config, method, path, params, body, headers, "populate");
|
|
137
138
|
const endpoint = apiIndex.getEndpoint(method, path);
|
|
138
|
-
const
|
|
139
|
+
const bodyHash = WRITE_METHODS.has(method) && body ? computeShapeHash(body) : undefined;
|
|
140
|
+
const { schema, shapeHash } = getOrBuildSchema(data, method, path, endpoint?.requestBodySchema, bodyHash);
|
|
139
141
|
const sdl = schemaToSDL(schema);
|
|
140
|
-
const result = { graphqlSchema: sdl };
|
|
142
|
+
const result = { graphqlSchema: sdl, shapeHash };
|
|
143
|
+
if (bodyHash)
|
|
144
|
+
result.bodyHash = bodyHash;
|
|
141
145
|
if (endpoint && endpoint.parameters.length > 0) {
|
|
142
146
|
result.parameters = endpoint.parameters.map((p) => ({
|
|
143
147
|
name: p.name,
|
|
@@ -233,16 +237,22 @@ server.tool("query_api", `Fetch data from a ${config.name} API endpoint, returni
|
|
|
233
237
|
try {
|
|
234
238
|
const rawData = await callApi(config, method, path, params, body, headers, "consume");
|
|
235
239
|
const endpoint = apiIndex.getEndpoint(method, path);
|
|
236
|
-
const
|
|
240
|
+
const bodyHash = WRITE_METHODS.has(method) && body ? computeShapeHash(body) : undefined;
|
|
241
|
+
const { schema, shapeHash } = getOrBuildSchema(rawData, method, path, endpoint?.requestBodySchema, bodyHash);
|
|
237
242
|
const { data, truncated, total } = truncateIfArray(rawData, limit, offset);
|
|
238
243
|
const queryResult = await executeQuery(schema, data, query);
|
|
239
|
-
if (
|
|
240
|
-
queryResult.
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
244
|
+
if (typeof queryResult === "object" && queryResult !== null) {
|
|
245
|
+
queryResult._shapeHash = shapeHash;
|
|
246
|
+
if (bodyHash)
|
|
247
|
+
queryResult._bodyHash = bodyHash;
|
|
248
|
+
if (truncated) {
|
|
249
|
+
queryResult._meta = {
|
|
250
|
+
total,
|
|
251
|
+
offset: offset ?? 0,
|
|
252
|
+
limit: limit ?? 50,
|
|
253
|
+
hasMore: true,
|
|
254
|
+
};
|
|
255
|
+
}
|
|
246
256
|
}
|
|
247
257
|
return {
|
|
248
258
|
content: [
|
|
@@ -380,9 +390,18 @@ server.tool("batch_query", `Fetch data from multiple ${config.name} API endpoint
|
|
|
380
390
|
const settled = await Promise.allSettled(requests.map(async (req) => {
|
|
381
391
|
const rawData = await callApi(config, req.method, req.path, req.params, req.body, req.headers, "none");
|
|
382
392
|
const endpoint = apiIndex.getEndpoint(req.method, req.path);
|
|
383
|
-
const
|
|
393
|
+
const bodyHash = WRITE_METHODS.has(req.method) && req.body
|
|
394
|
+
? computeShapeHash(req.body)
|
|
395
|
+
: undefined;
|
|
396
|
+
const { schema, shapeHash } = getOrBuildSchema(rawData, req.method, req.path, endpoint?.requestBodySchema, bodyHash);
|
|
384
397
|
const queryResult = await executeQuery(schema, rawData, req.query);
|
|
385
|
-
return {
|
|
398
|
+
return {
|
|
399
|
+
method: req.method,
|
|
400
|
+
path: req.path,
|
|
401
|
+
data: queryResult,
|
|
402
|
+
shapeHash,
|
|
403
|
+
...(bodyHash ? { bodyHash } : {}),
|
|
404
|
+
};
|
|
386
405
|
}));
|
|
387
406
|
const results = settled.map((outcome, i) => {
|
|
388
407
|
if (outcome.status === "fulfilled") {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "anyapi-mcp-server",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "A universal MCP server that connects any REST API (via OpenAPI spec) to AI assistants, with GraphQL-style field selection and automatic schema inference.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "SEE LICENSE IN LICENSE",
|
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
"scripts": {
|
|
32
32
|
"build": "tsc",
|
|
33
33
|
"start": "node build/index.js",
|
|
34
|
+
"test": "vitest run",
|
|
34
35
|
"prepublishOnly": "npm run build"
|
|
35
36
|
},
|
|
36
37
|
"engines": {
|
|
@@ -46,6 +47,7 @@
|
|
|
46
47
|
"devDependencies": {
|
|
47
48
|
"@types/js-yaml": "^4.0.9",
|
|
48
49
|
"@types/node": "^22.0.0",
|
|
49
|
-
"typescript": "^5.7.0"
|
|
50
|
+
"typescript": "^5.7.0",
|
|
51
|
+
"vitest": "^4.0.18"
|
|
50
52
|
}
|
|
51
53
|
}
|