@codacy/codacy-mcp 0.4.9 → 0.5.1

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 CHANGED
@@ -55,7 +55,7 @@ The following tools are available through the Codacy MCP Server:
55
55
 
56
56
  - `codacy_list_repository_pull_requests`: List pull requests from a repository that the user has access to. You can search this endpoint for either last-updated (default), impact or merged.
57
57
  - `codacy_list_pull_request_issues`: Returns a list of issues found in a pull request. We can request either new or fixed issues.
58
- - `codacy_get_repository_pull_request_files_coverage`: Get coverage information for all files in a pull request.
58
+ - `codacy_get_pull_request_files_coverage`: Get diff coverage information for all files in a pull request.
59
59
  - `codacy_get_pull_request_git_diff`: Returns the human-readable Git diff of a pull request.
60
60
 
61
61
  ### CLI Analysis
package/dist/index.js CHANGED
@@ -59,7 +59,7 @@ const toolDefinitions = {
59
59
  handler: Handlers.listOrganizationRepositoriesHandler,
60
60
  },
61
61
  codacy_search_organization_srm_items: {
62
- tool: Tools.searchSecurityItemsTool,
62
+ tool: Tools.searchOrganizationSecurityItemsTool,
63
63
  handler: Handlers.searchSecurityItemsHandler,
64
64
  },
65
65
  codacy_search_repository_srm_items: {
@@ -83,7 +83,7 @@ const toolDefinitions = {
83
83
  tool: Tools.getFileCoverageTool,
84
84
  handler: Handlers.getFileCoverageHandler,
85
85
  },
86
- codacy_get_repository_pull_request_files_coverage: {
86
+ codacy_get_pull_request_files_coverage: {
87
87
  tool: Tools.getRepositoryPullRequestFilesCoverageTool,
88
88
  handler: Handlers.getRepositoryPullRequestFilesCoverageHandler,
89
89
  },
@@ -1,25 +1,100 @@
1
1
  import util from 'util';
2
2
  import { exec as execChildProcess } from 'child_process';
3
+ import fs from 'fs';
4
+ import path from 'path';
3
5
  const exec = util.promisify(execChildProcess);
6
+ const CODACY_ACCOUNT_TOKEN = process.env.CODACY_ACCOUNT_TOKEN;
7
+ const CODACY_CLI_COMMAND = 'CODACY_CLI_V2_VERSION=1.0.0-main.232.a6a6368 codacy-cli';
4
8
  // Safeguard: Validate and sanitize command inputs
5
9
  const sanitizeCommand = (command) => {
6
10
  // Remove any shell metacharacters
7
11
  return command?.replace(/[;&|`$]/g, '') || '';
8
12
  };
9
- export const cliAnalyzeHandler = async (args) => {
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 configPath = path.join(rootPath, '.codacy', 'codacy.yaml');
34
+ if (fs.existsSync(configPath)) {
35
+ return true;
36
+ }
37
+ try {
38
+ const command = `${CODACY_CLI_COMMAND} init --api-token ${CODACY_ACCOUNT_TOKEN} --provider ${provider} --organization ${organization} --repository ${repository}`;
39
+ const options = { cwd: rootPath };
40
+ await exec(command, options);
41
+ await exec(`${CODACY_CLI_COMMAND} install`, options);
42
+ return true;
43
+ }
44
+ catch (error) {
45
+ console.error('Failed to initialize Codacy configuration:', error);
46
+ return false;
47
+ }
48
+ };
49
+ // Only treat stderr as an error if:
50
+ // 1. There's no stdout (meaning only errors occurred)
51
+ // 2. stderr contains actual error messages (not just progress logs)
52
+ const falseSuccess = (stdout, stderr) => stderr && (!stdout || /error|fail|exception/i.test(stderr));
53
+ // Run analysis with potential retry after installation
54
+ const runAnalysis = async (args) => {
55
+ const safeFile = sanitizeCommand(args.file);
56
+ const tool = args.tool ? `--tool ${args.tool}` : '';
57
+ const command = `${CODACY_CLI_COMMAND} analyze ${tool} --format sarif ${safeFile}`;
58
+ // Add options object with cwd and increased maxBuffer
59
+ const options = {
60
+ cwd: args.rootPath,
61
+ maxBuffer: 1024 * 1024 * 10, // 10MB buffer size
62
+ };
10
63
  try {
11
- const safeFile = sanitizeCommand(args.file);
12
- const command = `codacy-cli analyze --format sarif ${safeFile}`;
13
- // Add options object with cwd and increased maxBuffer
14
- const options = {
15
- cwd: args.rootPath,
16
- maxBuffer: 1024 * 1024 * 10, // 10MB buffer size
17
- };
18
64
  const { stdout, stderr } = await exec(command, options);
19
- // Only treat stderr as an error if:
20
- // 1. There's no stdout (meaning only errors occurred)
21
- // 2. stderr contains actual error messages (not just progress logs)
22
- if (stderr && (!stdout || /error|fail|exception/i.test(stderr))) {
65
+ if (falseSuccess(stdout, stderr)) {
66
+ throw new Error('Failed to run analysis. Trying to install codacy-cli...');
67
+ }
68
+ return { stdout, stderr };
69
+ }
70
+ catch (error) {
71
+ // If first attempt fails, try installing and retry
72
+ await exec(`${CODACY_CLI_COMMAND} install`, options);
73
+ return await exec(command, options);
74
+ }
75
+ };
76
+ export const cliAnalyzeHandler = async (args) => {
77
+ try {
78
+ // Ensure codacy-cli is installed
79
+ const isCLIAvailable = await ensureCodacyCLI();
80
+ if (!isCLIAvailable) {
81
+ return {
82
+ success: false,
83
+ errorType: 'cli-missing',
84
+ output: 'Failed to install codacy-cli. Please install it manually.',
85
+ };
86
+ }
87
+ // Check for mandatory configuration file and initialize if needed
88
+ const configExists = await ensureCodacyConfig(args.rootPath, args.provider, args.organization, args.repository);
89
+ if (!configExists) {
90
+ return {
91
+ success: false,
92
+ errorType: 'cli-config-missing',
93
+ output: 'Failed to find Codacy CLI configuration.',
94
+ };
95
+ }
96
+ const { stdout, stderr } = await runAnalysis(args);
97
+ if (falseSuccess(stdout, stderr)) {
23
98
  return {
24
99
  success: false,
25
100
  errorType: 'stderr',
@@ -11,7 +11,7 @@ export const toolNames = {
11
11
  CODACY_LIST_ORGANIZATIONS: 'codacy_list_organizations',
12
12
  CODACY_GET_FILE_ISSUES: 'codacy_get_file_issues',
13
13
  CODACY_GET_FILE_COVERAGE: 'codacy_get_file_coverage',
14
- CODACY_GET_REPOSITORY_PULL_REQUEST_FILES_COVERAGE: 'codacy_get_repository_pull_request_files_coverage',
14
+ CODACY_GET_PULL_REQUEST_FILES_COVERAGE: 'codacy_get_pull_request_files_coverage',
15
15
  CODACY_GET_PULL_REQUEST_GIT_DIFF: 'codacy_get_pull_request_git_diff',
16
16
  CODACY_LIST_PULL_REQUEST_ISSUES: 'codacy_list_pull_request_issues',
17
17
  CODACY_GET_REPOSITORY_WITH_ANALYSIS: 'codacy_get_repository_with_analysis',
@@ -1,19 +1,46 @@
1
- import { toolNames } from '../schemas.js';
1
+ import { generalOrganizationMistakes, generalRepositoryMistakes } from '../rules.js';
2
+ import { repositorySchema, toolNames } from '../schemas.js';
3
+ const availableCliTools = ['eslint', 'pmd', 'trivy', 'pylint', 'dartanalyzer'];
4
+ const rules = `
5
+ Use this tool to analyze the quality of a repository using Codacy command line.
6
+
7
+ Common use cases:
8
+ - When the user asks for an analysis of a repository
9
+ - When the user asks to scan the code
10
+ - When the user wants to analyze based on Codacy configuration
11
+ - When the user wants to apply fixes based on Codacy configuration and analysis
12
+ - When the user wants Codacy results immediately without waiting for the next scheduled analysis
13
+
14
+ Common mistakes:
15
+ - Using this tool to know the status of a repository
16
+ - Using this tool without specifying the rootPath
17
+ - Using this tool to know current results from Codacy. Use ${toolNames.CODACY_GET_REPOSITORY_WITH_ANALYSIS} instead.
18
+ - Filling the tool parameter when not asked specifically for it.
19
+ ${generalOrganizationMistakes}
20
+ ${generalRepositoryMistakes}
21
+ `;
2
22
  export const cliAnalyzeTool = {
3
23
  name: toolNames.CODACY_CLI_ANALYZE,
4
- description: 'Run quality analysis using Codacy CLI. Requires the Codacy CLI to be installed and configured.',
24
+ description: `Run quality analysis locally using Codacy CLI. \n ${rules}`,
5
25
  inputSchema: {
6
26
  type: 'object',
7
27
  properties: {
28
+ ...repositorySchema,
8
29
  rootPath: {
9
30
  type: 'string',
10
- description: 'Required. Root path to the project or repository in the local filesystem. This must be filled to ensure permissions are met for the tool to run.',
31
+ description: 'Root path to the project or repository in the local filesystem. This must be filled to ensure permissions are met for the tool to run.',
11
32
  },
12
33
  file: {
13
34
  type: 'string',
14
- description: 'Optional. Absolute path to the file to analyze, or pattern to match files (e.g., "src/**/*.js"). If left empty, the analysis will be executed for the whole project.',
35
+ description: 'Optional. Absolute path to the file to analyze, or directory to analyze (e.g., "src/"). If left empty, the analysis will be executed for the whole project.',
36
+ default: '',
37
+ },
38
+ tool: {
39
+ type: 'string',
40
+ description: `Optional. Tool to use for the analysis. If left empty, it will use all available tools. Possible values: ${availableCliTools.join(', ')}`,
15
41
  default: '',
16
42
  },
17
43
  },
44
+ required: ['provider', 'organization', 'repository', 'rootPath'],
18
45
  },
19
46
  };
@@ -10,8 +10,8 @@ const rules = `
10
10
  ${generalRepositoryMistakes}
11
11
  `;
12
12
  export const getRepositoryPullRequestFilesCoverageTool = {
13
- name: toolNames.CODACY_GET_REPOSITORY_PULL_REQUEST_FILES_COVERAGE,
14
- description: `Use this tool to get coverage information for all files in a pull request. \n ${rules}`,
13
+ name: toolNames.CODACY_GET_PULL_REQUEST_FILES_COVERAGE,
14
+ description: `Use this tool to get the diff coverage information for all files in a pull request. \n ${rules}`,
15
15
  inputSchema: {
16
16
  type: 'object',
17
17
  properties: {
@@ -1,12 +1,11 @@
1
1
  import { codacyLanguages, issueCategories } from '../utils.js';
2
- import { searchSecurityItemsTool } from './searchSecurityItemsTool.js';
3
2
  import { listFileIssuesTool } from './listFileIssuesTool.js';
4
3
  import { listPullRequestIssuesTool } from './listPullRequestIssuesTool.js';
5
4
  import { repositorySchema, branchSchema, defaultPagination, toolNames, } from '../schemas.js';
6
5
  import { generalOrganizationMistakes, generalRepositoryMistakes } from '../rules.js';
7
6
  const rules = `
8
7
  This is the primary tool for investigating general code quality concerns (e.g. best practices, performance, complexity, style) but NOT security issues.
9
- For security-related issues, use the ${searchSecurityItemsTool.name} tool instead.
8
+ For security-related issues, use the SRM tools instead.
10
9
  For listing the issues for specific files, use the ${listFileIssuesTool.name} tool instead.
11
10
  For listing the issues for specific pull requests, use the ${listPullRequestIssuesTool.name} tool instead.
12
11
 
@@ -1,4 +1,4 @@
1
- import { securityCategories, securityScanTypes, securityStatuses } from '../utils.js';
1
+ import { organizationSecurityScanTypes, repositorySecurityScanTypes, securityCategories, securityStatuses, } from '../utils.js';
2
2
  import { getPaginationWithSorting, organizationSchema, repositorySchema, toolNames, } from '../schemas.js';
3
3
  import { generalOrganizationMistakes, generalRepositoryMistakes } from '../rules.js';
4
4
  const rules = `
@@ -15,6 +15,7 @@ const rules = `
15
15
 
16
16
  Common mistakes:
17
17
  - Using this tool for quality issues
18
+ - Calling this tool after editing files to check for new results; changes need to be committed to the repository first.
18
19
  ${generalOrganizationMistakes}
19
20
  ${generalRepositoryMistakes}
20
21
  `;
@@ -44,27 +45,10 @@ const commonSrmProperties = {
44
45
  },
45
46
  note: "_other_ can be used to search for issues that don't have a security category",
46
47
  },
47
- scanTypes: {
48
- description: 'Array of security scan types to filter by.',
49
- type: 'array',
50
- items: {
51
- type: 'string',
52
- enum: Object.keys(securityScanTypes),
53
- },
54
- mapping: securityScanTypes,
55
- },
56
- segments: {
57
- type: 'array',
58
- description: 'Set of segments ids (type number). Segment is a Codacy concept that groups repositories by different criteria',
59
- items: {
60
- type: 'number',
61
- description: 'Segment id',
62
- },
63
- },
64
48
  };
65
- export const searchSecurityItemsTool = {
49
+ export const searchOrganizationSecurityItemsTool = {
66
50
  name: toolNames.CODACY_LIST_ORGANIZATION_SRM_ITEMS,
67
- description: `Primary tool to list security items/issues/vulnerabilities/findings, results are related to the organization's security and risk management (SRM) dashboard on Codacy. This tool contains pagination. Returns comprehensive security analysis including ${Object.keys(securityScanTypes).join(', ')} security issues.
51
+ description: `Primary tool to list security items/issues/vulnerabilities/findings, results are related to the organization's security and risk management (SRM) dashboard on Codacy. This tool contains pagination. Returns comprehensive security analysis including ${Object.keys(organizationSecurityScanTypes).join(', ')} security issues.
68
52
  \n ${rules}`,
69
53
  inputSchema: {
70
54
  type: 'object',
@@ -75,15 +59,15 @@ export const searchSecurityItemsTool = {
75
59
  type: 'object',
76
60
  description: 'Search parameters to filter the metrics of the security issues dashboard of an organization',
77
61
  properties: {
78
- repositories: {
62
+ ...commonSrmProperties,
63
+ scanTypes: {
64
+ description: 'Array of security scan types to filter by.',
79
65
  type: 'array',
80
- description: 'Use this to filter by repository name.',
81
66
  items: {
82
67
  type: 'string',
83
- description: 'Repository name',
68
+ enum: Object.keys(organizationSecurityScanTypes),
84
69
  },
85
70
  },
86
- ...commonSrmProperties,
87
71
  },
88
72
  },
89
73
  },
@@ -92,8 +76,9 @@ export const searchSecurityItemsTool = {
92
76
  };
93
77
  export const searchRepositorySecurityItemsTool = {
94
78
  name: toolNames.CODACY_LIST_REPOSITORY_SRM_ITEMS,
95
- description: `Tool to list security items/issues/vulnerabilities/findings, results are related to a repository's security and risk management (SRM) dashboard on Codacy. This tool contains pagination. Returns comprehensive security analysis including ${Object.keys(securityScanTypes).join(', ')} security issues.
96
- \n ${rules}`,
79
+ description: `Tool to list security items/issues/vulnerabilities/findings, results are related to a repository's security and risk management (SRM) dashboard on Codacy. This tool contains pagination. Returns comprehensive security analysis including ${Object.keys(repositorySecurityScanTypes).join(', ')} security issues.
80
+ \n ${rules}
81
+ - Using this tool for DAST and PenTesting scan types; use the codacy_search_organization_srm_items tool instead.`,
97
82
  inputSchema: {
98
83
  type: 'object',
99
84
  properties: {
@@ -104,6 +89,14 @@ export const searchRepositorySecurityItemsTool = {
104
89
  description: 'Search parameters to filter the metrics of the security issues dashboard of an organization',
105
90
  properties: {
106
91
  ...commonSrmProperties,
92
+ scanTypes: {
93
+ description: 'Array of security scan types to filter by.',
94
+ type: 'array',
95
+ items: {
96
+ type: 'string',
97
+ enum: Object.keys(repositorySecurityScanTypes),
98
+ },
99
+ },
107
100
  },
108
101
  },
109
102
  },
package/dist/src/utils.js CHANGED
@@ -79,12 +79,15 @@ export const securityCategories = [
79
79
  'XSS',
80
80
  '_other_',
81
81
  ];
82
- export const securityScanTypes = {
82
+ export const repositorySecurityScanTypes = {
83
83
  SAST: 'Code scanning',
84
84
  Secrets: 'Secret scanning',
85
85
  SCA: 'Dependency scanning',
86
86
  IaC: 'Infrastructure-as-code scanning',
87
87
  CICD: 'CI/CD scanning',
88
+ };
89
+ export const organizationSecurityScanTypes = {
90
+ ...repositorySecurityScanTypes,
88
91
  DAST: 'Dynamic Application Security Testing',
89
92
  PenTesting: 'Penetration testing',
90
93
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codacy/codacy-mcp",
3
- "version": "0.4.9",
3
+ "version": "0.5.1",
4
4
  "description": "Codacy MCP server",
5
5
  "license": "MIT",
6
6
  "author": "Codacy",