Skip to main content
Glama
README.md
# Property Hub MCP Server

> A multi-pod MCP server giving AI assistants trust-aware hotel review insights, semantic search and metered billing.

![Python](https://img.shields.io/badge/Python-3.11-3776AB?logo=python&logoColor=white)
![FastAPI](https://img.shields.io/badge/FastAPI-009688?logo=fastapi&logoColor=white)
![MongoDB](https://img.shields.io/badge/MongoDB-47A248?logo=mongodb&logoColor=white)
![Milvus](https://img.shields.io/badge/Milvus-00A1EA?logo=milvus&logoColor=white)
![Redis](https://img.shields.io/badge/Redis-Streams-DC382D?logo=redis&logoColor=white)
![OAuth](https://img.shields.io/badge/OAuth-2.0-3C3C3D)
![Docker](https://img.shields.io/badge/Docker-Compose-2496ED?logo=docker&logoColor=white)
![CircleCI](https://img.shields.io/badge/CircleCI-343434?logo=circleci&logoColor=white)

## Overview

Property Hub MCP Server turns a FastAPI application into a Model Context Protocol server using `fastapi-mcp`, so assistants such as ChatGPT and Claude can search hotels, ask questions about a property, compare options and rank them by aspect. The tools return interpreted insights derived from review facts stored in MongoDB, and a Milvus vector index with sentence-transformer embeddings powers semantic search.

The project also covers the operational side of running an MCP service in production. MCP sessions are patched to live in Redis, and Redis Streams bridge SSE traffic across pods, so any instance can serve any session. An OAuth 2.0 layer supports ChatGPT connectors, and middleware handles quota enforcement and Lago-based usage metering.

## Features

**MCP tools for hotel reviews**
- Four tools: `find_hotels`, `ask_about_property`, `compare_hotels` and `rank_by_aspect`. They return interpreted insights built from review facts.
- Geo filtering by city, country or radius.

**Search and data access**
- Semantic property search over a Milvus collection using sentence-transformer embeddings, plus collection statistics.
- MongoDB-backed property lookup by country, city or state with amenity filtering. These routes are excluded from the MCP tool list.
- A translation endpoint exposed as an MCP tool. It calls an external translation API, batches requests and cancels cleanly on shutdown.

**Multi-pod session handling**
- `fastapi-mcp` is patched so sessions are stored in Redis rather than in process memory.
- Redis Streams bridge SSE messages across pods, so a session is not tied to a single instance.

**Authentication**
- OAuth 2.0 for ChatGPT connectors: authorize, login, token (including refresh), dynamic client registration, JWKS and well-known metadata endpoints.
- JWT bearer tokens, using `python-jose`.

**Billing and metering**
- Quota middleware checks a subscription backend before handlers run. Enforcement and post-success deduction are both configurable.
- Usage middleware counts tokens per call with a configurable strategy and sends events to Lago. A ring-buffer debug endpoint, `/_debug/usage`, helps inspect recent events.
- An entitlements client with caching and fail-open behavior.

**Platform**
- Per-environment configuration for local, dev, staging and production.
- SSE-aware CORS, disconnect error suppression and JSON schema sanitizing for ChatGPT's validator.
- Docker, Docker Compose and a CircleCI configuration.

## Architecture

The service is a single FastAPI app with layered middleware. REST routes are auto-exposed as MCP tools through `fastapi-mcp` at `/mcp` and `/sse`. State that would normally be pinned to one process, namely MCP sessions and SSE streams, is moved into Redis so the service can scale horizontally.

```mermaid
flowchart LR
    subgraph sg1["Client"]
        n_client["ChatGPT / Claude MCP client"]
    end
    subgraph sg2["Backend"]
        n_api["FastAPI + fastapi-mcp app"]
        n_oauth["OAuth 2.0 endpoints"]
        n_middleware["Quota and usage middleware"]
    end
    subgraph sg3["Data"]
        n_redis[/"Redis sessions and streams"/]
        n_mongo[("MongoDB")]
        n_milvus[("Milvus vector DB")]
    end
    subgraph sg5["External services"]
        n_billing{{"Billing backend and Lago"}}
        n_translate{{"Translation API"}}
    end
    n_client -->|OAuth login and token| n_oauth
    n_client -->|MCP over SSE / JSON-RPC| n_api
    n_api -->|quota check and usage events| n_middleware
    n_middleware -->|validate quota, send usage| n_billing
    n_api -->|sessions and SSE stream bridge| n_redis
    n_api -->|reviews and properties| n_mongo
    n_api -->|vector search| n_milvus
    n_api -->|translation requests| n_translate
```

### How it works

1. The client (ChatGPT or Claude) authenticates through the OAuth 2.0 endpoints and receives a JWT bearer token.
2. It connects over MCP (SSE or JSON-RPC). The session is stored in Redis, and Redis Streams relay SSE messages so any pod can serve it.
3. Quota middleware validates the caller's subscription with the billing backend before the tool handler runs.
4. The handler reads review facts and properties from MongoDB, runs vector search against Milvus, or calls the translation API.
5. Usage middleware counts tokens for the call and sends a metering event to Lago.

## Tech Stack

| Layer | Technology |
|---|---|
| Language | Python 3.11 |
| Framework | FastAPI, fastapi-mcp, Pydantic |
| Server | Uvicorn |
| Database | MongoDB (pymongo) |
| Vector search | Milvus (pymilvus), sentence-transformers |
| Cache / sessions | Redis (Streams) |
| Auth | OAuth 2.0, python-jose (JWT) |
| Billing / metering | Lago, tiktoken, httpx |
| Infrastructure | Docker, Docker Compose |
| CI | CircleCI |

## Project Structure

```
.
├── app.py                      # FastAPI app, MCP setup, middleware wiring
├── mcp_session_patch_pubsub.py # Redis-backed MCP session patch
├── redis_session_store.py
├── redis_stream_bridge.py      # Cross-pod SSE bridge
├── cors_sse_middleware.py
├── sse_middleware_wrapper.py
├── src/
│   ├── api/                    # hotel reviews, properties, Milvus search, translation
│   └── utils/                  # database, milvus_connector, translator, logger
├── oauth/                      # OAuth 2.0 endpoints and models
├── quota/                      # quota middleware and billing client
├── usage/                      # usage middleware, Lago client, token strategy
├── entitlements/               # entitlements client
├── environments/               # per-environment config
├── test_chatgpt_flow.py
├── start.sh
├── Dockerfile
├── docker-compose.yml
└── .circleci/config.yml
```

## Getting Started

### Prerequisites

- Python 3.11
- Redis, MongoDB and Milvus instances
- Docker (optional)
- Access to the private `cuddly-nest-utils` package. It is not publicly available, so a full install requires access to it.

### Installation

```bash
git clone https://github.com/zain-mirza-1060/hotel-reviews-mcp-server.git
cd hotel-reviews-mcp-server
pip install -r requirements.txt
cp .env.example .env
```

### Environment Variables

Variables are defined in `.env.example`. The table lists the main ones.

| Variable | Description | Required |
|---|---|---|
| `ENV` | Environment: `local`, `dev`, `staging` or `production` | Yes |
| `MONGO_URI` | MongoDB connection string | Yes |
| `MONGO_DB_NAME` | MongoDB database name | No |
| `MONGO_COLLECTION` | MongoDB collection name | No |
| `MILVUS_URI` / `MILVUS_HOST` / `MILVUS_PORT` | Milvus connection details | No |
| `MILVUS_TOKEN` | Milvus auth token | No |
| `MILVUS_COLLECTION_NAME` | Milvus collection | No |
| `EMBEDDING_MODEL` | Sentence-transformers model name | No |
| `REDIS_HOST` | Redis host | Yes |
| `REDIS_PORT` | Redis port | No |
| `REDIS_SSL` | Enable TLS for Redis | No |
| `BILLING_URL` | Subscription backend URL | No |
| `BILLING_API_KEY` | API key for the billing backend | No |
| `ENFORCE_QUOTA` | Block over-quota requests | No |
| `DEDUCT_AFTER_SUCCESS` | Deduct credits after success | No |
| `USAGE_METERING_ENABLED` | Toggle usage metering | No |
| `CN_TOKEN_METHOD` / `CN_TOKEN_DIVISOR` | Token counting method and divisor | No |
| `TRANSLATION_API_URL` | Translation API URL | No |
| `OAUTH_ISSUER` | OAuth issuer URL | No |
| `DEBUG_SECRET` | Enables `/_debug/usage` | No |
| `HOST` / `PORT` | Bind host and port | No |

### Running locally

```bash
ENV=local ./start.sh
```

In `local` and `dev`, this starts Uvicorn with hot reload on port 8000. In other environments it runs three workers.

With Docker Compose:

```bash
docker-compose up --build -d
```

### Running tests

```bash
python test_chatgpt_flow.py
```

### Building

```bash
docker build --build-arg CN_API_TOKEN=<token> -t property-hub-mcp .
```

## Usage

Health check:

```bash
curl http://localhost:8000/health
```

Semantic property search:

```bash
curl -X POST http://localhost:8000/search-text \
  -H "Content-Type: application/json" \
  -d '{"query": "quiet beachfront hotel with great breakfast"}'
```

The request body above is illustrative, so check the Pydantic model in `src/api/property_queries_for_milvus.py` for the exact fields. The review tools are served at `/mcp/find-hotels`, `/mcp/ask-about-property`, `/mcp/compare-hotels` and `/mcp/rank-by-aspect`. The OAuth discovery document is at `/.well-known/oauth-authorization-server`. Point an MCP client at the server's `/mcp` or `/sse` endpoint.

## License

Released under the MIT License, see [LICENSE](LICENSE).