Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
import { throwOnErr } from '@core/util/result';
import { Telemetry, type TraceContext } from '@macro-inc/observability';
import { queryClient } from '@queries/client';
import { cognitionApiServiceClient } from '@service-cognition/client';
import type {
AddServerRequest,
ServerResponse,
StartAuthRequest,
UpdateServerRequest,
} from '@service-cognition/generated/schemas';
import { useMutation, useQuery } from '@tanstack/solid-query';
const KEYS = {
all: ['mcpServers'] as const,
list: ['mcpServers', 'list'] as const,
};
/** Stable placeholder for `neverSuspend` consumers (see below). */
const NO_SERVERS: ServerResponse[] = [];
function fetchMcpServers() {
return throwOnErr(
async () => await cognitionApiServiceClient.listMcpServers()
);
}
export function mcpServersQueryOptions() {
return { queryKey: KEYS.list, queryFn: fetchMcpServers };
}
export function useMcpServersQuery(options?: {
/**
* Poll for connection changes. OAuth finishes in another tab, and if this
* one stayed visible no focus refetch fires — surfaces that must flip
* promptly (the setup connector cards) pass a short interval.
*/
refetchInterval?: number;
/**
* Serve an empty placeholder instead of suspending on first load. Only
* for polling surfaces with their own Suspense boundary (the setup
* connector cards), where a query that has never succeeded would
* re-suspend on every scheduled refetch and blank the page rhythmically.
* Everything else keeps suspending — with the placeholder they would
* flash a fake "nothing connected" success state on first load.
*/
neverSuspend?: boolean;
/** Defer fetching until the surface needs the list. */
enabled?: () => boolean;
}) {
return useQuery(() => ({
...mcpServersQueryOptions(),
enabled: options?.enabled?.() ?? true,
refetchOnMount: 'always' as const,
refetchOnWindowFocus: 'always' as const,
refetchInterval: options?.refetchInterval,
placeholderData: options?.neverSuspend ? NO_SERVERS : undefined,
}));
}
function invalidateMcpServers() {
return queryClient.invalidateQueries({ queryKey: KEYS.list });
}
function upsertServer(server: ServerResponse) {
queryClient.setQueryData(
KEYS.list,
(current: ServerResponse[] | undefined) => {
if (!current) return [server];
const index = current.findIndex((s) => s.url === server.url);
if (index === -1) return [...current, server];
const next = [...current];
next[index] = server;
return next;
}
);
}
function removeServer(url: string) {
queryClient.setQueryData(
KEYS.list,
(current: ServerResponse[] | undefined) =>
current?.filter((s) => s.url !== url) ?? current
);
}
export function useAddMcpServerMutation() {
return useMutation(() => ({
mutationFn: async (request: AddServerRequest) =>
throwOnErr(
async () => await cognitionApiServiceClient.addMcpServer(request)
),
onSuccess: async (server: ServerResponse) => {
upsertServer(server);
await invalidateMcpServers();
},
}));
}
export function useUpdateMcpServerMutation() {
return useMutation(() => ({
mutationFn: async (request: UpdateServerRequest) =>
throwOnErr(
async () => await cognitionApiServiceClient.updateMcpServer(request)
),
onSuccess: async (server: ServerResponse) => {
upsertServer(server);
await invalidateMcpServers();
},
}));
}
export function useDeleteMcpServerMutation() {
return useMutation(() => ({
mutationFn: async (args: { url: string }) =>
throwOnErr(
async () => await cognitionApiServiceClient.deleteMcpServer(args)
),
onSuccess: async (_result: unknown, variables: { url: string }) => {
removeServer(variables.url);
await invalidateMcpServers();
},
}));
}
export function useStartMcpAuthMutation() {
return useMutation(() => ({
mutationFn: async ({
parentSpan,
...request
}: StartAuthRequest & { parentSpan?: TraceContext }) =>
throwOnErr(
async () =>
await (parentSpan ?? Telemetry.captureContext()).run(() =>
cognitionApiServiceClient.startMcpAuth(request)
)
),
}));
}