Files
lucioandClaude Fable 5.1 b757e82161
ci / checks (push) Successful in 1m50s
ci / corpus (push) Successful in 52s
MCP M8: read-only mode — lucisv-mcp --read-only lists five read tools (1 797 bytes), never writes inside a root; full mode unchanged
The mode filters the registry; an edit tool name answers unknown-tool.
Extension setting lucisv.mcp.mode (full | read-only; any other value is
read-only and logged), label and copy-config key lucisv-ro, both smokes
in the release script. Tests: tests/read_only.rs, QA lane tests/qa_m8.rs.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
2026-09-29 01:10:10 -07:00

200 lines
8.0 KiB
JavaScript

// 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;
}
/**
* Whether the server runs `--read-only`. Only unset or exactly `"full"` is full
* mode: a value that is neither setting (`"readonly"`, `null`, a number) fails
* safe to read-only, and says so in one line through `log`.
*/
function readOnlyMode(log = () => {}) {
const mode = vscode.workspace.getConfiguration('lucisv').get('mcp.mode');
if (mode === undefined || mode === 'full') return false;
if (mode !== 'read-only') {
log(`lucisv: lucisv.mcp.mode is "${String(mode)}", not "full" or "read-only"; using read-only.`);
}
return true;
}
/** 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;
* in read-only mode each gets `--read-only` and says so in its label.
* 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, log) {
if (!mcpEnabled()) return [];
const command = resolveBinary(context, bundledBinary);
const storage = (context.storageUri || context.globalStorageUri || {}).fsPath;
const readOnly = readOnlyMode(log);
return (vscode.workspace.workspaceFolders || []).map((folder) => {
const args = ['--root', folder.uri.fsPath];
if (storage) args.push('--index-cache', storage);
if (readOnly) args.push('--read-only');
const definition = new vscode.McpStdioServerDefinition(
readOnly ? `lucisv read-only (${folder.name})` : `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. A
* read-only server is keyed `lucisv-ro`, so both snippets can be pasted side by side.
*/
function configSnippet(host, command, roots, readOnly = false) {
const args = roots.flatMap((root) => ['--root', root]);
if (readOnly) args.push('--read-only');
const server = host.stdioType ? { type: 'stdio', command, args } : { command, args };
return JSON.stringify({ [host.key]: { [readOnly ? 'lucisv-ro' : 'lucisv']: server } }, null, 2);
}
/** Ask which host, copy its snippet in the configured mode, and say which mode and file. */
async function copyConfig(context, bundledBinary, log) {
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);
const readOnly = readOnlyMode(log);
await vscode.env.clipboard.writeText(configSnippet(picked.host, command, folderPaths(), readOnly));
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 (${readOnly ? 'read-only' : 'full'} mode) — 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, log)),
);
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, log),
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,
};