SmartPark Reservation MCP Server

SmartPark Reservation MCP Server

Provides a secure MCP tool to append confirmed parking reservations to durable storage with validation, audit logging, and concurrency safety.

Category
访问服务器

README

SmartPark Reservation MCP Server — Stage 3

A real MCP (Model Context Protocol) server, built with the official mcp Python SDK, that writes confirmed parking reservations to durable storage once a human administrator has approved them (Stage 2).

This is a separate repository/service from Stages 1 and 2 — Agent 2 calls this server's single tool over the network, the same way any MCP-compliant client would.

What it does

Exposes exactly one MCP tool: write_confirmed_reservation. Once the administrator (Agent 2) approves a reservation, Agent 2 calls this tool, which:

  1. Validates every field (rejects the | delimiter, newlines, null bytes, and overly long values — these would corrupt the file format or let a caller inject a fake extra record).

  2. Appends one line to data/confirmed_reservations.txt in the exact required format:

    Name | Car Number | Reservation Period | Approval Time
    
  3. Does this atomically and safely under concurrent calls (cross-platform file locking), and logs the attempt (success or rejection) to an audit log.

Architecture

 Agent 2 (Stage 2 Admin Agent)              Agent 3 (this repo)
┌──────────────────────────┐   MCP/HTTP    ┌────────────────────────────┐
│ apply_decision(confirmed) │ ────────────▶ │ POST /mcp                  │
│                           │  Bearer token │  BearerAuthMiddleware       │
│ integration/               │  required    │  RateLimitMiddleware        │
│  admin_agent_client.py     │              │  MCPServer                 │
│  -> record_confirmed_      │              │   └─ write_confirmed_       │
│     reservation()          │              │      reservation tool      │
└──────────────────────────┘              │       └─ validate ─┐        │
                                            │                    ▼        │
                                            │      data/confirmed_        │
                                            │      reservations.txt       │
                                            │      (file-locked append)   │
                                            │      + data/audit.log       │
                                            └────────────────────────────┘

Why the official MCP SDK (not just a REST endpoint)

Stage 3 asks for a real MCP server, or a FastAPI stand-in if that's not feasible. The official mcp Python SDK (MCPServer, formerly known as FastMCP) turned out to be fully usable here: it builds a standard streamable-HTTP ASGI app (so it composes with normal Starlette middleware for auth/rate-limiting) and ships with production security features out of the box — DNS-rebinding protection, host/origin allowlisting, and request-body size limits — which is exactly what "secure and resistant to unauthorized access" calls for. Building a hand-rolled JSON-RPC-over-FastAPI server would have meant re-implementing a worse version of these same protections.

Security measures

Concern Mitigation
Unauthorized callers BearerAuthMiddleware — every request needs Authorization: Bearer <MCP_API_KEY>, compared with hmac.compare_digest (no timing side-channel). No key configured → the server generates and prints a random one-time key rather than silently allowing unauthenticated access.
Abuse / DoS RateLimitMiddleware — fixed-window limit per client IP (default 30 req/min, configurable).
DNS rebinding / host spoofing The MCP SDK's built-in TransportSecuritySettings (enabled by default), with configurable ALLOWED_HOSTS/ALLOWED_ORIGINS for production.
Path traversal Structurally impossible — the output file path is fixed by server config and is never accepted as a tool argument.
Record/format injection Every field is validated to reject the `
Concurrent-write corruption Cross-platform atomic file locking (filelock) around every append — tested with 20 simultaneous threads writing with zero interleaved/corrupted lines.
Oversized/malformed requests max_request_body_size capped at 64KB (reservation payloads are tiny; anything bigger is rejected outright).
Traceability without leaking secrets Every write attempt (success or rejection) is appended to data/audit.log, with only a masked token prefix — the real API key is never logged.
Reliability A stuck lock surfaces as a clear, catchable TimeoutError after 5s rather than hanging the server; invalid input raises a specific InvalidReservationField the MCP layer turns into a normal tool-error result rather than crashing the process.

Project structure

parking_mcp_server/
├── app/
│   ├── config.py               # .env-driven configuration
│   ├── reservation_writer.py    # field validation + atomic file append
│   ├── audit_log.py              # append-only audit trail
│   ├── security.py                # BearerAuthMiddleware, RateLimitMiddleware
│   └── server.py                   # MCPServer + tool + ASGI wiring
├── integration/
│   ├── admin_agent_client.py       # what Stage 2 imports to call this server
│   └── db_patch_example.py         # exact diff for Stage 2's decision endpoint
├── tests/                           # 22 pytest tests across 4 modules
├── demo.py                           # end-to-end demo (live server + real MCP client)
├── view_reservations.py               # quick CLI viewer for the output file/audit log
├── run_server.sh / run_server.ps1
├── requirements.txt
├── .env.example
└── .github/workflows/ci.yml           # runs tests on both ubuntu-latest and windows-latest

Setup

Works identically on Windows, macOS, and Linux — pure Python (the official mcp SDK, Starlette/uvicorn, and the cross-platform filelock package; no OS-specific binaries).

python -m venv .venv && source .venv/bin/activate   # Windows: .venv\Scripts\activate
pip install -r requirements.txt
cp .env.example .env
# Set MCP_API_KEY in .env for a stable key -- otherwise a random one-time
# key is generated and printed at startup.

Windows-specific notes

  • Use run_server.ps1 instead of run_server.sh.
  • filelock uses msvcrt under the hood on Windows automatically — no extra configuration needed for the concurrency-safety guarantees.
  • CI runs the full test suite on windows-latest as well as ubuntu-latest (see .github/workflows/ci.yml).

Usage

Run the demo (starts its own server, no setup needed):

PYTHONPATH=. python demo.py

Run the real server:

./run_server.sh        # Linux/macOS
.\run_server.ps1        # Windows

It prints a generated MCP_API_KEY on first run if you haven't set one — copy it into your client's Authorization: Bearer <key> header (or into Stage 2's .env as MCP_API_KEY, see Integration below).

View what's been written:

PYTHONPATH=. python view_reservations.py

Run tests:

PYTHONPATH=. python -m pytest tests/ -v

Integration with Agent 2 (Stage 2)

  • integration/admin_agent_client.py — the client Stage 2 imports (record_confirmed_reservation(name, car_number, reservation_period, approval_time)), which opens a real MCP client session, authenticates, and calls the tool.
  • integration/db_patch_example.py — the exact before/after diff for Stage 2's POST /requests/{id}/decision endpoint: once a reservation is marked "confirmed", it now also calls record_confirmed_reservation(...) so the approval is durably recorded here, not just in Stage 2's own request-tracking table.
  • Stage 2's .env needs: MCP_SERVER_URL=http://127.0.0.1:8002/mcp and MCP_API_KEY=<the same key configured on this server>.

Notes / limitations (Stage 3 scope)

  • Rate limiting and the audit log are in-memory/local-file, appropriate for a single-process deployment; a multi-instance deployment would move the rate limiter to a shared store (e.g. Redis) and the audit log to a centralized logging system.
  • MCP_API_KEY is a single shared secret rather than per-client credentials — sufficient for a single trusted caller (Agent 2), but a multi-tenant deployment would want per-client tokens or the MCP SDK's full OAuth 2.1 support (auth_server_provider/AuthSettings, not used here since it requires standing up a separate OAuth issuer).
  • The reservations file is a flat text file, as specified; a higher-volume production deployment would likely write to a database instead, behind the same tool interface.

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选