Skip to content

🀯 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.

10.1 KB
Raw Blame
import {
  getDisabledPluginIds,
  GetStreamableMcpServerManifestInputSchema,
  StreamableHTTPAuthSchema,
} from '@lobechat/types';
import { TRPCError } from '@trpc/server';
import { z } from 'zod';

import {
  requireWorkspaceRoleWhenScoped,
  wsCompatProcedure,
} from '@/business/server/trpc-middlewares/workspaceAuth';
import { AgentModel } from '@/database/models/agent';
import { ConnectorModel } from '@/database/models/connector';
import { ConnectorToolModel } from '@/database/models/connectorTool';
import { ConnectorToolPermission } from '@/database/schemas';
import { type ToolCallContent } from '@/libs/mcp';
import { router } from '@/libs/trpc/lambda';
import { serverDatabase, telemetry } from '@/libs/trpc/lambda/middleware';
import { FileService } from '@/server/services/file';
import { mcpService } from '@/server/services/mcp';
import { processContentBlocks } from '@/server/services/mcp/contentProcessor';
import { isMcpAppsEnabled } from '@/server/services/mcp/featureGate';

import { scheduleToolCallReport } from './_helpers';

// Define Zod schemas for MCP Client parameters
const httpParamsSchema = z.object({
  auth: StreamableHTTPAuthSchema,
  headers: z.record(z.string(), z.string()).optional(),
  name: z.string().min(1),
  type: z.literal('http'),
  url: z.string().url(),
});

const stdioParamsSchema = z.object({
  args: z.array(z.string()).optional().default([]),
  command: z.string().min(1),
  name: z.string().min(1),
  type: z.literal('stdio'),
});

// Union schema for MCPClientParams
const mcpClientParamsSchema = z.union([httpParamsSchema, stdioParamsSchema]);

// Schema for metadata that frontend needs to pass (fields that backend cannot determine)
const metaSchema = z
  .object({
    // Custom plugin info (only for custom plugins)
    customPluginInfo: z
      .object({
        avatar: z.string().optional(),
        description: z.string().optional(),
        name: z.string().optional(),
      })
      .optional(),
    // Whether this is a custom plugin
    isCustomPlugin: z.boolean().optional(),
    // Session/topic ID
    sessionId: z.string().optional(),
    // Plugin manifest version
    version: z.string().optional(),
  })
  .optional();

const mcpProcedure = wsCompatProcedure
  .use(serverDatabase)
  .use(telemetry)
  .use(async ({ ctx, next }) => {
    return next({
      ctx: {
        fileService: new FileService(ctx.serverDB, ctx.userId, ctx.workspaceId ?? undefined),
      },
    });
  });

export const mcpRouter = router({
  getStreamableMcpServerManifest: mcpProcedure
    .input(GetStreamableMcpServerManifestInputSchema)
    .query(async ({ ctx, input }) => {
      return await mcpService.getStreamableMcpServerManifest(
        input.identifier,
        input.url,
        input.metadata,
        input.auth,
        input.headers,
        // With MCP Apps on, the tools only views call are listed and kept too
        { mcpApps: await isMcpAppsEnabled(ctx.userId) },
      );
    }),
  // --- MCP Interaction ---
  // listTools now accepts MCPClientParams directly
  listTools: mcpProcedure
    .input(mcpClientParamsSchema) // Use the unified schema
    .query(async ({ input }) => {
      // Pass the validated MCPClientParams to the service
      return await mcpService.listTools(input);
    }),

  // listResources now accepts MCPClientParams directly
  listResources: mcpProcedure
    .input(mcpClientParamsSchema) // Use the unified schema
    .query(async ({ input }) => {
      // Pass the validated MCPClientParams to the service
      return await mcpService.listResources(input);
    }),

  // listPrompts now accepts MCPClientParams directly
  listPrompts: mcpProcedure
    .input(mcpClientParamsSchema) // Use the unified schema
    .query(async ({ input }) => {
      // Pass the validated MCPClientParams to the service
      return await mcpService.listPrompts(input);
    }),

  /**
   * Read an MCP Apps `ui://` resource so the chat can render a tool result's
   * interactive view. A mutation, though it only reads: the params carry the
   * plugin's auth and headers, which must travel in a POST body, not a GET URL.
   * Refused while the `mcp_apps` flag is off, since the read opens a session
   * that advertises MCP Apps.
   */
  readUiResource: mcpProcedure
    .input(z.object({ params: mcpClientParamsSchema, uri: z.string().startsWith('ui://') }))
    .mutation(async ({ ctx, input }) => {
      if (!(await isMcpAppsEnabled(ctx.userId))) {
        throw new TRPCError({ code: 'FORBIDDEN', message: 'Interactive views are turned off' });
      }

      return await mcpService.readUiResource(input.params, input.uri);
    }),

  // callTool now accepts MCPClientParams, toolName, and args
  callTool: mcpProcedure
    // Executes tools with side effects β€” workspace viewers are read-only.
    .use(requireWorkspaceRoleWhenScoped('member'))
    .input(
      z.object({
        args: z.any(), // Arguments for the tool call
        /** An MCP App view's `tools/call`: only tools visible to the app run. */
        fromApp: z.boolean().optional(),
        fromEntrypoint: z.boolean().optional(),
        agentId: z.string().optional(),
        meta: metaSchema, // Optional metadata for reporting
        params: mcpClientParamsSchema, // Use the unified schema for client params
        toolName: z.string(),
      }),
    )
    .mutation(async ({ input, ctx }) => {
      if (input.fromEntrypoint && input.agentId) {
        const agentModel = new AgentModel(ctx.serverDB, ctx.userId, ctx.workspaceId ?? undefined);
        const config = await agentModel.getAgentConfigById(input.agentId);
        if (
          !config ||
          getDisabledPluginIds(config.plugins ?? undefined).includes(input.params.name)
        )
          throw new TRPCError({
            code: 'FORBIDDEN',
            message: 'This app is unavailable for the agent',
          });
      }
      // ── Connector tool permission gate ────────────────────────────────────
      // If the connector identifier matches a user_connectors entry, check whether
      // this specific tool has been disabled by the user. Disabled tools are still
      // present in the AI manifest (so the AI knows they exist) but are hard-blocked
      // here at execution time so the MCP server is never actually called.
      //
      // The row is the one the conversation runs the identifier on, ranked as
      // its runs rank them: a plugin call keeps the bare identifier, and the
      // owner's private row shadows the shared one in their private agent's own
      // topic. Only an agent the caller can see names the run.
      const connectorId = input.params.name;
      if (connectorId && ctx.userId) {
        const wsId = ctx.workspaceId ?? undefined;
        const connectorModel = new ConnectorModel(ctx.serverDB, ctx.userId, wsId);
        const agentModel = new AgentModel(ctx.serverDB, ctx.userId, wsId);
        const agentId =
          input.agentId && (await agentModel.existsById(input.agentId)) ? input.agentId : undefined;
        const [connector] = await connectorModel.resolveByIdentifiers([connectorId], {
          agentId,
          topicId: agentId ? input.meta?.sessionId : undefined,
        });
        if (connector) {
          // A personal row's tools live in the personal scope
          const connectorToolModel = new ConnectorToolModel(ctx.serverDB, ctx.userId, wsId, {
            agentRun: true,
          });
          const tools = await connectorToolModel.queryByConnector(connector.id);
          const tool = tools.find((t) => t.toolName === input.toolName);
          if (tool?.permission === ConnectorToolPermission.disabled) {
            return {
              content:
                `The tool "${input.toolName}" has been disabled by the user and cannot be executed. ` +
                `Please inform the user that this tool is currently disabled. ` +
                `They can re-enable it in Settings > Connectors.`,
              state: {
                content: [
                  {
                    text:
                      `The tool "${input.toolName}" has been disabled by the user and cannot be executed. ` +
                      `Please inform the user that this tool is currently disabled. ` +
                      `They can re-enable it in Settings > Connectors.`,
                    type: 'text',
                  },
                ],
                isError: false,
              },
              success: true,
            };
          }
        }
      }
      // ── End permission gate ───────────────────────────────────────────────

      const startTime = Date.now();
      let success = true;
      let errorCode: string | undefined;
      let errorMessage: string | undefined;
      let result: Awaited<ReturnType<typeof mcpService.callTool>> | undefined;

      try {
        // Create a closure that binds fileService and userId to processContentBlocks
        const boundProcessContentBlocks = async (blocks: ToolCallContent[]) => {
          return processContentBlocks(blocks, ctx.fileService);
        };

        // Pass the validated params, toolName, args, and bound processContentBlocks to the service
        result = await mcpService.callTool({
          argsStr: input.args,
          clientParams: input.params,
          fromApp: input.fromApp,
          fromEntrypoint: input.fromEntrypoint,
          mcpApps: await isMcpAppsEnabled(ctx.userId),
          processContentBlocks: boundProcessContentBlocks,
          toolName: input.toolName,
        });

        return result;
      } catch (error) {
        success = false;
        const err = error as Error;
        errorCode = 'CALL_FAILED';
        errorMessage = err.message;
        throw error;
      } finally {
        scheduleToolCallReport({
          errorCode,
          errorMessage,
          identifier: input.params.name,
          marketAccessToken: ctx.marketAccessToken,
          mcpType: 'http',
          meta: input.meta,
          requestPayload: input.args,
          result,
          startTime,
          success,
          telemetryEnabled: ctx.telemetryEnabled,
          toolName: input.toolName,
        });
      }
    }),
});