@terreno/admin-backend 57.20.0 → 57.22.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/CHANGELOG.md +81 -0
- package/biome.jsonc +1 -0
- package/dist/adminApp.d.ts +2 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +7 -1
- package/dist/mcpAdmin.d.ts +42 -0
- package/dist/mcpAdmin.js +212 -0
- package/e2e/smoke.ts +246 -0
- package/package.json +10 -7
- package/src/adminApp.migrations.test.ts +3 -2
- package/src/adminApp.models.test.ts +6 -3
- package/src/adminApp.test.ts +2 -2
- package/src/adminApp.ts +2 -0
- package/src/adminScriptJob.test.ts +10 -3
- package/src/adminSyncMutationAuthMismatch.test.ts +3 -2
- package/src/aggregateAdmin.test.ts +9 -5
- package/src/enrichAdminListFilters.test.ts +1 -1
- package/src/index.ts +8 -0
- package/src/mcpAdmin.test.ts +176 -0
- package/src/mcpAdmin.ts +236 -0
- package/tsconfig.typecheck.json +18 -0
package/src/mcpAdmin.ts
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ADMIN_MODEL_ACCESS,
|
|
3
|
+
McpOAuthClient,
|
|
4
|
+
McpOAuthToken,
|
|
5
|
+
McpRequestLog,
|
|
6
|
+
McpServiceToken,
|
|
7
|
+
McpToolSetting,
|
|
8
|
+
type ScriptRunner,
|
|
9
|
+
} from "@terreno/api";
|
|
10
|
+
import mongoose from "mongoose";
|
|
11
|
+
|
|
12
|
+
import type {AdminModelConfig, AdminScriptConfig} from "./adminApp";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* RBAC statements for the MCP admin screens. Spread into `createAccess({statements})` when
|
|
16
|
+
* `AdminApp` has `accessControl`; without it the screens use `IsAdmin`.
|
|
17
|
+
*/
|
|
18
|
+
export const mcpAdminStatements = {
|
|
19
|
+
adminMcpOAuthClient: ADMIN_MODEL_ACCESS,
|
|
20
|
+
adminMcpOAuthConnection: ADMIN_MODEL_ACCESS,
|
|
21
|
+
adminMcpRequestLog: ADMIN_MODEL_ACCESS,
|
|
22
|
+
adminMcpServiceToken: ADMIN_MODEL_ACCESS,
|
|
23
|
+
adminMcpTool: ADMIN_MODEL_ACCESS,
|
|
24
|
+
} as const;
|
|
25
|
+
|
|
26
|
+
/** Sidebar group the MCP screens share. */
|
|
27
|
+
export const MCP_ADMIN_GROUP = "MCP";
|
|
28
|
+
|
|
29
|
+
export interface McpAdminModelsOptions {
|
|
30
|
+
/** Sidebar group. Default `"MCP"`. */
|
|
31
|
+
group?: string;
|
|
32
|
+
/**
|
|
33
|
+
* Include the request log screen. Match `TerrenoApp`'s `mcpRequestLog`: the screen is empty
|
|
34
|
+
* without it. Default false, like the server option.
|
|
35
|
+
*/
|
|
36
|
+
requestLog?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Include the tool registry screen. Match `TerrenoApp`'s `mcpToolSettings`: without it the
|
|
39
|
+
* server ignores the switches. Default false, like the server option.
|
|
40
|
+
*/
|
|
41
|
+
toolSettings?: boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Admin screens for a Terreno MCP server: service tokens, OAuth clients, OAuth connections
|
|
46
|
+
* (token families), the request log, and the tool registry. Secret hashes are never listed, and
|
|
47
|
+
* "delete" on a token or connection revokes it instead of removing the row.
|
|
48
|
+
*/
|
|
49
|
+
export const mcpAdminModels = (options: McpAdminModelsOptions = {}): AdminModelConfig[] => {
|
|
50
|
+
const group = options.group ?? MCP_ADMIN_GROUP;
|
|
51
|
+
const models: AdminModelConfig[] = [
|
|
52
|
+
{
|
|
53
|
+
adminAccess: {resource: "adminMcpServiceToken"},
|
|
54
|
+
displayName: "MCP service tokens",
|
|
55
|
+
group,
|
|
56
|
+
hiddenFields: ["tokenHash"],
|
|
57
|
+
listFields: [
|
|
58
|
+
"name",
|
|
59
|
+
"tokenPrefix",
|
|
60
|
+
"userId",
|
|
61
|
+
"lastUsedAt",
|
|
62
|
+
"expiresAt",
|
|
63
|
+
"revokedAt",
|
|
64
|
+
"created",
|
|
65
|
+
],
|
|
66
|
+
model: McpServiceToken,
|
|
67
|
+
permissions: {create: false, delete: true, update: false},
|
|
68
|
+
routePath: "/mcp-service-tokens",
|
|
69
|
+
searchFields: ["name", "tokenPrefix"],
|
|
70
|
+
sortableFields: ["name", "created", "lastUsedAt", "expiresAt", "revokedAt"],
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
adminAccess: {resource: "adminMcpOAuthClient"},
|
|
74
|
+
displayName: "MCP OAuth clients",
|
|
75
|
+
group,
|
|
76
|
+
hiddenFields: ["clientSecretHash"],
|
|
77
|
+
listFields: [
|
|
78
|
+
"clientName",
|
|
79
|
+
"clientId",
|
|
80
|
+
"redirectUris",
|
|
81
|
+
"grantTypes",
|
|
82
|
+
"tokenEndpointAuthMethod",
|
|
83
|
+
"userId",
|
|
84
|
+
"revokedAt",
|
|
85
|
+
"created",
|
|
86
|
+
],
|
|
87
|
+
model: McpOAuthClient,
|
|
88
|
+
// Deleting a client removes its registration and revokes every token issued to it.
|
|
89
|
+
permissions: {create: false, delete: true, update: false},
|
|
90
|
+
routePath: "/mcp-oauth-clients",
|
|
91
|
+
searchFields: ["clientName", "clientId"],
|
|
92
|
+
sortableFields: ["clientName", "created", "revokedAt"],
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
adminAccess: {resource: "adminMcpOAuthConnection"},
|
|
96
|
+
displayName: "MCP OAuth connections",
|
|
97
|
+
group,
|
|
98
|
+
hiddenFields: ["accessTokenHash", "refreshTokenHash"],
|
|
99
|
+
listFields: [
|
|
100
|
+
"userId",
|
|
101
|
+
"clientId",
|
|
102
|
+
"familyId",
|
|
103
|
+
"scope",
|
|
104
|
+
"accessExpiresAt",
|
|
105
|
+
"refreshExpiresAt",
|
|
106
|
+
"rotatedAt",
|
|
107
|
+
"revokedAt",
|
|
108
|
+
"created",
|
|
109
|
+
],
|
|
110
|
+
model: McpOAuthToken,
|
|
111
|
+
// Each row is one access/refresh pair. Deleting any row revokes its whole connection.
|
|
112
|
+
permissions: {create: false, delete: true, update: false},
|
|
113
|
+
routePath: "/mcp-oauth-connections",
|
|
114
|
+
searchFields: ["clientId", "familyId"],
|
|
115
|
+
sortableFields: ["created", "accessExpiresAt", "refreshExpiresAt", "revokedAt"],
|
|
116
|
+
},
|
|
117
|
+
];
|
|
118
|
+
if (options.requestLog === true) {
|
|
119
|
+
models.push({
|
|
120
|
+
adminAccess: {resource: "adminMcpRequestLog"},
|
|
121
|
+
defaultSort: "-created",
|
|
122
|
+
displayName: "MCP request log",
|
|
123
|
+
group,
|
|
124
|
+
listFields: [
|
|
125
|
+
"created",
|
|
126
|
+
"toolName",
|
|
127
|
+
"outcome",
|
|
128
|
+
"durationMs",
|
|
129
|
+
"userId",
|
|
130
|
+
"credential",
|
|
131
|
+
"oauthClientId",
|
|
132
|
+
"serviceTokenId",
|
|
133
|
+
"errorName",
|
|
134
|
+
],
|
|
135
|
+
model: McpRequestLog,
|
|
136
|
+
permissions: {create: false, delete: false, update: false},
|
|
137
|
+
routePath: "/mcp-request-log",
|
|
138
|
+
searchFields: ["toolName", "oauthClientId", "errorName"],
|
|
139
|
+
sortableFields: ["created", "toolName", "durationMs", "outcome"],
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
if (options.toolSettings === true) {
|
|
143
|
+
models.push({
|
|
144
|
+
adminAccess: {resource: "adminMcpTool"},
|
|
145
|
+
defaultSort: "name",
|
|
146
|
+
displayName: "MCP tools",
|
|
147
|
+
group,
|
|
148
|
+
listFields: ["name", "enabled", "source", "registered", "lastRegisteredAt"],
|
|
149
|
+
model: McpToolSetting,
|
|
150
|
+
permissions: {create: false, delete: false, update: true},
|
|
151
|
+
readonlyFields: ["name", "source", "registered", "description", "lastRegisteredAt"],
|
|
152
|
+
routePath: "/mcp-tools",
|
|
153
|
+
searchFields: ["name", "description"],
|
|
154
|
+
sortableFields: ["name", "enabled", "source", "lastRegisteredAt"],
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
return models;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
const SIGN_OUT_SCRIPT_NAME = "mcp-sign-out-everywhere";
|
|
161
|
+
const OBJECT_ID_HEX = /^[0-9a-f]{24}$/i;
|
|
162
|
+
|
|
163
|
+
/** Resolve the `user` argument (an id or an email) to a user id. */
|
|
164
|
+
const resolveUserId = async (value: string): Promise<mongoose.Types.ObjectId | undefined> => {
|
|
165
|
+
// Only a 24-hex-character string is an id. isValidObjectId also accepts any 12-character
|
|
166
|
+
// string, which would read a short email like "ab@gmail.com" as an id.
|
|
167
|
+
if (OBJECT_ID_HEX.test(value)) {
|
|
168
|
+
return new mongoose.Types.ObjectId(value);
|
|
169
|
+
}
|
|
170
|
+
const userModel = mongoose.models.User;
|
|
171
|
+
if (!userModel) {
|
|
172
|
+
return undefined;
|
|
173
|
+
}
|
|
174
|
+
const email = value.trim();
|
|
175
|
+
const users = await userModel
|
|
176
|
+
.find({email: {$in: [...new Set([email, email.toLowerCase()])]}})
|
|
177
|
+
.limit(2)
|
|
178
|
+
.select({_id: 1})
|
|
179
|
+
.lean<Array<{_id: mongoose.Types.ObjectId}>>();
|
|
180
|
+
return users.length === 1 ? users[0]._id : undefined;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Revoke every MCP credential a user holds: OAuth connections and service tokens. Dry run counts
|
|
185
|
+
* what would be revoked.
|
|
186
|
+
*/
|
|
187
|
+
export const mcpSignOutEverywhere: ScriptRunner = async (wetRun, ctx) => {
|
|
188
|
+
const value = ctx?.args.getString("user");
|
|
189
|
+
if (typeof value !== "string" || value.trim().length === 0) {
|
|
190
|
+
return {results: ["Pass --user <user id or email>."], success: false};
|
|
191
|
+
}
|
|
192
|
+
const userId = await resolveUserId(value);
|
|
193
|
+
if (!userId) {
|
|
194
|
+
return {results: [`No single user matches "${value}".`], success: false};
|
|
195
|
+
}
|
|
196
|
+
if (!wetRun) {
|
|
197
|
+
const families = await McpOAuthToken.distinct("familyId", {
|
|
198
|
+
revokedAt: {$exists: false},
|
|
199
|
+
userId,
|
|
200
|
+
});
|
|
201
|
+
const tokens = await McpServiceToken.countDocuments({revokedAt: {$exists: false}, userId});
|
|
202
|
+
return {
|
|
203
|
+
results: [
|
|
204
|
+
`Would revoke ${families.length} OAuth connection(s) and ${tokens} service token(s) for user ${String(userId)}.`,
|
|
205
|
+
],
|
|
206
|
+
success: true,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
const families = await McpOAuthToken.revokeAllForUser(userId);
|
|
210
|
+
const tokens = await McpServiceToken.revokeAllForUser(userId);
|
|
211
|
+
await ctx?.addLog("info", `Revoked MCP credentials for user ${String(userId)}`);
|
|
212
|
+
return {
|
|
213
|
+
results: [
|
|
214
|
+
`Revoked ${families} OAuth connection(s) and ${tokens} service token(s) for user ${String(userId)}.`,
|
|
215
|
+
],
|
|
216
|
+
success: true,
|
|
217
|
+
};
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
/** Admin scripts for MCP operators. Pass to `AdminApp({scripts})`. */
|
|
221
|
+
export const mcpAdminScripts = (): AdminScriptConfig[] => [
|
|
222
|
+
{
|
|
223
|
+
args: [
|
|
224
|
+
{
|
|
225
|
+
description: "User id or email whose MCP credentials to revoke",
|
|
226
|
+
example: "someone@example.com",
|
|
227
|
+
name: "user",
|
|
228
|
+
required: true,
|
|
229
|
+
},
|
|
230
|
+
],
|
|
231
|
+
description:
|
|
232
|
+
"Revoke every MCP OAuth connection and service token a user holds. Clients must reconnect.",
|
|
233
|
+
name: SIGN_OUT_SCRIPT_NAME,
|
|
234
|
+
runner: mcpSignOutEverywhere,
|
|
235
|
+
},
|
|
236
|
+
];
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"noEmit": true,
|
|
5
|
+
"composite": false,
|
|
6
|
+
"incremental": false,
|
|
7
|
+
"declaration": false,
|
|
8
|
+
"declarationMap": false,
|
|
9
|
+
"types": [
|
|
10
|
+
"node",
|
|
11
|
+
"bun"
|
|
12
|
+
]
|
|
13
|
+
},
|
|
14
|
+
"exclude": [
|
|
15
|
+
"node_modules",
|
|
16
|
+
"dist"
|
|
17
|
+
]
|
|
18
|
+
}
|