Session-Based Authenticated MCP Server
MCP server with session-based authentication for AI assistants, providing secure access to a books database and currency conversion tools.
README
๐ 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.
โจ 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
# 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
# Start the MCP server
python -m mcp_server.server
3. Basic Usage
# 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:
{
"mcpServers": {
"authenticated-books": {
"command": "python",
"args": ["-m", "mcp_server.server"],
"cwd": "/path/to/MCP-server"
}
}
}
Claude Desktop Configuration
{
"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:
# 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
# Build and run with Docker Compose
docker-compose up --build
# The server will be available on localhost:8000
Production Deployment
# Build production image
docker build -t mcp-auth-server .
# Run container
docker run -p 8000:8000 mcp-auth-server
๐ Documentation
| Document | Description |
|---|---|
| ๐ Getting Started | Complete setup and usage guide |
| ๐๏ธ Architecture | System design and data flow diagrams |
| ๐ก API Reference | Complete tool documentation |
| ๐ Deployment | Production deployment guide |
| ๐ก Examples | Real-world usage scenarios |
๐๏ธ Architecture Overview
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
-
Authenticate:
authenticate(username="your_name")- Generates JWT token
- Creates session with 1-hour expiration
- Returns session details
-
Use Tools: Call any protected operation
- Automatic session validation
- No token parameters needed
- Clear error messages if session expired
-
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
# 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 file for details.
๐ค Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
See CONTRIBUTING.md for detailed guidelines.
๐ Links
๐ Support
- ๐ง Email: [Your Email]
- ๐ Issues: GitHub Issues
- ๐ Docs: Documentation
- ๐ฌ Discussions: GitHub Discussions
Ready to authenticate your MCP server? ๐ Start with the Getting Started Guide!
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.