@codacy/codacy-mcp 0.5.3 → 0.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -7
- package/dist/src/handlers/cliAnalyze.js +77 -70
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,9 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
MCP Server for the Codacy API, enabling access to repositories, files, quality, coverage, security and more.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
## Tools
|
|
5
|
+
## Features / Tools
|
|
8
6
|
|
|
9
7
|
The following tools are available through the Codacy MCP Server:
|
|
10
8
|
|
|
@@ -65,10 +63,19 @@ The following tools are available through the Codacy MCP Server:
|
|
|
65
63
|
|
|
66
64
|
## Setup
|
|
67
65
|
|
|
66
|
+
### Requirements
|
|
67
|
+
|
|
68
|
+
Ensure your machine has the following tools installed:
|
|
69
|
+
* git
|
|
70
|
+
* node.js
|
|
71
|
+
* ensure that the `npx` command runs without issues.
|
|
72
|
+
|
|
68
73
|
### Personal API Access Token
|
|
69
74
|
|
|
70
75
|
Get your Codacy's Account API Token from your [Codacy Account](https://app.codacy.com/account/access-management).
|
|
71
76
|
|
|
77
|
+
You'll need it later in the setup.
|
|
78
|
+
|
|
72
79
|
### Usage
|
|
73
80
|
|
|
74
81
|
In supported IDEs like VS Code, Cursor, and Windsurf, the easiest way to install Codacy's MCP Server is to do it from the Codacy extension. If you haven't yet, install the extension from within your IDE, or from any of the available marketplaces ([Microsoft](https://marketplace.visualstudio.com/items?itemName=codacy-app.codacy), [OpenVSX](https://open-vsx.org/extension/codacy-app/codacy)). From the extension panel, just click on Add Codacy MCP Server. Restart your IDE afterwards.
|
|
@@ -99,7 +106,7 @@ Depending on what you are connecting the MCP Server to, you can use the followin
|
|
|
99
106
|
|
|
100
107
|
### VS Code with Copilot
|
|
101
108
|
|
|
102
|
-
For connecting the MCP Server to Copilot in VS Code, add the following to the global config of the IDE:
|
|
109
|
+
1. For connecting the MCP Server to Copilot in VS Code, add the following to the global config of the IDE:
|
|
103
110
|
|
|
104
111
|
```json
|
|
105
112
|
{
|
|
@@ -120,18 +127,28 @@ For connecting the MCP Server to Copilot in VS Code, add the following to the gl
|
|
|
120
127
|
}
|
|
121
128
|
}
|
|
122
129
|
```
|
|
130
|
+
You can open the user settings.json file in:
|
|
123
131
|
|
|
124
|
-
|
|
132
|
+
`View > Command Palette > Preferences: Open User Settings (JSON)`
|
|
133
|
+
|
|
134
|
+
Or open the general settings.json file directly, which according to your OS should be located in:
|
|
125
135
|
|
|
126
136
|
- for macOS: `~/Library/Application Support/Code/User/settings.json`
|
|
127
|
-
- for Windows: `%APPDATA%\Code\User\settings.json`
|
|
137
|
+
- for Windows: `%APPDATA%\Code\User\settings.json` or `%APPDATA%\Roaming\Code\User\settings.json`
|
|
128
138
|
- for Linux: `~/.config/Code/User/settings.json`
|
|
129
139
|
|
|
130
|
-
|
|
140
|
+
Don't forget to update the value of `CODACY_ACCOUNT_TOKEN` with your token.
|
|
141
|
+
|
|
142
|
+
2. Make sure you have Agent mode enabled: [vscode://settings/chat.agent.enabled](vscode://settings/chat.agent.enabled)
|
|
131
143
|
|
|
144
|
+
3. Open the Copilot chat and switch the mode to `Agent`. You can check that the MCP server was enabled correctly by clicking on the `Select tools` icon, which should list all the available Codacy tools.
|
|
145
|
+
|
|
146
|
+

|
|
132
147
|
|
|
133
148
|
### Node Troubleshooting
|
|
134
149
|
|
|
150
|
+
#### Claude Desktop
|
|
151
|
+
|
|
135
152
|
When using NVM with Claude Desktop, NPX may not work. You should first install the MCP Server globally, and then use Node directly:
|
|
136
153
|
|
|
137
154
|
```bash
|
|
@@ -1,84 +1,98 @@
|
|
|
1
|
-
import util from 'util';
|
|
2
|
-
import { exec as execChildProcess } from 'child_process';
|
|
3
1
|
import fs from 'fs';
|
|
4
2
|
import path from 'path';
|
|
5
|
-
|
|
3
|
+
import { exec } from 'child_process';
|
|
6
4
|
const CODACY_ACCOUNT_TOKEN = process.env.CODACY_ACCOUNT_TOKEN;
|
|
7
|
-
const
|
|
5
|
+
const CLI_FILE_NAME = 'cli.sh';
|
|
6
|
+
const CLI_FOLDER_NAME = '.codacy';
|
|
7
|
+
const CLI_LOCAL_COMMAND = `${CLI_FOLDER_NAME}/${CLI_FILE_NAME}`;
|
|
8
|
+
const CLI_GLOBAL_COMMAND = 'codacy-cli';
|
|
9
|
+
// Set a larger buffer size (10MB)
|
|
10
|
+
const MAX_BUFFER_SIZE = 1024 * 1024 * 10;
|
|
11
|
+
const execAsync = (command, options) => {
|
|
12
|
+
return new Promise((resolve, reject) => {
|
|
13
|
+
exec(`CODACY_CLI_V2_VERSION=1.0.0-main.232.a6a6368 ${command}`, {
|
|
14
|
+
cwd: options.rootPath,
|
|
15
|
+
maxBuffer: MAX_BUFFER_SIZE, // To solve: stdout maxBuffer exceeded
|
|
16
|
+
}, (error, stdout, stderr) => {
|
|
17
|
+
if (error) {
|
|
18
|
+
reject(error);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
if (stderr && (!stdout || /error|fail|exception/i.test(stderr))) {
|
|
22
|
+
reject(new Error(stderr));
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
resolve({ stdout, stderr });
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
};
|
|
8
29
|
// Safeguard: Validate and sanitize command inputs
|
|
9
30
|
const sanitizeCommand = (command) => {
|
|
10
31
|
// Remove any shell metacharacters
|
|
11
32
|
return command?.replace(/[;&|`$]/g, '') || '';
|
|
12
33
|
};
|
|
13
|
-
// Check if codacy-cli is installed and install if needed
|
|
14
|
-
const ensureCodacyCLI = async () => {
|
|
15
|
-
try {
|
|
16
|
-
await exec('which codacy-cli');
|
|
17
|
-
return true;
|
|
18
|
-
}
|
|
19
|
-
catch {
|
|
20
|
-
try {
|
|
21
|
-
// Install codacy-cli using brew
|
|
22
|
-
await exec('brew install codacy/codacy-cli-v2/codacy-cli-v2');
|
|
23
|
-
return true;
|
|
24
|
-
}
|
|
25
|
-
catch (error) {
|
|
26
|
-
console.error('Failed to install codacy-cli:', error);
|
|
27
|
-
return false;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
// Check if .codacy/codacy.yaml exists and initialize if needed
|
|
32
|
-
const ensureCodacyConfig = async (rootPath, provider, organization, repository) => {
|
|
33
|
-
const extensions = ['yaml', 'yml'];
|
|
34
|
-
const configExists = extensions.some(ext => fs.existsSync(path.join(rootPath, '.codacy', `codacy.${ext}`)));
|
|
35
|
-
if (configExists) {
|
|
36
|
-
return true;
|
|
37
|
-
}
|
|
38
|
-
try {
|
|
39
|
-
const command = `${CODACY_CLI_COMMAND} init --api-token ${CODACY_ACCOUNT_TOKEN} --provider ${provider} --organization ${organization} --repository ${repository}`;
|
|
40
|
-
const options = { cwd: rootPath };
|
|
41
|
-
await exec(command, options);
|
|
42
|
-
await exec(`${CODACY_CLI_COMMAND} install`, options);
|
|
43
|
-
return true;
|
|
44
|
-
}
|
|
45
|
-
catch (error) {
|
|
46
|
-
console.error('Failed to initialize Codacy configuration:', error);
|
|
47
|
-
return false;
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
// Only treat stderr as an error if:
|
|
51
|
-
// 1. There's no stdout (meaning only errors occurred)
|
|
52
|
-
// 2. stderr contains actual error messages (not just progress logs)
|
|
53
|
-
const falseSuccess = (stdout, stderr) => stderr && (!stdout || /error|fail|exception/i.test(stderr));
|
|
54
34
|
// Run analysis with potential retry after installation
|
|
55
|
-
const runAnalysis = async (args) => {
|
|
35
|
+
const runAnalysis = async (cliCommand, args) => {
|
|
56
36
|
const safeFile = sanitizeCommand(args.file);
|
|
57
37
|
const tool = args.tool ? `--tool ${args.tool}` : '';
|
|
58
|
-
const command = `${
|
|
59
|
-
// Add options object with cwd and increased maxBuffer
|
|
38
|
+
const command = `${cliCommand} analyze ${tool} --format sarif ${safeFile}`;
|
|
60
39
|
const options = {
|
|
61
|
-
|
|
62
|
-
maxBuffer: 1024 * 1024 * 10, // 10MB buffer size
|
|
40
|
+
rootPath: args.rootPath,
|
|
63
41
|
};
|
|
42
|
+
return await execAsync(command, options);
|
|
43
|
+
};
|
|
44
|
+
const ensureCodacyConfig = async (cliCommand, rootPath, provider, organization, repository) => {
|
|
45
|
+
const codacyConfigPath = path.join(rootPath, CLI_FOLDER_NAME, 'codacy.yaml');
|
|
46
|
+
if (!fs.existsSync(codacyConfigPath)) {
|
|
47
|
+
const apiToken = CODACY_ACCOUNT_TOKEN ? `--api-token ${CODACY_ACCOUNT_TOKEN}` : '';
|
|
48
|
+
const repositoryAccess = repository && provider && organization
|
|
49
|
+
? `--provider ${provider} --organization ${organization} --repository ${repository}`
|
|
50
|
+
: '';
|
|
51
|
+
// initialize codacy-cli
|
|
52
|
+
await execAsync(`${cliCommand} init ${apiToken} ${repositoryAccess}`, { rootPath });
|
|
53
|
+
// install dependencies
|
|
54
|
+
await execAsync(`${cliCommand} install`, { rootPath });
|
|
55
|
+
}
|
|
56
|
+
return true;
|
|
57
|
+
};
|
|
58
|
+
const ensureCodacyCLIExists = async (rootPath, provider, organization, repository) => {
|
|
59
|
+
let isCLIAvailable = false;
|
|
64
60
|
try {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
61
|
+
await execAsync(`${CLI_LOCAL_COMMAND} --help`, { rootPath });
|
|
62
|
+
return CLI_LOCAL_COMMAND;
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
isCLIAvailable = false;
|
|
66
|
+
}
|
|
67
|
+
if (!isCLIAvailable) {
|
|
68
|
+
try {
|
|
69
|
+
await execAsync(`${CLI_GLOBAL_COMMAND} --help`, { rootPath });
|
|
70
|
+
return CLI_GLOBAL_COMMAND;
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
isCLIAvailable = false;
|
|
68
74
|
}
|
|
69
|
-
return { stdout, stderr };
|
|
70
75
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
76
|
+
// install locally if not available
|
|
77
|
+
const codacyFolder = path.join(rootPath, CLI_FOLDER_NAME);
|
|
78
|
+
if (!fs.existsSync(codacyFolder)) {
|
|
79
|
+
fs.mkdirSync(codacyFolder, { recursive: true });
|
|
75
80
|
}
|
|
81
|
+
// Download cli.sh if it doesn't exist
|
|
82
|
+
const codacyCliPath = path.join(codacyFolder, CLI_FILE_NAME);
|
|
83
|
+
if (!fs.existsSync(codacyCliPath)) {
|
|
84
|
+
await execAsync(`curl -Ls -o "${CLI_LOCAL_COMMAND}" https://raw.githubusercontent.com/codacy/codacy-cli-v2/main/codacy-cli.sh`, { rootPath });
|
|
85
|
+
await execAsync(`chmod +x "${CLI_LOCAL_COMMAND}"`, { rootPath });
|
|
86
|
+
}
|
|
87
|
+
// initialize codacy-cli
|
|
88
|
+
await ensureCodacyConfig(CLI_LOCAL_COMMAND, rootPath, provider, organization, repository);
|
|
89
|
+
return CLI_LOCAL_COMMAND;
|
|
76
90
|
};
|
|
77
91
|
export const cliAnalyzeHandler = async (args) => {
|
|
78
92
|
try {
|
|
79
93
|
// Ensure codacy-cli is installed
|
|
80
|
-
const
|
|
81
|
-
if (!
|
|
94
|
+
const cliCommand = await ensureCodacyCLIExists(args.rootPath, args.provider, args.organization, args.repository);
|
|
95
|
+
if (!cliCommand) {
|
|
82
96
|
return {
|
|
83
97
|
success: false,
|
|
84
98
|
errorType: 'cli-missing',
|
|
@@ -86,7 +100,7 @@ export const cliAnalyzeHandler = async (args) => {
|
|
|
86
100
|
};
|
|
87
101
|
}
|
|
88
102
|
// Check for mandatory configuration file and initialize if needed
|
|
89
|
-
const configExists = await ensureCodacyConfig(args.rootPath, args.provider, args.organization, args.repository);
|
|
103
|
+
const configExists = await ensureCodacyConfig(cliCommand, args.rootPath, args.provider, args.organization, args.repository);
|
|
90
104
|
if (!configExists) {
|
|
91
105
|
return {
|
|
92
106
|
success: false,
|
|
@@ -94,20 +108,13 @@ export const cliAnalyzeHandler = async (args) => {
|
|
|
94
108
|
output: 'Failed to find Codacy CLI configuration.',
|
|
95
109
|
};
|
|
96
110
|
}
|
|
97
|
-
const { stdout, stderr } = await runAnalysis(args);
|
|
98
|
-
if (falseSuccess(stdout, stderr)) {
|
|
99
|
-
return {
|
|
100
|
-
success: false,
|
|
101
|
-
errorType: 'stderr',
|
|
102
|
-
output: stderr,
|
|
103
|
-
};
|
|
104
|
-
}
|
|
111
|
+
const { stdout, stderr } = await runAnalysis(cliCommand, args);
|
|
105
112
|
// Try to extract JSON from the output if it's embedded in other text
|
|
106
113
|
const jsonMatch = /(\{[\s\S]*\}|\[[\s\S]*\])/.exec(stdout);
|
|
107
114
|
return {
|
|
108
115
|
success: true,
|
|
109
|
-
output: stdout,
|
|
110
116
|
result: jsonMatch ? JSON.parse(jsonMatch[0]) : null,
|
|
117
|
+
warnings: stderr,
|
|
111
118
|
};
|
|
112
119
|
}
|
|
113
120
|
catch (error) {
|