Skip to content

Commit 463ef80

Browse files
committed
improvement(mcp): extract extractStringHeaders helper for DRY
1 parent 6a3e350 commit 463ef80

File tree

1 file changed

+15
-17
lines changed
  • apps/sim/app/workspace/[workspaceId]/w/components/sidebar/components/settings-modal/components/mcp

1 file changed

+15
-17
lines changed

apps/sim/app/workspace/[workspaceId]/w/components/sidebar/components/settings-modal/components/mcp/mcp.tsx

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,18 @@ export function MCP({ initialServerId }: MCPProps) {
657657
}
658658
}, [formData, testConnection, createServerMutation, workspaceId, headersToRecord, resetForm])
659659

660+
/**
661+
* Extracts string-only headers from an unknown value.
662+
*/
663+
const extractStringHeaders = useCallback((headers: unknown): Record<string, string> => {
664+
if (typeof headers !== 'object' || headers === null) return {}
665+
return Object.fromEntries(
666+
Object.entries(headers).filter(
667+
(entry): entry is [string, string] => typeof entry[1] === 'string'
668+
)
669+
)
670+
}, [])
671+
660672
/**
661673
* Parses MCP JSON config into form data.
662674
* Accepts both `{ mcpServers: { name: { url, headers } } }` and `{ url, headers }` formats.
@@ -681,14 +693,7 @@ export function MCP({ initialServerId }: MCPProps) {
681693
return {
682694
name,
683695
url: config.url,
684-
headers:
685-
typeof config.headers === 'object' && config.headers !== null
686-
? Object.fromEntries(
687-
Object.entries(config.headers).filter(
688-
(entry): entry is [string, string] => typeof entry[1] === 'string'
689-
)
690-
)
691-
: {},
696+
headers: extractStringHeaders(config.headers),
692697
}
693698
}
694699

@@ -697,14 +702,7 @@ export function MCP({ initialServerId }: MCPProps) {
697702
return {
698703
name: '',
699704
url: parsed.url,
700-
headers:
701-
typeof parsed.headers === 'object' && parsed.headers !== null
702-
? Object.fromEntries(
703-
Object.entries(parsed.headers).filter(
704-
(entry): entry is [string, string] => typeof entry[1] === 'string'
705-
)
706-
)
707-
: {},
705+
headers: extractStringHeaders(parsed.headers),
708706
}
709707
}
710708

@@ -715,7 +713,7 @@ export function MCP({ initialServerId }: MCPProps) {
715713
return null
716714
}
717715
},
718-
[]
716+
[extractStringHeaders]
719717
)
720718

721719
/**

0 commit comments

Comments
 (0)