Skip to content

Commit 62393f2

Browse files
committed
fix mcp server error code
1 parent b1f1881 commit 62393f2

2 files changed

Lines changed: 92 additions & 3 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* @vitest-environment node
3+
*/
4+
import type { NextRequest } from 'next/server'
5+
import { beforeEach, describe, expect, it, vi } from 'vitest'
6+
7+
const { mockPerformDeleteMcpServer } = vi.hoisted(() => ({
8+
mockPerformDeleteMcpServer: vi.fn(),
9+
}))
10+
11+
vi.mock('@sim/db', () => ({
12+
db: {
13+
select: vi.fn(),
14+
},
15+
}))
16+
17+
vi.mock('@/lib/mcp/middleware', () => ({
18+
getParsedBody: () => undefined,
19+
withMcpAuth:
20+
() =>
21+
(
22+
handler: (
23+
request: NextRequest,
24+
context: {
25+
userId: string
26+
userName: string
27+
userEmail: string
28+
workspaceId: string
29+
requestId: string
30+
}
31+
) => Promise<Response>
32+
) =>
33+
(request: NextRequest) =>
34+
handler(request, {
35+
userId: 'user-1',
36+
userName: 'Test User',
37+
userEmail: 'test@example.com',
38+
workspaceId: 'workspace-1',
39+
requestId: 'request-1',
40+
}),
41+
}))
42+
43+
vi.mock('@/lib/mcp/orchestration', () => ({
44+
performCreateMcpServer: vi.fn(),
45+
performDeleteMcpServer: mockPerformDeleteMcpServer,
46+
}))
47+
48+
import { DELETE } from '@/app/api/mcp/servers/route'
49+
50+
function createDeleteRequest(serverId = 'server-1') {
51+
return new Request(
52+
`http://localhost:3000/api/mcp/servers?workspaceId=workspace-1&serverId=${serverId}`,
53+
{ method: 'DELETE' }
54+
) as NextRequest
55+
}
56+
57+
describe('MCP servers DELETE route', () => {
58+
beforeEach(() => {
59+
vi.clearAllMocks()
60+
})
61+
62+
it('returns 404 when orchestration reports a missing server', async () => {
63+
mockPerformDeleteMcpServer.mockResolvedValueOnce({
64+
success: false,
65+
error: 'Server not found',
66+
errorCode: 'not_found',
67+
})
68+
69+
const response = await DELETE(createDeleteRequest())
70+
const body = await response.json()
71+
72+
expect(response.status).toBe(404)
73+
expect(body).toEqual({ success: false, error: 'Server not found' })
74+
})
75+
76+
it('returns 500 when orchestration reports an internal delete failure', async () => {
77+
mockPerformDeleteMcpServer.mockResolvedValueOnce({
78+
success: false,
79+
error: 'Failed to delete MCP server',
80+
errorCode: 'internal',
81+
})
82+
83+
const response = await DELETE(createDeleteRequest())
84+
const body = await response.json()
85+
86+
expect(response.status).toBe(500)
87+
expect(body).toEqual({ success: false, error: 'Failed to delete MCP server' })
88+
})
89+
})

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ export const DELETE = withRouteHandler(
165165
})
166166
if (!result.success || !result.server) {
167167
return createMcpErrorResponse(
168-
new Error('Server not found or access denied'),
169-
'Server not found',
170-
404
168+
new Error(result.error || 'Failed to delete MCP server'),
169+
result.error || 'Failed to delete MCP server',
170+
mcpOrchestrationStatus(result.errorCode)
171171
)
172172
}
173173

0 commit comments

Comments
 (0)