From bddb5f4cbc1856016d1cfc7aa138d5edcf57651e Mon Sep 17 00:00:00 2001 From: Cyber MacGeddon Date: Wed, 30 Jul 2025 21:59:33 +0100 Subject: [PATCH] Logging strategy updates --- .../trustgraph/agent/mcp_tool/service.py | 12 ++++--- .../trustgraph/agent/react/agent_manager.py | 15 ++++---- .../trustgraph/agent/react/service.py | 34 +++++++++---------- .../trustgraph/agent/react/tools.py | 14 +++++--- 4 files changed, 41 insertions(+), 34 deletions(-) diff --git a/trustgraph-flow/trustgraph/agent/mcp_tool/service.py b/trustgraph-flow/trustgraph/agent/mcp_tool/service.py index 9f8d5eee..96ff73f7 100755 --- a/trustgraph-flow/trustgraph/agent/mcp_tool/service.py +++ b/trustgraph-flow/trustgraph/agent/mcp_tool/service.py @@ -5,11 +5,15 @@ name + parameters, output is the response, either a string or an object. """ import json +import logging from mcp.client.streamable_http import streamablehttp_client from mcp import ClientSession from ... base import ToolService +# Module logger +logger = logging.getLogger(__name__) + default_ident = "mcp-tool" class Service(ToolService): @@ -26,7 +30,7 @@ class Service(ToolService): async def on_mcp_config(self, config, version): - print("Got config version", version) + logger.info(f"Got config version {version}") if "mcp" not in config: return @@ -52,7 +56,7 @@ class Service(ToolService): else: remote_name = name - print("Invoking", remote_name, "at", url, flush=True) + logger.info(f"Invoking {remote_name} at {url}") # Connect to a streamable HTTP server async with streamablehttp_client(url) as ( @@ -86,13 +90,13 @@ class Service(ToolService): except BaseExceptionGroup as e: for child in e.exceptions: - print(child) + logger.debug(f"Child: {child}") raise e.exceptions[0] except Exception as e: - print(e) + logger.error(f"Error invoking MCP tool: {e}", exc_info=True) raise e @staticmethod diff --git a/trustgraph-flow/trustgraph/agent/react/agent_manager.py b/trustgraph-flow/trustgraph/agent/react/agent_manager.py index 33b32216..2cf57827 100644 --- a/trustgraph-flow/trustgraph/agent/react/agent_manager.py +++ b/trustgraph-flow/trustgraph/agent/react/agent_manager.py @@ -164,18 +164,18 @@ class AgentManager: async def reason(self, question, history, context): - print(f"calling reason: {question}", flush=True) + logger.debug(f"calling reason: {question}") tools = self.tools - print(f"in reason", flush=True) - print(tools, flush=True) + logger.debug("in reason") + logger.debug(f"tools: {tools}") tool_names = ",".join([ t for t in self.tools.keys() ]) - print("Tool names:", tool_names, flush=True) + logger.debug(f"Tool names: {tool_names}") variables = { "question": question, @@ -208,14 +208,14 @@ class AgentManager: ] } - print(json.dumps(variables, indent=4), flush=True) + logger.debug(f"Variables: {json.dumps(variables, indent=4)}") logger.info(f"prompt: {variables}") # Get text response from prompt service response_text = await context("prompt-request").agent_react(variables) - print(f"Response text:\n{response_text}", flush=True) + logger.debug(f"Response text:\n{response_text}") logger.info(f"response: {response_text}") @@ -233,7 +233,6 @@ class AgentManager: async def react(self, question, history, think, observe, context): logger.info(f"question: {question}") - print(f"question: {question}", flush=True) act = await self.reason( question = question, @@ -256,7 +255,7 @@ class AgentManager: else: raise RuntimeError(f"No action for {act.name}!") - print("TOOL>>>", act, flush=True) + logger.debug(f"TOOL>>> {act}") resp = await action.implementation(context).invoke( **act.arguments diff --git a/trustgraph-flow/trustgraph/agent/react/service.py b/trustgraph-flow/trustgraph/agent/react/service.py index d2a0d41c..c962bd0b 100755 --- a/trustgraph-flow/trustgraph/agent/react/service.py +++ b/trustgraph-flow/trustgraph/agent/react/service.py @@ -8,7 +8,7 @@ import sys import functools import logging -logging.basicConfig(level=logging.DEBUG) +# Module logger logger = logging.getLogger(__name__) from ... base import AgentService, TextCompletionClientSpec, PromptClientSpec @@ -81,7 +81,7 @@ class Processor(AgentService): async def on_tools_config(self, config, version): - print("Loading configuration version", version) + logger.info(f"Loading configuration version {version}") try: @@ -151,13 +151,13 @@ class Processor(AgentService): additional_context=additional ) - print(f"Loaded {len(tools)} tools", flush=True) - print("Tool configuration reloaded.", flush=True) + logger.info(f"Loaded {len(tools)} tools") + logger.info("Tool configuration reloaded.") except Exception as e: - print("on_tools_config Exception:", e, flush=True) - print("Configuration reload failed", flush=True) + logger.error(f"on_tools_config Exception: {e}", exc_info=True) + logger.error("Configuration reload failed") async def agent_request(self, request, respond, next, flow): @@ -176,16 +176,16 @@ class Processor(AgentService): else: history = [] - print(f"Question: {request.question}", flush=True) + logger.info(f"Question: {request.question}") if len(history) >= self.max_iterations: raise RuntimeError("Too many agent iterations") - print(f"History: {history}", flush=True) + logger.debug(f"History: {history}") async def think(x): - print(f"Think: {x}", flush=True) + logger.debug(f"Think: {x}") r = AgentResponse( answer=None, @@ -198,7 +198,7 @@ class Processor(AgentService): async def observe(x): - print(f"Observe: {x}", flush=True) + logger.debug(f"Observe: {x}") r = AgentResponse( answer=None, @@ -209,7 +209,7 @@ class Processor(AgentService): await respond(r) - print("Call React", flush=True) + logger.debug("Call React") act = await self.agent.react( question = request.question, @@ -219,11 +219,11 @@ class Processor(AgentService): context = flow, ) - print(f"Action: {act}", flush=True) + logger.debug(f"Action: {act}") if isinstance(act, Final): - print("Send final response...", flush=True) + logger.debug("Send final response...") if isinstance(act.final, str): f = act.final @@ -238,11 +238,11 @@ class Processor(AgentService): await respond(r) - print("Done.", flush=True) + logger.debug("Done.") return - print("Send next...", flush=True) + logger.debug("Send next...") history.append(act) @@ -269,9 +269,9 @@ class Processor(AgentService): except Exception as e: - print(f"agent_request Exception: {e}") + logger.error(f"agent_request Exception: {e}", exc_info=True) - print("Send error response...", flush=True) + logger.debug("Send error response...") r = AgentResponse( error=Error( diff --git a/trustgraph-flow/trustgraph/agent/react/tools.py b/trustgraph-flow/trustgraph/agent/react/tools.py index 80b5ba9a..e1a2af85 100644 --- a/trustgraph-flow/trustgraph/agent/react/tools.py +++ b/trustgraph-flow/trustgraph/agent/react/tools.py @@ -1,7 +1,11 @@ import json +import logging from .types import Argument +# Module logger +logger = logging.getLogger(__name__) + # This tool implementation knows how to put a question to the graph RAG # service class KnowledgeQueryImpl: @@ -21,7 +25,7 @@ class KnowledgeQueryImpl: async def invoke(self, **arguments): client = self.context("graph-rag-request") - print("Graph RAG question...", flush=True) + logger.debug("Graph RAG question...") return await client.rag( arguments.get("question") ) @@ -44,7 +48,7 @@ class TextCompletionImpl: async def invoke(self, **arguments): client = self.context("prompt-request") - print("Prompt question...", flush=True) + logger.debug("Prompt question...") return await client.question( arguments.get("question") ) @@ -67,13 +71,13 @@ class McpToolImpl: client = self.context("mcp-tool-request") - print(f"MCP tool invocation: {self.mcp_tool_id}...", flush=True) + logger.debug(f"MCP tool invocation: {self.mcp_tool_id}...") output = await client.invoke( name = self.mcp_tool_id, parameters = arguments, # Pass the actual arguments ) - print(output) + logger.debug(f"MCP tool output: {output}") if isinstance(output, str): return output @@ -94,7 +98,7 @@ class PromptImpl: async def invoke(self, **arguments): client = self.context("prompt-request") - print(f"Prompt template invocation: {self.template_id}...", flush=True) + logger.debug(f"Prompt template invocation: {self.template_id}...") return await client.prompt( id=self.template_id, variables=arguments