|
1 | 1 | import { Server } from '@modelcontextprotocol/sdk/server/index.js'; |
2 | | -import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js'; |
| 2 | +import { |
| 3 | + CallToolRequestSchema, |
| 4 | + ListToolsRequestSchema, |
| 5 | + ListPromptsRequestSchema, |
| 6 | + GetPromptRequestSchema, |
| 7 | + ListResourcesRequestSchema, |
| 8 | + ReadResourceRequestSchema |
| 9 | +} from '@modelcontextprotocol/sdk/types.js'; |
3 | 10 | import { InspectionTool, InspectionToolParams } from '../tools/InspectionTool.js'; |
4 | 11 | import { MarkdownFormatter } from '../formatters/MarkdownFormatter.js'; |
5 | 12 | import { JSONFormatter } from '../formatters/JSONFormatter.js'; |
@@ -27,6 +34,8 @@ export class RequestHandlers { |
27 | 34 | setup(): void { |
28 | 35 | this.setupToolListHandler(); |
29 | 36 | this.setupToolCallHandler(); |
| 37 | + this.setupPromptHandlers(); |
| 38 | + this.setupResourceHandlers(); |
30 | 39 | this.logger.info('Request handlers setup complete'); |
31 | 40 | } |
32 | 41 |
|
@@ -101,4 +110,203 @@ export class RequestHandlers { |
101 | 110 | }; |
102 | 111 | }); |
103 | 112 | } |
| 113 | + |
| 114 | + private setupPromptHandlers(): void { |
| 115 | + // List available prompts |
| 116 | + this.server.setRequestHandler(ListPromptsRequestSchema, async () => { |
| 117 | + const prompts = [ |
| 118 | + { |
| 119 | + name: 'analyze-project', |
| 120 | + description: 'Analyze a project for code quality issues', |
| 121 | + arguments: [ |
| 122 | + { |
| 123 | + name: 'projectPath', |
| 124 | + description: 'Path to the project to analyze', |
| 125 | + required: true, |
| 126 | + }, |
| 127 | + { |
| 128 | + name: 'profile', |
| 129 | + description: 'Inspection profile to use', |
| 130 | + required: false, |
| 131 | + }, |
| 132 | + ], |
| 133 | + }, |
| 134 | + { |
| 135 | + name: 'check-file', |
| 136 | + description: 'Check a specific file for issues', |
| 137 | + arguments: [ |
| 138 | + { |
| 139 | + name: 'filePath', |
| 140 | + description: 'Path to the file to check', |
| 141 | + required: true, |
| 142 | + }, |
| 143 | + ], |
| 144 | + }, |
| 145 | + { |
| 146 | + name: 'fix-issues', |
| 147 | + description: 'Get suggestions to fix detected issues', |
| 148 | + arguments: [ |
| 149 | + { |
| 150 | + name: 'projectPath', |
| 151 | + description: 'Path to the project', |
| 152 | + required: true, |
| 153 | + }, |
| 154 | + { |
| 155 | + name: 'severity', |
| 156 | + description: 'Minimum severity level (ERROR, WARNING, INFO)', |
| 157 | + required: false, |
| 158 | + }, |
| 159 | + ], |
| 160 | + }, |
| 161 | + ]; |
| 162 | + |
| 163 | + this.logger.debug('Listed prompts', { count: prompts.length }); |
| 164 | + return { prompts }; |
| 165 | + }); |
| 166 | + |
| 167 | + // Get a specific prompt |
| 168 | + this.server.setRequestHandler(GetPromptRequestSchema, async (request: any) => { |
| 169 | + const { name, arguments: args } = request.params; |
| 170 | + |
| 171 | + const prompts: Record<string, (args: any) => { messages: any[] }> = { |
| 172 | + 'analyze-project': (args: any) => ({ |
| 173 | + messages: [ |
| 174 | + { |
| 175 | + role: 'user', |
| 176 | + content: { |
| 177 | + type: 'text', |
| 178 | + text: `Please analyze the project at ${args.projectPath} using JetBrains inspections${args.profile ? ` with profile "${args.profile}"` : ''}.`, |
| 179 | + }, |
| 180 | + }, |
| 181 | + ], |
| 182 | + }), |
| 183 | + 'check-file': (args: any) => ({ |
| 184 | + messages: [ |
| 185 | + { |
| 186 | + role: 'user', |
| 187 | + content: { |
| 188 | + type: 'text', |
| 189 | + text: `Check the file at ${args.filePath} for code quality issues using JetBrains inspections.`, |
| 190 | + }, |
| 191 | + }, |
| 192 | + ], |
| 193 | + }), |
| 194 | + 'fix-issues': (args: any) => ({ |
| 195 | + messages: [ |
| 196 | + { |
| 197 | + role: 'user', |
| 198 | + content: { |
| 199 | + type: 'text', |
| 200 | + text: `Analyze the project at ${args.projectPath} and provide fix suggestions for issues${args.severity ? ` with severity ${args.severity} or higher` : ''}.`, |
| 201 | + }, |
| 202 | + }, |
| 203 | + ], |
| 204 | + }), |
| 205 | + }; |
| 206 | + |
| 207 | + const promptHandler = prompts[name]; |
| 208 | + if (promptHandler) { |
| 209 | + return promptHandler(args); |
| 210 | + } |
| 211 | + |
| 212 | + throw new Error(`Unknown prompt: ${name}`); |
| 213 | + }); |
| 214 | + } |
| 215 | + |
| 216 | + private setupResourceHandlers(): void { |
| 217 | + // List available resources |
| 218 | + this.server.setRequestHandler(ListResourcesRequestSchema, async () => { |
| 219 | + const resources = [ |
| 220 | + { |
| 221 | + uri: 'inspection://profiles', |
| 222 | + name: 'Available Inspection Profiles', |
| 223 | + description: 'List of available inspection profiles', |
| 224 | + mimeType: 'application/json', |
| 225 | + }, |
| 226 | + { |
| 227 | + uri: 'inspection://config', |
| 228 | + name: 'Current Configuration', |
| 229 | + description: 'Current MCP server configuration', |
| 230 | + mimeType: 'application/json', |
| 231 | + }, |
| 232 | + { |
| 233 | + uri: 'inspection://ides', |
| 234 | + name: 'Detected IDEs', |
| 235 | + description: 'List of detected JetBrains IDEs on the system', |
| 236 | + mimeType: 'application/json', |
| 237 | + }, |
| 238 | + ]; |
| 239 | + |
| 240 | + this.logger.debug('Listed resources', { count: resources.length }); |
| 241 | + return { resources }; |
| 242 | + }); |
| 243 | + |
| 244 | + // Read a specific resource |
| 245 | + this.server.setRequestHandler(ReadResourceRequestSchema, async (request: any) => { |
| 246 | + const { uri } = request.params; |
| 247 | + |
| 248 | + if (uri === 'inspection://profiles') { |
| 249 | + return { |
| 250 | + contents: [ |
| 251 | + { |
| 252 | + uri, |
| 253 | + mimeType: 'application/json', |
| 254 | + text: JSON.stringify( |
| 255 | + { |
| 256 | + profiles: [ |
| 257 | + 'Default', |
| 258 | + 'Project Default', |
| 259 | + 'Strict', |
| 260 | + 'Essential', |
| 261 | + ], |
| 262 | + description: 'Available inspection profiles for code analysis', |
| 263 | + }, |
| 264 | + null, |
| 265 | + 2, |
| 266 | + ), |
| 267 | + }, |
| 268 | + ], |
| 269 | + }; |
| 270 | + } |
| 271 | + |
| 272 | + if (uri === 'inspection://config') { |
| 273 | + const config = this.container.resolve<ConfigLoader>('ConfigLoader').getConfig(); |
| 274 | + return { |
| 275 | + contents: [ |
| 276 | + { |
| 277 | + uri, |
| 278 | + mimeType: 'application/json', |
| 279 | + text: JSON.stringify(config, null, 2), |
| 280 | + }, |
| 281 | + ], |
| 282 | + }; |
| 283 | + } |
| 284 | + |
| 285 | + if (uri === 'inspection://ides') { |
| 286 | + const { IDEDetector } = await import('../../core/ide/IDEDetector.js'); |
| 287 | + const { getIDEPaths } = await import('../../infrastructure/config/IDEPaths.js'); |
| 288 | + const detector = new IDEDetector(getIDEPaths(), this.logger); |
| 289 | + const ides = await detector.findAvailableIDEs(); |
| 290 | + |
| 291 | + return { |
| 292 | + contents: [ |
| 293 | + { |
| 294 | + uri, |
| 295 | + mimeType: 'application/json', |
| 296 | + text: JSON.stringify( |
| 297 | + { |
| 298 | + detected: ides, |
| 299 | + count: ides.length, |
| 300 | + }, |
| 301 | + null, |
| 302 | + 2, |
| 303 | + ), |
| 304 | + }, |
| 305 | + ], |
| 306 | + }; |
| 307 | + } |
| 308 | + |
| 309 | + throw new Error(`Unknown resource: ${uri}`); |
| 310 | + }); |
| 311 | + } |
104 | 312 | } |
0 commit comments