@terreno/api 57.0.0 → 57.2.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/.ai/skills/mongoose-schema-safety/SKILL.md +8 -0
- package/dist/actions.d.ts +7 -2
- package/dist/actions.js +60 -25
- package/dist/adminModelRouter.d.ts +14 -0
- package/dist/adminModelRouter.js +199 -0
- package/dist/adminTypes.d.ts +182 -0
- package/dist/adminTypes.js +5 -0
- package/dist/api.d.ts +44 -3
- package/dist/api.js +74 -26
- package/dist/auth.d.ts +18 -1
- package/dist/auth.js +122 -20
- package/dist/consentApp.d.ts +2 -0
- package/dist/consentApp.js +98 -0
- package/dist/index.d.ts +22 -1
- package/dist/index.js +51 -7
- package/dist/mcp/auth.d.ts +11 -0
- package/dist/mcp/auth.js +133 -0
- package/dist/mcp/createMCPRequest.d.ts +16 -0
- package/dist/mcp/createMCPRequest.js +26 -0
- package/dist/mcp/handlers.d.ts +8 -0
- package/dist/mcp/handlers.js +793 -0
- package/dist/mcp/query.d.ts +32 -0
- package/dist/mcp/query.js +232 -0
- package/dist/mcp/registry.d.ts +21 -0
- package/dist/mcp/registry.js +54 -0
- package/dist/mcp/schemaGenerator.d.ts +14 -0
- package/dist/mcp/schemaGenerator.js +379 -0
- package/dist/mcp/server.d.ts +8 -0
- package/dist/mcp/server.js +133 -0
- package/dist/mcp/toolGenerator.d.ts +14 -0
- package/dist/mcp/toolGenerator.js +246 -0
- package/dist/mcp/types.d.ts +85 -0
- package/dist/mcp/types.js +2 -0
- package/dist/openApi.d.ts +1 -1
- package/dist/openApi.js +26 -8
- package/dist/permissions.js +15 -15
- package/dist/rbac/access.d.ts +3 -0
- package/dist/rbac/access.js +294 -0
- package/dist/rbac/auditModel.d.ts +31 -0
- package/dist/rbac/auditModel.js +112 -0
- package/dist/rbac/backfillAdmins.d.ts +18 -0
- package/dist/rbac/backfillAdmins.js +139 -0
- package/dist/rbac/fieldViews.d.ts +3 -0
- package/dist/rbac/fieldViews.js +201 -0
- package/dist/rbac/middleware.d.ts +21 -0
- package/dist/rbac/middleware.js +108 -0
- package/dist/rbac/modelRouterAccess.d.ts +42 -0
- package/dist/rbac/modelRouterAccess.js +388 -0
- package/dist/rbac/permissionUtils.d.ts +8 -0
- package/dist/rbac/permissionUtils.js +209 -0
- package/dist/rbac/realtimeAccess.d.ts +18 -0
- package/dist/rbac/realtimeAccess.js +173 -0
- package/dist/rbac/resolve.d.ts +28 -0
- package/dist/rbac/resolve.js +387 -0
- package/dist/rbac/roleManager.d.ts +20 -0
- package/dist/rbac/roleManager.js +741 -0
- package/dist/rbac/roleModel.d.ts +42 -0
- package/dist/rbac/roleModel.js +267 -0
- package/dist/rbac/routes.d.ts +9 -0
- package/dist/rbac/routes.js +211 -0
- package/dist/rbac/scopes.d.ts +15 -0
- package/dist/rbac/scopes.js +155 -0
- package/dist/rbac/statements.d.ts +30 -0
- package/dist/rbac/statements.js +126 -0
- package/dist/rbac/types.d.ts +202 -0
- package/dist/rbac/types.js +2 -0
- package/dist/rbac/userPlugin.d.ts +12 -0
- package/dist/rbac/userPlugin.js +41 -0
- package/dist/realtime/changeStreamWatcher.d.ts +1 -1
- package/dist/realtime/changeStreamWatcher.js +48 -39
- package/dist/realtime/realtimeApp.js +4 -8
- package/dist/realtime/registry.d.ts +5 -0
- package/dist/realtime/registry.js +13 -1
- package/dist/scrubAdminFields.d.ts +17 -0
- package/dist/scrubAdminFields.js +192 -0
- package/dist/sync/executors.d.ts +1 -1
- package/dist/sync/executors.js +63 -40
- package/dist/sync/loadHarness.js +4 -5
- package/dist/sync/registry.d.ts +5 -0
- package/dist/sync/registry.js +13 -1
- package/dist/sync/routes.js +41 -36
- package/dist/sync/serialize.js +5 -6
- package/dist/terrenoApp.d.ts +18 -0
- package/dist/terrenoApp.js +46 -3
- package/dist/terrenoPlugin.d.ts +8 -1
- package/dist/tests/createTestData.js +8 -0
- package/dist/tests/models.d.ts +1 -0
- package/dist/tests/models.js +5 -0
- package/dist/tests/testHelper.js +1 -2
- package/dist/transformers.js +16 -1
- package/package.json +17 -13
- package/src/__snapshots__/openApi.test.ts.snap +192 -0
- package/src/actions.test.ts +194 -1
- package/src/actions.ts +65 -8
- package/src/adminModelRouter.ts +154 -0
- package/src/adminTypes.ts +206 -0
- package/src/api.test.ts +66 -1
- package/src/api.ts +131 -12
- package/src/auth.test.ts +135 -1
- package/src/auth.ts +81 -3
- package/src/consentApp.test.ts +24 -0
- package/src/consentApp.ts +100 -0
- package/src/index.ts +56 -0
- package/src/mcp/auth.test.ts +197 -0
- package/src/mcp/auth.ts +100 -0
- package/src/mcp/createMCPRequest.test.ts +47 -0
- package/src/mcp/createMCPRequest.ts +32 -0
- package/src/mcp/customTools.test.ts +54 -0
- package/src/mcp/handlers.ts +639 -0
- package/src/mcp/integration.test.ts +1493 -0
- package/src/mcp/query.test.ts +172 -0
- package/src/mcp/query.ts +179 -0
- package/src/mcp/registry.ts +70 -0
- package/src/mcp/schemaGenerator.test.ts +182 -0
- package/src/mcp/schemaGenerator.ts +354 -0
- package/src/mcp/server.test.ts +226 -0
- package/src/mcp/server.ts +86 -0
- package/src/mcp/toolGenerator.test.ts +284 -0
- package/src/mcp/toolGenerator.ts +199 -0
- package/src/mcp/types.ts +95 -0
- package/src/openApi.test.ts +107 -0
- package/src/openApi.ts +32 -1
- package/src/permissions.ts +3 -2
- package/src/rbac/access.test.ts +357 -0
- package/src/rbac/access.ts +201 -0
- package/src/rbac/auditModel.ts +103 -0
- package/src/rbac/backfillAdmins.ts +54 -0
- package/src/rbac/fieldViews.test.ts +39 -0
- package/src/rbac/fieldViews.ts +122 -0
- package/src/rbac/middleware.ts +55 -0
- package/src/rbac/modelRouterAccess.test.ts +336 -0
- package/src/rbac/modelRouterAccess.ts +331 -0
- package/src/rbac/permissionUtils.test.ts +112 -0
- package/src/rbac/permissionUtils.ts +83 -0
- package/src/rbac/realtimeAccess.test.ts +242 -0
- package/src/rbac/realtimeAccess.ts +138 -0
- package/src/rbac/resolve.test.ts +358 -0
- package/src/rbac/resolve.ts +285 -0
- package/src/rbac/roleManager.test.ts +915 -0
- package/src/rbac/roleManager.ts +440 -0
- package/src/rbac/roleModel.test.ts +114 -0
- package/src/rbac/roleModel.ts +196 -0
- package/src/rbac/routes.ts +170 -0
- package/src/rbac/scopes.test.ts +105 -0
- package/src/rbac/scopes.ts +81 -0
- package/src/rbac/statements.test.ts +67 -0
- package/src/rbac/statements.ts +73 -0
- package/src/rbac/types.ts +198 -0
- package/src/rbac/userPlugin.test.ts +55 -0
- package/src/rbac/userPlugin.ts +25 -0
- package/src/realtime/changeStreamWatcher.ts +20 -15
- package/src/realtime/realtime.test.ts +25 -1
- package/src/realtime/realtimeApp.ts +2 -5
- package/src/realtime/registry.ts +15 -0
- package/src/scrubAdminFields.test.ts +113 -0
- package/src/scrubAdminFields.ts +158 -0
- package/src/sync/executors.ts +31 -1
- package/src/sync/loadHarness.ts +8 -7
- package/src/sync/models.test.ts +262 -0
- package/src/sync/registry.ts +15 -0
- package/src/sync/routes.ts +25 -11
- package/src/sync/scripts/compactTombstones.test.ts +4 -6
- package/src/sync/serialize.ts +17 -9
- package/src/sync/sync.test.ts +52 -0
- package/src/sync/syncRoutes.test.ts +5 -2
- package/src/sync/syncSocket.test.ts +28 -0
- package/src/terrenoApp.test.ts +100 -0
- package/src/terrenoApp.ts +65 -3
- package/src/terrenoPlugin.ts +10 -1
- package/src/tests/bunSetup.ts +2 -0
- package/src/tests/createTestData.ts +5 -1
- package/src/tests/models.ts +6 -0
- package/src/tests/testHelper.ts +1 -3
- package/src/transformers.test.ts +12 -0
- package/src/transformers.ts +7 -1
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
|
|
3
|
+
import type {ModelRouterOptions} from "../api";
|
|
4
|
+
import {Permissions} from "../permissions";
|
|
5
|
+
import {buildListQuery} from "./query";
|
|
6
|
+
import type {MCPConfig, MCPToolArgs} from "./types";
|
|
7
|
+
|
|
8
|
+
interface TestDoc {
|
|
9
|
+
completed: boolean;
|
|
10
|
+
ownerId: string;
|
|
11
|
+
title: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const baseOptions: ModelRouterOptions<TestDoc> = {
|
|
15
|
+
permissions: {
|
|
16
|
+
create: [Permissions.IsAuthenticated],
|
|
17
|
+
delete: [Permissions.IsOwner],
|
|
18
|
+
list: [Permissions.IsAuthenticated],
|
|
19
|
+
read: [Permissions.IsAuthenticated],
|
|
20
|
+
update: [Permissions.IsOwner],
|
|
21
|
+
},
|
|
22
|
+
queryFields: ["completed", "title"],
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const build = (args: MCPToolArgs, config: MCPConfig = {}, options = baseOptions) => {
|
|
26
|
+
return buildListQuery({args, config, options});
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
describe("buildListQuery", () => {
|
|
30
|
+
it("keeps exact matches on allowed fields", () => {
|
|
31
|
+
const {query} = build({completed: true});
|
|
32
|
+
|
|
33
|
+
expect(query).toEqual({completed: true});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("drops pagination and population args", () => {
|
|
37
|
+
const {query} = build({limit: 10, page: 2, populate: "ownerId", sort: "-title"});
|
|
38
|
+
|
|
39
|
+
expect(query).toEqual({});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("drops undefined values", () => {
|
|
43
|
+
const {query} = build({completed: undefined, title: "keep"});
|
|
44
|
+
|
|
45
|
+
expect(query).toEqual({title: "keep"});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("ignores fields outside queryFields", () => {
|
|
49
|
+
const {query} = build({secret: "nope", title: "keep"});
|
|
50
|
+
|
|
51
|
+
expect(query).toEqual({title: "keep"});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("starts from defaultQueryParams", () => {
|
|
55
|
+
const {query} = build(
|
|
56
|
+
{title: "keep"},
|
|
57
|
+
{},
|
|
58
|
+
{...baseOptions, defaultQueryParams: {archived: false}}
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
expect(query).toEqual({archived: false, title: "keep"});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("ignores a queryField that is also excluded from MCP", () => {
|
|
65
|
+
const {query} = build({completed: true, title: "keep"}, {excludeFields: ["completed"]});
|
|
66
|
+
|
|
67
|
+
expect(query).toEqual({title: "keep"});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("ignores a nested queryField whose parent is excluded", () => {
|
|
71
|
+
const {error, query} = build(
|
|
72
|
+
{"metadata.secret": "oracle", title: "keep"},
|
|
73
|
+
{excludeFields: ["metadata"]},
|
|
74
|
+
{...baseOptions, queryFields: ["metadata.secret", "title"]}
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
expect(error).toBeUndefined();
|
|
78
|
+
expect(query).toEqual({title: "keep"});
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("rejects an excluded nested field inside a logical branch", () => {
|
|
82
|
+
const {error} = build(
|
|
83
|
+
{$or: [{"metadata.secret": "oracle"}]},
|
|
84
|
+
{excludeFields: ["metadata"]},
|
|
85
|
+
{...baseOptions, queryFields: ["metadata.secret", "title"]}
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
expect(error).toContain("metadata.secret");
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("allows comparison operators", () => {
|
|
92
|
+
const {query} = build({title: {$in: ["a", "b"]}});
|
|
93
|
+
|
|
94
|
+
expect(query).toEqual({title: {$in: ["a", "b"]}});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("allows nested comparison operators inside arrays", () => {
|
|
98
|
+
const {query} = build({$or: [{title: {$in: ["a"]}}, {completed: {$ne: true}}]});
|
|
99
|
+
|
|
100
|
+
expect(query).toEqual({$or: [{title: {$in: ["a"]}}, {completed: {$ne: true}}]});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("allows exact matches on embedded objects", () => {
|
|
104
|
+
const {error, query} = build({title: {nested: "value"}});
|
|
105
|
+
|
|
106
|
+
expect(error).toBeUndefined();
|
|
107
|
+
expect(query).toEqual({title: {nested: "value"}});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("rejects $where", () => {
|
|
111
|
+
const {error, query} = build({title: {$where: "return true"}});
|
|
112
|
+
|
|
113
|
+
expect(error).toContain("$where");
|
|
114
|
+
expect(query).toBeUndefined();
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("rejects $expr nested deeper in the value", () => {
|
|
118
|
+
const {error} = build({title: {$in: [{$expr: 1}]}});
|
|
119
|
+
|
|
120
|
+
expect(error).toContain("$expr");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("rejects $function inside a logical branch", () => {
|
|
124
|
+
const {error} = build({$and: [{title: {$function: "x"}}]});
|
|
125
|
+
|
|
126
|
+
expect(error).toContain("$function");
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("rejects a logical branch field outside queryFields", () => {
|
|
130
|
+
const {error} = build({$or: [{secret: "leak"}]});
|
|
131
|
+
|
|
132
|
+
expect(error).toContain("secret");
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("rejects a logical operator with a non-array value", () => {
|
|
136
|
+
const {error} = build({$or: {title: "a"}});
|
|
137
|
+
|
|
138
|
+
expect(error).toContain("$or");
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("rejects an empty logical array", () => {
|
|
142
|
+
const {error} = build({$and: []});
|
|
143
|
+
|
|
144
|
+
expect(error).toContain("$and");
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("rejects a logical branch that is not an object", () => {
|
|
148
|
+
const {error} = build({$and: ["title"]});
|
|
149
|
+
|
|
150
|
+
expect(error).toContain("$and");
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("validates nested logical operators", () => {
|
|
154
|
+
const {error, query} = build({$and: [{$or: [{title: "a"}, {completed: true}]}]});
|
|
155
|
+
|
|
156
|
+
expect(error).toBeUndefined();
|
|
157
|
+
expect(query).toEqual({$and: [{$or: [{title: "a"}, {completed: true}]}]});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("rejects a disallowed field inside a nested logical operator", () => {
|
|
161
|
+
const {error} = build({$and: [{$or: [{secret: "leak"}]}]});
|
|
162
|
+
|
|
163
|
+
expect(error).toContain("secret");
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it("does not treat a logical operator as a filterable field", () => {
|
|
167
|
+
const {error} = build({$nor: [{title: "a"}]});
|
|
168
|
+
|
|
169
|
+
// $nor is not in the logical allowlist, so it is dropped like any unknown field
|
|
170
|
+
expect(error).toBeUndefined();
|
|
171
|
+
});
|
|
172
|
+
});
|
package/src/mcp/query.ts
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import type {ModelRouterOptions} from "../api";
|
|
2
|
+
import {isFieldExcluded} from "./schemaGenerator";
|
|
3
|
+
import type {MCPConfig, MCPToolArgs} from "./types";
|
|
4
|
+
|
|
5
|
+
/** Args consumed by pagination/population, never treated as filters. */
|
|
6
|
+
export const RESERVED_LIST_ARGS = new Set(["limit", "page", "populate", "sort"]);
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Top-level logical operators an MCP client may send. Mirrors the REST list endpoint,
|
|
10
|
+
* which allows `$and` / `$or` and validates every nested key against `queryFields`.
|
|
11
|
+
*/
|
|
12
|
+
export const ALLOWED_LOGICAL_OPERATORS = new Set(["$and", "$or"]);
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Comparison operators allowed inside a field's value, e.g. `{completed: {$ne: true}}`.
|
|
16
|
+
* Deliberately excludes evaluation operators that can run arbitrary code or ignore
|
|
17
|
+
* indexes ($where, $expr, $function, $accumulator, $jsonSchema, $text).
|
|
18
|
+
*/
|
|
19
|
+
export const ALLOWED_FIELD_OPERATORS = new Set([
|
|
20
|
+
"$all",
|
|
21
|
+
"$eq",
|
|
22
|
+
"$exists",
|
|
23
|
+
"$gt",
|
|
24
|
+
"$gte",
|
|
25
|
+
"$in",
|
|
26
|
+
"$lt",
|
|
27
|
+
"$lte",
|
|
28
|
+
"$ne",
|
|
29
|
+
"$nin",
|
|
30
|
+
"$options",
|
|
31
|
+
"$regex",
|
|
32
|
+
"$size",
|
|
33
|
+
]);
|
|
34
|
+
|
|
35
|
+
const isPlainObject = (value: unknown): value is Record<string, unknown> => {
|
|
36
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Recursively reject `$`-prefixed keys that are not in the field-operator allowlist.
|
|
41
|
+
* Non-operator keys are left alone so exact matches on embedded documents still work.
|
|
42
|
+
*
|
|
43
|
+
* @returns The offending operator name, or undefined when the value is safe.
|
|
44
|
+
*/
|
|
45
|
+
const findDisallowedOperator = (value: unknown): string | undefined => {
|
|
46
|
+
if (Array.isArray(value)) {
|
|
47
|
+
for (const item of value) {
|
|
48
|
+
const found = findDisallowedOperator(item);
|
|
49
|
+
if (found) {
|
|
50
|
+
return found;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (!isPlainObject(value)) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
for (const [key, nested] of Object.entries(value)) {
|
|
61
|
+
if (key.startsWith("$") && !ALLOWED_FIELD_OPERATORS.has(key)) {
|
|
62
|
+
return key;
|
|
63
|
+
}
|
|
64
|
+
const found = findDisallowedOperator(nested);
|
|
65
|
+
if (found) {
|
|
66
|
+
return found;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return undefined;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Validate one branch of a `$and` / `$or` clause. Every key must either be another
|
|
75
|
+
* logical operator or a field listed in `queryFields`, matching REST's behavior.
|
|
76
|
+
*/
|
|
77
|
+
const validateLogicalBranch = (
|
|
78
|
+
branch: unknown,
|
|
79
|
+
allowedQueryFields: Set<string>
|
|
80
|
+
): string | undefined => {
|
|
81
|
+
if (!isPlainObject(branch)) {
|
|
82
|
+
return "Logical operators ($and/$or) take an array of filter objects";
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
for (const [key, value] of Object.entries(branch)) {
|
|
86
|
+
if (ALLOWED_LOGICAL_OPERATORS.has(key)) {
|
|
87
|
+
const error = validateLogicalClause(key, value, allowedQueryFields);
|
|
88
|
+
if (error) {
|
|
89
|
+
return error;
|
|
90
|
+
}
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (!allowedQueryFields.has(key)) {
|
|
94
|
+
return `${key} is not allowed as a filter field`;
|
|
95
|
+
}
|
|
96
|
+
const disallowed = findDisallowedOperator(value);
|
|
97
|
+
if (disallowed) {
|
|
98
|
+
return `${disallowed} is not an allowed query operator`;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return undefined;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const validateLogicalClause = (
|
|
106
|
+
operator: string,
|
|
107
|
+
value: unknown,
|
|
108
|
+
allowedQueryFields: Set<string>
|
|
109
|
+
): string | undefined => {
|
|
110
|
+
if (!Array.isArray(value) || value.length === 0) {
|
|
111
|
+
return `${operator} takes a non-empty array of filter objects`;
|
|
112
|
+
}
|
|
113
|
+
for (const branch of value) {
|
|
114
|
+
const error = validateLogicalBranch(branch, allowedQueryFields);
|
|
115
|
+
if (error) {
|
|
116
|
+
return error;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return undefined;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
export interface BuildListQueryResult {
|
|
123
|
+
query?: Record<string, unknown>;
|
|
124
|
+
error?: string;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Build a Mongo query from MCP list tool args.
|
|
129
|
+
*
|
|
130
|
+
* Only fields in `options.queryFields` may be filtered on, and values may use the
|
|
131
|
+
* comparison operators in {@link ALLOWED_FIELD_OPERATORS} plus top-level `$and` / `$or`.
|
|
132
|
+
* Anything else is rejected rather than silently dropped so the calling LLM can correct
|
|
133
|
+
* itself instead of receiving results for a query it did not ask for.
|
|
134
|
+
*/
|
|
135
|
+
export const buildListQuery = ({
|
|
136
|
+
args,
|
|
137
|
+
config,
|
|
138
|
+
options,
|
|
139
|
+
}: {
|
|
140
|
+
args: MCPToolArgs;
|
|
141
|
+
config: MCPConfig;
|
|
142
|
+
// noExplicitAny: ModelRouterOptions is generic over the consumer's document type
|
|
143
|
+
// biome-ignore lint/suspicious/noExplicitAny: ModelRouterOptions is generic over the consumer's document type
|
|
144
|
+
options: ModelRouterOptions<any>;
|
|
145
|
+
}): BuildListQueryResult => {
|
|
146
|
+
const query: Record<string, unknown> = {...(options.defaultQueryParams ?? {})};
|
|
147
|
+
const excludeFields = config.excludeFields ?? [];
|
|
148
|
+
const allowedQueryFields = new Set(
|
|
149
|
+
(options.queryFields ?? []).filter((field) => !isFieldExcluded(field, excludeFields))
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
for (const [key, value] of Object.entries(args)) {
|
|
153
|
+
if (RESERVED_LIST_ARGS.has(key) || value === undefined) {
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (ALLOWED_LOGICAL_OPERATORS.has(key)) {
|
|
158
|
+
const error = validateLogicalClause(key, value, allowedQueryFields);
|
|
159
|
+
if (error) {
|
|
160
|
+
return {error};
|
|
161
|
+
}
|
|
162
|
+
query[key] = value;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (!allowedQueryFields.has(key)) {
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const disallowed = findDisallowedOperator(value);
|
|
171
|
+
if (disallowed) {
|
|
172
|
+
return {error: `${disallowed} is not an allowed query operator`};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
query[key] = value;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return {query};
|
|
179
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type {Model} from "mongoose";
|
|
2
|
+
import type {ZodType} from "zod";
|
|
3
|
+
|
|
4
|
+
import type {ModelRouterOptions} from "../api";
|
|
5
|
+
import type {User} from "../auth";
|
|
6
|
+
import type {MCPConfig, MCPRegistryEntry, MCPToolArgs, MCPToolResult} from "./types";
|
|
7
|
+
|
|
8
|
+
export interface MCPCustomTool {
|
|
9
|
+
description: string;
|
|
10
|
+
handler: (args: MCPToolArgs, user?: User) => Promise<MCPToolResult>;
|
|
11
|
+
name: string;
|
|
12
|
+
zodSchema: ZodType;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const mcpRegistry: MCPRegistryEntry[] = [];
|
|
16
|
+
const mcpCustomTools: MCPCustomTool[] = [];
|
|
17
|
+
|
|
18
|
+
export const registerMCPModel = (
|
|
19
|
+
// noExplicitAny: Mongoose's invariant generics require any to accept arbitrary consumer models
|
|
20
|
+
// biome-ignore lint/suspicious/noExplicitAny: Mongoose's invariant generics require any to accept arbitrary consumer models
|
|
21
|
+
model: Model<any>,
|
|
22
|
+
config: MCPConfig,
|
|
23
|
+
// noExplicitAny: ModelRouterOptions is generic over the consumer's document type
|
|
24
|
+
// biome-ignore lint/suspicious/noExplicitAny: ModelRouterOptions is generic over the consumer's document type
|
|
25
|
+
options: ModelRouterOptions<any>
|
|
26
|
+
): void => {
|
|
27
|
+
mcpRegistry.push({
|
|
28
|
+
config,
|
|
29
|
+
model,
|
|
30
|
+
modelName: model.modelName,
|
|
31
|
+
options,
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Replace options on an existing MCP registry entry after TerrenoApp injects
|
|
37
|
+
* accessControl (same contract as updateRealtimeRegistryOptions).
|
|
38
|
+
*/
|
|
39
|
+
export const updateMCPRegistryOptions = (
|
|
40
|
+
modelName: string,
|
|
41
|
+
options: ModelRouterOptions<unknown>
|
|
42
|
+
): void => {
|
|
43
|
+
const existing = mcpRegistry.find((entry) => entry.modelName === modelName);
|
|
44
|
+
if (!existing) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
existing.options = options as MCPRegistryEntry["options"];
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export const registerMCPTool = (tool: MCPCustomTool): void => {
|
|
51
|
+
const existingIndex = mcpCustomTools.findIndex((registered) => registered.name === tool.name);
|
|
52
|
+
if (existingIndex >= 0) {
|
|
53
|
+
mcpCustomTools[existingIndex] = tool;
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
mcpCustomTools.push(tool);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export const getMCPRegistry = (): MCPRegistryEntry[] => {
|
|
60
|
+
return mcpRegistry;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export const getMCPCustomTools = (): MCPCustomTool[] => {
|
|
64
|
+
return mcpCustomTools;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export const clearMCPRegistry = (): void => {
|
|
68
|
+
mcpRegistry.length = 0;
|
|
69
|
+
mcpCustomTools.length = 0;
|
|
70
|
+
};
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
import mongoose, {Schema} from "mongoose";
|
|
3
|
+
import {z} from "zod";
|
|
4
|
+
|
|
5
|
+
import {generateInputSchema, generateToolDescription} from "./schemaGenerator";
|
|
6
|
+
import type {MCPConfig} from "./types";
|
|
7
|
+
|
|
8
|
+
/** The slice of the generated JSON Schema these tests assert on. */
|
|
9
|
+
interface JSONSchemaWithProperties {
|
|
10
|
+
properties?: Record<string, {description?: string} | undefined>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const createTestModel = () => {
|
|
14
|
+
const schema = new Schema({
|
|
15
|
+
completed: {default: false, description: "Whether the item is complete", type: Boolean},
|
|
16
|
+
count: {description: "A count", type: Number},
|
|
17
|
+
name: {description: "The name", required: true, type: String},
|
|
18
|
+
ownerId: {description: "Owner", ref: "User", type: Schema.Types.ObjectId},
|
|
19
|
+
secretField: {description: "A secret", type: String},
|
|
20
|
+
status: {description: "Status", enum: ["active", "inactive"], type: String},
|
|
21
|
+
tags: {description: "Tags", type: [String]},
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// Create model if it doesn't already exist
|
|
25
|
+
try {
|
|
26
|
+
return mongoose.model("MCPTestItem");
|
|
27
|
+
} catch {
|
|
28
|
+
return mongoose.model("MCPTestItem", schema);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
describe("generateInputSchema", () => {
|
|
33
|
+
const model = createTestModel();
|
|
34
|
+
const config: MCPConfig = {};
|
|
35
|
+
|
|
36
|
+
it("generates create schema with required fields", () => {
|
|
37
|
+
const schema = generateInputSchema(model, "create", config);
|
|
38
|
+
// Should be a ZodObject
|
|
39
|
+
expect(schema).toBeDefined();
|
|
40
|
+
|
|
41
|
+
// Parse valid input
|
|
42
|
+
const result = schema.safeParse({name: "test"});
|
|
43
|
+
expect(result.success).toBe(true);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("generates create schema that rejects missing required fields", () => {
|
|
47
|
+
const schema = generateInputSchema(model, "create", config);
|
|
48
|
+
const result = schema.safeParse({completed: true});
|
|
49
|
+
expect(result.success).toBe(false);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("generates read schema with id", () => {
|
|
53
|
+
const schema = generateInputSchema(model, "read", config);
|
|
54
|
+
const result = schema.safeParse({id: "507f1f77bcf86cd799439011"});
|
|
55
|
+
expect(result.success).toBe(true);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("generates list schema with pagination", () => {
|
|
59
|
+
const schema = generateInputSchema(model, "list", config, ["status", "completed"]);
|
|
60
|
+
const result = schema.safeParse({limit: 10, page: 1, status: "active"});
|
|
61
|
+
expect(result.success).toBe(true);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("generates update schema with id required", () => {
|
|
65
|
+
const schema = generateInputSchema(model, "update", config);
|
|
66
|
+
const result = schema.safeParse({id: "507f1f77bcf86cd799439011", name: "updated"});
|
|
67
|
+
expect(result.success).toBe(true);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("generates delete schema with id only", () => {
|
|
71
|
+
const schema = generateInputSchema(model, "delete", config);
|
|
72
|
+
const result = schema.safeParse({id: "507f1f77bcf86cd799439011"});
|
|
73
|
+
expect(result.success).toBe(true);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("excludes REST excludeFromCreate fields from the create schema", () => {
|
|
77
|
+
const schema = generateInputSchema(model, "create", config, undefined, undefined, ["ownerId"]);
|
|
78
|
+
const jsonSchema = z.toJSONSchema(schema) as JSONSchemaWithProperties;
|
|
79
|
+
expect(jsonSchema.properties?.ownerId).toBeUndefined();
|
|
80
|
+
expect(jsonSchema.properties?.name).toBeDefined();
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("excludes fields when excludeFields is set", () => {
|
|
84
|
+
const configWithExcludes: MCPConfig = {excludeFields: ["secretField"]};
|
|
85
|
+
const schema = generateInputSchema(model, "create", configWithExcludes);
|
|
86
|
+
|
|
87
|
+
// Should accept input without secretField
|
|
88
|
+
const result = schema.safeParse({name: "test"});
|
|
89
|
+
expect(result.success).toBe(true);
|
|
90
|
+
|
|
91
|
+
// secretField should not cause any error if present (zod strips unknown by default)
|
|
92
|
+
// but shouldn't be in the schema
|
|
93
|
+
const jsonSchema = z.toJSONSchema(schema) as JSONSchemaWithProperties;
|
|
94
|
+
expect(jsonSchema.properties?.secretField).toBeUndefined();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("respects maxLimit in list description", () => {
|
|
98
|
+
const configWithLimit: MCPConfig = {maxLimit: 25};
|
|
99
|
+
const schema = generateInputSchema(model, "list", configWithLimit);
|
|
100
|
+
const jsonSchema = z.toJSONSchema(schema) as JSONSchemaWithProperties;
|
|
101
|
+
expect(jsonSchema.properties?.limit?.description).toContain("25");
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
describe("generateToolDescription", () => {
|
|
106
|
+
const model = createTestModel();
|
|
107
|
+
|
|
108
|
+
it("generates list description with query fields", () => {
|
|
109
|
+
const desc = generateToolDescription(model, "list", {}, ["status", "completed"]);
|
|
110
|
+
expect(desc).toContain("List MCPTestItem");
|
|
111
|
+
expect(desc).toContain("status");
|
|
112
|
+
expect(desc).toContain("completed");
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("generates create description with field types and required info", () => {
|
|
116
|
+
const desc = generateToolDescription(model, "create", {});
|
|
117
|
+
expect(desc).toContain("Create");
|
|
118
|
+
expect(desc).toContain("name (String) required");
|
|
119
|
+
expect(desc).toContain("completed (Boolean)");
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("includes enum values in create description", () => {
|
|
123
|
+
const desc = generateToolDescription(model, "create", {});
|
|
124
|
+
expect(desc).toContain("status (enum: active|inactive)");
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("omits REST excludeFromCreate fields from the create description", () => {
|
|
128
|
+
const desc = generateToolDescription(model, "create", {}, undefined, undefined, ["ownerId"]);
|
|
129
|
+
expect(desc).toContain("Create");
|
|
130
|
+
expect(desc).not.toContain("ownerId");
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("includes ref model name in create description", () => {
|
|
134
|
+
const desc = generateToolDescription(model, "create", {});
|
|
135
|
+
expect(desc).toContain("ownerId (ref: User)");
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("generates read description with the declared populate-able refs", () => {
|
|
139
|
+
const desc = generateToolDescription(model, "read", {}, undefined, [{path: "ownerId"}]);
|
|
140
|
+
expect(desc).toContain("Read a single MCPTestItem");
|
|
141
|
+
expect(desc).toContain("ownerId (User)");
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("omits populate-able refs from the read description when none are declared", () => {
|
|
145
|
+
const desc = generateToolDescription(model, "read", {});
|
|
146
|
+
expect(desc).toContain("Read a single MCPTestItem");
|
|
147
|
+
expect(desc).not.toContain("Populate-able refs");
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("omits an excluded ref from the read description even when declared", () => {
|
|
151
|
+
const desc = generateToolDescription(model, "read", {excludeFields: ["ownerId"]}, undefined, [
|
|
152
|
+
{path: "ownerId"},
|
|
153
|
+
]);
|
|
154
|
+
expect(desc).not.toContain("Populate-able refs");
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it("generates update description with updatable field names", () => {
|
|
158
|
+
const desc = generateToolDescription(model, "update", {});
|
|
159
|
+
expect(desc).toContain("Update");
|
|
160
|
+
expect(desc).toContain("Updatable fields:");
|
|
161
|
+
expect(desc).toContain("name");
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("uses custom description when provided", () => {
|
|
165
|
+
const desc = generateToolDescription(model, "list", {description: "Custom items"});
|
|
166
|
+
expect(desc).toContain("Custom items");
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("excludes fields from description when excludeFields set", () => {
|
|
170
|
+
const desc = generateToolDescription(model, "list", {excludeFields: ["status"]}, [
|
|
171
|
+
"status",
|
|
172
|
+
"completed",
|
|
173
|
+
]);
|
|
174
|
+
expect(desc).not.toContain("status");
|
|
175
|
+
expect(desc).toContain("completed");
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("excludes fields from create description", () => {
|
|
179
|
+
const desc = generateToolDescription(model, "create", {excludeFields: ["secretField"]});
|
|
180
|
+
expect(desc).not.toContain("secretField");
|
|
181
|
+
});
|
|
182
|
+
});
|