Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions src/snyk/cli/mcp/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
const cliPath = await configuration.getCliPath();
/* eslint-disable @typescript-eslint/no-unsafe-return */
const args = ['mcp', '-t', 'stdio'];
const snykEnv = await getSnykMcpEnv(configuration);
const snykEnv = getSnykMcpEnv(configuration);
const processEnv: Env = {};
Object.entries(process.env).forEach(([key, value]) => {
processEnv[key] = value ?? '';
Expand Down Expand Up @@ -105,7 +105,7 @@
Logger.debug(`Windsurf base directory not found at ${baseDir}, skipping MCP configuration.`);
} else {
const cliPath = await configuration.getCliPath();
const env = await getSnykMcpEnv(configuration);
const env = getSnykMcpEnv(configuration);
await ensureMcpServerInJson(configPath, SERVER_KEY, cliPath, ['mcp', '-t', 'stdio'], env);
Logger.debug(`Ensured Windsurf MCP config at ${configPath}`);
}
Expand Down Expand Up @@ -136,7 +136,7 @@
if (autoConfigureMcpServer) {
const configPath = path.join(os.homedir(), '.cursor', 'mcp.json');
const cliPath = await configuration.getCliPath();
const env = await getSnykMcpEnv(configuration);
const env = getSnykMcpEnv(configuration);

await ensureMcpServerInJson(configPath, SERVER_KEY, cliPath, ['mcp', '-t', 'stdio'], env);
Logger.debug(`Ensured Cursor MCP config at ${configPath}`);
Expand Down Expand Up @@ -198,13 +198,13 @@
const existing = config.mcpServers[keyToUse];
const desired: McpServer = { command, args, env };

// Merge env: keep existing keys; override Snyk keys only if already present
// Merge env: keep existing keys; add or override Snyk keys
let resultingEnv: Env;
if (existing && existing.env) {
resultingEnv = { ...existing.env };
const overrideKeys: (keyof Env)[] = ['SNYK_TOKEN', 'SNYK_CFG_ORG', 'SNYK_API'];
const overrideKeys: (keyof Env)[] = ['SNYK_CFG_ORG', 'SNYK_API', 'IDE_CONFIG_PATH', 'TRUSTED_FOLDERS'];
for (const k of overrideKeys) {
if (Object.hasOwn(existing.env, k) && typeof env[k] !== 'undefined') {
if (typeof env[k] !== 'undefined') {
resultingEnv[k] = env[k];
}
}
Expand Down Expand Up @@ -239,15 +239,15 @@
for (const folder of folders) {
const root = folder.uri.fsPath;
const rulesPath = path.join(root, relativeRulesPath);
await fs.promises.mkdir(path.dirname(rulesPath), { recursive: true });

Check warning on line 242 in src/snyk/cli/mcp/mcp.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

Unexpected `await` inside a loop

Check warning on line 242 in src/snyk/cli/mcp/mcp.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Unexpected `await` inside a loop

Check warning on line 242 in src/snyk/cli/mcp/mcp.ts

View workflow job for this annotation

GitHub Actions / Build and Test (windows-latest)

Unexpected `await` inside a loop
let existing = '';
try {
existing = await fs.promises.readFile(rulesPath, 'utf8');

Check warning on line 245 in src/snyk/cli/mcp/mcp.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

Unexpected `await` inside a loop

Check warning on line 245 in src/snyk/cli/mcp/mcp.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Unexpected `await` inside a loop

Check warning on line 245 in src/snyk/cli/mcp/mcp.ts

View workflow job for this annotation

GitHub Actions / Build and Test (windows-latest)

Unexpected `await` inside a loop
} catch {
// ignore
}
if (existing !== rulesContent) {
await fs.promises.writeFile(rulesPath, rulesContent, 'utf8');

Check warning on line 250 in src/snyk/cli/mcp/mcp.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

Unexpected `await` inside a loop

Check warning on line 250 in src/snyk/cli/mcp/mcp.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Unexpected `await` inside a loop

Check warning on line 250 in src/snyk/cli/mcp/mcp.ts

View workflow job for this annotation

GitHub Actions / Build and Test (windows-latest)

Unexpected `await` inside a loop
Logger.debug(`Wrote local rules to ${rulesPath}`);
} else {
Logger.debug(`Local rules already up to date at ${rulesPath}.`);
Expand All @@ -265,7 +265,7 @@
const rulesPath = path.join(root, relativeRulesPath);
try {
if (fs.existsSync(rulesPath)) {
await fs.promises.unlink(rulesPath);

Check warning on line 268 in src/snyk/cli/mcp/mcp.ts

View workflow job for this annotation

GitHub Actions / Build and Test (ubuntu-latest)

Unexpected `await` inside a loop

Check warning on line 268 in src/snyk/cli/mcp/mcp.ts

View workflow job for this annotation

GitHub Actions / Build and Test (macos-latest)

Unexpected `await` inside a loop

Check warning on line 268 in src/snyk/cli/mcp/mcp.ts

View workflow job for this annotation

GitHub Actions / Build and Test (windows-latest)

Unexpected `await` inside a loop
Logger.debug(`Deleted local rules from ${rulesPath}`);
}
} catch (err) {
Expand Down Expand Up @@ -309,7 +309,7 @@
);
}

async function getSnykMcpEnv(configuration: IConfiguration): Promise<Env> {
function getSnykMcpEnv(configuration: IConfiguration): Env {
const env: Env = {};
if (configuration.organization) {
env.SNYK_CFG_ORG = configuration.organization;
Expand All @@ -321,11 +321,7 @@
if (trustedFolders.length > 0) {
env.TRUSTED_FOLDERS = trustedFolders.join(';');
}
const token = await configuration.getToken();
const authMethod = configuration.getAuthenticationMethod();
if ((authMethod === 'pat' || authMethod === 'token') && token) {
env.SNYK_TOKEN = token;
}
env.IDE_CONFIG_PATH = vscode.env.appName;

return env;
}