| 6 | import { disposeAll } from "../utils"; |
| 7 | |
| 8 | export class McpTools implements IAmDisposable { |
| 9 | private readonly disposables: IAmDisposable[] = []; |
| 10 | |
| 11 | constructor( |
| 12 | private readonly dartCapabilities: DartCapabilities, |
| 13 | private readonly dartToolingDaemon: DartToolingDaemon | undefined, |
| 14 | ) { |
| 15 | this.disposables.push(vs.lm.registerTool("get_dart_tooling_daemon_dtd_uri", { |
| 16 | invoke: async () => { |
| 17 | if (!dartCapabilities.supportsToolingDaemon) |
| 18 | throw new Error("DTD is not available for this version of Flutter/Dart, please upgrade."); |
| 19 | const dtdUri = await dartToolingDaemon?.dtdUri; |
| 20 | return dtdUri |
| 21 | ? new vs.LanguageModelToolResult([new vs.LanguageModelTextPart(dtdUri)]) |
| 22 | : undefined; |
| 23 | }, |
| 24 | })); |
| 25 | |
| 26 | this.disposables.push(vs.lm.registerTool("dart_format", { |
| 27 | invoke: async (options) => { |
| 28 | const input = options.input as any; |
| 29 | const filePath = input?.filePath; |
| 30 | if (typeof filePath !== "string") |
| 31 | throw new Error("The Dart format tool requires a string 'filePath' property."); |
| 32 | if (!path.isAbsolute(filePath)) |
| 33 | throw new Error("The Dart format tool requires an absolute path for the 'filePath' property."); |
| 34 | if (!filePath.toLowerCase().endsWith(".dart")) |
| 35 | throw new Error("The Dart format tool can only format .dart files."); |
| 36 | |
| 37 | const fileUri = vs.Uri.file(filePath); |
| 38 | const document = await vs.workspace.openTextDocument(fileUri); |
| 39 | await vs.window.showTextDocument(document); |
| 40 | await vs.commands.executeCommand("editor.action.formatDocument"); |
| 41 | await document.save(); |
| 42 | |
| 43 | return new vs.LanguageModelToolResult([new vs.LanguageModelTextPart(`Formatted '${filePath}'. You may need to re-read this file to get the latest content before making further changes.`)]); |
| 44 | }, |
| 45 | prepareInvocation: (options) => { |
| 46 | const input = options.input as any; |
| 47 | const filePath = input?.filePath; |
| 48 | return { |
| 49 | invocationMessage: |
| 50 | typeof filePath === "string" |
| 51 | ? `Formatting ${path.basename(filePath)}` |
| 52 | : "Formatting Dart file", |
| 53 | }; |
| 54 | } |
| 55 | })); |
| 56 | |
| 57 | this.disposables.push(vs.lm.registerTool("dart_fix", { |
| 58 | invoke: async (options) => { |
| 59 | const input = options.input as any; |
| 60 | const filePath = input?.filePath; |
| 61 | if (filePath) { |
| 62 | if (typeof filePath !== "string") |
| 63 | throw new Error("The Dart fix tool only accepts string or falsy 'filePath' property."); |
| 64 | if (!path.isAbsolute(filePath)) |
| 65 | throw new Error("The Dart fix tool requires an absolute path for the 'filePath' property if provided."); |
nothing calls this directly
no outgoing calls
no test coverage detected