Skip to main content
Glama
360XView

mcp-auth-poc

by 360XView
README.md
# MCP Authentication & Security Proof of Concept

> **Purpose**: Test and validate authentication patterns for remote MCP servers before integrating into the main e2b2 learning platform.

## 🎯 Project Context

This PoC is part of the **e2b2 intelligent learning platform** project. The main platform uses MCP (Model Context Protocol) tools to deliver personalized coding education through Claude Desktop.

**Why This PoC Exists:**
- Validate token-based authentication for remote MCP servers
- Test development workflow with security enabled
- Understand Replit deployment process
- Provide a reference implementation for the main project
- Ensure authentication doesn't impede rapid development

**Connection to Main Project:**
- Main project location: `../v3/`
- Main project uses 8 core MCP tools (see `../v3/docs/`)
- This PoC validates the authentication layer that will wrap those tools
- Learnings from this PoC will be applied to the production architecture

## πŸ“‹ What This PoC Does

A minimal MCP server with:
- βœ… **2 simple tools** (greeting generator and message echo)
- βœ… **Token-based authentication** (optional, can be disabled for development)
- βœ… **SSE transport** for remote connections
- βœ… **User context injection** (simulates student identification)
- βœ… **Well-documented code** (serves as example for main implementation)

## πŸ—οΈ Architecture Overview

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Claude Desktop β”‚ (or OpenAI client)
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚ HTTP + Bearer Token
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Auth Middlewareβ”‚ ◄── Validates token, extracts user
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚ Adds user context
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   MCP Server    β”‚ ◄── Tools receive authenticated user
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Tool Handlers  β”‚ ◄── get_greeting, echo_message
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

**Key Design Decisions:**
- **Token-based auth** (not OAuth) for simplicity in PoC
- **Environment variable** toggle for auth (easy local dev)
- **User context** passed to all tools (pattern for main project)
- **Minimal dependencies** (FastMCP, Uvicorn, python-dotenv)

## πŸ“š Documentation Structure

```
docs/
β”œβ”€β”€ 01_CONTEXT.md              # Why this PoC exists, relationship to main project
β”œβ”€β”€ 02_AUTHENTICATION.md       # Authentication architecture and decisions
β”œβ”€β”€ 03_LOCAL_DEVELOPMENT.md    # Local setup and testing
β”œβ”€β”€ 04_REPLIT_DEPLOYMENT.md    # Deploying to Replit
β”œβ”€β”€ 05_CLIENT_SETUP.md         # Connecting Claude Desktop / OpenAI
└── 06_LESSONS_LEARNED.md      # Findings to apply to main project
```

## πŸš€ Quick Start

### Prerequisites
- Python 3.10+
- pip or uv package manager
- Claude Desktop (or OpenAI compatible client)

### Local Development (No Auth)

```bash
# 1. Navigate to project
cd /path/to/v3_3_auth_and_sec

# 2. Install dependencies
pip install -r requirements.txt

# 3. Run server
python server.py
```

Server runs at `http://localhost:8000`

### Local Development (With Auth)

```bash
# 1. Create environment file
cp .env.example .env

# 2. Edit .env and set AUTH_ENABLED=true

# 3. Run server
python server.py
```

### Testing the Tools

**Option 1: Claude Desktop** (see `docs/05_CLIENT_SETUP.md`)
```json
{
  "mcpServers": {
    "auth-poc": {
      "command": "python",
      "args": ["/absolute/path/to/server.py"]
    }
  }
}
```

**Option 2: MCP Inspector**
```bash
npx @modelcontextprotocol/inspector python server.py
```

**Option 3: Direct HTTP (with auth)**
```bash
curl http://localhost:8000/tools \
  -H "Authorization: Bearer tok_test1_abc123"
```

## πŸ“ Project Structure

```
v3_3_auth_and_sec/
β”œβ”€β”€ README.md                 # This file - project overview
β”œβ”€β”€ requirements.txt          # Python dependencies
β”œβ”€β”€ .env.example              # Example configuration
β”œβ”€β”€ .env                      # Your local config (git-ignored)
β”œβ”€β”€ .gitignore                # Ignore patterns
β”‚
β”œβ”€β”€ server.py                 # Main MCP server (well-documented)
β”œβ”€β”€ auth_middleware.py        # Authentication logic (well-documented)
β”œβ”€β”€ tools.py                  # Tool implementations (well-documented)
β”‚
└── docs/                     # Comprehensive documentation
    β”œβ”€β”€ 01_CONTEXT.md
    β”œβ”€β”€ 02_AUTHENTICATION.md
    β”œβ”€β”€ 03_LOCAL_DEVELOPMENT.md
    β”œβ”€β”€ 04_REPLIT_DEPLOYMENT.md
    β”œβ”€β”€ 05_CLIENT_SETUP.md
    └── 06_LESSONS_LEARNED.md
```

## πŸ”‘ Authentication Flow

```mermaid
sequenceDiagram
    participant Client as Claude Desktop
    participant Middleware as Auth Middleware
    participant Server as MCP Server
    participant Tool as Tool Handler

    Client->>Middleware: Request + Bearer Token
    Middleware->>Middleware: Validate Token
    Middleware->>Middleware: Extract User Info
    Middleware->>Server: Request + User Context
    Server->>Tool: Execute with User
    Tool-->>Client: Response with User Data
```

## πŸ§ͺ Testing Checklist

- [ ] Local server runs without auth
- [ ] Local server runs with auth enabled
- [ ] Claude Desktop connects locally (no auth)
- [ ] Claude Desktop connects locally (with auth)
- [ ] Deploy to Replit successfully
- [ ] Claude Desktop connects to Replit (with auth)
- [ ] Multiple users can connect with different tokens
- [ ] Invalid tokens are rejected with clear errors

## πŸ“– Key Files to Understand

1. **`server.py`** - Entry point, shows how FastMCP + auth middleware integrate
2. **`auth_middleware.py`** - Core authentication logic, user extraction
3. **`tools.py`** - Example tools that use user context
4. **`docs/02_AUTHENTICATION.md`** - Deep dive on authentication architecture

## πŸŽ“ Learning Objectives

After working through this PoC, you should understand:
- βœ… How to add authentication to FastMCP servers
- βœ… How to toggle auth for development vs production
- βœ… How to inject user context into MCP tool handlers
- βœ… How to deploy authenticated MCP servers to Replit
- βœ… How to connect remote authenticated servers to Claude Desktop
- βœ… Patterns to apply to the main e2b2 platform

## πŸ”„ Next Steps After PoC

1. **Validate locally** - Ensure auth works as expected
2. **Deploy to Replit** - Test remote authentication
3. **Document learnings** - Update `docs/06_LESSONS_LEARNED.md`
4. **Apply to main project** - Integrate patterns into `../v3/`

## 🀝 Development Notes

**Code Style:**
- Comprehensive docstrings on all functions
- Inline comments for complex logic
- Type hints for clarity
- Clear variable names

**This code is intentionally over-documented** to serve as a reference for the main project implementation.

## πŸ”— Related Documentation

- Main Project: `../v3/README.md`
- MCP Specification: https://modelcontextprotocol.io
- FastMCP Docs: https://github.com/jlowin/fastmcp
- Replit Docs: https://docs.replit.com

## πŸ“ž Questions?

If you encounter issues:
1. Check the relevant doc in `docs/`
2. Review code comments in the `.py` files
3. Consult `docs/06_LESSONS_LEARNED.md` for known issues
4. Refer back to the main project's architecture docs

---

**Project Status**: PoC Phase
**Last Updated**: 2025-10-11
**Main Project**: e2b2 Learning Platform (`../v3/`)