bad-mcp

bad-mcp

A harness for building test MCP servers that reproduce common attack patterns, enabling detection evaluation via streaming HTTP or stdio.

Category
访问服务器

README

bad-mcp

A small, extensible harness for building test MCP servers that reproduce common MCP attack patterns, so you can measure whether your detector catches them. Built on the official MCP Python SDK (mcp 2.x).

Serves over Streamable HTTP (point a detector at a URL — the realistic "unvetted remote server" case) or stdio, and can also emit an event log offline with no client at all.

Authorized testing only. This is a detection fixture, not a working attack. Run it in an isolated environment dedicated to evaluating your own tooling.

Safety model

Every scenario is deliberately inert:

  • Injection / poisoning strings are realistic (so your detector has real signal to match), but the runtime behaviour is harmless: tools return canned text.
  • The only side effect a "successful" attack produces is appending a line to a local sink.log — a stand-in for exfiltration that touches nothing sensitive and never leaves the machine.
  • All referenced secrets are canaries (~/.bad-mcp/canary_secret, CANARY-TOKEN-...). No real files are read; there is no network egress.
  • ssrf_discovery's internal targets (169.254.169.254, 127.0.0.1:8080, 10.0.0.5) are advertised only in a fake discovery document. Nothing in this repo ever issues a request to them — verify with grep -rn "169.254.169.254" . and confirm every hit is a string literal, not an argument to httpx/requests/urllib/socket.

Attack scenarios

name pattern
benign_control clean tools — use it to measure false positives
rug_pull tool is benign at approval time, then its description/schema silently mutates and a tools/list_changed notification is sent
description_poisoning tool descriptions carry hidden instructions: imperative overrides, HTML-comment smuggling, invisible Unicode-Tag smuggling, fake "SYSTEM:" directives
tool_shadowing duplicate tool names in one manifest, plus a tool impersonating a trusted server's tool and redirecting it
prompt_injection manifest stays clean; the injection instead rides in a tool result (read_ticket) and a notification (get_notification) — probes detectors that only scan descriptions
ssrf_discovery an OAuth/OIDC discovery document (.well-known/oauth-authorization-server, .well-known/openid-configuration) points authorization_endpoint / token_endpoint / jwks_uri at internal/link-local/cloud-metadata addresses — advertised only, never fetched by this fixture
data_exfiltration a benign-looking tool's schema asks for excessive up-front context (full_context, environment, ...), plus a tool that advertises over-broad permissions (admin_tool)

Install

Requires Python 3.10+ (the mcp SDK's minimum).

python3.10 -m venv venv && . venv/bin/activate
pip install -e .

This installs the repo as the bad_mcp package (see pyproject.toml), which is what makes python -m bad_mcp.cli below work.

Use

List scenarios:

python -m bad_mcp.cli list

Produce an event log offline (no MCP client needed — best for iterating on a static detector):

python -m bad_mcp.cli simulate --out events.jsonl
python detector.py events.jsonl

Run a live remote server over Streamable HTTP and point your detector's "MCP server URL" input at it (this is the realistic setup — an unvetted remote MCP server):

python -m bad_mcp.cli serve-http --port 8971 --out events.jsonl
# detector connects to:  http://127.0.0.1:8971/mcp

The MCP endpoint is mounted at /mcp by default (--path to change). Your detector connects like any MCP client and will observe the rug-pull change mid-session, the poisoned descriptions, and the shadowed/duplicate tools — while the harness records the ground truth to events.jsonl for scoring.

Remote access from another host is off by default (DNS-rebinding protection). To allow it in an isolated test network:

# bind publicly and permit a specific Host header
python -m bad_mcp.cli serve-http --host 0.0.0.0 --port 8971 \
  --allow-host lab-box:8971 --out events.jsonl

# or, for a throwaway isolated net, skip the Host check entirely
python -m bad_mcp.cli serve-http --host 0.0.0.0 \
  --insecure-disable-host-check --out events.jsonl

ssrf_discovery only serves its discovery document over HTTP mode, at the two .well-known paths below (offline simulate also renders it once, as evidence, without an HTTP server):

python -m bad_mcp.cli serve-http --scenarios ssrf_discovery --port 8971 --out events.jsonl &
curl -s http://127.0.0.1:8971/.well-known/oauth-authorization-server

There is also a local stdio mode for driving with a stdio MCP client:

python -m bad_mcp.cli serve --scenarios rug_pull,tool_shadowing --out events.jsonl

Pick scenarios and the rug-pull trigger:

python -m bad_mcp.cli simulate \
  --scenarios benign_control,rug_pull --trigger after_call --out events.jsonl

Event log (JSONL)

One JSON object per line. Two categories share the stream:

  • evidence (ground_truth: false) — what a detector is allowed to see:
    • manifest_snapshot — the tool list served on each list_tools, with per-tool desc_sha / input_schema_sha / raw input_schema (diff the hashes across snapshots to catch rug-pulls; scan input_schema for excessive-context parameter names).
    • tool_call — a tool invocation with its arguments.
    • tool_result — the text a tool call actually returned, logged for every call so runtime-only injections (not just poisoned manifests) are visible to an offline detector.
    • notification — e.g. tools/list_changed, or notifications/message carrying an injected instruction.
    • discovery_document — the OAuth/OIDC .well-known document served (or rendered offline), with its endpoints.
  • ground truth (ground_truth: true) — labels for scoring: rug_pull_activated, poisoned_tool_present, duplicate_tool_name, shadow_impersonation, exfil_simulated, injected_result_served, injected_notification_sent, ssrf_discovery_served, context_exfiltration, excessive_scope_advertised.

detector.py is a baseline detector: it raises findings from evidence only, then scores itself against the ground-truth labels. Replace it with your own tool and reuse the scoring harness.

Note: tool_shadowing and rug_pull interact by design. Two tools sharing a name make a naive "hash changed since last snapshot" heuristic report a spurious rug-pull on that name — a useful reminder to key rug-pull detection on stable tool identity, not name alone.

Note: prompt_injection's notification variant (get_notification) calls ServerSession.send_log_message, the SDK's logging-notification API. In mcp 2.x this capability is deprecated and delivery is per-request opt-in (a real client has to have asked for it), so whether the notification actually reaches a live client varies. Ground truth and the notification evidence record are emitted by the scenario unconditionally either way — the event log, not the wire, is what a detector is scored against.

Extending

Add a subclass of Scenario in scenarios.py and register it in REGISTRY:

class MyScenario(Scenario):
    name = "my_attack"
    def tools(self, state):
        return [types.Tool(name="...", description="...", inputSchema={...})]
    async def on_call(self, name, arguments, ctx, state):
        return [types.TextContent(type="text", text="...")]

Emit a ground-truth label whenever your attack actually fires (see self._label_once(...) and self.events.emit(..., ground_truth=True)) so it shows up in scoring.

推荐服务器

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

官方
精选