# Craft the core prompt for the LLM, injecting relevant history and state
history_text = "\n".join([f"{msg['sender_type']} ({msg['sender_id']}) [{msg['type']}]: {msg['content']}" for msg in history])
state_text = json.dumps(shared_state, indent=2)
prompt = f"""
You are a {self.agent_type} with ID {self.id}. Your current task context (ID: {self.task_id}) is managed by the Master Context Protocol Service.
Review the task history and shared state below. Determine your next step to contribute to the overall R&D goal defined in the shared state ('initial_goal').
Your actions should be one of the following types: 'tool_call', 'update_state', 'delegate', 'report_completion', 'ask_human_for_help'.
Output your thought process first, then your chosen action in JSON format. If no action is needed now, explain why.
Current Shared State:
{state_text}
Task History:
{history_text}
Available Tools: {list(self.tools.keys())}
Think step-by-step. What is the current situation? What needs to be done next based on the goal and state? Which action is most appropriate?