mcp-town-explorer

mcp-town-explorer

A demo MCP server for exploring Massachusetts town data (housing, schools, crime, distance to Boston) via tools and resources, designed to teach MCP architecture with dual-host setup.

Category
访问服务器

README

mcp-town-explorer

A learning project that makes every MCP boundary visible: host, client, server, transport, discovery, invocation. The trick that makes the boundaries legible is one server, two hosts: a no-LLM host and an LLM host talk to the exact same server with zero server changes.

What MCP is

The Model Context Protocol (MCP) is a standard way for an application (the host) to let a language model use external tools and data through a uniform interface. The host embeds an MCP client that speaks JSON-RPC to one or more MCP servers; each server advertises tools (callable functions) and resources (readable content) with machine-readable schemas. The point is decoupling: any MCP-speaking host can use any MCP server without custom glue, because discovery ("what tools exist?") and invocation ("call this tool with these arguments") are standardized.

Architecture

Architecture: user to host (LLM + MCP client) over JSON-RPC/stdio to server to data

Which file is which:

  • Host: v1_cli/host.py and v2_llm/host.py. The host owns the user interaction, and in V2 owns the LLM and the validation logic.
  • Client: ClientSession from the MCP SDK, constructed inside each host. It is not hand-written JSON-RPC; the SDK provides it.
  • Server: server/server.py. Built with FastMCP; exposes the tools and resource. Neither host imports it -- they only launch it as a subprocess and speak the protocol.

For a deeper walk-through of the MCP plumbing (transport, client, handshake, schemas, result shapes), see docs/notes.md.

Setup

Uses uv. Python is pinned to 3.12.

uv sync

Run V1 (no LLM, no API key)

Argv selects the tool; the model is never involved. This isolates the protocol from model behavior.

uv run python v1_cli/host.py list-tools            # print the raw advertised JSON schema
uv run python v1_cli/host.py housing Winchester
uv run python v1_cli/host.py distance Winchester
uv run python v1_cli/host.py schools Lexington
uv run python v1_cli/host.py safety Woburn
uv run python v1_cli/host.py resource Winchester   # read the town://{town} resource
uv run python v1_cli/host.py --verbose housing Winchester   # log the protocol lifecycle

The error path, deliberately shown:

uv run python v1_cli/host.py housing Nowhere
# -> ERROR from server: Unknown town: 'Nowhere'. Known towns: ...
# exits non-zero; the server error surfaces across the protocol rather than being swallowed

Run V2 (adds the LLM)

Natural language in. The model sees the tool schemas, proposes a call, the host validates it, the client executes it, the result is fed back, and the model explains. The loop repeats (capped at 5 iterations) until the model stops requesting tools.

Put OPENAI_API_KEY in a .env file at the repo root (it is git-ignored); the host loads it automatically via python-dotenv. Exporting the variable works too.

# .env at the repo root contains: OPENAI_API_KEY=sk-...   (loaded automatically)
uv run python v2_llm/host.py "Compare schools in Winchester and Lexington"
uv run python v2_llm/host.py --show-tokens "How safe is Woburn?"   # print tool-schema token cost

# or export it instead of using .env:
export OPENAI_API_KEY=sk-...
uv run python v2_llm/host.py "How safe is Woburn?"

The validation step is the reason V2 exists. Before any execution the host checks the proposed tool name against an allowlist and, for tools that take a town, checks that the town exists in the dataset; a rejected call is logged ([REJECTED] ...) and never reaches the server. The exact line where a model proposal becomes an execution is commented in v2_llm/host.py.

Provenance

This dataset describes real Massachusetts towns, but it is stitched together from several sources of differing years and methodologies. It is a demo for teaching MCP architecture. Do not use it for any real decision (buying a home, choosing a school district, judging safety).

Sources, one per column:

column source
median_home_price Zillow Home Value Index (ZHVI), town level
school_rating GreatSchools, rounded average of the town's public schools (GreatSchools publishes no single district number)
violent_crime_rate NeighborhoodScout, incidents per 1,000 (2024 FBI-derived vintage)
distance_to_boston_mi computed: haversine from the town's US Census/Wikipedia centroid to Boston City Hall (42.3601, -71.0589)
population US Census (2020 decennial or ACS estimate)

Per-column source URLs, vintages, retrieval notes, and known caveats are recorded in PROVENANCE.md. The habit is the lesson: record where every number came from, even in a teaching dataset.

What this does NOT demonstrate

  • Deployment. stdio transport means the server is a local subprocess the host spawns. There is no network service, no container, no host/port.
  • Auth. No authentication or authorization between host and server. The V2 "permission boundary" is application-level input validation, not identity or access control.
  • Remote transport. No HTTP/SSE/streamable transport. Everything is local stdio.
  • Multi-user / concurrency. One host, one server subprocess, one user, one request at a time.

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选