Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix(mcp): show one toast when the pre-save connection test fails (#18267)

🐾 smolpawse0a26af82f0b
diff --git a/__tests__/components/features/mcp-page/custom-server-editor.test.tsx b/__tests__/components/features/mcp-page/custom-server-editor.test.tsx
index 6dfe2e3086..007df86996 100644
--- a/__tests__/components/features/mcp-page/custom-server-editor.test.tsx
+++ b/__tests__/components/features/mcp-page/custom-server-editor.test.tsx
@@ -20,6 +20,8 @@ import { MOCK_DEFAULT_USER_SETTINGS } from "#/mocks/handlers";
import { ActiveBackendProvider } from "#/contexts/active-backend-context";
import { CustomServerEditor } from "#/components/features/mcp-page/custom-server-editor";
import { useSettings } from "#/hooks/query/use-settings";
+import { createAgentServerQueryClient } from "#/query-client-config";
+import * as toastHandlers from "#/utils/custom-toast-handlers";
import type { Settings } from "#/types/settings";
import type { MCPServerConfig } from "#/types/mcp-server";
@@ -130,14 +132,13 @@ function EditOAuthEditorOnceSettingsLoaded({
);
}
-function renderWith(ui: React.ReactNode) {
+function renderWith(
+ ui: React.ReactNode,
+ client = new QueryClient({ defaultOptions: { queries: { retry: false } } }),
+) {
return render(ui, {
wrapper: ({ children }) => (
- <QueryClientProvider
- client={
- new QueryClient({ defaultOptions: { queries: { retry: false } } })
- }
- >
+ <QueryClientProvider client={client}>
<ActiveBackendProvider>{children}</ActiveBackendProvider>
</QueryClientProvider>
),
@@ -190,6 +191,40 @@ describe("CustomServerEditor", () => {
});
});
+ it.each([
+ ["Save", "submit-button"],
+ ["Test connection", "mcp-test-connection"],
+ ])(
+ "shows one error toast when %s cannot test the connection",
+ async (_, button) => {
+ // Arrange: use the app's global mutation handler and the real editor hooks.
+ vi.spyOn(McpService, "testServer").mockRejectedValue(
+ new AxiosError("Agent Server unavailable"),
+ );
+ const errorToast = vi
+ .spyOn(toastHandlers, "displayErrorToast")
+ .mockImplementation(() => {});
+ const onClose = vi.fn();
+ renderWith(
+ <EditEditorOnceSettingsLoaded onClose={onClose} />,
+ createAgentServerQueryClient(),
+ );
+ await screen.findByTestId("mcp-custom-editor");
+
+ // Act
+ fireEvent.click(screen.getByTestId(button));
+
+ // Assert: the failure is reported once and no save or dismissal follows.
+ await waitFor(() => expect(errorToast).toHaveBeenCalled());
+ await waitFor(() => expect(screen.getByTestId(button)).toBeEnabled());
+ expect(errorToast.mock.calls).toEqual([["Agent Server unavailable"]]);
+ expect(SettingsService.createMcpServer).not.toHaveBeenCalled();
+ expect(SettingsService.patchMcpServer).not.toHaveBeenCalled();
+ expect(onClose).not.toHaveBeenCalled();
+ expect(screen.getByTestId("mcp-custom-editor")).toBeInTheDocument();
+ },
+ );
+
it("keeps the modal open and does not call onClose when the add mutation fails", async () => {
// Simulate a backend rejection β€” the editor should surface the
// failure as an `onError` toast and leave the modal open so the
diff --git a/src/components/features/mcp-page/custom-server-editor.tsx b/src/components/features/mcp-page/custom-server-editor.tsx
index b35df6cd7d..d840faa4ab 100644
--- a/src/components/features/mcp-page/custom-server-editor.tsx
+++ b/src/components/features/mcp-page/custom-server-editor.tsx
@@ -193,7 +193,6 @@ export function CustomServerEditor({
});
}
},
- onError: handleError,
});
};