CtrEditor/Documentation/MCP/MCP_LLM_Guide.md

14 KiB

CtrEditor MCP Server - LLM Guide

MCP 2025-06-18 compliant | Compatible: Claude Desktop + Cursor

🎯 September 2025 Update - CPython Migration Completed

IronPython Elimination Complete:

  • Migrated from IronPython to CPython for unified Python environment
  • Now uses the same TSNet Python environment across all components
  • Improved performance and reduced dependencies
  • Async Python execution with proper timeout handling
  • Enhanced error handling and process management

System Architecture:

  • Python Runtime: CPython 3.12 (shared with TSNet)
  • Execution Method: Process-based (via temporary scripts)
  • Environment Path: D:\Proyectos\VisualStudio\CtrEditor\bin\Debug\net8.0-windows8.0\tsnet
  • Integration: PythonInterop.cs for seamless C#-Python communication

Command Efficiency Tiers

🚀 Ultra-Fast (Use Liberally)

  • get_simulation_status → ~25 tokens, instant
  • get_ctreditor_status → ~20 tokens, instant
  • start/stop_simulation → ~15 tokens, 2-3 sec

🟡 Medium (Use When Needed)

  • get_debug_stats → ~30 tokens, instant
  • search_debug_log → 50-200 tokens (max_lines=3-10)
  • build_project → 100-500 tokens (errors only)
  • execute_python → 50-500 tokens (depends on script)
  • execute_tsnet_direct → ~50 tokens, 3-5 sec (bypasses Python)
  • python_help → 100-300 tokens
  • clear_debug_buffer → ~20 tokens, instant

🔴 Heavy (Use Sparingly)

  • list_objects → 500-2000+ tokens
  • take_screenshot → 100-300 tokens + file

🔧 Core Operations

Process Management

{"tool": "start_ctreditor", "parameters": {}}
{"tool": "get_ctreditor_status", "parameters": {}}
{"tool": "stop_ctreditor", "parameters": {}}

Build & Debug

Sistema de debug logging circular completamente refactorizado y validado

  • Buffer circular automático: mantiene últimos 1000 eventos
  • Captura en tiempo real: System.Diagnostics.Trace integration
  • Timer de limpieza automática: previene memory leaks
  • Thread-safe: ConcurrentQueue para alta concurrencia
  • Performance validada: maneja miles de eventos sin degradación
{"tool": "build_project", "parameters": {}}
{"tool": "get_debug_stats", "parameters": {}}
{"tool": "search_debug_log", "parameters": {"pattern": "error|exception", "max_lines": 5}}
{"tool": "search_debug_log", "parameters": {"pattern": "Tanque.*Nivel|PIPE.*Apply", "max_lines": 10}}
{"tool": "clear_debug_buffer", "parameters": {}}

Simulation Control

{"tool": "get_simulation_status", "parameters": {}}
{"tool": "start_simulation", "parameters": {}}
{"tool": "stop_simulation", "parameters": {}}
{"tool": "execute_tsnet_direct", "parameters": {}}

TSNet Direct Execution NEW September 2025

Revolutionary bypass of Python mocks for real C# object access:

  • Purpose: Execute TSNet hydraulic simulation directly using real C# objects
  • Bypass: Eliminates Python mock objects and validation issues
  • Speed: 3-5 seconds (faster than Python-based execution)
  • Validation: Uses real tank configurations, not serialized data
  • Debug: Enhanced validation logging with detailed value inspection
{"tool": "execute_tsnet_direct", "parameters": {}}

Key Benefits:

  • Real Objects: Direct access to C# osHydTank, osHydPump objects
  • No Mocks: Bypasses Python serialization limitations
  • Enhanced Debugging: Detailed validation logs with actual values
  • Faster Execution: Native C# method calls vs Python interop
  • Accurate Validation: Uses real CurrentLevelM, MinLevelM, MaxLevelM values

Object Management

{"tool": "list_objects", "parameters": {}}
{"tool": "create_object", "parameters": {"type": "osHydPump", "x": 10, "y": 5}}
{"tool": "update_object", "parameters": {"id": "123", "properties": {"PumpHead": 75.0}}}
{"tool": "delete_objects", "parameters": {"ids": ["123", "456"]}}

Python Debug Scripts ENHANCED

Post-Migration Benefits (September 2025):

  • Unified Environment: Same CPython used for TSNet and MCP
  • Better Performance: Process-based execution with proper isolation
  • Enhanced Reliability: Async execution with timeout protection
  • Improved Error Handling: Clear error messages and stack traces
{"tool": "execute_python", "parameters": {"code": "print(f'Total objects: {len(objects)}')"}}
{"tool": "execute_python", "parameters": {
  "code": "result = [obj for obj in objects if 'Pump' in str(type(obj))]",
  "return_variables": ["result"]
}}
{"tool": "python_help", "parameters": {"object_name": "app"}}

Optimal Workflows

Quick Development Cycle

{"tool": "get_ctreditor_status", "parameters": {}}
{"tool": "build_project", "parameters": {}}
{"tool": "start_simulation", "parameters": {}}
{"tool": "execute_python", "parameters": {"code": "print(f'Objects: {len(objects)}, Running: {app.IsSimulationRunning}')"}}
{"tool": "get_simulation_status", "parameters": {}}

TSNet Development & Testing NEW

{"tool": "get_ctreditor_status", "parameters": {}}
{"tool": "execute_tsnet_direct", "parameters": {}}
{"tool": "search_debug_log", "parameters": {"pattern": "VALIDACION ERROR|TSNet Direct|InitialLevelM", "max_lines": 5}}
{"tool": "get_simulation_status", "parameters": {}}

Bug Investigation & Performance Testing

{"tool": "get_debug_stats", "parameters": {}}
{"tool": "search_debug_log", "parameters": {"pattern": "error|exception|fail", "max_lines": 5}}
{"tool": "search_debug_log", "parameters": {"pattern": "MCP Server.*herramienta", "max_lines": 3}}
{"tool": "execute_python", "parameters": {"code": "print([type(obj).__name__ for obj in objects[:5]])"}}
{"tool": "get_simulation_status", "parameters": {}}

Debug Log Stress Testing 🔥 NEW

{"tool": "start_simulation", "parameters": {}}
{"tool": "get_debug_stats", "parameters": {}}
{"tool": "search_debug_log", "parameters": {"pattern": "Tanque.*Nivel|Bomba.*MaxFlow", "max_lines": 10}}
{"tool": "stop_simulation", "parameters": {}}
{"tool": "get_debug_stats", "parameters": {}}

System Recovery

{"tool": "stop_ctreditor", "parameters": {}}
{"tool": "start_ctreditor", "parameters": {}}

📊 Key Object Properties

Python Debug Variables (CPython Environment)

  • app: MainViewModel (simulation state, canvas, objects)
  • canvas: MainCanvas (Width, Height, visual elements)
  • objects: ObservableCollection of all simulable objects
  • get_objects(): Returns List