Skip to main content
Glama
omiderfanmanesh

Session-Based Authenticated MCP Server

README.md
# ๐Ÿ” Session-Based Authenticated MCP Server

A **Model Context Protocol (MCP) server** with **session-based authentication** that provides secure access to books database and currency conversion tools. **Designed specifically for AI assistants** to use authentication seamlessly.

![Python](https://img.shields.io/badge/python-3.12+-green.svg)
![MCP](https://img.shields.io/badge/MCP-1.0-orange.svg)
![Docker](https://img.shields.io/badge/docker-ready-blue.svg)
![Tests](https://img.shields.io/badge/tests-30%20passing-brightgreen.svg)
![License](https://img.shields.io/badge/license-MIT-blue.svg)

## โœจ Key Features

### ๐Ÿ” **Session-Based Authentication**
- Simple authentication with username only
- Automatic session management (1-hour expiration)
- No token parameters required in tool calls
- Perfect for AI assistant integration

### ๐Ÿ“š **Books Database**
- Search books by title, author, genre, year
- Paginated results with limit/offset
- Individual book lookup by ID
- CSV-based storage with Excel conversion

### ๐Ÿ’ฑ **Currency Exchange**
- Convert between 10+ major currencies
- Real-time synthetic exchange rates
- Precise decimal calculations
- Error handling for invalid currencies

### ๐Ÿค– **AI Assistant Optimized**
- Seamless integration with GitHub Copilot, Claude Desktop, Cursor IDE
- No complex token passing required
- Simple authenticate-then-use workflow
- Comprehensive error messages and hints

## ๐Ÿš€ Quick Start

### 1. Installation

```bash
# Clone the repository
git clone https://github.com/omiderfanmanesh/MCP-server.git
cd MCP-server

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

### 2. Run the Server

```bash
# Start the MCP server
python -m mcp_server.server
```

### 3. Basic Usage

```bash
# First, authenticate
"Please authenticate me as 'developer'"

# Then use any protected tools
"Find me some science fiction books"
"Convert 100 USD to EUR"
"Check my session status"
"Log me out when done"
```

## ๐Ÿ”ง Integration Examples

### Cursor IDE Configuration

Add to your MCP settings:

```json
{
  "mcpServers": {
    "authenticated-books": {
      "command": "python",
      "args": ["-m", "mcp_server.server"],
      "cwd": "/path/to/MCP-server"
    }
  }
}
```

### Claude Desktop Configuration

```json
{
  "mcpServers": {
    "authenticated-books": {
      "command": "python",
      "args": ["-m", "mcp_server.server"],
      "cwd": "/path/to/MCP-server"
    }
  }
}
```

## ๐Ÿ“‹ Available Tools

| Tool | Description | Authentication Required |
|------|-------------|------------------------|
| `authenticate` | Create new user session | โŒ No |
| `session_status` | Check current session info | โŒ No |
| `logout` | End current session | โŒ No |
| `books_query` | Search books database | โœ… Yes |
| `exchange_convert` | Convert currencies | โœ… Yes |

## ๐Ÿงช Testing

This project includes a comprehensive test suite with 30 test cases covering all functionality:

```bash
# Run all tests
python -m pytest tests/ -v

# Run specific test categories
python -m pytest tests/test_mcp_server.py::TestSessionAuthentication -v
python -m pytest tests/test_mcp_server.py::TestBooksRepository -v
python -m pytest tests/test_mcp_server.py::TestExchangeRates -v
```

### Test Coverage

- โœ… JWT token generation and validation
- โœ… Session authentication flow
- โœ… Protected operations security
- โœ… Books database queries and filtering
- โœ… Currency conversion with all supported currencies
- โœ… Error handling and edge cases
- โœ… Integration workflows
- โœ… Session expiration and cleanup

## ๐Ÿณ Docker Deployment

### Local Development

```bash
# Build and run with Docker Compose
docker-compose up --build

# The server will be available on localhost:8000
```

### Production Deployment

```bash
# Build production image
docker build -t mcp-auth-server .

# Run container
docker run -p 8000:8000 mcp-auth-server
```

## ๐Ÿ“š Documentation

| Document | Description |
|----------|-------------|
| [๐Ÿ“– Getting Started](docs/GETTING_STARTED.md) | Complete setup and usage guide |
| [๐Ÿ—๏ธ Architecture](docs/ARCHITECTURE.md) | System design and data flow diagrams |
| [๐Ÿ“ก API Reference](docs/API.md) | Complete tool documentation |
| [๐Ÿš€ Deployment](docs/DEPLOYMENT.md) | Production deployment guide |
| [๐Ÿ’ก Examples](docs/EXAMPLES.md) | Real-world usage scenarios |

## ๐Ÿ—๏ธ Architecture Overview

```mermaid
graph TB
    A[AI Assistant] -->|authenticate| B[MCP Server]
    B -->|session_id| C[Session Storage]
    A -->|books_query| B
    B -->|check session| C
    C -->|valid| D[Books Database]
    A -->|exchange_convert| B
    B -->|check session| C
    C -->|valid| E[Exchange Service]
```

### Core Components

- **Session Manager**: Handles authentication and session lifecycle
- **Books Repository**: CSV-based database with search capabilities
- **Exchange Service**: Multi-currency conversion with synthetic rates
- **Security Layer**: JWT-based session validation
- **Error Handler**: Comprehensive error messages and recovery hints

## ๐Ÿ”„ Authentication Flow

1. **Authenticate**: `authenticate(username="your_name")`
   - Generates JWT token
   - Creates session with 1-hour expiration
   - Returns session details

2. **Use Tools**: Call any protected operation
   - Automatic session validation
   - No token parameters needed
   - Clear error messages if session expired

3. **Logout**: `logout()`
   - Cleans up session data
   - Clears authentication state

## ๐ŸŒŸ Why Session-Based?

Unlike traditional JWT implementations that require token parameters in every API call, this server uses **session-based authentication** specifically designed for AI assistants:

### Problems with Token Parameters
- AI assistants can't easily pass tokens to tool calls
- Complex parameter management
- Poor user experience

### Session-Based Solution
- โœ… Authenticate once, use forever (until expiration)
- โœ… No token parameters in tool schemas
- โœ… Perfect for AI assistant workflows
- โœ… Automatic session management

## ๐Ÿ› ๏ธ Development

### Project Structure

```
MCP-server/
โ”œโ”€โ”€ mcp_server/           # Main server code
โ”‚   โ”œโ”€โ”€ server.py         # MCP server with authentication
โ”‚   โ”œโ”€โ”€ books.py          # Books database repository
โ”‚   โ””โ”€โ”€ exchange.py       # Currency exchange service
โ”œโ”€โ”€ tests/                # Comprehensive test suite
โ”‚   โ””โ”€โ”€ test_mcp_server.py
โ”œโ”€โ”€ docs/                 # Complete documentation
โ”‚   โ”œโ”€โ”€ ARCHITECTURE.md   # System design
โ”‚   โ”œโ”€โ”€ API.md           # Tool reference
โ”‚   โ”œโ”€โ”€ DEPLOYMENT.md    # Production guide
โ”‚   โ”œโ”€โ”€ EXAMPLES.md      # Usage scenarios
โ”‚   โ””โ”€โ”€ GETTING_STARTED.md
โ”œโ”€โ”€ data/                 # Books database
โ”‚   โ””โ”€โ”€ books.csv
โ”œโ”€โ”€ docker-compose.yml    # Docker deployment
โ”œโ”€โ”€ Dockerfile           # Container definition
โ””โ”€โ”€ requirements.txt     # Dependencies
```

### Development Setup

```bash
# Install in development mode
pip install -e .

# Run tests with coverage
python -m pytest tests/ --cov=mcp_server

# Run server with debug logging
python -m mcp_server.server --debug
```

## ๐Ÿ“„ License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## ๐Ÿค Contributing

1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

See [CONTRIBUTING.md](CONTRIBUTING.md) for detailed guidelines.

## ๐Ÿ”— Links

- [Model Context Protocol Specification](https://modelcontextprotocol.io/)
- [MCP Python SDK](https://github.com/modelcontextprotocol/python-sdk)
- [Claude Desktop Integration](https://claude.ai/desktop)
- [Cursor IDE](https://cursor.sh/)

## ๐Ÿ“ž Support

- ๐Ÿ“ง Email: [Your Email]
- ๐Ÿ› Issues: [GitHub Issues](https://github.com/omiderfanmanesh/MCP-server/issues)
- ๐Ÿ“– Docs: [Documentation](docs/)
- ๐Ÿ’ฌ Discussions: [GitHub Discussions](https://github.com/omiderfanmanesh/MCP-server/discussions)

---

**Ready to authenticate your MCP server?** ๐Ÿš€ Start with the [Getting Started Guide](docs/GETTING_STARTED.md)!