how-to-test-an-mcp-server

how-to-test-an-mcp-server

A minimal MCP server with get_weather and create_ticket tools, used for testing MCP servers across protocol, unit, eval, transport, and auth layers.

Category
访问服务器

README

how-to-test-an-mcp-server

A runnable companion repo for testing an MCP server: a minimal two-tool MCP Python server, MCP Inspector CLI protocol checks, deterministic pytest unit tests on the tool functions, a Claude-based tool-selection eval harness with semantic assertions across N runs, and transport-specific (stdio/HTTP/SSE) and auth/authorization pytest suites.

Companion code for the Autonoma blog post: How to Test an MCP Server

Requirements

  • Python 3.10+
  • Node 18+ and jq (only for the MCP Inspector CLI checks)
  • An ANTHROPIC_API_KEY (only for the tool-selection eval harness)

Quickstart

git clone https://github.com/Autonoma-Tools/how-to-test-an-mcp-server.git
cd how-to-test-an-mcp-server
python3 -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
python src/server.py

Project structure

src/
  __init__.py
  tools.py       Plain-Python get_weather / create_ticket -- no MCP, no LLM
  auth.py        Shared JWT auth helpers for the HTTP/SSE transports
  server.py      The MCP server: stdio (default), plus --transport http/sse
scripts/
  inspector_checks.sh   MCP Inspector CLI protocol checks (stdio)
tests/
  test_tools_unit.py        Deterministic unit tests
  test_transport_stdio.py   stdio transport handshake + clean-exit tests
  test_transport_http.py    HTTP transport + bearer-token scoping tests
  test_transport_sse.py     SSE transport incremental delivery + resume tests
  test_auth.py              Auth: rejection, scoping, expiry/refresh
eval/
  eval_tool_selection.py    Claude-based tool-selection eval (N=15 runs/scenario)
examples/
  quickstart_client.py   End-to-end stdio client demo
  run_eval_demo.sh       How to run the eval harness
  run_test_suites.sh     Runs every layer below, in order
conftest.py              Makes `from src.tools import ...` work from any cwd
requirements.txt
  • src/ — the MCP server and its plain-Python tool implementations.
  • scripts/ — the MCP Inspector CLI protocol-check script.
  • tests/ — the pytest suites for the unit, transport, and auth layers.
  • eval/ — the Claude-based tool-selection eval harness.
  • examples/ — runnable examples you can execute as-is.

The five testing layers

This repo mirrors the testing pyramid described in the blog post. Each layer answers a different question about the server.

1. Protocol-level checks (MCP Inspector CLI)

"Does the server speak MCP correctly over stdio?"

bash scripts/inspector_checks.sh

Starts src/server.py under @modelcontextprotocol/inspector --cli, asserts tools/list returns both tools with non-empty schemas, and that tools/call on get_weather returns content mentioning temperature. Requires Node 18+ and jq.

2. Deterministic unit tests

"Is the underlying tool logic correct, independent of MCP entirely?"

pytest tests/test_tools_unit.py -v

Imports get_weather / create_ticket directly from src/tools.py — no protocol, no transport, no LLM. Exact-match assertions only. This is the fastest, cheapest, most reliable layer, and should catch the large majority of regressions before anything else runs.

3. Tool-selection eval harness

"Does an LLM reliably pick the right tool for a natural-language ask?"

export ANTHROPIC_API_KEY=sk-ant-...
python eval/eval_tool_selection.py

LLM tool-calling is probabilistic, so this harness runs each scenario 15 times against Claude and reports a per-scenario pass rate rather than a single pass/fail. Scenarios below a 90% pass rate are flagged FLAKY. Assertions here are property-based (correct tool, argument satisfies a loose check) rather than exact-match.

4. Transport tests (stdio / HTTP / SSE)

"Does the server behave correctly across every transport it supports?"

# stdio: handshake + tool call + clean process exit, no manual setup
pytest tests/test_transport_stdio.py -v

# HTTP: spawns its own server on :8787
pytest tests/test_transport_http.py -v

# SSE: spawns its own server on :8788, tests incremental delivery + resume
pytest tests/test_transport_sse.py -v

The HTTP and SSE test files start and tear down their own server subprocess (module-scoped pytest fixtures) — you don't need to run python src/server.py --transport http yourself first, though you can (--transport http --port 8787 / --transport sse --port 8787) if you want to poke at it manually with curl.

5. Auth / authorization tests

"Is the auth boundary actually enforced — rejection, scoping, and expiry?"

pytest tests/test_auth.py -v

Covers three angles against the HTTP-transport server (its own instance, on :8789): no-token/invalid-token rejection with no tool side effect, scope enforcement (a token scoped to one tool gets 403 on the other), and token expiry/refresh (an expired JWT is rejected; a freshly issued replacement succeeds over the same connection).

Run everything at once

bash examples/run_test_suites.sh

The HTTP/SSE auth model (read before poking at it)

The HTTP and SSE transports are a small, hand-rolled, stdlib-only JSON-RPC-over-HTTP and Server-Sent-Events layer (see src/server.py) — not part of the official MCP SDK's transports. They exist specifically to give the auth and transport tests something concrete to exercise. Auth is HS256 JWTs verified against a hardcoded demo secret in src/auth.py (JWT_SECRET). This is intentional for a teaching repo and is not production-ready auth — see the module docstrings in src/auth.py and tests/test_auth.py for specifics.

About

This repository is maintained by Autonoma as reference material for the linked blog post. Autonoma builds autonomous AI agents that plan, execute, and maintain end-to-end tests directly from your codebase.

If something here is wrong, out of date, or unclear, please open an issue.

License

Released under the MIT License © 2026 Autonoma Labs.

推荐服务器

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

官方
精选