Logging strategy updates

This commit is contained in:
Cyber MacGeddon 2025-07-30 21:59:33 +01:00
parent 851c0cd9d2
commit bddb5f4cbc
4 changed files with 41 additions and 34 deletions

View file

@ -5,11 +5,15 @@ name + parameters, output is the response, either a string or an object.
""" """
import json import json
import logging
from mcp.client.streamable_http import streamablehttp_client from mcp.client.streamable_http import streamablehttp_client
from mcp import ClientSession from mcp import ClientSession
from ... base import ToolService from ... base import ToolService
# Module logger
logger = logging.getLogger(__name__)
default_ident = "mcp-tool" default_ident = "mcp-tool"
class Service(ToolService): class Service(ToolService):
@ -26,7 +30,7 @@ class Service(ToolService):
async def on_mcp_config(self, config, version): 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 if "mcp" not in config: return
@ -52,7 +56,7 @@ class Service(ToolService):
else: else:
remote_name = name 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 # Connect to a streamable HTTP server
async with streamablehttp_client(url) as ( async with streamablehttp_client(url) as (
@ -86,13 +90,13 @@ class Service(ToolService):
except BaseExceptionGroup as e: except BaseExceptionGroup as e:
for child in e.exceptions: for child in e.exceptions:
print(child) logger.debug(f"Child: {child}")
raise e.exceptions[0] raise e.exceptions[0]
except Exception as e: except Exception as e:
print(e) logger.error(f"Error invoking MCP tool: {e}", exc_info=True)
raise e raise e
@staticmethod @staticmethod

View file

@ -164,18 +164,18 @@ class AgentManager:
async def reason(self, question, history, context): async def reason(self, question, history, context):
print(f"calling reason: {question}", flush=True) logger.debug(f"calling reason: {question}")
tools = self.tools tools = self.tools
print(f"in reason", flush=True) logger.debug("in reason")
print(tools, flush=True) logger.debug(f"tools: {tools}")
tool_names = ",".join([ tool_names = ",".join([
t for t in self.tools.keys() t for t in self.tools.keys()
]) ])
print("Tool names:", tool_names, flush=True) logger.debug(f"Tool names: {tool_names}")
variables = { variables = {
"question": question, "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}") logger.info(f"prompt: {variables}")
# Get text response from prompt service # Get text response from prompt service
response_text = await context("prompt-request").agent_react(variables) 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}") logger.info(f"response: {response_text}")
@ -233,7 +233,6 @@ class AgentManager:
async def react(self, question, history, think, observe, context): async def react(self, question, history, think, observe, context):
logger.info(f"question: {question}") logger.info(f"question: {question}")
print(f"question: {question}", flush=True)
act = await self.reason( act = await self.reason(
question = question, question = question,
@ -256,7 +255,7 @@ class AgentManager:
else: else:
raise RuntimeError(f"No action for {act.name}!") raise RuntimeError(f"No action for {act.name}!")
print("TOOL>>>", act, flush=True) logger.debug(f"TOOL>>> {act}")
resp = await action.implementation(context).invoke( resp = await action.implementation(context).invoke(
**act.arguments **act.arguments

View file

@ -8,7 +8,7 @@ import sys
import functools import functools
import logging import logging
logging.basicConfig(level=logging.DEBUG) # Module logger
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
from ... base import AgentService, TextCompletionClientSpec, PromptClientSpec from ... base import AgentService, TextCompletionClientSpec, PromptClientSpec
@ -81,7 +81,7 @@ class Processor(AgentService):
async def on_tools_config(self, config, version): async def on_tools_config(self, config, version):
print("Loading configuration version", version) logger.info(f"Loading configuration version {version}")
try: try:
@ -151,13 +151,13 @@ class Processor(AgentService):
additional_context=additional additional_context=additional
) )
print(f"Loaded {len(tools)} tools", flush=True) logger.info(f"Loaded {len(tools)} tools")
print("Tool configuration reloaded.", flush=True) logger.info("Tool configuration reloaded.")
except Exception as e: except Exception as e:
print("on_tools_config Exception:", e, flush=True) logger.error(f"on_tools_config Exception: {e}", exc_info=True)
print("Configuration reload failed", flush=True) logger.error("Configuration reload failed")
async def agent_request(self, request, respond, next, flow): async def agent_request(self, request, respond, next, flow):
@ -176,16 +176,16 @@ class Processor(AgentService):
else: else:
history = [] history = []
print(f"Question: {request.question}", flush=True) logger.info(f"Question: {request.question}")
if len(history) >= self.max_iterations: if len(history) >= self.max_iterations:
raise RuntimeError("Too many agent iterations") raise RuntimeError("Too many agent iterations")
print(f"History: {history}", flush=True) logger.debug(f"History: {history}")
async def think(x): async def think(x):
print(f"Think: {x}", flush=True) logger.debug(f"Think: {x}")
r = AgentResponse( r = AgentResponse(
answer=None, answer=None,
@ -198,7 +198,7 @@ class Processor(AgentService):
async def observe(x): async def observe(x):
print(f"Observe: {x}", flush=True) logger.debug(f"Observe: {x}")
r = AgentResponse( r = AgentResponse(
answer=None, answer=None,
@ -209,7 +209,7 @@ class Processor(AgentService):
await respond(r) await respond(r)
print("Call React", flush=True) logger.debug("Call React")
act = await self.agent.react( act = await self.agent.react(
question = request.question, question = request.question,
@ -219,11 +219,11 @@ class Processor(AgentService):
context = flow, context = flow,
) )
print(f"Action: {act}", flush=True) logger.debug(f"Action: {act}")
if isinstance(act, Final): if isinstance(act, Final):
print("Send final response...", flush=True) logger.debug("Send final response...")
if isinstance(act.final, str): if isinstance(act.final, str):
f = act.final f = act.final
@ -238,11 +238,11 @@ class Processor(AgentService):
await respond(r) await respond(r)
print("Done.", flush=True) logger.debug("Done.")
return return
print("Send next...", flush=True) logger.debug("Send next...")
history.append(act) history.append(act)
@ -269,9 +269,9 @@ class Processor(AgentService):
except Exception as e: 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( r = AgentResponse(
error=Error( error=Error(

View file

@ -1,7 +1,11 @@
import json import json
import logging
from .types import Argument from .types import Argument
# Module logger
logger = logging.getLogger(__name__)
# This tool implementation knows how to put a question to the graph RAG # This tool implementation knows how to put a question to the graph RAG
# service # service
class KnowledgeQueryImpl: class KnowledgeQueryImpl:
@ -21,7 +25,7 @@ class KnowledgeQueryImpl:
async def invoke(self, **arguments): async def invoke(self, **arguments):
client = self.context("graph-rag-request") client = self.context("graph-rag-request")
print("Graph RAG question...", flush=True) logger.debug("Graph RAG question...")
return await client.rag( return await client.rag(
arguments.get("question") arguments.get("question")
) )
@ -44,7 +48,7 @@ class TextCompletionImpl:
async def invoke(self, **arguments): async def invoke(self, **arguments):
client = self.context("prompt-request") client = self.context("prompt-request")
print("Prompt question...", flush=True) logger.debug("Prompt question...")
return await client.question( return await client.question(
arguments.get("question") arguments.get("question")
) )
@ -67,13 +71,13 @@ class McpToolImpl:
client = self.context("mcp-tool-request") 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( output = await client.invoke(
name = self.mcp_tool_id, name = self.mcp_tool_id,
parameters = arguments, # Pass the actual arguments parameters = arguments, # Pass the actual arguments
) )
print(output) logger.debug(f"MCP tool output: {output}")
if isinstance(output, str): if isinstance(output, str):
return output return output
@ -94,7 +98,7 @@ class PromptImpl:
async def invoke(self, **arguments): async def invoke(self, **arguments):
client = self.context("prompt-request") 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( return await client.prompt(
id=self.template_id, id=self.template_id,
variables=arguments variables=arguments