generated from codacy/codacy-public-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
200 lines (188 loc) · 5.78 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/env node
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import {
CallToolRequestSchema,
ListToolsRequestSchema,
Tool,
} from '@modelcontextprotocol/sdk/types.js';
import { OpenAPI } from './src/api/client/index.js';
import * as Tools from './src/tools/index.js';
import type { ToolKeys } from './src/schemas.js';
import * as Handlers from './src/handlers/index.js';
import { validateOrganization } from './src/middleware/validation.js';
// Check for API key
const CODACY_ACCOUNT_TOKEN = process.env.CODACY_ACCOUNT_TOKEN;
if (!CODACY_ACCOUNT_TOKEN) {
console.error('Error: CODACY_ACCOUNT_TOKEN environment variable is required');
process.exit(1);
}
OpenAPI.BASE = 'https://app.codacy.com/api/v3';
OpenAPI.HEADERS = {
'api-token': CODACY_ACCOUNT_TOKEN || '',
'X-Codacy-Origin': 'mcp-server',
};
const server = new Server(
{
name: 'codacy-mcp-server',
version: '0.1.0',
},
{
capabilities: {
tools: {},
resources: {},
triggers: {
patterns: [
'codacy',
'code quality',
'code analysis',
'security vulnerabilities',
'repository issues',
'pull request analysis',
'code coverage',
'issues',
'security',
'srm',
'analysis',
'tool',
'pattern',
'pull request',
'repository',
'file',
'coverage',
'git',
'diff',
'branch',
'commit',
'severity',
'organization',
],
},
},
}
);
interface ToolDefinition {
tool: Tool;
handler: (args: any) => Promise<any>;
}
const toolDefinitions: { [key in ToolKeys]: ToolDefinition } = {
codacy_list_organization_repositories: {
tool: Tools.listOrganizationRepositoriesTool,
handler: Handlers.listOrganizationRepositoriesHandler,
},
codacy_search_organization_srm_items: {
tool: Tools.searchOrganizationSecurityItemsTool,
handler: Handlers.searchSecurityItemsHandler,
},
codacy_search_repository_srm_items: {
tool: Tools.searchRepositorySecurityItemsTool,
handler: Handlers.searchRepositorySecurityItemsHandler,
},
codacy_list_repository_issues: {
tool: Tools.searchRepositoryIssuesTool,
handler: Handlers.searchRepositoryIssuesHandler,
},
codacy_list_repository_pull_requests: {
tool: Tools.listRepositoryPullRequestsTool,
handler: Handlers.listRepositoryPullRequestsHandler,
},
codacy_list_files: { tool: Tools.listFilesTool, handler: Handlers.listFilesHandler },
codacy_get_file_issues: {
tool: Tools.listFileIssuesTool,
handler: Handlers.getFileIssuesHandler,
},
codacy_get_file_coverage: {
tool: Tools.getFileCoverageTool,
handler: Handlers.getFileCoverageHandler,
},
codacy_get_pull_request_files_coverage: {
tool: Tools.getRepositoryPullRequestFilesCoverageTool,
handler: Handlers.getRepositoryPullRequestFilesCoverageHandler,
},
codacy_get_pull_request_git_diff: {
tool: Tools.getPullRequestGitDiffTool,
handler: Handlers.getPullRequestGitDiffHandler,
},
codacy_list_pull_request_issues: {
tool: Tools.listPullRequestIssuesTool,
handler: Handlers.listPullRequestIssuesHandler,
},
codacy_get_repository_with_analysis: {
tool: Tools.getRepositoryWithAnalysisTool,
handler: Handlers.getRepositoryWithAnalysisHandler,
},
codacy_get_file_with_analysis: {
tool: Tools.getFileWithAnalysisTool,
handler: Handlers.getFileWithAnalysisHandler,
},
codacy_get_file_clones: {
tool: Tools.getFileClonesTool,
handler: Handlers.getFileClonesHandler,
},
codacy_get_repository_pull_request: {
tool: Tools.getRepositoryPullRequestTool,
handler: Handlers.getRepositoryPullRequestHandler,
},
codacy_get_issue: {
tool: Tools.getIssueTool,
handler: Handlers.getIssueHandler,
},
codacy_get_pattern: {
tool: Tools.getPatternTool,
handler: Handlers.getPatternHandler,
},
codacy_list_repository_tool_patterns: {
tool: Tools.listRepositoryToolPatternsTool,
handler: Handlers.listRepositoryToolPatternsHandler,
},
codacy_list_tools: {
tool: Tools.listToolsTool,
handler: Handlers.listToolsHandler,
},
codacy_list_repository_tools: {
tool: Tools.listRepositoryToolsTool,
handler: Handlers.listRepositoryToolsHandler,
},
codacy_list_organizations: {
tool: Tools.listOrganizationsTool,
handler: Handlers.listOrganizationsHandler,
},
codacy_cli_analyze: {
tool: Tools.cliAnalyzeTool,
handler: Handlers.cliAnalyzeHandler,
},
};
// Register tools
server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: Object.values(toolDefinitions).map(({ tool }) => tool),
}));
// Register request handlers
server.setRequestHandler(CallToolRequestSchema, async request => {
try {
if (!request.params.arguments) {
throw new Error('Arguments are required');
}
const toolDefinition = toolDefinitions[request.params.name as ToolKeys];
if (!toolDefinition) throw new Error(`Unknown tool: ${request.params.name}`);
// Validate organization if the tool requires it
if (request.params.arguments.organization) {
request.params.arguments = validateOrganization(request.params.arguments);
}
const result = await toolDefinition.handler(request.params.arguments);
return {
content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
};
} catch (error) {
console.error(error);
throw error;
}
});
async function runServer() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error('Codacy MCP Server running on stdio');
}
runServer().catch(error => {
console.error('Fatal error in main():', error);
process.exit(1);
});