Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

feat: agent-delegate API + MCP tools/agents, catalog updates

nirholas2a254999aca7
diff --git a/api/_lib/agent-delegate.js b/api/_lib/agent-delegate.js
new file mode 100644
index 000000000..bed49fae1
--- /dev/null
+++ b/api/_lib/agent-delegate.js
@@ -0,0 +1,47 @@
+import { sql } from './db.js';
+import { normalizeLegacyPolicy } from './embed-policy.js';
+import { llmComplete } from './llm.js';
+
+export class AgentNotFoundError extends Error {
+ constructor(agentId) {
+ super('target agent not found');
+ this.name = 'AgentNotFoundError';
+ this.agentId = agentId;
+ }
+}
+
+/**
+ * Run a single LLM turn AS the target agent: load its manifest (system prompt +
+ * configured model) and answer `message`. The delegated turn has no tool access,
+ * so it cannot itself delegate — recursion is structurally impossible here.
+ *
+ * Transport concerns (auth, rate limiting, recursion-depth header) are enforced
+ * by callers at their boundary, not in this shared core.
+ *
+ * @param {{ toAgentId: string, message: string }} params
+ * @returns {Promise<{ response: string, agentId: string, model: string }>}
+ * @throws {AgentNotFoundError} when no live agent matches `toAgentId`
+ * @throws {import('./llm.js').LlmUnavailableError} when no LLM provider is configured
+ */
+export async function runAgentDelegation({ toAgentId, message }) {
+ const [agent] = await sql`
+ SELECT id, name, description, embed_policy, meta
+ FROM agent_identities
+ WHERE id = ${toAgentId} AND deleted_at IS NULL
+ `;
+ if (!agent) throw new AgentNotFoundError(toAgentId);
+
+ const policy = normalizeLegacyPolicy(agent.embed_policy);
+ const model = policy?.brain?.model || 'claude-haiku-4-5-20251001';
+ const systemPrompt =
+ agent.meta?.brain?.instructions ||
+ `You are ${agent.name}. ${agent.description || ''}`.trim();
+
+ const result = await llmComplete({
+ system: systemPrompt,
+ user: message,
+ maxTokens: 1024,
+ anthropicModel: model,
+ });
+ return { response: result.text, agentId: toAgentId, model: result.model };
+}
diff --git a/api/_mcp/catalog.js b/api/_mcp/catalog.js
index e11388def..2cbf38e03 100644
--- a/api/_mcp/catalog.js
+++ b/api/_mcp/catalog.js
@@ -4,8 +4,9 @@ import { toolDefs as avatarDefs } from './tools/avatars.js';
import { toolDefs as modelDefs } from './tools/models.js';
import { toolDefs as solanaDefs } from './tools/solana.js';
import { toolDefs as pumpfunDefs } from './tools/pumpfun.js';
+import { toolDefs as agentDefs } from './tools/agents.js';
-const allDefs = [...avatarDefs, ...modelDefs, ...solanaDefs, ...pumpfunDefs];
+const allDefs = [...avatarDefs, ...modelDefs, ...solanaDefs, ...pumpfunDefs, ...agentDefs];
// Schema objects for tools/list — strip internal fields (scope, handler).
export const TOOL_CATALOG = allDefs.map(({ scope: _s, handler: _h, ...schema }) => schema);
diff --git a/api/_mcp/tools/agents.js b/api/_mcp/tools/agents.js
new file mode 100644
index 000000000..5b4132b61
--- /dev/null
+++ b/api/_mcp/tools/agents.js
@@ -0,0 +1,50 @@
+import { limits } from '../../_lib/rate-limit.js';
+import { runAgentDelegation, AgentNotFoundError } from '../../_lib/agent-delegate.js';
+
+function rpcError(code, message, data) {
+ const e = new Error(message);
+ e.code = code;
+ e.data = data;
+ return e;
+}
+
+export const toolDefs = [
+ {
+ name: 'call_agent',
+ title: 'Call agent',
+ description:
+ 'Send a message to another three.ws agent and get its response. Use this to delegate specialized tasks.',
+ inputSchema: {
+ type: 'object',
+ properties: {
+ agent_id: { type: 'string', description: "The agent's ID" },
+ message: { type: 'string', description: 'The message to send' },
+ },
+ required: ['agent_id', 'message'],
+ additionalProperties: false,
+ },
+ scope: 'avatars:read',
+ async handler(args, auth) {
+ // Same 10/min ceiling as the HTTP delegate endpoint, keyed to the caller.
+ const rl = await limits.agentDelegate(auth.userId || auth.rateKey || 'anon');
+ if (!rl.success)
+ throw rpcError(-32000, 'rate_limited', {
+ retry_after: Math.ceil((rl.reset - Date.now()) / 1000),
+ });
+
+ try {
+ const out = await runAgentDelegation({
+ toAgentId: args.agent_id,
+ message: args.message,
+ });
+ return {
+ content: [{ type: 'text', text: out.response }],
+ structuredContent: out,
+ };
+ } catch (err) {
+ if (err instanceof AgentNotFoundError) throw new Error('target agent not found');
+ throw err;
+ }
+ },
+ },
+];
diff --git a/api/agent-delegate.js b/api/agent-delegate.js
index d43743635..efcbb5fef 100644
--- a/api/agent-delegate.js
+++ b/api/agent-delegate.js
@@ -1,9 +1,8 @@
-import { sql } from './_lib/db.js';
import { authenticateBearer, extractBearer, getSessionUser } from './_lib/auth.js';
import { cors, json, method, wrap, error, readJson } from './_lib/http.js';
import { limits } from './_lib/rate-limit.js';
-import { normalizeLegacyPolicy } from './_lib/embed-policy.js';
-import { llmComplete, LlmUnavailableError } from './_lib/llm.js';
+import { LlmUnavailableError } from './_lib/llm.js';
+import { runAgentDelegation, AgentNotFoundError } from './_lib/agent-delegate.js';
export default wrap(async (req, res) => {
if (cors(req, res, { methods: 'POST,OPTIONS' })) return;
@@ -30,28 +29,14 @@ export default wrap(async (req, res) => {
const rl = await limits.agentDelegate(fromAgentId || userId || 'anon');
if (!rl.success) return error(res, 429, 'rate_limited', 'delegate rate limit exceeded');
- const [agent] = await sql`
- SELECT id, name, description, embed_policy, meta
- FROM agent_identities
- WHERE id = ${toAgentId} AND deleted_at IS NULL
- `;
- if (!agent) return error(res, 404, 'not_found', 'target agent not found');
-
- const policy = normalizeLegacyPolicy(agent.embed_policy);
- const model = policy?.brain?.model || 'claude-haiku-4-5-20251001';
- const systemPrompt =
- agent.meta?.brain?.instructions ||
- `You are ${agent.name}. ${agent.description || ''}`.trim();
-
- let result;
try {
- result = await llmComplete({ system: systemPrompt, user: message, maxTokens: 1024, anthropicModel: model });
+ const out = await runAgentDelegation({ toAgentId, message });
+ return json(res, 200, out);
} catch (err) {
- if (err instanceof LlmUnavailableError) {
+ if (err instanceof AgentNotFoundError)
+ return error(res, 404, 'not_found', 'target agent not found');
+ if (err instanceof LlmUnavailableError)
return error(res, 503, 'llm_unavailable', 'agent delegation is not available right now');
- }
return error(res, 502, 'upstream_error', `LLM call failed: ${err.message}`);
}
-
- return json(res, 200, { response: result.text, agentId: toAgentId, model: result.model });
});