|
| 1 | +import { defineAddon, defineAddonOptions } from '@sveltejs/cli-core'; |
| 2 | +import { parseJson } from '@sveltejs/cli-core/parsers'; |
| 3 | + |
| 4 | +const options = defineAddonOptions() |
| 5 | + .add('ide', { |
| 6 | + question: 'Which client would you like to use?', |
| 7 | + type: 'multiselect', |
| 8 | + default: [], |
| 9 | + options: [ |
| 10 | + { value: 'claude-code', label: 'claude code' }, |
| 11 | + { value: 'cursor', label: 'Cursor' }, |
| 12 | + { value: 'gemini', label: 'Gemini' }, |
| 13 | + { value: 'opencode', label: 'opencode' }, |
| 14 | + { value: 'vscode', label: 'VSCode' }, |
| 15 | + { value: 'other', label: 'Other' } |
| 16 | + ], |
| 17 | + required: true |
| 18 | + }) |
| 19 | + .add('setup', { |
| 20 | + question: 'What setup you want to use?', |
| 21 | + type: 'select', |
| 22 | + default: 'remote', |
| 23 | + options: [ |
| 24 | + { value: 'local', label: 'Local', hint: 'will use stdio' }, |
| 25 | + { value: 'remote', label: 'Remote', hint: 'will use a remote endpoint' } |
| 26 | + ], |
| 27 | + required: true |
| 28 | + }) |
| 29 | + .build(); |
| 30 | + |
| 31 | +export default defineAddon({ |
| 32 | + id: 'mcp', |
| 33 | + shortDescription: 'Svelte MCP', |
| 34 | + homepage: 'https://svelte.dev/docs/mcp', |
| 35 | + options, |
| 36 | + run: ({ sv, options }) => { |
| 37 | + const getLocalConfig = (o?: { type?: 'stdio' | 'local'; env?: boolean }) => { |
| 38 | + return { |
| 39 | + ...(o?.type ? { type: o.type } : {}), |
| 40 | + command: 'npx', |
| 41 | + args: ['-y', '@sveltejs/mcp'], |
| 42 | + ...(o?.env ? { env: {} } : {}) |
| 43 | + }; |
| 44 | + }; |
| 45 | + const getRemoteConfig = (o?: { type?: 'http' | 'remote' }) => { |
| 46 | + return { |
| 47 | + ...(o?.type ? { type: o.type } : {}), |
| 48 | + url: 'https://mcp.svelte.dev/mcp' |
| 49 | + }; |
| 50 | + }; |
| 51 | + |
| 52 | + const configurator: Record< |
| 53 | + (typeof options.ide)[number], |
| 54 | + | { |
| 55 | + schema?: string; |
| 56 | + mcpServersKey?: string; |
| 57 | + filePath: string; |
| 58 | + typeLocal?: 'stdio' | 'local'; |
| 59 | + typeRemote?: 'http' | 'remote'; |
| 60 | + env?: boolean; |
| 61 | + } |
| 62 | + | { other: true } |
| 63 | + > = { |
| 64 | + 'claude-code': { |
| 65 | + filePath: '.mcp.json', |
| 66 | + typeLocal: 'stdio', |
| 67 | + typeRemote: 'http', |
| 68 | + env: true |
| 69 | + }, |
| 70 | + cursor: { |
| 71 | + filePath: '.cursor/mcp.json' |
| 72 | + }, |
| 73 | + gemini: { |
| 74 | + filePath: '.gemini/settings.json' |
| 75 | + }, |
| 76 | + opencode: { |
| 77 | + schema: 'https://opencode.ai/config.json', |
| 78 | + mcpServersKey: 'mcp', |
| 79 | + filePath: 'opencode.json', |
| 80 | + typeLocal: 'local', |
| 81 | + typeRemote: 'remote' |
| 82 | + }, |
| 83 | + vscode: { |
| 84 | + mcpServersKey: 'servers', |
| 85 | + filePath: '.vscode/mcp.json' |
| 86 | + }, |
| 87 | + other: { |
| 88 | + other: true |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + for (const ide of options.ide) { |
| 93 | + const value = configurator[ide]; |
| 94 | + if ('other' in value) continue; |
| 95 | + |
| 96 | + const { mcpServersKey, filePath, typeLocal, typeRemote, env, schema } = value; |
| 97 | + sv.file(filePath, (content) => { |
| 98 | + const { data, generateCode } = parseJson(content); |
| 99 | + if (schema) { |
| 100 | + data['$schema'] = schema; |
| 101 | + } |
| 102 | + const key = mcpServersKey || 'mcpServers'; |
| 103 | + data[key] ??= {}; |
| 104 | + data[key].svelte = |
| 105 | + options.setup === 'local' |
| 106 | + ? getLocalConfig({ type: typeLocal, env }) |
| 107 | + : getRemoteConfig({ type: typeRemote }); |
| 108 | + return generateCode(); |
| 109 | + }); |
| 110 | + } |
| 111 | + }, |
| 112 | + nextSteps({ highlighter, options }) { |
| 113 | + const steps = []; |
| 114 | + |
| 115 | + if (options.ide.includes('other')) { |
| 116 | + steps.push( |
| 117 | + `For other clients: ${highlighter.website(`https://svelte.dev/docs/mcp/${options.setup}-setup#Other-clients`)}` |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + return steps; |
| 122 | + } |
| 123 | +}); |
0 commit comments