Skip to content

Commit d69055b

Browse files
committed
better error messages, confirmed that permissions works correctly
1 parent 13976a2 commit d69055b

4 files changed

Lines changed: 21 additions & 7 deletions

File tree

apps/sim/app/api/mcp/servers/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { and, eq, isNull } from 'drizzle-orm'
22
import type { NextRequest } from 'next/server'
33
import { createLogger } from '@/lib/logs/console/logger'
4-
import { withMcpAuth } from '@/lib/mcp/middleware'
4+
import { getParsedBody, withMcpAuth } from '@/lib/mcp/middleware'
55
import { mcpService } from '@/lib/mcp/service'
66
import { validateMcpServerUrl } from '@/lib/mcp/url-validator'
77
import { createMcpErrorResponse, createMcpSuccessResponse } from '@/lib/mcp/utils'
@@ -46,7 +46,7 @@ export const GET = withMcpAuth('read')(
4646
export const POST = withMcpAuth('write')(
4747
async (request: NextRequest, { userId, workspaceId, requestId }) => {
4848
try {
49-
const body = await request.json()
49+
const body = getParsedBody(request) || (await request.json())
5050

5151
logger.info(`[${requestId}] Registering new MCP server:`, {
5252
name: body.name,

apps/sim/app/api/mcp/tools/discover/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { NextRequest } from 'next/server'
22
import { createLogger } from '@/lib/logs/console/logger'
3-
import { withMcpAuth } from '@/lib/mcp/middleware'
3+
import { getParsedBody, withMcpAuth } from '@/lib/mcp/middleware'
44
import { mcpService } from '@/lib/mcp/service'
55
import type { McpToolDiscoveryResponse } from '@/lib/mcp/types'
66
import { categorizeError, createMcpErrorResponse, createMcpSuccessResponse } from '@/lib/mcp/utils'
@@ -61,7 +61,7 @@ export const GET = withMcpAuth('read')(
6161
export const POST = withMcpAuth('read')(
6262
async (request: NextRequest, { userId, workspaceId, requestId }) => {
6363
try {
64-
const body = await request.json()
64+
const body = getParsedBody(request) || (await request.json())
6565
const { serverIds } = body
6666

6767
if (!Array.isArray(serverIds)) {

apps/sim/app/api/mcp/tools/execute/route.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,19 @@ export const POST = withMcpAuth('read')(
104104

105105
if (result.isError) {
106106
logger.warn(`[${requestId}] Tool execution returned error for ${toolName} on ${serverId}`)
107-
return createMcpErrorResponse(transformedResult, 'Tool execution failed', 400)
107+
return createMcpErrorResponse(
108+
transformedResult,
109+
transformedResult.error || 'Tool execution failed',
110+
400
111+
)
108112
}
109113
logger.info(`[${requestId}] Successfully executed tool ${toolName} on server ${serverId}`)
110114
return createMcpSuccessResponse(transformedResult)
111115
} catch (error) {
112116
logger.error(`[${requestId}] Error executing MCP tool:`, error)
113117

114118
const { message, status } = categorizeError(error)
115-
return createMcpErrorResponse(new Error(message), 'Tool execution failed', status)
119+
return createMcpErrorResponse(new Error(message), message, status)
116120
}
117121
}
118122
)

apps/sim/lib/mcp/service.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,27 @@ class McpService {
3737
if (!envMatches) return value
3838

3939
let resolvedValue = value
40+
const missingVars: string[] = []
41+
4042
for (const match of envMatches) {
4143
const envKey = match.slice(2, -2).trim()
4244
const envValue = envVars[envKey]
4345

4446
if (envValue === undefined) {
45-
logger.warn(`Environment variable "${envKey}" not found in MCP server config`)
47+
missingVars.push(envKey)
4648
continue
4749
}
4850

4951
resolvedValue = resolvedValue.replace(match, envValue)
5052
}
53+
54+
if (missingVars.length > 0) {
55+
throw new Error(
56+
`Missing required environment variable${missingVars.length > 1 ? 's' : ''}: ${missingVars.join(', ')}. ` +
57+
`Please set ${missingVars.length > 1 ? 'these variables' : 'this variable'} in your workspace or personal environment settings.`
58+
)
59+
}
60+
5161
return resolvedValue
5262
}
5363

0 commit comments

Comments
 (0)