From 77e5a9ca6da68fe89ac7d43f5629f6c77f523bfa Mon Sep 17 00:00:00 2001 From: Vicente Adolfo Bolea Sanchez Date: Thu, 4 Jun 2026 11:46:42 -0400 Subject: [PATCH] Auto-translate natural language prompts to DSL via vtk-mcp When a vtk-mcp server is available, the client now calls is_dsl_prompt() before code generation; if the input is plain natural language it calls translate_prompt_to_dsl() to convert it to the structured VTK pipeline DSL before RAG retrieval and generation. VTKMCPClient.translate_prompt() accepts optional model, base_url, and api_key overrides so callers can direct the translation to a specific model or Ollama endpoint without changing server config. --- src/vtk_prompt/client.py | 11 ++++++++++- src/vtk_prompt/vtk_mcp_client.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/vtk_prompt/client.py b/src/vtk_prompt/client.py index ad5bdae..d70e505 100644 --- a/src/vtk_prompt/client.py +++ b/src/vtk_prompt/client.py @@ -356,7 +356,16 @@ def query( } ) else: - # Normal path: build context and prompt + # Normal path: translate to DSL if needed, then build context and prompt + if mcp_client: + is_dsl = mcp_client._call_tool("is_dsl_prompt", {"text": message}) + if is_dsl not in ("true", "True", True): + translated = mcp_client.translate_prompt(message) + if translated: + if self.verbose: + logger.debug("DSL translation:\n%s", translated) + message = translated + context_snippets = None if mcp_client: mcp_context = mcp_client.get_enriched_context(message, top_k=top_k) diff --git a/src/vtk_prompt/vtk_mcp_client.py b/src/vtk_prompt/vtk_mcp_client.py index e31cf63..a80f2f4 100644 --- a/src/vtk_prompt/vtk_mcp_client.py +++ b/src/vtk_prompt/vtk_mcp_client.py @@ -180,6 +180,36 @@ def validate_code(self, code: str) -> str | None: except Exception: return None + def translate_prompt( + self, + query: str, + model: str | None = None, + base_url: str | None = None, + api_key: str | None = None, + ) -> str | None: + """Translate a natural language query into the VTK pipeline DSL. + + Args: + query: Natural language prompt. + model: LiteLLM model override (e.g. ``ollama/llama3``). + base_url: Base URL for OpenAI-compatible endpoints (e.g. Ollama). + api_key: API key for the endpoint. + + Returns the DSL string, or None if the tool call fails. + """ + args: dict = {"query": query} + if model: + args["model"] = model + if base_url: + args["base_url"] = base_url + if api_key: + args["api_key"] = api_key + result = self._call_tool("translate_prompt_to_dsl", args) + if not result or result.startswith("Error:"): + logger.warning("DSL translation failed: %s", result) + return None + return result + def get_enriched_context(self, query: str, top_k: int = 5) -> str: """Build context for the LLM combining code examples, docs, and VTK class hints.""" parts = []