Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 0 additions & 185 deletions apps/mobile/src/features/mcp/api.ts

This file was deleted.

49 changes: 26 additions & 23 deletions apps/mobile/src/features/mcp/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import {
authorizeMcpInstallation,
getMcpInstallationTools,
getMcpRecommendedServers,
getMcpServerInstallations,
installCustomMcpServer,
installMcpTemplate,
refreshMcpInstallationTools,
uninstallMcpServer,
updateMcpServerInstallation,
updateMcpToolApproval,
} from "./api";
import { getPostHogApiClient } from "@/lib/posthogApiClient";
import type {
InstallCustomMcpServerOptions,
InstallMcpTemplateOptions,
Expand All @@ -29,23 +18,24 @@ const mcpKeys = {
export function useMcpMarketplace() {
return useQuery({
queryKey: mcpKeys.marketplace(),
queryFn: getMcpRecommendedServers,
queryFn: () => getPostHogApiClient().getMcpServers(),
staleTime: 5 * 60 * 1000,
});
}

export function useMcpInstallations() {
return useQuery({
queryKey: mcpKeys.installations(),
queryFn: getMcpServerInstallations,
queryFn: () => getPostHogApiClient().getMcpServerInstallations(),
staleTime: 30 * 1000,
});
}

export function useMcpInstallationTools(installationId: string | null) {
return useQuery({
queryKey: mcpKeys.tools(installationId ?? ""),
queryFn: () => getMcpInstallationTools(installationId as string),
queryFn: () =>
getPostHogApiClient().getMcpInstallationTools(installationId as string),
enabled: !!installationId,
staleTime: 30 * 1000,
});
Expand All @@ -61,7 +51,7 @@ export function useInstallCustomMcpServer() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (options: InstallCustomMcpServerOptions) =>
installCustomMcpServer(options),
getPostHogApiClient().installCustomMcpServer(options),
onSuccess: () => invalidateInstallations(queryClient),
});
}
Expand All @@ -70,7 +60,7 @@ export function useInstallMcpTemplate() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (options: InstallMcpTemplateOptions) =>
installMcpTemplate(options),
getPostHogApiClient().installMcpTemplate(options),
onSuccess: () => invalidateInstallations(queryClient),
});
}
Expand All @@ -84,31 +74,39 @@ export function useUpdateMcpServerInstallation() {
}: {
installationId: string;
updates: UpdateMcpServerInstallationOptions;
}) => updateMcpServerInstallation(installationId, updates),
}) =>
getPostHogApiClient().updateMcpServerInstallation(
installationId,
updates,
),
onSuccess: () => invalidateInstallations(queryClient),
});
}

export function useUninstallMcpServer() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (installationId: string) => uninstallMcpServer(installationId),
mutationFn: (installationId: string) =>
getPostHogApiClient().uninstallMcpServer(installationId),
onSuccess: () => invalidateInstallations(queryClient),
});
}

export function useAuthorizeMcpInstallation() {
return useMutation({
mutationFn: (args: Parameters<typeof authorizeMcpInstallation>[0]) =>
authorizeMcpInstallation(args),
mutationFn: (args: {
installation_id: string;
install_source?: "posthog" | "posthog-code" | "posthog-mobile";
posthog_code_callback_url?: string;
}) => getPostHogApiClient().authorizeMcpInstallation(args),
});
}

export function useRefreshMcpInstallationTools() {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (installationId: string) =>
refreshMcpInstallationTools(installationId),
getPostHogApiClient().refreshMcpInstallationTools(installationId),
onSuccess: (_, installationId) => {
queryClient.invalidateQueries({
queryKey: mcpKeys.tools(installationId),
Expand All @@ -129,7 +127,12 @@ export function useUpdateMcpToolApproval() {
installationId: string;
toolName: string;
approval_state: McpApprovalState;
}) => updateMcpToolApproval(installationId, toolName, approval_state),
}) =>
getPostHogApiClient().updateMcpToolApproval(
installationId,
toolName,
approval_state,
),
onSuccess: (_, { installationId }) => {
queryClient.invalidateQueries({
queryKey: mcpKeys.tools(installationId),
Expand Down
40 changes: 20 additions & 20 deletions apps/mobile/src/features/mcp/oauth.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import * as Linking from "expo-linking";
import * as WebBrowser from "expo-web-browser";
import {
authorizeMcpInstallation,
installCustomMcpServer,
installMcpTemplate,
} from "./api";
import { getPostHogApiClient } from "@/lib/posthogApiClient";
import type {
InstallCustomMcpServerOptions,
InstallMcpTemplateOptions,
Expand Down Expand Up @@ -51,11 +47,12 @@ export async function installTemplateWithOAuth(
"install_source" | "posthog_code_callback_url"
>,
): Promise<McpServerInstallation | "cancelled"> {
const response: McpInstallResponse = await installMcpTemplate({
...options,
install_source: INSTALL_SOURCE,
posthog_code_callback_url: OAUTH_CALLBACK_URL,
});
const response: McpInstallResponse =
await getPostHogApiClient().installMcpTemplate({
...options,
install_source: INSTALL_SOURCE,
posthog_code_callback_url: OAUTH_CALLBACK_URL,
});

if (!isOAuthRedirect(response)) return response;

Expand All @@ -75,11 +72,12 @@ export async function installCustomWithOAuth(
"install_source" | "posthog_code_callback_url"
>,
): Promise<McpServerInstallation | "cancelled"> {
const response: McpInstallResponse = await installCustomMcpServer({
...options,
install_source: INSTALL_SOURCE,
posthog_code_callback_url: OAUTH_CALLBACK_URL,
});
const response: McpInstallResponse =
await getPostHogApiClient().installCustomMcpServer({
...options,
install_source: INSTALL_SOURCE,
posthog_code_callback_url: OAUTH_CALLBACK_URL,
});

if (!isOAuthRedirect(response)) return response;
const outcome = await waitForOAuthCallback(response.redirect_url);
Expand All @@ -94,11 +92,13 @@ export async function installCustomWithOAuth(
export async function reauthorizeInstallation(
installationId: string,
): Promise<"completed" | "cancelled"> {
const { redirect_url } = await authorizeMcpInstallation({
installation_id: installationId,
install_source: INSTALL_SOURCE,
posthog_code_callback_url: OAUTH_CALLBACK_URL,
});
const { redirect_url } = await getPostHogApiClient().authorizeMcpInstallation(
{
installation_id: installationId,
install_source: INSTALL_SOURCE,
posthog_code_callback_url: OAUTH_CALLBACK_URL,
},
);
return waitForOAuthCallback(redirect_url);
}

Expand Down
Loading
Loading