| 1 | import z from "zod"; |
| 2 | |
| 3 | // This is the schema for an entry in e.g. Claude Desktop, Claude code mcp config |
| 4 | const httpOrSseMcpJsonSchema = z.object({ |
| 5 | type: z.union([z.literal("sse"), z.literal("http")]).optional(), |
| 6 | url: z.string(), // .url() fails with e.g. IP addresses |
| 7 | headers: z.record(z.string(), z.string()).optional(), |
| 8 | }); |
| 9 | export type HttpMcpJsonConfig = z.infer<typeof httpOrSseMcpJsonSchema>; |
| 10 | export type SseMcpJsonConfig = z.infer<typeof httpOrSseMcpJsonSchema>; |
| 11 | |
| 12 | const stdioMcpJsonSchema = z.object({ |
| 13 | type: z.literal("stdio").optional(), |
| 14 | command: z.string(), |
| 15 | args: z.array(z.string()).optional(), |
| 16 | env: z.record(z.string(), z.string()).optional(), |
| 17 | envFile: z.string().optional(), |
| 18 | }); |
| 19 | export type StdioMcpJsonConfig = z.infer<typeof stdioMcpJsonSchema>; |
| 20 | |
| 21 | export const mcpServersJsonSchema = z.union([ |
| 22 | httpOrSseMcpJsonSchema, |
| 23 | stdioMcpJsonSchema, |
| 24 | ]); |
| 25 | export type McpJsonConfig = z.infer<typeof mcpServersJsonSchema>; |
| 26 | |
| 27 | export const mcpServersRecordSchema = z.record( |
| 28 | z.string(), |
| 29 | mcpServersJsonSchema, |
| 30 | ); |
| 31 | export type McpServersJsonConfigRecord = z.infer<typeof mcpServersRecordSchema>; |
| 32 | |
| 33 | export const claudeDesktopLikeConfigFileSchema = z.object({ |
| 34 | mcpServers: mcpServersRecordSchema, |
| 35 | }); |
| 36 | export type McpServersJsonConfigFile = z.infer< |
| 37 | typeof claudeDesktopLikeConfigFileSchema |
| 38 | >; |
| 39 | |
| 40 | export const claudeCodeLikeConfigFileSchema = z.object({ |
| 41 | mcpServers: mcpServersRecordSchema.optional(), |
| 42 | projects: z.record( |
| 43 | z.string(), |
| 44 | z.object({ |
| 45 | mcpServers: mcpServersRecordSchema.optional(), |
| 46 | }), |
| 47 | ), |
| 48 | }); |
| 49 | export type claudeCodeLikeConfigFileSchema = z.infer< |
| 50 | typeof claudeCodeLikeConfigFileSchema |
| 51 | >; |