feat: support settings

This commit is contained in:
Henry Li
2026-01-20 23:43:21 +08:00
parent 7ead7c93f8
commit 1b70e00642
25 changed files with 1355 additions and 217 deletions

View File

@@ -0,0 +1,24 @@
import { env } from "@/env";
import type { MCPConfig } from "./types";
export async function loadMCPConfig() {
const response = await fetch(
`${env.NEXT_PUBLIC_BACKEND_BASE_URL}/api/mcp/config`,
);
return response.json() as Promise<MCPConfig>;
}
export async function updateMCPConfig(config: MCPConfig) {
const response = await fetch(
`${env.NEXT_PUBLIC_BACKEND_BASE_URL}/api/mcp/config`,
{
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(config),
},
);
return response.json();
}

View File

@@ -0,0 +1,44 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { loadMCPConfig, updateMCPConfig } from "./api";
export function useMCPConfig() {
const { data, isLoading, error } = useQuery({
queryKey: ["mcpConfig"],
queryFn: () => loadMCPConfig(),
});
return { config: data, isLoading, error };
}
export function useEnableMCPServer() {
const queryClient = useQueryClient();
const { config } = useMCPConfig();
return useMutation({
mutationFn: async ({
serverName,
enabled,
}: {
serverName: string;
enabled: boolean;
}) => {
if (!config) {
throw new Error("MCP config not found");
}
if (!config.mcp_servers[serverName]) {
throw new Error(`MCP server ${serverName} not found`);
}
await updateMCPConfig({
mcp_servers: {
...config.mcp_servers,
[serverName]: {
...config.mcp_servers[serverName],
enabled,
},
},
});
},
onSuccess: () => {
void queryClient.invalidateQueries({ queryKey: ["mcpConfig"] });
},
});
}

View File

@@ -0,0 +1,2 @@
export * from "./api";
export * from "./types";

View File

@@ -0,0 +1,8 @@
export interface MCPServerConfig extends Record<string, unknown> {
enabled: boolean;
description: string;
}
export interface MCPConfig {
mcp_servers: Record<string, MCPServerConfig>;
}