Matrix MCP Server
Enables interaction with Matrix homeservers through the Model Context Protocol, providing tools for room management, messaging, user profiles, and search capabilities.
README
Matrix MCP Server
A comprehensive Model Context Protocol (MCP) server that provides secure access to Matrix homeserver functionality. Built with TypeScript, this server enables MCP clients to interact with Matrix rooms, messages, users, and more through a standardized interface.
Features
- š OAuth 2.0 Authentication with token exchange support
- š± 15 Matrix Tools organized by functionality tiers
- š Multi-homeserver Support with configurable endpoints
- š Real-time Operations with ephemeral client management
- š Production Ready with comprehensive error handling
- š Rich Responses with detailed Matrix data
Quick Start
Prerequisites
- Node.js 20+ and npm
- Matrix homeserver access (Synapse, Dendrite, etc.)
- MCP client (Claude Desktop, VS Code with MCP extension, etc.)
Installation
# Clone the repository
git clone <repository-url>
cd matrix-mcp-server
# Install dependencies
npm install
# Build the project
npm run build
# Configure environment
cp .env.example .env
# Edit .env with your settings
# Start the server
npm start
Development Mode
# Start with hot reload (OAuth disabled for easier testing)
npm run dev
# Or start with OAuth enabled
ENABLE_OAUTH=true npm run dev
Available Tools
š Tier 0: Read-Only Tools
Room Tools
-
list-joined-rooms- Get all rooms the user has joined- No parameters required
- Returns room names, IDs, and member counts
-
get-room-info- Get detailed room informationroomId(string): Matrix room ID (e.g.,!roomid:domain.com)- Returns name, topic, settings, creator, and member count
-
get-room-members- List all members in a roomroomId(string): Matrix room ID- Returns display names and user IDs of joined members
Message Tools
-
get-room-messages- Retrieve recent messages from a roomroomId(string): Matrix room IDlimit(number, default: 20): Maximum messages to retrieve- Returns formatted message content including text and images
-
get-messages-by-date- Filter messages by date rangeroomId(string): Matrix room IDstartDate(string): ISO 8601 format (e.g.,2024-01-01T00:00:00Z)endDate(string): ISO 8601 format- Returns messages within the specified timeframe
-
identify-active-users- Find most active users by message countroomId(string): Matrix room IDlimit(number, default: 10): Maximum users to return- Returns users ranked by message activity
User Tools
-
get-user-profile- Get profile information for any usertargetUserId(string): Target user's Matrix ID (e.g.,@user:domain.com)- Returns display name, avatar, presence, and shared rooms
-
get-my-profile- Get your own profile information- No parameters required
- Returns your profile, device info, and room statistics
-
get-all-users- List all users known to your client- No parameters required
- Returns display names and user IDs from client cache
Search Tools
search-public-rooms- Discover public rooms to joinsearchTerm(string, optional): Filter by name or topicserver(string, optional): Specific server to searchlimit(number, default: 20): Maximum rooms to return- Returns room details, topics, and member counts
Notification Tools
-
get-notification-counts- Check unread messages and mentionsroomFilter(string, optional): Specific room ID to check- Returns unread counts, mentions, and recent activity
-
get-direct-messages- List all DM conversationsincludeEmpty(boolean, default: false): Include DMs with no recent messages- Returns DM partners, last messages, and unread status
āļø Tier 1: Action Tools
Messaging Tools
-
send-message- Send messages to roomsroomId(string): Matrix room IDmessage(string): Message contentmessageType(enum: "text" | "html" | "emote", default: "text"): Message formattingreplyToEventId(string, optional): Event ID to reply to- Supports plain text, HTML formatting, and emote actions
-
send-direct-message- Send private messages to userstargetUserId(string): Target user's Matrix IDmessage(string): Message content- Automatically creates DM rooms if needed
Room Management Tools
-
create-room- Create new Matrix roomsroomName(string): Name for the new roomisPrivate(boolean, default: false): Room privacy settingtopic(string, optional): Room topic/descriptioninviteUsers(array, optional): User IDs to invite initiallyroomAlias(string, optional): Human-readable room alias- Creates rooms with appropriate security settings
-
join-room- Join rooms by ID or aliasroomIdOrAlias(string): Room ID or alias to join- Works with invitations and public rooms
-
leave-room- Leave Matrix roomsroomId(string): Room ID to leavereason(string, optional): Reason for leaving- Cleanly exits rooms with optional reason
-
invite-user- Invite users to roomsroomId(string): Room to invite user totargetUserId(string): User ID to invite- Respects room permissions and power levels
Room Administration Tools
-
set-room-name- Update room display namesroomId(string): Room to modifyroomName(string): New room name- Requires appropriate room permissions
-
set-room-topic- Update room topics/descriptionsroomId(string): Room to modifytopic(string): New room topic- Requires appropriate room permissions
Authentication & Configuration
Authentication Modes
The server supports two authentication modes:
OAuth Mode (ENABLE_OAUTH=true)
- Full OAuth 2.0 integration with your identity provider
- Supports token exchange for Matrix homeserver authentication
- Secure multi-user access with proper token management
- Recommended for production deployments
Development Mode (ENABLE_OAUTH=false)
- Direct access without OAuth authentication
- Requires Matrix access tokens as headers
- Simplified setup for testing and development
- Not recommended for production
Environment Variables
Create a .env file with your configuration:
# Core Configuration
PORT=3000
ENABLE_OAUTH=true # Enable OAuth authentication
ENABLE_TOKEN_EXCHANGE=true # Exchange OAuth tokens for Matrix tokens
CORS_ALLOWED_ORIGINS="" # Comma-separated allowed origins (empty = allow all)
# HTTPS Configuration (optional)
ENABLE_HTTPS=false
SSL_KEY_PATH="/path/to/private.key"
SSL_CERT_PATH="/path/to/certificate.crt"
# Identity Provider (OAuth mode)
IDP_ISSUER_URL="https://keycloak.example.com/realms/matrix"
IDP_AUTHORIZATION_URL="https://keycloak.example.com/realms/matrix/protocol/openid-connect/auth"
IDP_TOKEN_URL="https://keycloak.example.com/realms/matrix/protocol/openid-connect/token"
OAUTH_CALLBACK_URL="http://localhost:3000/callback"
# Matrix Configuration
MATRIX_HOMESERVER_URL="https://matrix.example.com"
MATRIX_DOMAIN="matrix.example.com"
MATRIX_CLIENT_ID="your-matrix-client-id"
MATRIX_CLIENT_SECRET="your-matrix-client-secret"
Client Integration
Claude Code
Remember, the MATRIX_ACCESS_TOKEN header is an optional header. You should delete it if you have token exchange working. Obtain MATRIX_MCP_TOKEN from MCP Inspector.
claude mcp add --transport http matrix-server http://localhost:3000/mcp -H "matrix_user_id: @user1:matrix.example.com" -H "matrix_homeserver_url: https://localhost:8008" -H "matrix_access_token: ${MATRIX_ACCESS_TOKEN}" -H "Authorization: Bearer ${MATRIX_MCP_TOKEN}"
VS Code
Remember, the matrix_access_token header is an optional header. You should delete it if you have token exchange working.
In mcp.json:
{
"servers": {
"matrix-mcp": {
"url": "http://localhost:3000/mcp",
"type": "http",
"headers": {
"matrix_access_token": "${input:matrix-access-token}",
"matrix_user_id": "@<your-matrix-username>:<your-homeserver-domain>",
"matrix_homeserver_url": "<your-homeserver-url>"
}
}
},
"inputs": [
{
"id": "matrix-access-token",
"type": "promptString",
"description": "Your OAuth access token"
}
]
}
Testing with MCP Inspector
# Start the server
npm run dev
# In another terminal, run the inspector
npx @modelcontextprotocol/inspector
Connect to http://localhost:3000/mcp to authenticate and test all available tools.
Development
Available Scripts
npm run build # Build TypeScript to dist/
npm run dev # Development server with hot reload
npm run start # Production server
npm run lint # Run ESLint
npm run test # Run tests
Project Structure
src/
āāā http-server.ts # Main HTTP server entry point
āāā server.ts # MCP server configuration
āāā tools/ # Tool implementations
ā āāā tier0/ # Read-only tools
ā ā āāā rooms.ts # Room information tools
ā ā āāā messages.ts # Message retrieval tools
ā ā āāā users.ts # User profile tools
ā ā āāā search.ts # Room search tools
ā ā āāā notifications.ts # Notification tools
ā āāā tier1/ # Action tools
ā āāā messaging.ts # Message sending tools
ā āāā room-management.ts # Room lifecycle tools
ā āāā room-admin.ts # Room administration tools
āāā matrix/ # Matrix client management
āāā utils/ # Helper utilities
āāā types/ # TypeScript type definitions
Security Considerations
- š Token Management: All Matrix clients are ephemeral and cleaned up after operations
- š”ļø OAuth Integration: Prevents direct Matrix token exposure through OAuth proxy
- š Permission Checks: Respects Matrix room power levels and permissions
- š« Input Validation: Comprehensive parameter validation using Zod schemas
- š CORS Support: Configurable origin restrictions for web clients
Architecture
The server implements a three-layer architecture:
- HTTP Layer (
http-server.ts): Express server with OAuth integration - MCP Layer (
server.ts): Tool registration and request routing - Matrix Layer (
tools/): Matrix homeserver communication
Each tool creates ephemeral Matrix clients that authenticate via your configured method, perform the requested operation, and clean up automatically.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
E2B
Using MCP to run code via e2b.
Neon Database
MCP server for interacting with Neon Management API and databases
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.