yer-retb/Asynchronous-MCP-Server-with-WebRTC-Signaling
If you are the rightful owner of Asynchronous-MCP-Server-with-WebRTC-Signaling and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
This project is an asynchronous backend WebSocket signaling server built with Django and Channels, implementing a custom protocol called MCP (Message Context Protocol).
๐ก MCP Signaling Server (Django + Channels + ASGI)
This is an asynchronous backend WebSocket signaling server built with Django and Channels, implementing a custom protocol called MCP (Message Context Protocol). It allows React frontend clients to exchange WebRTC signaling messages in real-time without using REST APIs.
๐ Features
- ASGI-compliant Django server with real-time WebSocket support
- Custom signaling protocol: MCP (Message Context Protocol)
- Room-based signaling (multi-user capable)
- Supports
offer
,answer
,candidate
, andleave
messages - Works with any frontend WebRTC client (React, etc.)
- Includes test script for two-clients simulation
๐งฑ Project Structure
backend/
โโโ mcp_server/ # Django project
โ โโโ settings.py # ASGI + Channels config
โ โโโ asgi.py # ASGI application
โโโ signaling/ # Signaling app
โ โโโ consumers.py # SignalConsumer logic
โ โโโ routing.py # WebSocket routing rules
โโโ requirements.txt # Python dependencies
โโโ manage.py # Django CLI tool
โโโ test_mcp.py # Python script to test with 2 clients
โโโ venv/ # Virtual environment (not included in repo)
โ๏ธ Installation & Setup
-
Clone the repo and enter backend folder:
git clone https://github.com/yer-retb/Asynchronous-MCP-Server-with-WebRTC-Signaling.git mcp-signaling-server cd mcp-signaling-server/backend
-
Create and activate virtual environment:
python3 -m venv venv source venv/bin/activate
-
Install dependencies:
pip install -r requirements.txt
-
Apply migrations (optional if using Django admin):
python manage.py migrate
-
Run the server using Daphne:
daphne mcp_server.asgi:application
Server will be available at:
http://127.0.0.1:8000
- WebSocket endpoint:
ws://127.0.0.1:8000/ws/signal/?room=ROOM_NAME
๐ MCP Protocol (Message Context Protocol)
Supported Message Types
Type | Purpose |
---|---|
offer | Initial connection offer |
answer | Response to the offer |
candidate | ICE candidate for connection |
leave | User leaves the session |
Example Message Formats
// Offer
{ "type": "offer", "sdp": "..." }
// Answer
{ "type": "answer", "sdp": "..." }
// ICE Candidate
{ "type": "candidate", "candidate": "..." }
// Leave
{ "type": "leave" }
Clients send these messages through the WebSocket. The server forwards each message to all other peers in the same room.
๐งช Testing the Backend
Use the included script to test with 2 simulated clients:
python test_mcp.py
Expected output:
[Client1] Connected
[Client1] Sent offer
[Client2] Connected
[Client2] Received: {"type": "offer", "sdp": "fake-sdp-from-Client1"}
๐ก Technical Decisions
- Django Channels: Built-in WebSocket support that works seamlessly with ASGI
- Daphne: Production-ready ASGI server compatible with Channels
- In-memory rooms: Kept simple using Python dictionaries for development
- Room-based signaling: Easier to scale, test, and organize peer communication
๐ฌ MCP Frontend Client (React + TypeScript)
This is the frontend application for the MCP (Message Context Protocol) WebRTC signaling system. It connects to a Django ASGI backend via WebSockets and enables peer-to-peer communication through WebRTC DataChannels.
๐ Features
- Room-based signaling with custom WebSocket protocol (MCP)
- Peer-to-peer messaging via WebRTC Data Channels
- Dynamic UI for joining rooms, sending messages, and monitoring peer presence
- Displays room name and real-time connection status
- Smooth transition animations and stateful room handling
๐ Project Structure
mcp-frontend/
โโโ public/ # Static assets
โโโ src/ # App source code
โ โโโ App.tsx # Main component logic
โ โโโ main.tsx # React DOM mounting
โโโ index.html # HTML entry
โโโ package.json # Project metadata
โโโ vite.config.ts # Vite config for TSX support
โ๏ธ Setup & Run
-
Enter the mcp-frontend folder:
cd mcp-frontend
-
Install dependencies:
npm install
-
Start development server:
npm run dev
App will run at: http://localhost:5173
๐ WebSocket Protocol (MCP)
The frontend communicates with the backend via WebSocket using a custom protocol:
const socket = new WebSocket(`ws://localhost:8000/ws/signal/?room=ROOM_NAME`);
- On connection, a WebRTC
RTCPeerConnection
is created - A
DataChannel
namedchat
is established for P2P messaging - All signaling messages (
offer
,answer
,candidate
) are exchanged via socket
๐ฌ Messaging Flow
- User joins a room
- First user becomes offerer and opens DataChannel
- Second user responds and establishes P2P connection
- Messages are exchanged directly between peers
๐ก๏ธ Peer Limit Protection
Only two peers are allowed per room. When a third user attempts to join:
- The backend rejects the WebSocket connection
- The frontend detects and displays an error without rendering the chat interface
๐น Demo Video
Click the thumbnail above to view the demo video on Google Drive