diff --git a/apps/sim/executor/handlers/agent/agent-handler.ts b/apps/sim/executor/handlers/agent/agent-handler.ts index c2ad061ba8..8e85ed866a 100644 --- a/apps/sim/executor/handlers/agent/agent-handler.ts +++ b/apps/sim/executor/handlers/agent/agent-handler.ts @@ -1,6 +1,6 @@ -import { getEnv } from '@/lib/env' import { createLogger } from '@/lib/logs/console/logger' import { createMcpToolId } from '@/lib/mcp/utils' +import { getBaseUrl } from '@/lib/urls/utils' import { getAllBlocks } from '@/blocks' import type { BlockOutput } from '@/blocks/types' import { BlockType } from '@/executor/consts' @@ -261,8 +261,7 @@ export class AgentBlockHandler implements BlockHandler { } } - const appUrl = getEnv('NEXT_PUBLIC_APP_URL') - const url = new URL(`${appUrl}/api/mcp/tools/discover`) + const url = new URL('/api/mcp/tools/discover', getBaseUrl()) url.searchParams.set('serverId', serverId) if (context.workspaceId) { url.searchParams.set('workspaceId', context.workspaceId) @@ -316,7 +315,7 @@ export class AgentBlockHandler implements BlockHandler { } } - const execResponse = await fetch(`${appUrl}/api/mcp/tools/execute`, { + const execResponse = await fetch(`${getBaseUrl()}/api/mcp/tools/execute`, { method: 'POST', headers, body: JSON.stringify({ @@ -640,7 +639,7 @@ export class AgentBlockHandler implements BlockHandler { ) { logger.info('Using HTTP provider request (browser environment)') - const url = new URL('/api/providers', getEnv('NEXT_PUBLIC_APP_URL') || '') + const url = new URL('/api/providers', getBaseUrl()) const response = await fetch(url.toString(), { method: 'POST', headers: { 'Content-Type': 'application/json' }, diff --git a/apps/sim/executor/handlers/router/router-handler.ts b/apps/sim/executor/handlers/router/router-handler.ts index dc253a9154..58e27ba45d 100644 --- a/apps/sim/executor/handlers/router/router-handler.ts +++ b/apps/sim/executor/handlers/router/router-handler.ts @@ -1,5 +1,5 @@ -import { env } from '@/lib/env' import { createLogger } from '@/lib/logs/console/logger' +import { getBaseUrl } from '@/lib/urls/utils' import { generateRouterPrompt } from '@/blocks/blocks/router' import type { BlockOutput } from '@/blocks/types' import { BlockType } from '@/executor/consts' @@ -40,8 +40,7 @@ export class RouterBlockHandler implements BlockHandler { const providerId = getProviderFromModel(routerConfig.model) try { - const baseUrl = env.NEXT_PUBLIC_APP_URL || '' - const url = new URL('/api/providers', baseUrl) + const url = new URL('/api/providers', getBaseUrl()) // Create the provider request with proper message formatting const messages = [{ role: 'user', content: routerConfig.prompt }] diff --git a/apps/sim/lib/urls/utils.ts b/apps/sim/lib/urls/utils.ts index 217426dd2a..3b572a079a 100644 --- a/apps/sim/lib/urls/utils.ts +++ b/apps/sim/lib/urls/utils.ts @@ -6,7 +6,7 @@ import { isProd } from '@/lib/environment' * @returns The base URL string (e.g., 'http://localhost:3000' or 'https://example.com') */ export function getBaseUrl(): string { - if (typeof window !== 'undefined') { + if (typeof window !== 'undefined' && window.location?.origin) { return window.location.origin }