Skip to content

Commit 9f37035

Browse files
waleedlatif1claude
andcommitted
fix(mcp): include serverId in OAuth postMessage; honor stored secret in edit modal
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent 8714650 commit 9f37035

3 files changed

Lines changed: 24 additions & 6 deletions

File tree

apps/sim/app/api/mcp/oauth/callback/route.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ function escapeHtml(value: string): string {
3030
.replace(/'/g, '&#39;')
3131
}
3232

33-
function htmlClose(message: string, ok: boolean): NextResponse {
33+
function htmlClose(message: string, ok: boolean, serverId?: string): NextResponse {
3434
const safeMessage = escapeHtml(message)
3535
const title = ok ? 'Connected' : 'Connection failed'
36+
const serverIdLiteral = serverId ? JSON.stringify(serverId) : 'undefined'
3637
const body = `<!doctype html><html><head><meta charset="utf-8"><title>${title}</title></head><body style="font-family: system-ui; padding: 24px"><p>${safeMessage}</p><script>
37-
try { window.opener && window.opener.postMessage({ type: 'mcp-oauth', ok: ${ok ? 'true' : 'false'} }, window.location.origin) } catch (e) {}
38+
try { window.opener && window.opener.postMessage({ type: 'mcp-oauth', ok: ${ok ? 'true' : 'false'}, serverId: ${serverIdLiteral} }, window.location.origin) } catch (e) {}
3839
setTimeout(function () { window.close() }, 800)
3940
</script></body></html>`
4041
return new NextResponse(body, {
@@ -93,7 +94,7 @@ export const GET = withRouteHandler(async (request: NextRequest) => {
9394
await clearVerifier(row.id)
9495

9596
if (result !== 'AUTHORIZED') {
96-
return htmlClose('Authorization did not complete.', false)
97+
return htmlClose('Authorization did not complete.', false, server.id)
9798
}
9899

99100
try {
@@ -103,7 +104,7 @@ export const GET = withRouteHandler(async (request: NextRequest) => {
103104
logger.warn('Post-auth tools refresh failed', toError(e).message)
104105
}
105106

106-
return htmlClose('Connected. You can close this window.', true)
107+
return htmlClose('Connected. You can close this window.', true, server.id)
107108
} catch (error) {
108109
logger.error('MCP OAuth callback failed', error)
109110
return htmlClose('Authorization failed. Please try again.', false)

apps/sim/app/workspace/[workspaceId]/settings/components/mcp/components/mcp-server-form-modal/mcp-server-form-modal.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ interface McpServerFormData {
3939
headers?: HeaderEntry[]
4040
oauthClientId?: string
4141
oauthClientSecret?: string
42+
hasOauthClientSecret?: boolean
4243
}
4344

4445
export interface McpServerFormConfig {
@@ -346,7 +347,7 @@ export function McpServerFormModal({
346347
setActiveHeaderIndex(null)
347348
setUrlScrollLeft(0)
348349
setHeaderScrollLeft({})
349-
setShowAdvanced(!!(data.oauthClientId || data.oauthClientSecret))
350+
setShowAdvanced(!!(data.oauthClientId || data.oauthClientSecret || data.hasOauthClientSecret))
350351
setOauthClientSecretTouched(false)
351352
}
352353
if (open !== prevOpen) {

apps/sim/app/workspace/[workspaceId]/settings/components/mcp/mcp.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ export function MCP({ initialServerId }: MCPProps) {
171171
timeout?: number
172172
headers?: { key: string; value: string }[]
173173
oauthClientId?: string
174+
hasOauthClientSecret?: boolean
174175
}
175176
| undefined
176177
>(undefined)
@@ -205,8 +206,22 @@ export function MCP({ initialServerId }: MCPProps) {
205206
useEffect(() => {
206207
function onMessage(event: MessageEvent) {
207208
if (event.origin !== window.location.origin) return
208-
const data = event.data as { type?: string; ok?: boolean } | null
209+
const data = event.data as { type?: string; ok?: boolean; serverId?: string } | null
209210
if (data?.type !== 'mcp-oauth') return
211+
if (data.serverId) {
212+
const serverId = data.serverId
213+
const interval = oauthPopupIntervalsRef.current.get(serverId)
214+
if (interval !== undefined) {
215+
window.clearInterval(interval)
216+
oauthPopupIntervalsRef.current.delete(serverId)
217+
}
218+
setConnectingOauthServers((prev) => {
219+
if (!prev.has(serverId)) return prev
220+
const next = new Set(prev)
221+
next.delete(serverId)
222+
return next
223+
})
224+
}
210225
if (data.ok) {
211226
queryClient.invalidateQueries({ queryKey: mcpKeys.serversList(workspaceId) })
212227
queryClient.invalidateQueries({ queryKey: mcpKeys.toolsList(workspaceId) })
@@ -393,6 +408,7 @@ export function MCP({ initialServerId }: MCPProps) {
393408
timeout: 30000,
394409
headers,
395410
oauthClientId: server.oauthClientId || undefined,
411+
hasOauthClientSecret: server.hasOauthClientSecret === true,
396412
})
397413
setShowEditModal(true)
398414
}

0 commit comments

Comments
 (0)