-
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathavatars.js
More file actions
398 lines (391 loc) · 11.3 KB
/
Copy pathavatars.js
File metadata and controls
398 lines (391 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import {
listAvatars,
getAvatar,
getAvatarBySlug,
searchPublicAvatars,
resolveAvatarUrl,
deleteAvatar,
} from '../../_lib/avatars.js';
import {
renderModelViewerHtml,
formatAvatarList,
safeCssValue,
safeCssLength,
safeHttpsUrl,
} from '../render.js';
import { readMcpPolicyByAvatar } from '../embed-policy.js';
import { resolveRenderParams, renderAvatarImage } from '../../_lib/avatar-render.js';
import { logAudit } from '../../_lib/audit.js';
function rpcError(code, message, data) {
const e = new Error(message);
e.code = code;
e.data = data;
return e;
}
// MCP tool annotations (2025-06-18 spec). destructiveHint defaults to TRUE when
// omitted, so every tool sets all four hints explicitly. Reads of account/gallery
// state are not idempotent (the underlying data and signed URLs change between
// calls) but never write anything.
const READ_ANNOTATIONS = {
readOnlyHint: true,
destructiveHint: false,
idempotentHint: false,
openWorldHint: true,
};
export const toolDefs = [
{
name: 'list_my_avatars',
title: 'List my avatars',
annotations: READ_ANNOTATIONS,
description:
"List the authenticated user's avatars. Returns id, name, slug, size, visibility, and direct model_url (when visibility permits).",
inputSchema: {
type: 'object',
properties: {
limit: { type: 'integer', minimum: 1, maximum: 100, default: 25 },
cursor: {
type: 'string',
description: 'Opaque pagination cursor from previous response.',
},
visibility: { type: 'string', enum: ['private', 'unlisted', 'public'] },
},
additionalProperties: false,
},
scope: 'avatars:read',
async handler(args, auth) {
const result = await listAvatars({
userId: auth.userId,
limit: args.limit || 25,
cursor: args.cursor,
visibility: args.visibility,
});
return {
content: [{ type: 'text', text: formatAvatarList(result.avatars) }],
structuredContent: result,
};
},
},
{
name: 'get_avatar',
title: 'Get avatar',
annotations: READ_ANNOTATIONS,
description:
'Fetch a single avatar by id or by owner+slug. Returns metadata and a model_url (public/unlisted) or short-lived signed URL (private).',
inputSchema: {
type: 'object',
properties: {
id: { type: 'string', format: 'uuid' },
slug: { type: 'string' },
},
additionalProperties: false,
},
scope: 'avatars:read',
async handler(args, auth) {
const avatar = args.id
? await getAvatar({ id: args.id, requesterId: auth.userId })
: args.slug
? await getAvatarBySlug({
ownerId: auth.userId,
slug: args.slug,
requesterId: auth.userId,
})
: null;
if (!avatar) throw new Error('avatar not found');
const urlInfo = await resolveAvatarUrl(avatar);
const merged = { ...avatar, ...urlInfo };
return {
content: [{ type: 'text', text: JSON.stringify(merged, null, 2) }],
structuredContent: merged,
};
},
},
{
name: 'search_public_avatars',
title: 'Search public avatars',
annotations: READ_ANNOTATIONS,
description:
'Search the public avatar gallery. Useful for finding characters to render without prior knowledge of an id.',
inputSchema: {
type: 'object',
properties: {
q: { type: 'string', description: 'Free-text search over name and description.' },
tag: { type: 'string', description: 'Filter to one tag.' },
limit: { type: 'integer', minimum: 1, maximum: 50, default: 12 },
},
additionalProperties: false,
},
async handler(args, auth) {
// Unauthenticated callers (x402/anonymous) are capped at 10 to prevent bulk enumeration.
const maxLimit = auth.userId ? 50 : 10;
const result = await searchPublicAvatars({
q: args.q,
tag: args.tag,
limit: Math.min(args.limit || 12, maxLimit),
});
return {
content: [
{ type: 'text', text: formatAvatarList(result.avatars, { public: true }) },
],
structuredContent: result,
};
},
},
{
name: 'render_avatar',
title: 'Render avatar',
// Builds viewer HTML in-memory; persists nothing — a read, not a write.
annotations: READ_ANNOTATIONS,
description:
'Produce an HTML <model-viewer> snippet that renders the given avatar. ' +
'Return this text as an inline HTML artifact to display an interactive 3D avatar.',
inputSchema: {
type: 'object',
properties: {
id: { type: 'string', format: 'uuid' },
slug: { type: 'string' },
auto_rotate: { type: 'boolean', default: true },
background: {
type: 'string',
description: 'CSS background color or gradient.',
default: 'transparent',
},
height: { type: 'string', default: '480px' },
width: { type: 'string', default: '100%' },
camera_orbit: {
type: 'string',
description: 'model-viewer camera-orbit value, e.g. "0deg 80deg 2m".',
},
poster: {
type: 'string',
description: 'Optional poster image URL shown while loading.',
},
ar: {
type: 'boolean',
default: true,
description: 'Include AR button for mobile.',
},
},
additionalProperties: false,
},
scope: 'avatars:read',
async handler(args, auth) {
const avatar = args.id
? await getAvatar({ id: args.id, requesterId: auth.userId })
: args.slug
? await getAvatarBySlug({
ownerId: auth.userId,
slug: args.slug,
requesterId: auth.userId,
})
: null;
if (!avatar) throw new Error('avatar not found');
// surfaces.mcp gate — check if a registered agent owns this avatar
const _mcpPolicy = await readMcpPolicyByAvatar(avatar.id);
if (_mcpPolicy && _mcpPolicy.surfaces?.mcp === false) {
throw rpcError(
-32000,
'embed_denied_surface',
'This agent disallows the MCP surface.',
);
}
const urlInfo = await resolveAvatarUrl(avatar, { expiresIn: 3600 });
const html = renderModelViewerHtml({
src: urlInfo.url,
name: avatar.name,
poster: safeHttpsUrl(args.poster),
background: safeCssValue(args.background, 'transparent'),
height: safeCssLength(args.height, '480px'),
width: safeCssLength(args.width, '100%'),
autoRotate: args.auto_rotate !== false,
ar: args.ar !== false,
cameraOrbit: safeCssValue(args.camera_orbit, ''),
});
// Keep chat text short so claude.ai doesn't dump the full HTML into the
// transcript. The HTML goes in the resource entry, which clients render
// as an inline artifact when mimeType is text/html.
const summary = `Rendered avatar "${avatar.name}". Display the attached text/html resource as an inline HTML artifact.`;
return {
content: [
{ type: 'text', text: summary },
{
type: 'resource',
resource: {
uri: `avatar://${avatar.id}`,
mimeType: 'text/html',
text: html,
},
},
],
structuredContent: { html, avatar: { ...avatar, ...urlInfo } },
};
},
},
{
name: 'render_avatar_image',
title: 'Render an avatar to an image',
// Persists the render to storage, but the cache keys on the exact
// parameters: repeating a call returns the cached image with no
// additional effect — idempotent, non-destructive write.
annotations: {
readOnlyHint: false,
destructiveHint: false,
idempotentHint: true,
openWorldHint: true,
},
description:
'Render a stored avatar to a real PNG/JPEG/WebP image (headless three.js) and ' +
'return its URL — ready for an <img> tag, social card, or game loader. Choose a ' +
'camera framing (scene), an optional pose preset and ARKit-52 expression. Renders ' +
'are cached, so repeat calls with the same parameters return instantly. Works on ' +
'your own avatars (any visibility) and on public avatars.',
inputSchema: {
type: 'object',
properties: {
avatar_id: { type: 'string', format: 'uuid', description: 'The avatar to render.' },
scene: {
type: 'string',
enum: ['full-body', 'upper-body', 'portrait', 'headshot'],
default: 'upper-body',
description: 'Camera framing preset.',
},
pose: {
type: 'string',
description: 'Optional pose preset id (see pose_model / the render catalog).',
},
expression: {
type: 'object',
description: 'Optional ARKit-52 morph map, e.g. {"mouthSmile":0.6}.',
additionalProperties: { type: 'number' },
},
size: {
type: 'integer',
minimum: 64,
maximum: 2048,
default: 512,
description: 'Square pixel dimension.',
},
bg: {
type: 'string',
default: 'transparent',
description: 'Background: a CSS color or "transparent".',
},
format: {
type: 'string',
enum: ['png', 'jpeg', 'webp'],
default: 'png',
},
},
required: ['avatar_id'],
additionalProperties: false,
},
scope: 'avatars:read',
async handler(args, auth) {
// Same visibility gate as get_avatar: an owner sees any visibility; a
// non-owner sees only public/unlisted; private-not-yours reads as missing.
const avatar = await getAvatar({ id: args.avatar_id, requesterId: auth.userId });
if (!avatar) throw new Error('avatar not found');
const urlInfo = await resolveAvatarUrl(avatar);
if (!urlInfo?.url) {
return {
content: [
{ type: 'text', text: 'Error: this avatar has no model to render yet.' },
],
isError: true,
};
}
const resolved = resolveRenderParams({
scene: args.scene,
size: args.size,
bg: args.bg,
format: args.format,
pose: args.pose,
expression: args.expression,
});
if (resolved.error) {
return {
content: [{ type: 'text', text: `Error: ${resolved.error.message}` }],
isError: true,
};
}
let out;
try {
out = await renderAvatarImage({
avatar,
glbUrl: urlInfo.url,
params: resolved.params,
awaitUpload: true,
});
} catch (err) {
return {
content: [
{
type: 'text',
text: `Render failed: ${err?.message || 'the avatar could not be rendered'}.`,
},
],
isError: true,
};
}
return {
content: [
{
type: 'text',
text: `Rendered "${avatar.name}" (${resolved.params.scene}).\n${out.imageUrl}`,
},
],
structuredContent: {
image_url: out.imageUrl,
scene: resolved.params.scene,
cached: out.cached,
},
};
},
},
{
name: 'delete_avatar',
title: 'Delete avatar',
// Deletes a user's avatar — the destructive hint is real here.
annotations: {
readOnlyHint: false,
destructiveHint: true,
idempotentHint: false,
openWorldHint: true,
},
description: 'Soft-delete an avatar you own. Requires avatars:delete scope.',
inputSchema: {
type: 'object',
properties: {
id: { type: 'string', format: 'uuid' },
confirm: {
type: 'boolean',
description: 'Must be true to confirm permanent deletion.',
},
},
required: ['id'],
additionalProperties: false,
},
scope: 'avatars:delete',
async handler(args, auth) {
if (!args.confirm) {
return {
content: [
{
type: 'text',
text: 'Set confirm: true to permanently delete this avatar.',
},
],
isError: true,
};
}
const result = await deleteAvatar({ id: args.id, userId: auth.userId });
if (!result) throw new Error('avatar not found or not yours');
logAudit({
userId: auth.userId,
action: 'delete_avatar',
resourceId: args.id,
meta: { via: 'mcp' },
});
return { content: [{ type: 'text', text: `Deleted avatar ${args.id}.` }] };
},
},
];