active_view
Activate and open views in Autodesk Revit by providing view element IDs, with support for single or multiple views and automatic validation of view elements.
Instructions
激活并打开Revit中的视图,遵循JSON-RPC 2.0规范。 mcp_tool使用时params不要有任何注释信息
特性:
支持打开单个或多个视图
自动验证视图元素有效性
过滤模板视图
完善的错误处理机制
参数: ctx (Context): FastMCP上下文对象 method (str): JSON-RPC方法名,默认为"ActiveView" params (List[Dict]): 视图参数列表,每个字典包含: - elementId (Union[int, str]): 视图元素ID
返回: dict: JSON-RPC 2.0格式的响应,结构为: 成功时: { "jsonrpc": "2.0", "result": [ { "elementId": "视图元素ID", "name": "视图名称", "familyName": "视图族名称" }, ... ], "id": request_id } 失败时: { "jsonrpc": "2.0", "error": { "code": int, "message": str, "data": any }, "id": request_id }
错误代码: -32600: 无效请求 -32602: 无效参数(元素不是视图/是模板视图/无效元素) -32603: 内部错误 -32700: 解析错误
示例: # 激活单个视图 response = active_view(ctx, params=[{"elementId": 123456}])
注意: 1. 无法激活模板视图(会返回错误) 2. 如果传入多个视图ID,会依次尝试激活,最后一个成功的视图将成为当前视图 3. 返回的列表包含所有成功激活的视图ID
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| method | No | ActiveView | |
| params | No |
Implementation Reference
- xml_revit_mcp/server.py:50-55 (registration)The active_view tool is included in the GENERAL_TOOLS list, which contains general purpose Revit tools registered to the MCP server.GENERAL_TOOLS = [ get_commands, execute_commands, call_func, find_elements, update_elements, delete_elements, parameter_elements, get_locations, move_elements, show_elements, active_view, get_selected_elements, link_dwg_and_activate_view, get_view_data ]
- xml_revit_mcp/server.py:144-146 (registration)Registration loop that applies the MCP server.tool() decorator to all functions in GENERAL_TOOLS, including active_view.# 注册通用工具 for tool in GENERAL_TOOLS: server.tool()(tool)
- tests/ActiveView.py:19-20 (helper)Test script demonstrating the RPC call to 'ActiveView' method, likely corresponding to the active_view MCP tool.send_tcp_data(json_rpc_request)
- The send_command method in RevitConnection is used by tool handlers to send RPC commands to Revit, including for active_view which likely calls 'ActiveView' RPC.def send_command(self, command_type: str, params: Union[Dict[str, Any], List[Dict[str, Any]]] = None) -> Dict[ str, Any]: """ 向Revit发送命令并返回响应 参数: command_type (str): 命令类型 params (Dict[str, Any] 或 List[Dict[str, Any]]): 命令参数 返回: Dict[str, Any]: 命令响应 异常: ConnectionError: 连接错误 TimeoutError: 请求超时 ValueError: 参数或响应无效 Exception: 其他错误 """ # 确保连接 if not self.sock and not self.connect(): raise ConnectionError("无法连接到Revit") try: logger.info(f"发送命令: {command_type}") logger.debug(f"命令参数: {params}") # 导入并创建请求对象 from .rpc import JsonRPCRequest, JsonRPCResponse command = JsonRPCRequest(method=command_type, params=params) command_json = json.dumps(command.__dict__) # 发送命令 self.sock.sendall(command_json.encode('utf-8')) logger.debug("命令已发送,等待响应...") # 使用 receive_full_response 接收完整数据流 response_data = self.receive_full_response() logger.debug(f"已接收 {len(response_data)} 字节数据") # 解析响应 try: response_dict = json.loads(response_data.decode('utf-8')) response = JsonRPCResponse( id=response_dict.get("id"), result=response_dict.get("result"), error=response_dict.get("error") ) except json.JSONDecodeError as e: logger.error(f"无法解析Revit响应: {str(e)}") if response_data: logger.error(f"原始响应 (前200字节): {response_data[:200]}") raise ValueError(f"无效的Revit响应: {str(e)}") # 处理错误 if response.error: error_message = response.error.get("message", "未知错误") error_code = response.error.get("code", -1) error_data = response.error.get("data") logger.error(f"Revit错误 (代码: {error_code}): {error_message}") if error_data: logger.error(f"错误数据: {error_data}") raise Exception(f"Revit错误: {error_message}") return response.result or {}