// lucisv-mcp in VS Code: register the bundled MCP server with VS Code's agent // mode, one server per workspace folder, and hand everybody else (Claude Code, // Cursor) a config snippet naming the same binary. Nothing here talks MCP — // VS Code spawns the server itself from the definitions returned below // (docs/features/mcp-packaging/README.md, docs/mcp/PLAN.md §5.2). const fs = require('fs'); const path = require('path'); const vscode = require('vscode'); /** The id `package.json` contributes under `mcpServerDefinitionProviders`. */ const PROVIDER_ID = 'lucisv.mcp'; /** The binary's bare name: what the ladder falls back to on `PATH`. */ const BINARY = 'lucisv-mcp'; /** The scopes a user writes `lucisv.mcp.path` in, most specific first — see `userServerPath` in extension.js. */ const USER_SCOPES = [ 'workspaceFolderLanguageValue', 'workspaceLanguageValue', 'globalLanguageValue', 'workspaceFolderValue', 'workspaceValue', 'globalValue', ]; /** The three hosts the copy-config command knows, each with the file its snippet goes in. */ const HOSTS = [ { label: 'Claude Code', file: '.mcp.json', key: 'mcpServers', stdioType: false }, { label: 'Cursor', file: '.cursor/mcp.json', key: 'mcpServers', stdioType: false }, { label: 'VS Code', file: '.vscode/mcp.json', key: 'servers', stdioType: true }, ]; /** `lucisv.mcp.path` if the user set it (blank counts as unset), else `undefined`. */ function userMcpPath() { const inspected = vscode.workspace.getConfiguration('lucisv').inspect('mcp.path'); if (!inspected) return undefined; for (const scope of USER_SCOPES) { const value = inspected[scope]; if (typeof value === 'string' && value.trim() !== '') return value; } return undefined; } /** The binary to spawn: the user's setting, else the one in this package, else `lucisv-mcp` on PATH. */ function resolveBinary(context, bundledBinary) { return userMcpPath() || (bundledBinary && bundledBinary(context, BINARY)) || BINARY; } /** Whether `lucisv.mcp.enabled` is on; anything but an explicit `false` is. */ function mcpEnabled() { return vscode.workspace.getConfiguration('lucisv').get('mcp.enabled') !== false; } /** The workspace folders' paths, in order. */ function folderPaths() { return (vscode.workspace.workspaceFolders || []).map((folder) => folder.uri.fsPath); } /** * One stdio definition per workspace folder, or none while the setting is off. * The constructor is positional (label, command, args, env, version) in * vscode.d.ts — an object argument would become the label — and `cwd` is a * plain property set afterwards. */ function serverDefinitions(context, bundledBinary, version) { if (!mcpEnabled()) return []; const command = resolveBinary(context, bundledBinary); const storage = (context.storageUri || context.globalStorageUri || {}).fsPath; return (vscode.workspace.workspaceFolders || []).map((folder) => { const args = ['--root', folder.uri.fsPath]; if (storage) args.push('--index-cache', storage); const definition = new vscode.McpStdioServerDefinition( `lucisv (${folder.name})`, command, args, {}, version, ); definition.cwd = folder.uri; return definition; }); } /** The JSON a host's config file wants for this server, pretty-printed. */ function configSnippet(host, command, roots) { const args = roots.flatMap((root) => ['--root', root]); const server = host.stdioType ? { type: 'stdio', command, args } : { command, args }; return JSON.stringify({ [host.key]: { lucisv: server } }, null, 2); } /** Ask which host, copy its snippet, and say which file it goes in. */ async function copyConfig(context, bundledBinary) { const picked = await vscode.window.showQuickPick( HOSTS.map((host) => ({ label: host.label, description: host.file, host })), { placeHolder: 'Copy the lucisv MCP server config for…' }, ); if (!picked) return; const command = resolveBinary(context, bundledBinary); await vscode.env.clipboard.writeText(configSnippet(picked.host, command, folderPaths())); const bundled = bundledBinary && bundledBinary(context, BINARY); const moving = command === bundled ? ' The binary path points into this extension and changes when it updates.' : ''; vscode.window.showInformationMessage( `lucisv: copied the ${picked.host.label} MCP config — paste it into ${picked.host.file}.${moving}`, ); } /** Whether `command` names a file: as given if it has a directory part, else somewhere on PATH. */ function findable(command) { if (command.includes('/') || command.includes('\\')) return fs.existsSync(command); const names = process.platform === 'win32' ? [command, `${command}.exe`] : [command]; return (process.env.PATH || '') .split(path.delimiter) .filter((dir) => dir !== '') .some((dir) => names.some((name) => fs.existsSync(path.join(dir, name)))); } /** * Called by VS Code just before it spawns a server. A binary that is not there * (the universal package with nothing on PATH, a stale `lucisv.mcp.path`) is * not started: one line in the output channel says why, instead of a spawn * error in VS Code's MCP log. */ function resolveDefinition(definition, log) { if (findable(definition.command)) return definition; log(`lucisv: cannot start the MCP server: "${definition.command}" not found — install lucisv-mcp on PATH or set lucisv.mcp.path.`); return undefined; } /** Whether this VS Code has the MCP provider API (1.101+). */ function hasMcpApi() { return Boolean( vscode.lm && typeof vscode.lm.registerMcpServerDefinitionProvider === 'function' && typeof vscode.McpStdioServerDefinition === 'function', ); } /** Register the provider (when this VS Code can host it) and the copy-config command. */ function activate(context, helpers = {}) { const { bundledBinary, outputChannel } = helpers; const log = (line) => outputChannel && outputChannel.appendLine(line); context.subscriptions.push( vscode.commands.registerCommand('lucisv.mcp.copyConfig', () => copyConfig(context, bundledBinary)), ); if (!hasMcpApi()) { log('lucisv: this VS Code has no MCP server API (needs 1.101+); use "lucisv: Copy MCP server config" for other agents.'); return; } if (!mcpEnabled()) log('lucisv: lucisv.mcp.enabled is false; the MCP server is not offered to agent mode.'); const { version } = require('./package.json'); const changed = new vscode.EventEmitter(); context.subscriptions.push( changed, vscode.lm.registerMcpServerDefinitionProvider(PROVIDER_ID, { onDidChangeMcpServerDefinitions: changed.event, provideMcpServerDefinitions: () => serverDefinitions(context, bundledBinary, version), resolveMcpServerDefinition: (definition) => resolveDefinition(definition, log), }), vscode.workspace.onDidChangeWorkspaceFolders(() => changed.fire()), vscode.workspace.onDidChangeConfiguration((event) => { if (event.affectsConfiguration('lucisv.mcp')) changed.fire(); }), ); } module.exports = { activate, resolveBinary, serverDefinitions, configSnippet, findable, HOSTS, PROVIDER_ID, };