Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -339,12 +339,12 @@ export function McpToolPermissionsPage({ serverId }: McpToolPermissionsPageProps
}, [permissionFilter, searchInput, tools]);
const filteredToolGroups = useMemo<ToolGroup[]>(() => [
{
id: 'read-only',
id: 'read-only' as const,
label: 'Read-only tools',
tools: filteredTools.filter((tool) => tool.annotations?.readOnlyHint === true),
},
{
id: 'write-delete',
id: 'write-delete' as const,
label: 'Write/delete tools',
tools: filteredTools.filter((tool) => tool.annotations?.readOnlyHint !== true),
},
Expand Down
4 changes: 2 additions & 2 deletions packages/web/src/ee/features/chat/mcp/mcpToolSets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ vi.mock('@/lib/posthog', () => ({
}));

vi.mock('@/lib/redis', () => ({
redis: {
getRedisClient: () => ({
get: (...args: unknown[]) => mockRedisGet(...args),
set: (...args: unknown[]) => mockRedisSet(...args),
},
}),
}));

vi.mock('ai', () => ({
Expand Down
19 changes: 13 additions & 6 deletions packages/web/src/ee/features/chat/mcp/mcpToolSets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { __unsafePrisma } from '@/prisma';
import { McpServerToolPermission, Prisma } from '@sourcebot/db';
import { captureEvent } from '@/lib/posthog';
import type { AskMcpAnalyticsSource } from '@/lib/posthogEvents';
import { redis } from '@/lib/redis';
import { getRedisClient } from '@/lib/redis';
import {
createMissingMcpServerToolRows,
getMcpServerToolPermission,
Expand Down Expand Up @@ -129,6 +129,7 @@ function getMcpListToolsCacheKey(client: McpToolSet): string {

async function getCachedListTools(cacheKey: string): Promise<ListToolsResult | undefined> {
try {
const redis = getRedisClient();
const cached = await redis.get(cacheKey);
return cached ? JSON.parse(cached) as ListToolsResult : undefined;
} catch (error) {
Expand All @@ -142,6 +143,7 @@ async function getCachedListTools(cacheKey: string): Promise<ListToolsResult | u

async function setCachedListTools(cacheKey: string, toolDefinitions: ListToolsResult) {
try {
const redis = getRedisClient();
await redis.set(cacheKey, JSON.stringify(toolDefinitions), 'EX', MCP_LIST_TOOLS_CACHE_TTL_SECONDS);
} catch (error) {
logger.warn('Failed to cache MCP tool definitions.', {
Expand Down Expand Up @@ -200,10 +202,14 @@ export async function getMcpTools(clients: McpToolSet[], analyticsContext?: McpT
const prefix = `mcp_${sanitizedName}`;
await createMissingMcpServerToolRows({
serverId,
tools: toolDefinitions.tools.map((tool) => ({
toolName: tool.name,
readOnlyHint: tool.annotations?.readOnlyHint,
})),
tools: toolDefinitions.tools.map((tool) => {
const readOnlyHint = tool.annotations?.readOnlyHint;

return {
toolName: tool.name,
...(typeof readOnlyHint === 'boolean' ? { readOnlyHint } : {}),
};
}),
});
const permissionsByServerId = await getMcpServerToolPermissionsByServerId({
serverIds: [serverId],
Expand All @@ -212,10 +218,11 @@ export async function getMcpTools(clients: McpToolSet[], analyticsContext?: McpT

for (const [toolName, tool] of Object.entries(tools)) {
const def = toolDefinitions.tools.find(t => t.name === toolName);
const readOnlyHint = def?.annotations?.readOnlyHint;
const permission = getMcpServerToolPermission(
permissionsByToolName,
toolName,
def?.annotations?.readOnlyHint,
typeof readOnlyHint === 'boolean' ? readOnlyHint : undefined,
);
if (permission === McpServerToolPermission.DISABLED) {
continue;
Expand Down
7 changes: 6 additions & 1 deletion packages/web/src/lib/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ import 'server-only';

import { createRedisClient } from '@sourcebot/shared';

export const redis = createRedisClient();
let redis: ReturnType<typeof createRedisClient> | undefined;

export function getRedisClient() {
redis ??= createRedisClient();
return redis;
}
Loading