Spaces:
Sleeping
Sleeping
File size: 5,520 Bytes
05c5ed5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | "use server";
import { mcpClientsManager } from "lib/ai/mcp/mcp-manager";
import { z } from "zod";
import { McpServerTable } from "lib/db/pg/schema.pg";
import { mcpOAuthRepository, mcpRepository } from "lib/db/repository";
import {
canCreateMCP,
canManageMCPServer,
canShareMCPServer,
getCurrentUser,
} from "lib/auth/permissions";
export async function selectMcpClientsAction() {
// Get current user to filter MCP servers
const currentUser = await getCurrentUser();
if (!currentUser) {
return [];
}
// Get all MCP servers the user can access (their own + shared)
const accessibleServers = await mcpRepository.selectAllForUser(
currentUser.id,
);
const accessibleIds = new Set(accessibleServers.map((s) => s.id));
// Get all active clients and filter to only accessible ones
const list = await mcpClientsManager.getClients();
return list
.filter(({ id }) => accessibleIds.has(id))
.map(({ client, id }) => {
const server = accessibleServers.find((s) => s.id === id);
return {
...client.getInfo(),
id,
userId: server?.userId,
visibility: server?.visibility,
isOwner: server?.userId === currentUser.id,
canManage: server
? server.userId === currentUser.id || currentUser.role === "admin"
: false,
};
});
}
export async function selectMcpClientAction(id: string) {
const client = await mcpClientsManager.getClient(id);
if (!client) {
throw new Error("Client not found");
}
return {
...client.client.getInfo(),
id,
};
}
export async function saveMcpClientAction(
server: typeof McpServerTable.$inferInsert,
) {
if (process.env.NOT_ALLOW_ADD_MCP_SERVERS) {
throw new Error("Not allowed to add MCP servers");
}
// Get current user
const currentUser = await getCurrentUser();
if (!currentUser) {
throw new Error("You must be logged in to create MCP connections");
}
// Check if user has permission to create/edit MCP connections
const hasPermission = await canCreateMCP();
if (!hasPermission) {
throw new Error("You don't have permission to create MCP connections");
}
// Validate name to ensure it only contains alphanumeric characters and hyphens
const nameSchema = z.string().regex(/^[a-zA-Z0-9\-]+$/, {
message:
"Name must contain only alphanumeric characters (A-Z, a-z, 0-9) and hyphens (-)",
});
const result = nameSchema.safeParse(server.name);
if (!result.success) {
throw new Error(
"Name must contain only alphanumeric characters (A-Z, a-z, 0-9) and hyphens (-)",
);
}
// Check for duplicate names if creating a featured server
if (server.visibility === "public") {
// Only admins can create featured MCP servers
const canShare = await canShareMCPServer();
if (!canShare) {
throw new Error("Only administrators can feature MCP servers");
}
// Check if a featured server with this name already exists
const existing = await mcpRepository.existsByServerName(server.name);
if (existing && !server.id) {
throw new Error("A featured MCP server with this name already exists");
}
}
// Add userId to the server object
const serverWithUser = {
...server,
userId: currentUser.id,
visibility: server.visibility || "private",
};
return mcpClientsManager.persistClient(serverWithUser);
}
export async function existMcpClientByServerNameAction(serverName: string) {
return await mcpRepository.existsByServerName(serverName);
}
export async function removeMcpClientAction(id: string) {
// Get the MCP server to check ownership
const mcpServer = await mcpRepository.selectById(id);
if (!mcpServer) {
throw new Error("MCP server not found");
}
// Check if user has permission to delete this specific MCP server
const canManage = await canManageMCPServer(
mcpServer.userId,
mcpServer.visibility,
);
if (!canManage) {
throw new Error("You don't have permission to delete this MCP connection");
}
await mcpClientsManager.removeClient(id);
}
export async function refreshMcpClientAction(id: string) {
await mcpClientsManager.refreshClient(id);
}
export async function authorizeMcpClientAction(id: string) {
await refreshMcpClientAction(id);
const client = await mcpClientsManager.getClient(id);
if (client?.client.status != "authorizing") {
throw new Error("Not Authorizing");
}
return client.client.getAuthorizationUrl()?.toString();
}
export async function checkTokenMcpClientAction(id: string) {
const session = await mcpOAuthRepository.getAuthenticatedSession(id);
// for wait connect to mcp server
await mcpClientsManager.getClient(id).catch(() => null);
return !!session?.tokens;
}
export async function callMcpToolAction(
id: string,
toolName: string,
input: unknown,
) {
return mcpClientsManager.toolCall(id, toolName, input);
}
export async function callMcpToolByServerNameAction(
serverName: string,
toolName: string,
input: unknown,
) {
return mcpClientsManager.toolCallByServerName(serverName, toolName, input);
}
export async function shareMcpServerAction(
id: string,
visibility: "public" | "private",
) {
// Only admins can feature MCP servers
const canShare = await canShareMCPServer();
if (!canShare) {
throw new Error("Only administrators can feature MCP servers");
}
// Update the visibility of the MCP server
await mcpRepository.updateVisibility(id, visibility);
return { success: true };
}
|