send_code_to_revit
Execute custom C# code directly in Autodesk Revit to automate tasks, modify elements, or interact with project data using predefined templates and execution parameters.
Instructions
Send C# code to Revit for execution. The code will be inserted into a template with access to the Revit Document and parameters. Your code should be written to work within the Execute method of the template.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | The C# code to execute in Revit. This code will be inserted into the Execute method of a template with access to Document and parameters. | |
| parameters | No | Optional execution parameters that will be passed to your code |
Implementation Reference
- src/tools/send_code_to_revit.ts:22-57 (handler)The handler function for the 'send_code_to_revit' tool. It prepares parameters, sends the code to the Revit client using withRevitConnection, and returns a success or error message.async (args, extra) => { const params = { code: args.code, parameters: args.parameters || [], }; try { const response = await withRevitConnection(async (revitClient) => { return await revitClient.sendCommand("send_code_to_revit", params); }); return { content: [ { type: "text", text: `Code execution successful!\nResult: ${JSON.stringify( response, null, 2 )}`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Code execution failed: ${ error instanceof Error ? error.message : String(error) }`, }, ], }; } }
- src/tools/send_code_to_revit.ts:9-21 (schema)Input schema for the tool using Zod: required 'code' string and optional 'parameters' array.{ code: z .string() .describe( "The C# code to execute in Revit. This code will be inserted into the Execute method of a template with access to Document and parameters." ), parameters: z .array(z.any()) .optional() .describe( "Optional execution parameters that will be passed to your code" ), },
- src/tools/send_code_to_revit.ts:5-59 (registration)Function that registers the 'send_code_to_revit' tool on the MCP server, including name, description, input schema, and handler.export function registerSendCodeToRevitTool(server: McpServer) { server.tool( "send_code_to_revit", "Send C# code to Revit for execution. The code will be inserted into a template with access to the Revit Document and parameters. Your code should be written to work within the Execute method of the template.", { code: z .string() .describe( "The C# code to execute in Revit. This code will be inserted into the Execute method of a template with access to Document and parameters." ), parameters: z .array(z.any()) .optional() .describe( "Optional execution parameters that will be passed to your code" ), }, async (args, extra) => { const params = { code: args.code, parameters: args.parameters || [], }; try { const response = await withRevitConnection(async (revitClient) => { return await revitClient.sendCommand("send_code_to_revit", params); }); return { content: [ { type: "text", text: `Code execution successful!\nResult: ${JSON.stringify( response, null, 2 )}`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Code execution failed: ${ error instanceof Error ? error.message : String(error) }`, }, ], }; } } ); }