Cover image
Try Now
2025-04-05

开发阶段的MCP服务器的登记,深层调试和消毒的盾牌

3 years

Works with Finder

1

Github Watches

0

Github Forks

2

Github Stars

Shield MCP

Python Version License MCP Compatible

A security middleware for Model Context Protocol (MCP) servers that enhances security and monitoring capabilities without modifying the official SDK. This package provides tools for securing and monitoring MCP tool calls, following the best practices outlined in the MCP documentation. Abstract yourself while interact at MCP development.

Table of Contents

Features

  • Tool Access Control: Whitelist-based access control for MCP tools
  • Result Sanitization: Configurable sanitization of tool outputs
  • Structured Logging: Comprehensive audit logging using structlog
  • Rate Limiting: Token bucket algorithm for rate limiting
  • Error Handling: Standardized error handling and formatting
  • MCP Inspector Compatible: Works seamlessly with the MCP Inspector tool

Requirements

System Requirements

  • Python 3.8 or higher
  • pip (Python package installer)
  • virtualenv (recommended for development)

Quick Start

from shieldmcp import secure_tool
from shieldmcp.sanitizers import ToolSanitizer
from shieldmcp.rate_limit import RateLimitConfig

# Define allowed tools
ALLOWED_TOOLS = {"search", "read_file", "write_file"}

# Create a text sanitizer
text_sanitizer = ToolSanitizer.createTextSanitizer(
    max_length=1000,
    sensitive_patterns=[
        r"\b\d{16}\b",  # Credit card numbers
        r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b"  # Email addresses
    ]
)

# Configure rate limiting
rate_limit = RateLimitConfig(
    requests_per_minute=60,  # 1 request per second
    burst_size=10  # Allow bursts of up to 10 requests
)

# Apply the decorator to your MCP tools
@secure_tool(
    allowed_tools=ALLOWED_TOOLS,
    sanitize_fn=text_sanitizer,
    user_id="user123",
    session_id="session456",
    rate_limit=rate_limit
)
def search(query: str):
    # Your tool implementation
    return results

Components

Decorators (decorators.py)

The main @secure_tool decorator that orchestrates all security features:

@secure_tool(
    allowed_tools={"tool1", "tool2"},  # Set of allowed tool names
    sanitize_fn=your_sanitizer,        # Optional result sanitization function
    user_id="user123",                 # Optional user identifier
    session_id="session456",           # Optional session identifier
    rate_limit=RateLimitConfig(        # Optional rate limit configuration
        requests_per_minute=60,
        burst_size=10
    )
)
def your_tool():
    pass

Audit Logging (audit.py)

Structured logging using structlog:

from shieldmcp import ToolAudit

audit = ToolAudit()
audit.logToolCallStart(
    tool_name="search",
    args={"query": "test"},
    user_id="user123"
)

Access Control (access.py)

Tool access validation:

from shieldmcp import ToolAccess

access = ToolAccess(allowed_tools={"tool1", "tool2"})
access.validateToolAccess("tool1")  # Raises ValueError if not allowed

Sanitizers (sanitizers.py)

Result sanitization utilities:

from shieldmcp import ToolSanitizer

# Create a custom sanitizer
sanitizer = ToolSanitizer.createTextSanitizer(
    max_length=1000,
    sensitive_patterns=[r"\b\d{16}\b"]
)

# Use it directly
clean_text = sanitizer("Your text with sensitive data")

Rate Limiting (rate_limit.py)

Token bucket rate limiting:

from shieldmcp import RateLimitConfig

# Configure rate limits
config = RateLimitConfig(
    requests_per_minute=60,
    burst_size=10
)

Best Practices

Tool Access Control

  • Always define a whitelist of allowed tools
  • Use the most restrictive set of tools possible
  • Regularly review and update the whitelist

Result Sanitization

  • Sanitize all text output
  • Define patterns for sensitive data
  • Set reasonable length limits

Logging

  • Include user and session IDs when available
  • Log both successful and failed operations
  • Use structured logging for better analysis

Rate Limiting

  • Set appropriate limits based on tool complexity
  • Consider burst sizes for better user experience
  • Monitor rate limit hits in logs

Development

Setup Development Environment

# Clone the repository
git clone https://github.com/shieldmcp/shieldmcp.git
cd shieldmcp

# Create virtual environment
python -m venv venv
source venv/bin/activate  # or `venv\Scripts\activate` on Windows

# Install development dependencies
pip install -r requirements.txt

Running Tests

pytest tests/

Roadmap

Planned Features

  • Support for Clerk MCP and Github MCP
  • Extended documentation
  • TypeScript support

Acknowledgments


Feel free to make any inquiries.

相关推荐

  • Joshua Armstrong
  • Confidential guide on numerology and astrology, based of GG33 Public information

  • https://suefel.com
  • Latest advice and best practices for custom GPT development.

  • Emmet Halm
  • Converts Figma frames into front-end code for various mobile frameworks.

  • Elijah Ng Shi Yi
  • Advanced software engineer GPT that excels through nailing the basics.

  • Alexandru Strujac
  • Efficient thumbnail creator for YouTube videos

  • https://maiplestudio.com
  • Find Exhibitors, Speakers and more

  • Yusuf Emre Yeşilyurt
  • I find academic articles and books for research and literature reviews.

  • Carlos Ferrin
  • Encuentra películas y series en plataformas de streaming.

  • https://zenepic.net
  • Embark on a thrilling diplomatic quest across a galaxy on the brink of war. Navigate complex politics and alien cultures to forge peace and avert catastrophe in this immersive interstellar adventure.

  • https://reddgr.com
  • Delivers concise Python code and interprets non-English comments

  • apappascs
  • 发现市场上最全面,最新的MCP服务器集合。该存储库充当集中式枢纽,提供了广泛的开源和专有MCP服务器目录,并提供功能,文档链接和贡献者。

  • ShrimpingIt
  • MCP系列GPIO Expander的基于Micropython I2C的操作,源自ADAFRUIT_MCP230XX

  • pontusab
  • 光标与风浪冲浪社区,查找规则和MCP

  • av
  • 毫不费力地使用一个命令运行LLM后端,API,前端和服务。

  • 1Panel-dev
  • 🔥1Panel提供了直观的Web接口和MCP服务器,用于在Linux服务器上管理网站,文件,容器,数据库和LLMS。

  • Mintplex-Labs
  • 带有内置抹布,AI代理,无代理构建器,MCP兼容性等的多合一桌面和Docker AI应用程序。

  • GeyserMC
  • 与Minecraft客户端/服务器通信的库。

  • modelcontextprotocol
  • 模型上下文协议服务器

  • awslabs
  • AWS MCP服务器 - 将AWS最佳实践直接带入您的开发工作流程的专门MCP服务器

    Reviews

    4 (1)
    Avatar
    user_nA4yyF0o
    2025-04-18

    ShieldMCP is an essential tool for anyone working with MCP applications. It offers robust protection and seamless integration with your workflows, making it a must-have for developers. The interface is intuitive, and the setup process is straightforward. As a loyal MCP user, I highly recommend giving ShieldMCP a try! Check it out here: https://github.com/shieldmcp/shieldmcp.