# ⚡ Quick Email Demo (Simple) - Template Middleware Resource Resolution
*Request ID: {{ request_id }} | Generated: {{ current_time }}*

## 🎯 Template Parameter Middleware Integration Demo

### Features
- **Template Middleware**: Uses same resource resolution as tool parameters
- **Resource Variables**: Direct `{{resource://uri}}` resolution
- **Live Integration**: Real Gmail data via Template Parameter Middleware
- **Unified System**: Same resolution system for tools AND prompts

### 📊 **TEMPLATE MIDDLEWARE RESOURCE RESOLUTION:**
- **Resolution Method**: {{ auth_status }}
- **Your Email**: `{{ user_email }}`
- **Resource System**: Template Parameter Middleware (same as tools)
{% if labels %}
- **Your Gmail Labels** (via middleware resolution):
{% for label in labels[:3] %}
  - 📧 {{ label.name }} (ID: {{ label.id }})
{% endfor %}
{% else %}
- **Labels**: Authentication required - run `start_google_auth('your.email@gmail.com')`
{% endif %}
- **System Labels**: {{ system_count }} (resolved via middleware)
- **Custom Labels**: {{ user_count }} (resolved via middleware)

### 🔧 **RESOURCE RESOLUTION DEMONSTRATION:**

This prompt demonstrates Template Parameter Middleware resource resolution:

```yaml
Resource Resolution Pipeline:
  1. Prompt Template Contains: "{{user://current/email}}"
  2. Template Middleware Detects: Resource URI pattern
  3. Middleware Resolves: FastMCP resource system
  4. Result Injected: {{ user_email }}
  5. Template Renders: Final content with resolved data

Resolved Resources:
  - user://current/email → "{{ user_email }}"
  - service://gmail/labels → {{ user_count + system_count }} total labels
  - Authentication Status → {{ auth_status }}
```

### 🎭 **MIDDLEWARE RESOURCE CONTEXT:**

```xml
<template_middleware_demo>
  <resolution_status>{{ auth_status }}</resolution_status>
  <resources_resolved>
    <user_email source="user://current/email">{{ user_email }}</user_email>
    <gmail_labels source="service://gmail/labels" count="{{ user_count + system_count }}">
      {% for label in labels[:5] %}
      <label id="{{ label.id }}" name="{{ label.name }}" type="user"/>
      {% endfor %}
    </gmail_labels>
  </resources_resolved>
  <middleware_integration>true</middleware_integration>
</template_middleware_demo>
```

## 📧 **Template Middleware In Action: Tool Call with Resource Resolution**

This demonstrates how `{{user://current/email}}` gets resolved by Template Parameter Middleware in BOTH the prompt AND the tool parameters:

```xml
<mcp_tool_call>
  <tool>send_gmail_message</tool>
  <params>
    <!-- This resource variable gets resolved by Template Parameter Middleware: -->
    <user_google_email>{{user://current/email}}</user_google_email>
    <to>demo@example.com</to>
    <subject>Template Middleware Demo - {{ current_time[:10] }}</subject>
    <body><![CDATA[
Hello!

This email demonstrates Template Parameter Middleware resource resolution working in both:
1. PROMPT templates (what you're reading now)
2. TOOL parameters (this email's user_google_email field)

📊 Middleware-Resolved Data in This Prompt:
• Resolved Email: {{ user_email }} (from {{user://current/email}})
• Resolution Status: {{ auth_status }}
{% for label in labels[:5] %}
• Gmail Label: {{ label.name }} (ID: {{ label.id }})
{% endfor %}
• System Labels: {{ system_count }} • Custom Labels: {{ user_count }}

🔧 How Template Middleware Works:
1. Detects {{resource://uri}} patterns in tool parameters
2. Resolves via FastMCP resource system
3. Substitutes resolved values before tool execution
4. Same system now works for prompt templates!

✅ Benefits:
• Unified resource resolution system
• Same {{resource://uri}} syntax everywhere
• Automatic resolution for both tools and prompts
• No manual resource fetching needed

Best regards,
Template Parameter Middleware Demo
    ]]></body>
  </params>
</mcp_tool_call>
```

## 🎯 **Key Middleware Integration Points**

### 🔄 **Resource Resolution Flow:**
1. **Prompt Render**: This template resolves `user://current/email` → `{{ user_email }}`
2. **Tool Parameter**: The tool call above also resolves `{{user://current/email}}`
3. **Same System**: Both use Template Parameter Middleware
4. **Unified Experience**: Consistent behavior across prompts and tools

### ⚡ **Template Middleware Benefits:**
- **Consistent**: Same `{{resource://uri}}` syntax for prompts and tools
- **Automatic**: No manual resource fetching required
- **Cached**: Resources cached across multiple resolutions
- **Reliable**: Fallback handling for auth/connection issues

### 🛠️ **Before vs After:**

**Before** (manual resource fetching):
```python
gmail_data = await context.read_resource("service://gmail/labels")
user_email = await context.read_resource("user://current/email")
# Manual processing and template context building...
```

**After** (Template Middleware):
```python
# Resources automatically resolved in template:
# {{user://current/email}} → actual email
# {{service://gmail/labels}} → actual labels
# No manual fetching needed!
```

{% if not authenticated %}
### 🔧 **Setup Required**
To see your real Gmail data: `start_google_auth('your.email@gmail.com')`
{% endif %}

---
*Template Parameter Middleware Integration Demo • Same system for tools AND prompts!*