diff --git a/roles/web-app-mattermost/README.md b/roles/web-app-mattermost/README.md index 19faa6c9f2..74c7988e88 100644 --- a/roles/web-app-mattermost/README.md +++ b/roles/web-app-mattermost/README.md @@ -44,6 +44,7 @@ flowchart LR svc_javascript["javascript"] svc_prometheus["prometheus"] svc_container_backup["container_backup"] + svc_mattermostmcp["mattermostmcp"] end subgraph dependents [Dependents] dpt_web_app_nextcloud["web-app-nextcloud 🐳🐝"] @@ -128,7 +129,7 @@ Mattermost exposes a Model Context Protocol server through the prepackaged Agent | Property | Value | |----------|-------| -| Endpoint | `/plugins/mattermost-ai/mcp-server/mcp` on the `mattermost` service, container port `services.mattermost.ports.internal.http` | +| Endpoint | `/mcp` on the `mattermostmcp` sidecar; the adapter reaches `/plugins/mattermost-ai/mcp-server/mcp` on the `mattermost` service upstream | | Health path | `/plugins/mattermost-ai/mcp-server/.well-known/oauth-protected-resource` | | Transport | `streamable_http` (stateless); SSE is not served | | Auth | `Authorization: Bearer ` | diff --git a/roles/web-app-mattermost/files/mcp/tools.json b/roles/web-app-mattermost/files/mcp/tools.json new file mode 100644 index 0000000000..2903994129 --- /dev/null +++ b/roles/web-app-mattermost/files/mcp/tools.json @@ -0,0 +1,90 @@ +{ + "get_channel_info": { + "description": "Get channel info.", + "input_schema": { + "properties": {}, + "type": "object" + }, + "mutating": false + }, + "get_channel_members": { + "description": "Get channel members.", + "input_schema": { + "properties": {}, + "type": "object" + }, + "mutating": false + }, + "get_team_info": { + "description": "Get team info.", + "input_schema": { + "properties": {}, + "type": "object" + }, + "mutating": false + }, + "get_team_members": { + "description": "Get team members.", + "input_schema": { + "properties": {}, + "type": "object" + }, + "mutating": false + }, + "get_user_channels": { + "description": "Get user channels.", + "input_schema": { + "properties": {}, + "type": "object" + }, + "mutating": false + }, + "list_agents": { + "description": "List agents.", + "input_schema": { + "properties": {}, + "type": "object" + }, + "mutating": false + }, + "read_channel": { + "description": "Read channel.", + "input_schema": { + "properties": {}, + "type": "object" + }, + "mutating": false + }, + "read_file": { + "description": "Read file.", + "input_schema": { + "properties": {}, + "type": "object" + }, + "mutating": false + }, + "read_post": { + "description": "Read post.", + "input_schema": { + "properties": {}, + "type": "object" + }, + "mutating": false + }, + "search_posts": { + "description": "Search posts.", + "input_schema": { + "properties": {}, + "type": "object" + }, + "mutating": false + }, + "search_users": { + "description": "Search users.", + "input_schema": { + "properties": {}, + "type": "object" + }, + "mutating": false + } +} diff --git a/roles/web-app-mattermost/files/playwright/test-mcp-guest.js b/roles/web-app-mattermost/files/playwright/test-mcp-guest.js index 95108c2abb..55192b95f4 100644 --- a/roles/web-app-mattermost/files/playwright/test-mcp-guest.js +++ b/roles/web-app-mattermost/files/playwright/test-mcp-guest.js @@ -2,7 +2,7 @@ const { test, expect } = require("@playwright/test"); const { skipUnlessServiceEnabled } = require("./service-gating"); -const MCP_ENDPOINT_PATH = "/plugins/mattermost-ai/mcp-server/mcp"; +const MCP_ENDPOINT_PATH = process.env.MCP_ENDPOINT_PATH; exports.register = function (shared) { test("guest: the MCP endpoint rejects unauthenticated access", async ({ page }) => { diff --git a/roles/web-app-mattermost/meta/mcp.yml b/roles/web-app-mattermost/meta/mcp.yml index 5df3a4cf23..42d9edb5fb 100644 --- a/roles/web-app-mattermost/meta/mcp.yml +++ b/roles/web-app-mattermost/meta/mcp.yml @@ -1,7 +1,7 @@ --- enabled: true shared: true -classification: plugin_server +classification: adapter_server direction: server source_url: https://docs.mattermost.com/agents/mcpserver/README.html minimum_version: "10.0" @@ -9,21 +9,49 @@ transport: streamable_http exposure: internal auth: bearer_token auth_subject: service_account -implementation: plugin +implementation: adapter credential: owner: mcp-web-app-mattermost - source: token_store - key: web-app-mattermost + source: credentials + key: mcp_bearer allowed_consumers: - web-app-openwebui - web-app-hermes - web-app-openclaw +adapter: + type: mcp_passthrough + image: python + version: "3.13-slim" + digest: sha256:9662417aace5ae7b8e2609cce472b72a8958e134ba372808abe9cc1a0c0125e6 + specification_path: roles/web-app-mattermost/files/mcp/tools.json + specification_sha256: sha256:0355a3990ff80867e972ccdddfae412a5d4d81da9b6aa51250649f275903ba15 endpoint: - service_key: mattermost - path: /plugins/mattermost-ai/mcp-server/mcp + service_key: mattermostmcp + path: /mcp port_key: http +limits: + request_bytes: 65536 + response_bytes: 1048576 + timeout_seconds: 15 + concurrent_requests: 4 + page_size: 100 + result_items: 100 + stream_seconds: 300 tools: + schema_sha256: sha256:1f3feea3ceeee056215289789666b5d2246a8c7482920c2f78c4be767a7a1c27 allowlist: + - get_channel_info + - get_channel_members + - get_team_info + - get_team_members + - get_user_channels + - list_agents + - read_channel + - read_file + - read_post + - search_posts + - search_users + writer_allowlist: - add_channel_member - create_channel - create_post diff --git a/roles/web-app-mattermost/meta/schema.yml b/roles/web-app-mattermost/meta/schema.yml new file mode 100644 index 0000000000..6bbe7a546c --- /dev/null +++ b/roles/web-app-mattermost/meta/schema.yml @@ -0,0 +1,8 @@ +--- +credentials: + mcp_bearer: + description: "Bearer an MCP client must present to this role's adapter instance. Distinct from the credential the adapter presents upstream: holding a token the + application accepts says nothing about who called the adapter." + algorithm: alphanumeric + validation: "^[A-Za-z0-9]{32,}$" + length: 48 diff --git a/roles/web-app-mattermost/meta/services.yml b/roles/web-app-mattermost/meta/services.yml index 78fb46177a..767ae36254 100644 --- a/roles/web-app-mattermost/meta/services.yml +++ b/roles/web-app-mattermost/meta/services.yml @@ -100,3 +100,17 @@ container_backup: # nocheck: bond-baseline backup host joins outside the variant bond: 1 enabled: "{{ 'svc-bkp-volume-2-local' in group_names }}" shared: true +# nocheck: dockerfile-custom the Dockerfile is owned by svc-ai-mcp-adapter and staged per instance, so every adapter-backed provider shares one audited build +mattermostmcp: + image: python + # nocheck: docker-version base of the self-built stdlib adapter, not the served application + version: "3.13-slim" + custom: true + name: mattermostmcp + cpus: "0.25" + mem_reservation: 16m + mem_limit: 24m + pids_limit: 64 + ports: + internal: + http: 8080 diff --git a/roles/web-app-mattermost/tasks/00_core.yml b/roles/web-app-mattermost/tasks/00_core.yml index ec7cd3022a..0574b70ec9 100644 --- a/roles/web-app-mattermost/tasks/00_core.yml +++ b/roles/web-app-mattermost/tasks/00_core.yml @@ -1,6 +1,13 @@ --- - include_tasks: utils/once/flag.yml +- name: "🔌 Stage the MCP adapter instance for {{ application_id }}" + ansible.builtin.include_tasks: "{{ lookup('path_absolute', 'roles/svc-ai-mcp-adapter/tasks/instance.yml') }}" + vars: + mcp_adapter_instance_dir: "{{ MATTERMOST_MCP_INSTANCE_DIR }}" + mcp_adapter_upstream_key: true + when: MATTERMOST_MCP_ENABLED | bool + - name: "🗑️ Remove stale nginx config for renamed domain chat.{{ DOMAIN_PRIMARY }}" # nocheck: domain-spot ansible.builtin.file: path: "{{ [lookup('nginx', 'directories.configuration.servers'), 'https', 'chat.' ~ DOMAIN_PRIMARY ~ '.conf'] | path_join }}" diff --git a/roles/web-app-mattermost/tasks/utils/mcp.yml b/roles/web-app-mattermost/tasks/utils/mcp.yml index 061bc4bdb0..29f79c5703 100644 --- a/roles/web-app-mattermost/tasks/utils/mcp.yml +++ b/roles/web-app-mattermost/tasks/utils/mcp.yml @@ -19,10 +19,22 @@ - name: "🔑 Hand MCP clients a bearer token" ansible.builtin.include_tasks: "{{ role_path }}/tasks/utils/mcp/token.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: "{{ [MATTERMOST_MCP_INSTANCE_DIR, 'upstream.env'] | path_join }}" + mcp_upstream_key: "{{ lookup('mcp_credential', application_id) }}" + +- 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') }}" + +- name: "🚿 Flush compose handlers for {{ application_id }}" + ansible.builtin.meta: flush_handlers + - name: "🩺 Prove the MCP contract of {{ application_id }}" ansible.builtin.include_tasks: "{{ lookup('path_absolute', 'roles/svc-ai-mcp-adapter/tasks/probe.yml') }}" vars: - mcp_probe_bearer: "{{ lookup('mcp_credential', application_id) }}" + mcp_probe_bearer: "{{ MATTERMOST_MCP_BEARER }}" mcp_probe_path_key: "" mcp_probe_read_tool: "get_user_channels" mcp_probe_read_args: {} diff --git a/roles/web-app-mattermost/templates/compose.yml.j2 b/roles/web-app-mattermost/templates/compose.yml.j2 index 7a6f3f2e4a..d2d445e270 100644 --- a/roles/web-app-mattermost/templates/compose.yml.j2 +++ b/roles/web-app-mattermost/templates/compose.yml.j2 @@ -17,5 +17,32 @@ {{ lookup('container_networks') | indent(4) }} {{ lookup('container_depends_on', application_id) | indent(4) }} + {% if MATTERMOST_MCP_ENABLED | bool %} + {% set service_name = 'mattermostmcp' %} + {% set docker_compose_env = [MATTERMOST_MCP_INSTANCE_DIR, 'upstream.env'] | path_join %} + {{ service_name }}: + {% include 'roles/sys-svc-container/templates/base.yml.j2' %} + {{ lookup('container_image', application_id, 'mattermostmcp') }} + build: + context: {{ MATTERMOST_MCP_INSTANCE_DIR }} + dockerfile: Dockerfile + args: + MCP_ADAPTER_BASE_IMAGE: {{ lookup('config', application_id, 'services.mattermostmcp.image') }} + MCP_ADAPTER_BASE_DIGEST: "{{ lookup('config', application_id, 'mcp.adapter.digest') }}" + {{ lookup('compose_only', 'container_name', MATTERMOST_MCP_CONTAINER) }} + read_only: true + cap_drop: + - ALL + security_opt: + - no-new-privileges:true + environment: + ADAPTER_CONTRACT: {{ MATTERMOST_MCP_CONTRACT | to_json | to_json }} + ADAPTER_BEARER: {{ MATTERMOST_MCP_BEARER | to_json }} + ADAPTER_PORT: "{{ MATTERMOST_MCP_PORT }}" + expose: + - "{{ MATTERMOST_MCP_PORT }}" + {{ lookup('container_networks', own_network_only=True, provider_self_alias=False) | indent(4) }} + {% endif %} + {{ lookup('compose_volumes', application_id) }} {{ lookup('compose_networks') }} diff --git a/roles/web-app-mattermost/templates/playwright.env.j2 b/roles/web-app-mattermost/templates/playwright.env.j2 index a3332d3364..0137ffedbb 100644 --- a/roles/web-app-mattermost/templates/playwright.env.j2 +++ b/roles/web-app-mattermost/templates/playwright.env.j2 @@ -24,6 +24,7 @@ MATOMO_SERVICE_ENABLED={{ lookup('config', application_id, 'services.matomo.enab MATTERMOST_BASE_URL={{ lookup('tls', 'web-app-mattermost', 'url.base') | dotenv_quote }} +MCP_ENDPOINT_PATH={{ MATTERMOST_MCP_UPSTREAM_PATH | dotenv_quote }} MCP_SERVICE_ENABLED={{ lookup('config', application_id, 'mcp.enabled') | string | lower }} OIDC_ISSUER_URL={{ OIDC.CLIENT.ISSUER_URL | dotenv_quote }} @@ -44,4 +45,3 @@ SEAWEEDFS_FILER_URL={{ ((lookup('tls', 'web-app-seaweedfs', 'protocols.web') ~ ' SEAWEEDFS_SERVICE_ENABLED={{ lookup('config', application_id, 'services.seaweedfs.enabled') | string | lower }} SSO_SERVICE_ENABLED={{ lookup('config', application_id, 'services.sso.enabled') | string | lower }} - diff --git a/roles/web-app-mattermost/vars/main.yml b/roles/web-app-mattermost/vars/main.yml index cd28f2c987..36b7c5e19c 100644 --- a/roles/web-app-mattermost/vars/main.yml +++ b/roles/web-app-mattermost/vars/main.yml @@ -20,3 +20,19 @@ MATTERMOST_KEYCLOAK_ADMIN_USER: "{{ lookup('config', 'web-app-keycloak', 'servic MATTERMOST_KEYCLOAK_ADMIN_PASS: "{{ lookup('users', 'administrator').password }}" MATTERMOST_LDAP_CONTAINER_ADDRESS: "{{ lookup('container_address', 'svc-db-openldap', 'openldap') }}" + +MATTERMOST_MCP_CONTAINER: "{{ lookup('config', application_id, 'services.mattermostmcp.name') }}" +MATTERMOST_MCP_PORT: "{{ lookup('config', application_id, 'services.mattermostmcp.ports.internal.http') }}" +MATTERMOST_MCP_INSTANCE_DIR: "{{ [lookup('container', application_id, 'directories.instance'), 'mcp'] | path_join }}" +MATTERMOST_MCP_UPSTREAM_PATH: "/plugins/{{ MATTERMOST_MCP_PLUGIN_ID }}/mcp-server/mcp" +MATTERMOST_MCP_UPSTREAM_URL: "http://{{ MATTERMOST_CONTAINER }}:{{ container_port }}{{ MATTERMOST_MCP_UPSTREAM_PATH }}" +MATTERMOST_MCP_BEARER: "{{ lookup('config', application_id, 'credentials.mcp_bearer') }}" +MATTERMOST_MCP_CONTRACT: + upstream_kind: mcp + provider: "{{ application_id }}" + upstream_url: "{{ MATTERMOST_MCP_UPSTREAM_URL }}" + auth_subject: "{{ lookup('config', application_id, 'mcp.auth_subject') }}" + mutating_tools_enabled: "{{ lookup('config', application_id, 'mcp.tools.mutating_tools_enabled') | bool }}" + tools: "{{ lookup('file', 'mcp/tools.json') | from_json }}" + limits: "{{ lookup('config', application_id, 'mcp.limits') }}" + schema_sha256: "{{ lookup('config', application_id, 'mcp.tools.schema_sha256') }}"