(server: McpServer)
| 7 | import { ensureWorkspaceInitialized } from "../../utils/workspace.js"; |
| 8 | |
| 9 | export function registerForgeScriptTool(server: McpServer): void { |
| 10 | server.tool( |
| 11 | "forge_script", |
| 12 | "Run a Forge script from the workspace", |
| 13 | { |
| 14 | scriptPath: z.string().describe("Path to the script file (e.g., 'script/Deploy.s.sol')"), |
| 15 | sig: z.string().optional().describe("Function signature to call (default: 'run()')"), |
| 16 | rpcUrl: z.string().optional().describe("JSON-RPC URL (default: http://localhost:8545)"), |
| 17 | broadcast: z.boolean().optional().describe("Broadcast the transactions"), |
| 18 | verify: z.boolean().optional().describe("Verify the contract on Etherscan (needs API key)") |
| 19 | }, |
| 20 | async ({ scriptPath, sig = "run()", rpcUrl, broadcast = false, verify = false }) => { |
| 21 | const installed = await checkFoundryInstalled(); |
| 22 | if (!installed) { |
| 23 | return { |
| 24 | content: [{ type: "text", text: FOUNDRY_NOT_INSTALLED_ERROR }], |
| 25 | isError: true |
| 26 | }; |
| 27 | } |
| 28 | |
| 29 | try { |
| 30 | const workspace = await ensureWorkspaceInitialized(); |
| 31 | |
| 32 | // Check if script exists |
| 33 | const scriptFullPath = path.join(workspace, scriptPath); |
| 34 | const scriptExists = await fs.access(scriptFullPath).then(() => true).catch(() => false); |
| 35 | if (!scriptExists) { |
| 36 | return { |
| 37 | content: [{ |
| 38 | type: "text", |
| 39 | text: `Script does not exist at ${scriptFullPath}` |
| 40 | }], |
| 41 | isError: true |
| 42 | }; |
| 43 | } |
| 44 | |
| 45 | const resolvedRpcUrl = await resolveRpcUrl(rpcUrl); |
| 46 | let command = `cd ${workspace} && ${FOUNDRY_PATHS.forgePath} script ${scriptPath} --sig "${sig}"`; |
| 47 | |
| 48 | if (resolvedRpcUrl) { |
| 49 | command += ` --rpc-url "${resolvedRpcUrl}"`; |
| 50 | } |
| 51 | |
| 52 | if (broadcast) { |
| 53 | command += ` --broadcast`; |
| 54 | } |
| 55 | |
| 56 | if (verify) { |
| 57 | command += ` --verify`; |
| 58 | } |
| 59 | |
| 60 | const result = await executeCommand(command); |
| 61 | |
| 62 | return { |
| 63 | content: [{ |
| 64 | type: "text", |
| 65 | text: result.success |
| 66 | ? `Script executed successfully:\n${result.message}` |
no test coverage detected