mcp.ymlΒΆ

---
- name: "πŸ” Read the MCP token this instance already issued"
  ansible.builtin.set_fact:
    gitea_mcp_token: "{{ lookup('mcp_credential', application_id) }}"

- name: "πŸ” Resolve {{ application_id }} hosting node + container id"
  ansible.builtin.include_tasks: "{{ lookup('path_absolute', 'roles/sys-svc-compose/tasks/utils/swarm/resolve_host_cid.yml') }}"
  vars:
    resolve_service_key: "{{ entity_name }}"
    resolve_output_prefix: GITEA_API
    resolve_compose_cid: "{{ GITEA_CONTAINER_ADDRESS }}"

- name: "🩺 Check the stored token against the running instance"
  ansible.builtin.include_tasks: "{{ role_path }}/tasks/utils/mcp/probe.yml"
  when: gitea_mcp_token | length > 0

- name: "πŸ”‘ Issue the access token MCP clients present"
  ansible.builtin.include_tasks: "{{ role_path }}/tasks/utils/mcp/provision.yml"
  when: gitea_mcp_token | length == 0 or gitea_mcp_token_probe.rc | default(1) != 0

- name: "🩺 Re-check the freshly minted token"
  ansible.builtin.include_tasks: "{{ role_path }}/tasks/utils/mcp/probe.yml"
  when: gitea_mcp_token_probe is not defined or gitea_mcp_token_probe.rc != 0

- name: "πŸ›‘ Refuse an instance whose MCP token every client would be rejected with"
  ansible.builtin.assert:
    that:
      - gitea_mcp_token_probe.rc == 0
    fail_msg: >-
      Gitea rejected the token stored for {{ application_id }}, so every MCP
      client would be configured with a bearer the API answers 401 to.

- name: "🩺 Verify the upstream sidecar answers that bearer"
  ansible.builtin.include_tasks: "{{ role_path }}/tasks/utils/mcp/verify.yml"

- name: "πŸ”‘ Hand the minted upstream token to the adapter"
  ansible.builtin.include_tasks: "{{ lookup('path_absolute', 'roles/svc-ai-mcp-adapter/tasks/upstream_key.yml') }}"
  vars:
    mcp_upstream_key_path: "{{ [GITEA_MCP_INSTANCE_DIR, 'upstream.env'] | path_join }}"
    mcp_upstream_key: "{{ gitea_mcp_token }}"

- name: "🧱 Rebuild the MCP adapter of {{ application_id }} now the bearer exists"
  ansible.builtin.include_tasks: "{{ lookup('path_absolute', 'roles/svc-ai-mcp-adapter/tasks/rebuild.yml') }}"
  when: not mcp_upstream_written.changed

- name: "🚿 Flush compose handlers for {{ application_id }}"
  ansible.builtin.meta: flush_handlers