FastMCP SMS Server
README.md
# ๐ฑ FastMCP SMS Server
A production-oriented **Model Context Protocol (MCP) server** built with **FastMCP**, **JWT authentication**, **SQLAlchemy**, and the **TextBee SMS API**.
This project demonstrates how an MCP server can authenticate users, identify the authenticated user from a JWT, access user-specific data through SQLAlchemy, and expose SMS functionality as an MCP tool.
---
## โจ Features
* ๐ FastMCP server
* ๐ JWT authentication with **RS256**
* ๐ Public/private RSA key verification
* ๐ค User registration and login
* ๐๏ธ SQLAlchemy database integration
* ๐ซ Access token based authentication
* ๐งโ๐ป Authenticated user identification using JWT `sub`
* ๐ฑ Send SMS through TextBee
* ๐ User-specific API credentials
* ๐ค MCP client support
* ๐ฅ๏ธ Claude Desktop integration
* โก FastAPI authentication server
---
## ๐๏ธ Architecture
```text
โโโโโโโโโโโโโโโโโโโโโโโ
โ Claude Desktop โ
โ MCP Client โ
โโโโโโโโโโโโฌโโโโโโโโโโโ
โ
โ MCP
โผ
โโโโโโโโโโโโโโโโโโโโโโโ
โ FastMCP Server โ
โ โ
โ JWT Verification โ
โ MCP Tools โ
โโโโโโโโโโโโฌโโโโโโโโโโโ
โ
โโโโโโโโโโโโดโโโโโโโโโโโ
โ โ
โผ โผ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ
โ SQLAlchemy โ โ TextBee โ
โ Database โ โ SMS API โ
โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโ
โฒ
โ
โ user_id
โ
โโโโโโโโโโโโโโโ
โ JWT Token โ
โ โ
โ sub โ
โ username โ
โ scope โ
โ iss โ
โ aud โ
โโโโโโโโโโโโโโโ
```
---
# ๐ Project Structure
```text
mcp/
โ
โโโ src/
โ โ
โ โโโ auth_server/
โ โ โโโ __init__.py
โ โ โโโ main.py
โ โ โโโ database.py
โ โ โโโ models.py
โ โ โโโ schemas.py
โ โ โโโ security.py
โ โ
โ โโโ mcp_server.py
โ โโโ mcp_database.py
โ โโโ client.py
โ
โโโ keys/
โ โโโ private_key.pem
โ โโโ public_key.pem
โ
โโโ .gitignore
โโโ requirements.txt
โโโ README.md
```
> **Never commit `private_key.pem` or real API credentials to GitHub.**
---
# ๐ ๏ธ Technologies
| Technology | Purpose |
| ------------ | ----------------------------- |
| Python | Backend |
| FastMCP | MCP server |
| FastAPI | Authentication server |
| SQLAlchemy | Database ORM |
| SQLite | Development database |
| PyJWT | JWT creation and verification |
| Cryptography | RSA cryptography |
| Pwdlib | Password hashing |
| HTTPX | HTTP requests |
| TextBee | SMS delivery |
---
# ๐ Installation
## 1. Clone the repository
```bash
git clone https://github.com/YOUR_USERNAME/YOUR_REPOSITORY.git
cd YOUR_REPOSITORY
```
---
## 2. Create a virtual environment
### Windows
```powershell
python -m venv .venv
```
Activate it:
```powershell
.venv\Scripts\activate
```
### Linux / macOS
```bash
python3 -m venv .venv
source .venv/bin/activate
```
---
## 3. Install dependencies
```bash
pip install -r requirements.txt
```
---
# ๐ Generate RSA Keys
This project uses **RS256**.
The authentication server signs JWTs using the private key:
```text
private_key.pem
```
The MCP server verifies them using:
```text
public_key.pem
```
Generate a key pair with OpenSSL:
```bash
openssl genrsa -out keys/private_key.pem 2048
```
Then:
```bash
openssl rsa \
-in keys/private_key.pem \
-pubout \
-out keys/public_key.pem
```
On Windows PowerShell, the same commands can be run if OpenSSL is installed.
---
# ๐ค Authentication Server
The authentication server provides:
```text
POST /register
POST /login
```
Start it with:
```powershell
uvicorn auth_server.main:app --port 9000
```
The authentication server will run at:
```text
http://127.0.0.1:9000
```
---
# ๐ Register a User
Example:
```powershell
curl.exe -X POST http://127.0.0.1:9000/register `
-H "Content-Type: application/json" `
-d '{\"username\":\"kanchan\",\"password\":\"1234\"}'
```
Response:
```json
{
"message": "User created successfully",
"user_id": 1,
"username": "kanchan"
}
```
---
# ๐ Login
```powershell
curl.exe -X POST http://127.0.0.1:9000/login `
-H "Content-Type: application/json" `
-d '{\"username\":\"kanchan\",\"password\":\"1234\"}'
```
Response:
```json
{
"access_token": "YOUR_JWT_TOKEN",
"token_type": "bearer"
}
```
The JWT contains claims such as:
```json
{
"sub": "1",
"username": "kanchan",
"scope": "profile:read",
"iss": "http://localhost:9000",
"aud": "my-mcp-server",
"iat": 1234567890,
"exp": 1234571490
}
```
---
# ๐ JWT Authentication
The MCP server uses an RSA public key to verify the JWT.
```python
verifier = JWTVerifier(
public_key=PUBLIC_KEY,
issuer="http://localhost:9000",
audience="my-mcp-server",
algorithm="RS256",
)
```
The authentication flow is:
```text
User
โ
โ username + password
โผ
Auth Server
โ
โ signs JWT with private key
โผ
Access Token
โ
โผ
MCP Client
โ
โ Bearer token
โผ
FastMCP
โ
โ verifies signature with public key
โผ
MCP Tool
```
---
# ๐๏ธ SQLAlchemy Integration
The MCP tools use SQLAlchemy to access the database.
A database session is created using:
```python
db = SessionLocal()
```
Example:
```python
stmt = select(User).where(User.id == user_id)
user = db.scalar(stmt)
```
The session is closed after the operation:
```python
finally:
db.close()
```
---
# ๐ค Getting the Authenticated User
The MCP server does not need the client to provide a `user_id`.
Instead, the user ID comes from the verified JWT:
```python
token = get_access_token()
user_id = int(token.claims["sub"])
```
Then SQLAlchemy can find the user:
```python
stmt = select(User).where(User.id == user_id)
user = db.scalar(stmt)
```
This gives the MCP server the identity of the user who made the request.
---
# ๐ฑ Send SMS Tool
The project exposes an MCP tool similar to:
```python
@mcp.tool()
def send_sms(
recipient: str,
message: str,
) -> dict:
...
```
The client only needs to provide:
```text
recipient
message
```
It does **not** need to provide:
```text
user_id
api_key
device_id
```
The server can determine the authenticated user from the JWT and retrieve that user's TextBee configuration from the database.
---
# ๐ค Claude Desktop
The MCP server can be connected to Claude Desktop as a local MCP server.
Example configuration:
```json
{
"mcpServers": {
"my-mcp-server": {
"command": "E:\\mcp\\.venv\\Scripts\\python.exe",
"args": [
"E:\\mcp\\src\\mcp_server.py"
]
}
}
}
```
The configuration file is located at:
```text
%APPDATA%\Claude\claude_desktop_config.json
```
After modifying the configuration, restart Claude Desktop.
Your MCP tools should then become available to Claude.
---
# โ ๏ธ Security
Do **not** commit secrets to GitHub.
Add the following to `.gitignore`:
```gitignore
.venv/
__pycache__/
*.pyc
.env
.env.*
users.db
keys/private_key.pem
*.log
```
Never commit:
```text
private_key.pem
```
or:
```text
TextBee API keys
JWT secrets
database passwords
```
For production, store secrets in environment variables or a dedicated secrets manager.
---
# ๐ Current Authentication Flow
```text
โโโโโโโโโโโโโโโโ
โ User โ
โโโโโโโโฌโโโโโโโโ
โ
โ Login
โผ
โโโโโโโโโโโโโโโโโโโโ
โ Auth Server โ
โ FastAPI โ
โโโโโโโโโโฌโโโโโโโโโโ
โ
โ RS256 JWT
โผ
โโโโโโโโโโโโโโโโโโโโ
โ MCP Client โ
โ Claude / Custom โ
โโโโโโโโโโฌโโโโโโโโโโ
โ
โ Access Token
โผ
โโโโโโโโโโโโโโโโโโโโ
โ FastMCP Server โ
โ โ
โ JWTVerifier โ
โโโโโโโโโโฌโโโโโโโโโโ
โ
โ Verified JWT
โผ
โโโโโโโโโโโโโโโโโโโโ
โ MCP Tool โ
โ โ
โ get_access_token โ
โโโโโโโโโโฌโโโโโโโโโโ
โ
โ sub โ user_id
โผ
โโโโโโโโโโโโโโโโโโโโ
โ SQLAlchemy โ
โ โ
โ User โ
โโโโโโโโโโฌโโโโโโโโโโ
โ
โ User credentials
โผ
โโโโโโโโโโโโโโโโโโโโ
โ TextBee โ
โ SMS Gateway โ
โโโโโโโโโโโโโโโโโโโโ
```
---
# ๐งช Development
Start the authentication server:
```powershell
uvicorn auth_server.main:app --port 9000
```
Start the MCP server:
```powershell
python mcp_server.py
```
For Claude Desktop, configure the MCP server using the `stdio` transport.
---
# ๐ง Roadmap
* [x] FastMCP server
* [x] FastAPI authentication server
* [x] User registration
* [x] User login
* [x] JWT authentication
* [x] RS256 signing
* [x] SQLAlchemy integration
* [x] Authenticated user lookup
* [x] TextBee SMS integration
* [x] Claude Desktop local integration
* [ ] OAuth 2.0 authorization server
* [ ] Multi-user TextBee credential management
* [ ] PostgreSQL support
* [ ] Refresh tokens
* [ ] Token revocation
* [ ] Production deployment
* [ ] HTTPS
* [ ] Rate limiting
* [ ] Audit logging
---
# ๐ What This Project Demonstrates
This project is primarily a learning and development example for understanding how the following technologies work together:
```text
MCP
+
FastMCP
+
JWT
+
RS256
+
FastAPI
+
SQLAlchemy
+
External APIs
```
The main goal is to demonstrate how an MCP tool can securely identify the authenticated user and perform user-specific operations.
---
# ๐ License
This project is available under the MIT License.
See `LICENSE` for details.
---
## โญ Contributing
Contributions, suggestions, and improvements are welcome.
If you find a bug or have an idea, feel free to open an issue or submit a pull request.
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo issues