# 에이전트 초기화 흐름

`TroubleshootingAgent` 클래스(`agent_config/agent.py`)의 초기화 과정입니다.

## 초기화 순서

```mermaid
graph TD
    S1["1. BedrockModel 초기화<br/><i>model_id: claude-sonnet-4</i>"]
    S2["2. System Prompt 설정<br/><i>도구 설명 / 워크플로우 규칙 / 권한 검증 규칙</i>"]
    S3["3. Gateway URL 조회<br/><i>SSM: /app/.../gateway_url</i>"]
    S4["4. MCP Client 초기화<br/><i>streamablehttp_client + Bearer Token</i>"]
    S5["5. MCP 도구 목록 조회<br/><i>list_tools_sync()</i>"]
    S6["6. Agent 생성<br/><i>model + system_prompt + tools + hooks</i>"]

    S1 --> S2 --> S3 --> S4 --> S5 --> S6

    style S1 fill:#232F3E,stroke:#FF9900,color:#fff
    style S2 fill:#232F3E,stroke:#FF9900,color:#fff
    style S3 fill:#527FFF,stroke:#fff,color:#fff
    style S4 fill:#527FFF,stroke:#fff,color:#fff
    style S5 fill:#3F8624,stroke:#fff,color:#fff
    style S6 fill:#FF9900,stroke:#232F3E,color:#232F3E
```

## 핵심 코드

```python
class TroubleshootingAgent:
    def __init__(self, bearer_token, memory_hook=None,
                 bedrock_model_id="global.anthropic.claude-sonnet-4-20250514-v1:0"):
        self.model = BedrockModel(model_id=self.model_id)

        # Gateway URL 조회
        gateway_url = get_ssm_parameter("/app/troubleshooting/agentcore/gateway_url")

        self.tools = [current_time]

        # MCP 클라이언트 초기화
        if gateway_url and bearer_token != "dummy":
            self.gateway_client = MCPClient(
                lambda: streamablehttp_client(
                    gateway_url,
                    headers={"Authorization": f"Bearer {bearer_token}"},
                )
            )
            self.gateway_client.start()
            mcp_tools = self.gateway_client.list_tools_sync()
            self.tools.extend(mcp_tools)

        # Agent 생성
        self.agent = Agent(
            model=self.model,
            system_prompt=self.system_prompt,
            tools=self.tools,
            hooks=[self.memory_hook] if self.memory_hook else [],
        )
```

## MCP 클라이언트 연결

| 구성 요소                   | 역할                          |
| ----------------------- | --------------------------- |
| `MCPClient`             | MCP 프로토콜 클라이언트 래퍼           |
| `streamablehttp_client` | HTTP 기반 스트리밍 연결             |
| `gateway_url`           | AgentCore Gateway 엔드포인트     |
| `Bearer Token`          | Cognito에서 발급받은 Access Token |

## System Prompt의 도구 사용 규칙

```
**CRITICAL DATABASE CONNECTIVITY RULES:**
- Source: ALWAYS use EC2 instance ID (e.g., i-1234567890abcdef0) - NEVER use IPs
- Database Destination: If "database" in hostname, ALWAYS use resolved IP address
- Port: Database connections default to port 3306 (MySQL)
- Protocol: Use TCP for database connections
- CRITICAL: NEVER use action="fix" without explicit user consent
```
