Asynchronous-MCP-Server-with-WebRTC-Signaling

yer-retb/Asynchronous-MCP-Server-with-WebRTC-Signaling

3.1

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, and leave 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

  1. 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
    
  2. Create and activate virtual environment:

    python3 -m venv venv
    source venv/bin/activate
    
  3. Install dependencies:

    pip install -r requirements.txt
    
  4. Apply migrations (optional if using Django admin):

    python manage.py migrate
    
  5. 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

TypePurpose
offerInitial connection offer
answerResponse to the offer
candidateICE candidate for connection
leaveUser 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

  1. Enter the mcp-frontend folder:

    cd mcp-frontend
    
  2. Install dependencies:

    npm install
    
  3. 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 named chat 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

Watch the demo video on Google Drive

Click the thumbnail above to view the demo video on Google Drive