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.

πŸ› fix(connector): restore credentials in edit mode, prevent silent wipe on save (#15909)

LiJianbbbe1a96d728
diff --git a/apps/server/src/routers/lambda/connector.ts b/apps/server/src/routers/lambda/connector.ts
index 43e44197a0d..fd963038e11 100644
--- a/apps/server/src/routers/lambda/connector.ts
+++ b/apps/server/src/routers/lambda/connector.ts
@@ -115,6 +115,33 @@ export const connectorRouter = router({
return toolsByConnector;
}),
+ /**
+ * Return the connector record with decrypted user-set credentials so the
+ * edit form can pre-fill accurately. Only the connector owner can call this
+ * (enforced by connectorProcedure ownership check).
+ *
+ * Machine-managed secrets are intentionally excluded:
+ * - OAuth access/refresh tokens (type 'oauth2') β†’ stripped, returned as null
+ * - oidcConfig.clientSecret (DCR-registered secret) β†’ stripped
+ * User-set credentials (bearer token, custom headers) are returned as-is so
+ * the edit form can display them.
+ */
+ getForEdit: connectorProcedure
+ .input(z.object({ id: z.string().uuid() }))
+ .query(async ({ input, ctx }) => {
+ const connector = await ctx.connectorModel.findById(input.id);
+ if (!connector)
+ throw new TRPCError({ code: 'NOT_FOUND', message: 'Connector not found' });
+
+ const { oidcConfig, credentials, ...rest } = connector;
+ const safeOidcConfig = oidcConfig ? { ...oidcConfig, clientSecret: undefined } : oidcConfig;
+ // OAuth tokens are machine-managed β€” don't return them; the UI only needs
+ // to know an OAuth flow is configured (reflected via oidcConfig presence).
+ const safeCredentials = credentials?.type === 'oauth2' ? null : credentials;
+
+ return { ...rest, credentials: safeCredentials, oidcConfig: safeOidcConfig };
+ }),
+
/**
* The exact redirect URI the server will send to the OAuth/DCR endpoints.
* The Add modal must display THIS value (not a client-derived origin) so the
diff --git a/src/features/Connectors/CustomConnectorModal/index.tsx b/src/features/Connectors/CustomConnectorModal/index.tsx
index 6a9f55a13a2..7aa4e8caf70 100644
--- a/src/features/Connectors/CustomConnectorModal/index.tsx
+++ b/src/features/Connectors/CustomConnectorModal/index.tsx
@@ -1,6 +1,7 @@
import { type LobeToolCustomPlugin } from '@lobechat/types';
-import { memo, useMemo } from 'react';
+import { memo, useEffect, useMemo, useRef, useState } from 'react';
+import type { ConnectorCredentials, OIDCConfig } from '@/database/schemas';
import { ConnectorSourceType } from '@/database/schemas';
import DevModal from '@/features/PluginDevModal';
import { useToolStore } from '@/store/tool';
@@ -84,6 +85,7 @@ const CustomConnectorModal = memo<CustomConnectorModalProps>(
({ open, onClose, connectorId, onEditSuccess }) => {
const createConnector = useToolStore((s) => s.createConnector);
const updateConnector = useToolStore((s) => s.updateConnector);
+ const getConnectorForEdit = useToolStore((s) => s.getConnectorForEdit);
const startConnectorOAuth = useToolStore((s) => s.startConnectorOAuth);
const syncConnectorTools = useToolStore((s) => s.syncConnectorTools);
const fetchConnectors = useToolStore((s) => s.fetchConnectors);
@@ -94,21 +96,50 @@ const CustomConnectorModal = memo<CustomConnectorModalProps>(
const isEditMode = Boolean(connectorId);
- // Build the pre-fill value for edit mode from the stored connector.
+ // Full connector data (with decrypted credentials) fetched for the edit form.
+ // null = not yet loaded; object = loaded (credentials may still be null if none set).
+ type EditFetchedData = {
+ credentials: Exclude<ConnectorCredentials, { type: 'oauth2' }> | null;
+ oidcConfig: Omit<OIDCConfig, 'clientSecret'> | null | undefined;
+ };
+ const [editFetchedData, setEditFetchedData] = useState<EditFetchedData | null>(null);
+ const editFetchController = useRef<AbortController | null>(null);
+
+ useEffect(() => {
+ if (!open || !connectorId) {
+ setEditFetchedData(null);
+ return;
+ }
+ // Cancel any in-flight fetch from a previous open
+ editFetchController.current?.abort();
+ const controller = new AbortController();
+ editFetchController.current = controller;
+
+ setEditFetchedData(null);
+ getConnectorForEdit(connectorId).then((data) => {
+ if (controller.signal.aborted) return;
+ setEditFetchedData({
+ credentials: (data?.credentials ?? null) as EditFetchedData['credentials'],
+ oidcConfig: (data?.oidcConfig ?? null) as EditFetchedData['oidcConfig'],
+ });
+ });
+
+ return () => {
+ controller.abort();
+ };
+ }, [open, connectorId]);
+
+ // Build the pre-fill value for edit mode once the credentials fetch completes.
+ // Returns undefined while loading so DevModal defers seeding the form.
const editValue = useMemo((): LobeToolCustomPlugin | undefined => {
- if (!isEditMode || !connector) return undefined;
+ if (!isEditMode || !connector || editFetchedData === null) return undefined;
const c = connector as typeof connector & {
mcpStdioConfig?: { args?: string[]; command?: string; env?: Record<string, string> };
- oidcConfig?: { clientId?: string; clientSecret?: string; scheme?: string };
};
-
- const oidcConfig = c.oidcConfig;
const mcpStdioConfig = c.mcpStdioConfig;
- const credentials = (c as typeof c & {
- credentials?: { headers?: Record<string, string>; token?: string; type?: string };
- }).credentials;
+ const { credentials, oidcConfig } = editFetchedData;
const authType = oidcConfig
? 'oauth2'
@@ -125,12 +156,15 @@ const CustomConnectorModal = memo<CustomConnectorModalProps>(
args: mcpStdioConfig?.args,
auth: {
clientId: oidcConfig?.clientId,
- token: authType === 'bearer' ? credentials?.token : undefined,
+ token: authType === 'bearer' ? (credentials as { token: string })?.token : undefined,
type: authType === 'header' ? 'none' : authType,
},
command: mcpStdioConfig?.command,
env: mcpStdioConfig?.env,
- headers: authType === 'header' ? credentials?.headers : undefined,
+ headers:
+ authType === 'header'
+ ? (credentials as { headers: Record<string, string> })?.headers
+ : undefined,
type: (connector.mcpConnectionType ?? 'http') as 'http' | 'stdio',
url: connector.mcpServerUrl ?? undefined,
},
@@ -138,7 +172,7 @@ const CustomConnectorModal = memo<CustomConnectorModalProps>(
identifier: connector.identifier,
type: 'customPlugin' as const,
};
- }, [isEditMode, connector]);
+ }, [isEditMode, connector, editFetchedData]);
const handleSave = async (value: LobeToolCustomPlugin, ctx?: { oauthPopup?: Window | null }) => {
const mcp = (value.customParams?.mcp ?? {}) as {
@@ -168,11 +202,16 @@ const CustomConnectorModal = memo<CustomConnectorModalProps>(
patch.credentials = null;
} else if (authType === 'bearer' && mcp.auth?.token?.trim()) {
patch.credentials = { token: mcp.auth.token.trim(), type: 'bearer' as const };
- } else if (authType === 'header') {
+ } else if (authType !== 'oauth2') {
+ // Auth radio 'none' covers both "no auth" and "header auth" (headers live in
+ // the Advanced section, not the auth radio). Mirror the create-mode logic:
+ // any filled headers β†’ header credentials; empty β†’ clear credentials.
const headers = cleanRecord(mcp.headers);
- if (headers) patch.credentials = { headers, type: 'header' as const };
- } else if (authType === 'none') {
- patch.credentials = null;
+ if (headers) {
+ patch.credentials = { headers, type: 'header' as const };
+ } else {
+ patch.credentials = null;
+ }
}
if (authType === 'oauth2') {
diff --git a/src/features/PluginDevModal/index.tsx b/src/features/PluginDevModal/index.tsx
index 4de1fdcee23..9b612f17bc2 100644
--- a/src/features/PluginDevModal/index.tsx
+++ b/src/features/PluginDevModal/index.tsx
@@ -4,7 +4,7 @@ import { type LobeToolCustomPlugin } from '@lobechat/types';
import { Button, Drawer, Flexbox } from '@lobehub/ui';
import { App, Form, Popconfirm } from 'antd';
import { useResponsive } from 'antd-style';
-import { memo, useEffect, useState } from 'react';
+import { memo, useEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import MCPManifestForm from './MCPManifestForm';
@@ -45,9 +45,20 @@ const DevModal = memo<DevModalProps>(
const { mobile } = useResponsive();
const [form] = Form.useForm();
const authType = Form.useWatch(['customParams', 'mcp', 'auth', 'type'], form);
+
+ // Seed the form once per modal open, waiting for `value` to arrive (it may
+ // be undefined initially while edit-mode credentials are being fetched).
+ const seededRef = useRef(false);
useEffect(() => {
- form.setFieldsValue(value);
- }, []);
+ if (!open) {
+ seededRef.current = false;
+ return;
+ }
+ if (value !== undefined && !seededRef.current) {
+ form.setFieldsValue(value);
+ seededRef.current = true;
+ }
+ }, [open, value]);
const doSave = async (values: LobeToolCustomPlugin, ctx?: { oauthPopup?: Window | null }) => {
if (!onSave) {
diff --git a/src/store/tool/slices/connector/action.ts b/src/store/tool/slices/connector/action.ts
index ef37af6328e..2bd5d69b709 100644
--- a/src/store/tool/slices/connector/action.ts
+++ b/src/store/tool/slices/connector/action.ts
@@ -22,6 +22,15 @@ export class ConnectorActionImpl {
this.#set({ connectors: data as any, isConnectorsInit: true }, false, 'fetchConnectors');
};
+ /**
+ * Fetch the connector with its decrypted user-set credentials for the edit
+ * form. Does NOT update the store β€” caller uses the result directly.
+ * Machine-managed OAuth tokens are excluded server-side.
+ */
+ getConnectorForEdit = async (id: string) => {
+ return lambdaClient.connector.getForEdit.query({ id });
+ };
+
createConnector = async (
params: Parameters<typeof lambdaClient.connector.create.mutate>[0],
): Promise<string> => {
@@ -53,7 +62,10 @@ export class ConnectorActionImpl {
updateConnector = async (
id: string,
patch: {
- credentials?: null;
+ credentials?:
+ | { token: string; type: 'bearer' }
+ | { headers: Record<string, string>; type: 'header' }
+ | null;
isEnabled?: boolean;
mcpServerUrl?: string;
name?: string;