Skip to main content
Glama
MCPGuard-Lab
by Lc517

MCPGuard-Lab

A security experiment project for MCP tool invocation based on the real mcp==2.0.0 SDK. It does not rely on external large models or API keys, and implements tool whitelisting, parameter validation, file boundary enforcement, SSRF protection, sensitive operation confirmation, output sanitization, tool description poisoning detection, and JSONL auditing through deterministic policies. Regression test cases verify that protections are not broken by subsequent changes.

Usage boundary: This project is intended only for local experimentation, authorized testing, and defense research. It does not include attack functionality against unauthorized targets.

Implemented Capabilities

  • MCP 2.0 stdio server and real client round-trip testing.

  • Six demonstration tools: safe calculation, file reading within root directory, note query, public URL request, tool metadata inspection, and write note after confirmation.

  • Tool whitelist and strict JSON Schema validation, rejecting extra parameters.

  • File path normalization, rejecting absolute paths, directory traversal, and root directory escape.

  • URL protocol, user info, port, and DNS resolution result checks, rejecting loopback, private network, link-local, and other non-public addresses.

  • Write operation confirmed=true gate.

  • Explicit labeling of untrusted tool outputs; recursive sanitization of passwords, tokens, API keys, and Bearer credentials.

  • High-risk prompt injection signal detection in tool names/descriptions.

  • Each decision written to JSONL: request ID, time, tool, parameters, allow/deny, reason, and result.

  • pytest unit/integration tests and reproducible attack regression reports.

Related MCP server: meok-mcp-injection-scan-mcp

Architecture

MCP Client
   │ stdio / MCP 2.0
   ▼
MCP Server ──► GuardedGateway ──► PolicyEngine
                      │                 ├─ allowlist + schema
                      │                 ├─ path boundary
                      │                 ├─ URL / SSRF
                      │                 └─ confirmation
                      ├─ Tool handler
                      ├─ untrusted labeling + redaction
                      └─ JSONL audit

The core principle is "decide first, execute second, sanitize output, and leave traces throughout." Prompt text does not directly gain additional permissions; permissions are determined by the whitelist and policies in the code.

Quick Start (Windows)

py -3.10 -m venv .venv
.\.venv\Scripts\python.exe -m pip install -e ".[dev]"
.\.venv\Scripts\python.exe -m pytest
.\.venv\Scripts\python.exe -m mcpguard.regression

Or run directly:

powershell -ExecutionPolicy Bypass -File .\scripts\verify.ps1

Reports will be generated in:

  • reports/regression-report.md

  • reports/regression-report.json

  • reports/regression-audit.jsonl

MCP Client Configuration Example

First run pip install -e ., then replace the following structure with the absolute path of the current repository:

{
  "mcpServers": {
    "mcpguard-lab": {
      "command": "D:/path/to/mcpguard-lab/.venv/Scripts/python.exe",
      "args": ["-m", "mcpguard.server"],
      "env": {
        "MCPGUARD_WORKSPACE": "D:/path/to/mcpguard-lab/fixtures/workspace",
        "MCPGUARD_NOTES": "D:/path/to/mcpguard-lab/fixtures/workspace/notes",
        "MCPGUARD_AUDIT": "D:/path/to/mcpguard-lab/reports/audit.jsonl"
      }
    }
  }
}

Real-World Boundaries

  • Tool description poisoning detection is a high-signal rule layer and cannot replace manual review and a fixed whitelist.

  • confirmed=true is an explicit confirmation gate in local experiments; production environments should use confirmation credentials issued by the host UI/identity system that cannot be forged by the model itself.

  • The URL tool currently disables redirects to avoid passing the check on the first hop and then redirecting to an internal network; a production version should also limit response size, content type, and egress network.

  • Labeling untrusted content reduces the risk of "data being treated as instructions," but the final host agent must still follow trust boundaries.

  • Regression data only represents fixed test cases within the repository and does not guarantee blocking all prompt injections or all MCP risks.

Table of Contents

src/mcpguard/       核心实现
tests/              单元测试与真实 MCP stdio 集成测试
fixtures/           可公开的本地测试数据
reports/            自动生成的测试和审计证据
scripts/verify.ps1  一键验证入口

References

Available Tools

6 tools
calculatorB

Evaluate numeric arithmetic without eval or shell execution.

Inputs: expression (required).

fetch_urlB

Fetch a public HTTP(S) URL after SSRF policy checks.

Inputs: url (required).

inspect_tool_metadataB

Screen an external tool name and description for high-signal prompt injection.

Inputs: name (required), description (required).

query_notesC

Search local Markdown notes. Returned text is untrusted data.

Inputs: query (required).

read_fileB

Read one UTF-8 text file inside the configured workspace root.

Inputs: path (required).

write_noteC

Write a Markdown note after explicit confirmation.

Inputs: title (required), content (required), confirmed (required).

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    TDQS

    B3.4/5.0

    Scored across 6 tools

    Disambiguation5/5

    Each tool targets a clearly distinct operation: arithmetic, file reading, note searching, URL fetching, security inspection, and note writing. No overlap in purpose.

    Naming Consistency4/5

    Five tools follow verb_noun pattern (read_file, query_notes, fetch_url, inspect_tool_metadata, write_note). The calculator tool uses a noun only, which is a minor deviation but still readable.

    Tool Count5/5

    Six tools is well-scoped for a security-focused lab server. Each tool earns its place without bloat or deficiency.

    Completeness4/5

    Covers core operations for a guarded lab environment: read, write, search notes, fetch URLs, compute, and inspect tool metadata. Missing file write or note deletion, but these are likely intentional safety constraints.

    Maintenance

    ActivityInactive
    ResponsivenessNo recent community issues

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      A
      quality
      B
      maintenance
      Open-source security scanner for MCP configs + source. Prompt injection, credential leaks, SSRF, tool poisoning. pip install mcp-safeguard. GitHub Action @v0.9.6. Black Hat Europe Arsenal 2026.
      7
      11
      MIT
    • F
      license
      Not graded
      quality
      B
      maintenance
      Provides real-time RCE, SSRF, and env leak interception for AI tool calls, with MCP server mode offering diagnostic and repair suggestions.
      -