π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import { type LobeTool } from '@lobechat/types';
import { TRPCError } from '@trpc/server';
import { z } from 'zod';
import { withScopedPermission } from '@/business/server/trpc-middlewares/rbacPermission';
import { wsCompatProcedure } from '@/business/server/trpc-middlewares/workspaceAuth';
import { PluginModel } from '@/database/models/plugin';
import { router } from '@/libs/trpc/lambda';
import { serverDatabase } from '@/libs/trpc/lambda/middleware';
import { assertWorkspaceRowManageable } from './_helpers/assertWorkspaceRowManageable';
import { redactForeignComposioLinks } from './_helpers/composioProjection';
import { recordLegacyPluginWrite } from './_helpers/legacyPluginWriteMetrics';
const pluginProcedure = wsCompatProcedure.use(serverDatabase).use(async (opts) => {
const { ctx } = opts;
const wsId = ctx.workspaceId ?? undefined;
return opts.next({
ctx: { pluginModel: new PluginModel(ctx.serverDB, ctx.userId, wsId) },
});
});
/**
* Legacy `user_installed_plugins` rows. Current clients install MCP servers as
* connectors (`connector.create`) and only read, update and remove rows here.
*/
/**
* `customParams.composio` (a Composio account and the member it runs as) is
* server-owned: only the Composio connect flow writes it. Every member reads
* the workspace's account ids from the projection, so a client-written block
* could forge a "legacy" mirror of a teammate's private connection under any
* identifier and run it. A create drops the client's block; an update keeps
* the stored one.
*/
const withServerComposio = (
clientParams: unknown,
stored?: { composio?: unknown } | null,
): Record<string, unknown> | undefined => {
if (!clientParams || typeof clientParams !== 'object') return clientParams as undefined;
const { composio: _client, ...rest } = clientParams as Record<string, unknown>;
return stored?.composio === undefined ? rest : { ...rest, composio: stored.composio };
};
export const pluginRouter = router({
/**
* @deprecated Kept unchanged for released clients that still install MCPs as
* plugin rows. Remove once `legacy_plugin_write_calls_total` is close to zero.
*/
createOrInstallPlugin: pluginProcedure
.use(withScopedPermission('agent:update'))
.input(
z.object({
customParams: z.any().optional(),
identifier: z.string(),
manifest: z.any(),
settings: z.any().optional(),
type: z.enum(['plugin', 'customPlugin']),
}),
)
.mutation(async ({ input, ctx }) => {
recordLegacyPluginWrite('createOrInstallPlugin', input.type, ctx.clientMetadata);
const result = await ctx.pluginModel.findById(input.identifier);
// if not exist, we should create the plugin
if (!result) {
const data = await ctx.pluginModel.create({
customParams: withServerComposio(input.customParams),
identifier: input.identifier,
manifest: input.manifest,
settings: input.settings,
type: input.type,
});
return data.identifier;
}
// or we can just update the plugin manifest β but only the creator (or a
// workspace owner) may overwrite an existing row's manifest.
assertWorkspaceRowManageable(ctx, result.userId, 'plugin');
await ctx.pluginModel.update(input.identifier, { manifest: input.manifest });
}),
/**
* @deprecated Kept unchanged for released clients that still add custom MCPs
* as plugin rows. Remove once `legacy_plugin_write_calls_total` is close to zero.
*/
createPlugin: pluginProcedure
.use(withScopedPermission('agent:update'))
.input(
z.object({
customParams: z.any().optional(),
identifier: z.string(),
manifest: z.any().optional(),
type: z.enum(['plugin', 'customPlugin']),
}),
)
.mutation(async ({ input, ctx }) => {
recordLegacyPluginWrite('createPlugin', input.type, ctx.clientMetadata);
const data = await ctx.pluginModel.create({
customParams: withServerComposio(input.customParams),
identifier: input.identifier,
manifest: input.manifest,
type: input.type,
});
return data.identifier;
}),
getPlugins: wsCompatProcedure.use(serverDatabase).query(async ({ ctx }): Promise<LobeTool[]> => {
const pluginModel = new PluginModel(ctx.serverDB, ctx.userId, ctx.workspaceId ?? undefined);
return redactForeignComposioLinks(await pluginModel.query({ forClient: true }), ctx);
}),
removePlugin: pluginProcedure
.use(withScopedPermission('agent:update'))
.input(z.object({ id: z.string() }))
.mutation(async ({ input, ctx }) => {
const target = await ctx.pluginModel.findById(input.id);
// Missing row β keep the delete idempotent, nothing to authorize.
if (!target) return;
assertWorkspaceRowManageable(ctx, target.userId, 'plugin');
return ctx.pluginModel.delete(input.id);
}),
updatePlugin: pluginProcedure
.use(withScopedPermission('agent:update'))
.input(
z.object({
customParams: z.any().optional(),
id: z.string(),
manifest: z.any().optional(),
settings: z.any().optional(),
}),
)
.mutation(async ({ input, ctx }) => {
const target = await ctx.pluginModel.findById(input.id);
if (!target) throw new TRPCError({ code: 'NOT_FOUND', message: 'Plugin not found' });
assertWorkspaceRowManageable(ctx, target.userId, 'plugin');
return ctx.pluginModel.update(input.id, {
customParams:
input.customParams === undefined
? undefined
: withServerComposio(input.customParams, target.customParams),
manifest: input.manifest,
settings: input.settings,
});
}),
});
export type PluginRouter = typeof pluginRouter;