agentleFS
cloud-ru-techcloud-ru-tech/mcp-server-mattermost/AGENTS.md

mcp-server-mattermost

AGENTS.md45 starsChanged 10 months agoMITView on GitHub

MCP server for Mattermost integration. Exposes Mattermost REST API operations as MCP tools for AI assistants (Claude Desktop, Cursor). Architecture: Required environment variables: - MATTERMOST_URL - Mattermost server URL - MATTERMOST_TOKEN - Bot or user access token (conditional: required unless MATTERMOST_ALLOW_HTTP_CLIENT_TOKENS is enabled) Optional: - MATTERMOST_TIMEOUT (default: 30) - MATTERMOST_MAX_RETRIES (default: 3) - MATTERMOST_VERIFY_SSL (default: true) - MATTERMOST_LOG_LEVEL (default: INFO) - MATTERMOST_LOG_FORMAT (default: json) - Log format: 'json' for production, 'text' for development - MATTERMOST_API_VERSION (default: v4) - MATTERMOST_MAX_CONNECTIONS (default:…

  • Commits and pushes
  • Where files are headingNew AGENTS.md files have more rules about keeping secrets out of the code, and fewer about checking a change actually works.

    Of the AGENTS.md files we read, the 11,773 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.

  • In this repository2 other files it gives its agents.

The file

Show its rules and text

We split this AGENTS.md into the rules it gives an agent

13 rules. About 0 are common, 6 are distinctive, and 7 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈6

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 7

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use respx for mocking httpx requests.

    line 106
  2. Very specific:

    Use Capability enum from enums.py — never raw strings:

    line 172
  3. Distinctive:

    All tool functions use this pattern:

    line 223
  4. Distinctive:

    Write project artifacts in English, including documentation, code comments, commit messages, and issue and PR/MR titles and descriptions.

    line 238
  5. Very specific:

    Do not include Co-Authored-By: trailers for AI agents in commit messages.

    line 243
  6. Very specific:

    Update version in pyproject.toml

    line 250
  7. Very specific:

    Update __version__ fallback in src/mcp_server_mattermost/__init__.py

    line 251
  8. Very specific:

    Update CHANGELOG.md with changes

    line 252
  9. Distinctive:

    Create GitHub Release from tag → triggers PyPI publish

    line 256
  10. Distinctive:

    Before committing, always run the full lint suite and fix any issues:

    line 260
  11. Distinctive:

    Do NOT commit code that has lint, type, or test errors.

    line 269
  12. Very specific:

    When using markdown (images, lists, bold) inside HTML blocks (<div>, <details>) in MkDocs — add md_in_html extension and markdown attribute …

    line 273
  13. Very specific:

    When adding CSS class to an image in MkDocs — use attr_list extension with !alt{ .classname } instead of wrapping in <div class="...">.

    line 274

≈ “Common” is an estimate: at least 1 in 5 of the 16,595 AGENTS.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
AGENTS.md

CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

Project Overview

MCP server for Mattermost integration. Exposes Mattermost REST API operations as MCP tools for AI assistants (Claude Desktop, Cursor).

Architecture:

MCP Client (Claude Desktop, Cursor)
    ↓ (stdio JSON-RPC)
FastMCP Server
    ├── Tools Layer (channels, messages, posts, users, teams, files, bookmarks)
    ├── Pydantic Models (validation + schema generation)
    └── MattermostClient (async HTTP with retry)
        ↓ (HTTPS)
Mattermost Server (REST API v4)

Documentation References

Commands

# Install dependencies
uv sync

# Run tests
uv run pytest

# Run specific test file
uv run pytest tests/test_config.py -v

# Lint
uv run ruff check src tests

# Format
uv run ruff format src tests

# Type check
uv run mypy src

# Run the server
uv run mcp-server-mattermost

Code Style

  • Python 3.10+ (use list[], dict[], X | None syntax)
  • 120-character line length
  • Google-style docstrings
  • Type hints required (mypy strict mode)
  • Two blank lines after imports
  • Ruff with ALL rules enabled (see pyproject.toml for exceptions)

Key Source Files

  • src/mcp_server_mattermost/__init__.py - Package entry, exports main() and __version__
  • src/mcp_server_mattermost/server.py - FastMCP instance creation, lifespan, FileSystemProvider
  • src/mcp_server_mattermost/deps.py - Dependency injection providers (get_client)
  • src/mcp_server_mattermost/auth.py - MattermostTokenVerifier for per-client token auth
  • src/mcp_server_mattermost/client.py - Async HTTP client with retry logic
  • src/mcp_server_mattermost/config.py - Settings via pydantic-settings (env vars prefixed MATTERMOST_)
  • src/mcp_server_mattermost/exceptions.py - Exception hierarchy (MattermostMCPError base)
  • src/mcp_server_mattermost/logging.py - Logging setup (stderr per MCP spec)
  • src/mcp_server_mattermost/middleware.py - FastMCP middleware for error handling
  • src/mcp_server_mattermost/enums.py - Capability and ToolTag enums for tool metadata
  • src/mcp_server_mattermost/tools/ - MCP tool implementations by category
  • src/mcp_server_mattermost/models/ - Pydantic models for validation

Configuration

Required environment variables:

  • MATTERMOST_URL - Mattermost server URL
  • MATTERMOST_TOKEN - Bot or user access token (conditional: required unless MATTERMOST_ALLOW_HTTP_CLIENT_TOKENS is enabled)

Optional:

  • MATTERMOST_TIMEOUT (default: 30)
  • MATTERMOST_MAX_RETRIES (default: 3)
  • MATTERMOST_VERIFY_SSL (default: true)
  • MATTERMOST_LOG_LEVEL (default: INFO)
  • MATTERMOST_LOG_FORMAT (default: json) - Log format: 'json' for production, 'text' for development
  • MATTERMOST_API_VERSION (default: v4)
  • MATTERMOST_MAX_CONNECTIONS (default: 100) - Max HTTP connections in the shared pool
  • MATTERMOST_MAX_KEEPALIVE_CONNECTIONS (default: 20) - Max idle keepalive connections, clamped to max connections
  • MATTERMOST_KEEPALIVE_EXPIRY (default: 30.0) - Idle keepalive connection lifetime in seconds
  • MATTERMOST_ALLOW_HTTP_CLIENT_TOKENS (default: false) - Allow HTTP clients to use their own Mattermost tokens
  • MATTERMOST_HTTP_HOST_ORIGIN_PROTECTION (default: none) - Host/Origin protection: off, auto, or strict
  • MATTERMOST_HTTP_ALLOWED_HOSTS (default: none) - Extra allowed Host header values (JSON array or comma-separated)
  • MATTERMOST_HTTP_ALLOWED_ORIGINS (default: none) - Extra allowed Origin header values (JSON array or comma-separated)

Testing

Unit Tests

Tests use pytest with pytest-asyncio. Test fixtures in tests/conftest.py:

  • clean_env - Removes MATTERMOST_* env vars for test isolation
  • mock_settings - Sets valid test environment variables

Use respx for mocking httpx requests.

Integration Tests

Integration tests in tests/integration/ use fastmcp.Client for in-memory MCP testing.

Key insight: FastMCP provides Client(server) that connects directly to server instance via in-memory transport — no subprocess, no network overhead, full MCP protocol.

from fastmcp import Client
from mcp_server_mattermost.server import mcp

async def test_list_public_channels(mattermost_env):
    async with Client(mcp) as client:
        result = await client.call_tool("list_public_channels", {"team_id": team_id})
        assert any(ch["name"] == "town-square" for ch in result.data)

What this tests:

  • MCP protocol (tools/list, tools/call, JSON-RPC)
  • Pydantic validation at tool layer
  • FastMCP routing
  • MattermostClient HTTP logic
  • Real Mattermost API

References:

Run integration tests:

# With Testcontainers (needs Docker)
uv run pytest tests/integration

# Against external server
export MATTERMOST_URL=https://your-server.com
export MATTERMOST_TOKEN=your-bot-token
uv run pytest tests/integration

MCP Tool Annotations

FastMCP defaults (important to remember):

  • readOnlyHint: false — explicitly set True for read-only operations
  • destructiveHint: true — explicitly set False for reversible write operations
  • idempotentHint: false — explicitly set True if repeated call = no-op

Operation classification:

  • Read-only → readOnlyHint=True, idempotentHint=True
  • Write idempotent (join, add_reaction, pin) → destructiveHint=False, idempotentHint=True
  • Write non-idempotent (post_message, create_channel) → destructiveHint=False
  • Destructive (delete_message) → no annotations needed (default true)

"Destructive" = irreversible loss of data.

Tool Capability Metadata

Each tool declares a capability in meta for agent-based filtering:

  • read — retrieves data, no side effects (get, list, search)
  • write — modifies state within existing resources (post, update, join, pin, react, upload)
  • create — creates new top-level entities (channels, DMs)
  • delete — permanently destroys a resource

Use Capability enum from enums.py — never raw strings:

from fastmcp.tools import tool
from mcp_server_mattermost.enums import Capability, ToolTag

@tool(
    annotations={"readOnlyHint": True, "idempotentHint": True},
    tags={ToolTag.MATTERMOST, ToolTag.CHANNEL},
    meta={"capability": Capability.READ},
)
async def list_public_channels(...):

Classification rules:

  • create = only for new standalone entities (channels, DMs)
  • write = all other modifications, including "create" operations on dependent resources (bookmarks, messages, reactions, files exist only inside a channel)
  • delete = resource permanently ceases to exist
  • Each tool gets exactly one capability

Profiles are defined client-side, not server-side. The server provides capability labels, clients assemble profiles from them:

PROFILES = {
    "reader":  {"read"},
    "writer":  {"read", "write"},
    "manager": {"read", "write", "create"},
    "admin":   {"read", "write", "create", "delete"},
}

MCP Tool Description Best Practices

Description formula: WHAT it does + WHEN to use + DISAMBIGUATION

# Disambiguation example between similar tools:
"""Get recent messages from a channel.

Returns messages in reverse chronological order.
Use for reading channel conversation history.
For searching by keywords, use search_messages instead.  # ← disambiguation
"""

Code Patterns

FastMCP Dependency Injection

All tool functions use this pattern:

from fastmcp.dependencies import Depends
from mcp_server_mattermost.deps import get_client

client: MattermostClient = Depends(get_client),  # noqa: B008

The # noqa: B008 suppresses ruff's flake8-bugbear warning "Do not perform function calls in argument defaults". This is intentional — Depends() is a FastMCP/FastAPI DI marker, not a mutable default. The function call happens at request time, not at function definition.

Project Language

Write project artifacts in English, including documentation, code comments, commit messages, and issue and PR/MR titles and descriptions. Match the user's language in conversation.

Commit Messages

Do not include Co-Authored-By: trailers for AI agents in commit messages. Human contributor co-authorship trailers are welcome.

Versioning

This project uses manual versioning:

  1. Update version in pyproject.toml
  2. Update __version__ fallback in src/mcp_server_mattermost/__init__.py
  3. Update CHANGELOG.md with changes
  4. Commit: git commit -m "chore: bump version to X.Y.Z"
  5. Tag: git tag -a vX.Y.Z -m "Release vX.Y.Z"
  6. Push: git push origin main --tags
  7. Create GitHub Release from tag → triggers PyPI publish

Pre-commit Checklist

Before committing, always run the full lint suite and fix any issues:

uv run ruff check src tests   # lint
uv run ruff format src tests  # format
uv run mypy src               # type check
uv run pytest                 # tests

Do NOT commit code that has lint, type, or test errors.

Lessons Learned

  • When using markdown (images, lists, bold) inside HTML blocks (<div>, <details>) in MkDocs — add md_in_html extension and markdown attribute on the HTML tag, otherwise content renders as plain text
  • When adding CSS class to an image in MkDocs — use attr_list extension with ![alt](path){ .classname } instead of wrapping in <div class="...">. Cleaner, no HTML needed

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.