MCP Dispatch Agent
An MCP server for freight dispatchers that takes natural language orders, selects vehicles, checks regulations via RAG, and prepares documents for clients and drivers.
README
MCP Dispatch Agent
An AI assistant for a freight dispatcher, built on the Model Context Protocol. It takes an order in natural language, picks a vehicle from the fleet, checks the relevant regulations and prepares documents for the client and the driver.
The project has two independent halves:
- MCP server - tools, resources and prompts. Runs on its own; any MCP client can connect to it.
- Agent - a model-to-tool loop on the Claude Messages API. Connects to the server as an ordinary client, using the same Bearer token.
Quick start
git clone https://github.com/yurii-sheremeta/mcp-dispatch-agent.git
cd mcp-dispatch-agent
cp .env.example .env # fill in your tokens and ANTHROPIC_API_KEY
Local:
python -m venv .venv && source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -r requirements.txt
python -m src.rag.ingest # build the document index
python -m src.server.main # server on http://localhost:8000
Docker:
docker compose up --build
Check that it works:
curl http://localhost:8000/health
Talk to the agent (separate terminal, server must be running):
python -m src.agent
Architecture
flowchart TB
U([Dispatcher]) --> A
subgraph AG["Agent - model to tool loop"]
A[loop.py<br/>Claude Messages API]
G[guardrails.py<br/>context boundaries]
M[memory.py<br/>conversation history]
A --- G
A --- M
end
A -->|Bearer token, HTTP| S
subgraph SV["MCP server :8000"]
AU[auth.py<br/>Bearer and roles]
S[main.py<br/>streamable-http]
MO[monitoring.py<br/>/health /metrics]
AU --> S
S --- MO
end
S --> T1[fleet.py]
S --> T2[docs.py]
S --> T3[external.py]
T1 --> DB[(SQLite<br/>fleet, drivers,<br/>orders)]
T2 --> RAG[(BM25 index<br/>5 documents)]
T3 --> API[[ECB rates - Open-Meteo]]
S -.-> LOG[/logs/server.log/]
Routing. The model picks the source itself, based on tool descriptions and
the rules in prompts/routing.md:
| Question | Source |
|---|---|
| "which reefers are free" | SQLite - list_vehicles |
| "how long may a driver drive" | RAG - search_regulations |
| "what is that in dollars" | ECB rates - currency_rate |
| "find a truck and check the driver's hours" | SQLite plus RAG in sequence |
Data sources
| Source | Contents | Module |
|---|---|---|
| SQLite | 10 vehicles, 8 drivers, 3 clients, orders | src/db/ |
| Documents (RAG) | 5 markdown files: driving time, tachograph, contract, claims, cargo types | src/rag/, data/docs/ |
| External APIs | ECB exchange rates, Open-Meteo weather | src/server/tools/external.py |
Document search uses BM25, not embeddings. This is a deliberate decision:
Anthropic has no embeddings endpoint, and a second API key or a ~400 MB local
model is not justified for five short documents. src/rag/retriever.py exposes a
narrow search(query, k) interface, so moving to semantic search means replacing
a single file.
Tools
| Tool | Source | Access | Context mechanism demonstrated |
|---|---|---|---|
list_vehicles |
SQLite | guest, dispatcher | ctx.info |
plan_assignment |
SQLite | dispatcher | report_progress, debug with extra, set_state |
book_vehicle |
SQLite | dispatcher | send_notification, disable_components, error paths |
get_stats |
SQLite | guest, dispatcher | - |
search_regulations |
RAG | guest, dispatcher | ctx.info |
get_contract_clause |
RAG | guest, dispatcher | - |
list_documents |
RAG | guest, dispatcher | - |
currency_rate |
ECB | guest, dispatcher | external failure handling |
route_weather |
Open-Meteo | guest, dispatcher | external failure handling |
Resources: resource://fleet, resource://tariffs,
resource://client/{registry_id}/profile
Prompts: quote_letter, driver_brief
Security
Authorization. Bearer token in the Authorization header. Two tokens, two
roles:
dispatcher- every tool;guest- read-only;plan_assignmentandbook_vehicleare unavailable.
/health and /metrics are open on purpose: otherwise external monitoring could
not reach them.
Safe context boundaries (src/agent/guardrails.py, prompts/guardrails.md):
- Tool results are wrapped in
<tool_output trust="data">. Text inside a document is data, not instructions to the model. - Instruction-override attempts ("ignore previous instructions",
<system>) are detected and flagged; the agent continues with the original request. - Personal data never reaches the logs:
redact()masks names, phone numbers and tokens. Logs carry identifiers only (DRV-03, a plate number). - Tokens in logs are masked down to the last four characters.
Not committed to the repository: .env, the database, the index and the logs
- see
.gitignore.
Logging and monitoring
Logs go to both the console and logs/server.log in a single format. Structured
data is passed in a separate field and rendered as JSON - readable for a human,
parsable by a machine:
2026-08-13 13:11:40 [INFO ] server | === MCP Dispatch Server starting ===
2026-08-13 13:11:40 [INFO ] server | Database ready | {"booked": 2, "free": 7, "service": 1}
2026-08-13 13:11:48 [WARNING] auth | AUTH FAIL | {"client": "172.17.0.1", "reason": "no Authorization header"}
2026-08-13 13:11:48 [INFO ] auth | AUTH OK | {"client": "172.17.0.1", "role": "dispatcher", "token": "***"}
2026-08-13 13:12:03 [DEBUG] fleet | Vehicle rejected | {"plate": "TR-9911", "reason": "tachograph: 1.5 h left, 4.9 h needed"}
2026-08-13 13:12:05 [INFO ] fleet | Assignment scan finished | {"chosen": "TR-4471", "price": 330, "checked": 10}
2026-08-13 13:12:19 [ERROR] fleet | Vehicle unavailable | {"plate": "TR-2280", "status": "booked"}
A token value is never visible in the logs: mask() keeps the last four
characters and redact() in the formatter additionally strips the token key
entirely.
Endpoints:
curl http://localhost:8000/health # {"status":"ok","uptime_seconds":312,...}
curl http://localhost:8000/metrics # request, error, 401 and tool-call counters
/metrics reports uptime, request count, rejected-authorization count, errors,
per-tool call counts, average assignment duration, memory usage and fleet state.
Demonstration
python -m scripts.reset_db # restore the fleet to its initial state
python -m src.server.main # terminal 1: server
./scripts/demo.ps1 # terminal 2: curl scenario (401 / 200 / metrics / logs)
python -m scripts.client_demo # full scenario: progress, logs, RAG, booking
python -m scripts.check_roles # guest vs dispatcher permissions
python -m src.agent # interactive chat with the agent
python -m scripts.capture_demo runs the whole scenario in one pass and writes
logs/demo_run.md — verbatim output of every step against a live server: tests,
authorization, role separation, tool calls with progress and rejection reasons,
agent routing, metrics, and one example of each required log record type.
Tests
pytest -q # 42 tests
ruff check src tests scripts # lint
Coverage: authorization (401 without a token, 401 with a wrong one, 200 with a
valid one, /health staying public), role separation, token masking, vehicle
selection logic, RAG relevance, prompt-injection detection, PII masking, and
source routing.
CI (.github/workflows/ci.yml) runs lint and tests, builds the Docker image,
starts the container and verifies that /health responds while an unauthorized
request receives a 401.
Layout
src/
server/ MCP server: main, auth, monitoring, logging_conf
tools/ fleet - docs (RAG) - external (APIs)
agent/ loop, guardrails, memory, router
rag/ store (chunking) - retriever (BM25) - ingest (CLI)
db/ schema.sql - seed.sql - repo.py
prompts/ Prompt Book: system - routing - guardrails - templates
data/docs/ 5 documents for RAG
tests/ 42 tests
scripts/ demo.ps1 - client_demo - check_roles - reset_db
Limitations
This is a training project, not a production system.
- The documents in
data/docs/are simplified training extracts, not current legal instruments. Each file carries a notice in its header. Wording must be verified against the applicable regulation before operational use. - The data is fictitious: plate numbers, registry IDs and driver names do not correspond to real ones.
- Tariffs and distances are a simplified model: a lookup table of distances between seven cities and a linear per-kilometre rate instead of a routing service and real pricing.
- Counters live in process memory. Several replicas would need a Prometheus
exporter; the
monitoring.pyinterface anticipates that. - Session state is not a database. The assignment draft lives as long as the connection does; a confirmed booking is written to SQLite immediately.
- BM25 instead of embeddings - see the Data sources section.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。