* 处理读取资源
(clientId: string, message: MCPMessage)
| 240 | * 处理读取资源 |
| 241 | */ |
| 242 | private async handleReadResource(clientId: string, message: MCPMessage): Promise<void> { |
| 243 | const uri = message.params?.uri as string; |
| 244 | |
| 245 | try { |
| 246 | let content: MCPResourceContent; |
| 247 | |
| 248 | if (uri === 'file://workspace') { |
| 249 | const { readdir } = await import('fs/promises'); |
| 250 | const files = await readdir(process.cwd()); |
| 251 | content = { |
| 252 | uri, |
| 253 | mimeType: 'application/json', |
| 254 | text: JSON.stringify(files, null, 2), |
| 255 | }; |
| 256 | } else if (uri === 'git://status') { |
| 257 | const { execSync } = await import('child_process'); |
| 258 | const status = execSync('git status --porcelain', { encoding: 'utf-8' }); |
| 259 | content = { |
| 260 | uri, |
| 261 | mimeType: 'text/plain', |
| 262 | text: status, |
| 263 | }; |
| 264 | } else if (uri === 'git://log') { |
| 265 | const { execSync } = await import('child_process'); |
| 266 | const log = execSync('git log --oneline -10', { encoding: 'utf-8' }); |
| 267 | content = { |
| 268 | uri, |
| 269 | mimeType: 'text/plain', |
| 270 | text: log, |
| 271 | }; |
| 272 | } else { |
| 273 | throw new Error(`Unknown resource URI: ${uri}`); |
| 274 | } |
| 275 | |
| 276 | this.sendResponse(clientId, message.id, { contents: [content] }); |
| 277 | } catch (error) { |
| 278 | this.sendError(clientId, -32603, `Failed to read resource: ${uri}`, error); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * 处理列出工具 |
no test coverage detected