send_code_to_revit
Execute C# code within Autodesk Revit to automate tasks, modify elements, and interact with project data through the Revit MCP server.
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-58 (handler)The main handler function for the 'send_code_to_revit' tool. It prepares the parameters, uses withRevitConnection to send the command to the Revit client, and formats the success or error response.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 using Zod for the tool parameters: 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)Specific registration function for the 'send_code_to_revit' tool, called during dynamic tool loading. Includes tool name, description, 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) }`, }, ], }; } } ); }
- src/utils/ConnectionManager.ts:8-48 (helper)Helper utility to manage connection to Revit client on localhost:8080, execute an operation, and disconnect. Used by the tool handler.export async function withRevitConnection<T>( operation: (client: RevitClientConnection) => Promise<T> ): Promise<T> { const revitClient = new RevitClientConnection("localhost", 8080); try { // 连接到Revit客户端 if (!revitClient.isConnected) { await new Promise<void>((resolve, reject) => { const onConnect = () => { revitClient.socket.removeListener("connect", onConnect); revitClient.socket.removeListener("error", onError); resolve(); }; const onError = (error: any) => { revitClient.socket.removeListener("connect", onConnect); revitClient.socket.removeListener("error", onError); reject(new Error("connect to revit client failed")); }; revitClient.socket.on("connect", onConnect); revitClient.socket.on("error", onError); revitClient.connect(); setTimeout(() => { revitClient.socket.removeListener("connect", onConnect); revitClient.socket.removeListener("error", onError); reject(new Error("连接到Revit客户端失败")); }, 5000); }); } // 执行操作 return await operation(revitClient); } finally { // 断开连接 revitClient.disconnect(); } }
- src/tools/register.ts:6-48 (registration)Global tool registration that dynamically loads and invokes register functions from all tool files in src/tools, including send_code_to_revit.ts.export async function registerTools(server: McpServer) { // 获取当前文件的目录路径 const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); // 读取tools目录下的所有文件 const files = fs.readdirSync(__dirname); // 过滤出.ts或.js文件,但排除index文件和register文件 const toolFiles = files.filter( (file) => (file.endsWith(".ts") || file.endsWith(".js")) && file !== "index.ts" && file !== "index.js" && file !== "register.ts" && file !== "register.js" ); // 动态导入并注册每个工具 for (const file of toolFiles) { try { // 构建导入路径 const importPath = `./${file.replace(/\.(ts|js)$/, ".js")}`; // 动态导入模块 const module = await import(importPath); // 查找并执行注册函数 const registerFunctionName = Object.keys(module).find( (key) => key.startsWith("register") && typeof module[key] === "function" ); if (registerFunctionName) { module[registerFunctionName](server); console.error(`已注册工具: ${file}`); } else { console.warn(`警告: 在文件 ${file} 中未找到注册函数`); } } catch (error) { console.error(`注册工具 ${file} 时出错:`, error); } } }