Skip to content

Conversation

JonDotsoy
Copy link
Contributor

@JonDotsoy JonDotsoy commented Aug 8, 2025

Overview

This PR implements a comprehensive tool filtering mechanism that allows users to selectively ignore specific tools from remote MCP servers using the --ignore-tool command line flag.

Changes Made

🚀 New Features

  1. Tool Filtering with --ignore-tool flag

    • Added support for multiple --ignore-tool flags to specify tools that should be filtered out
    • Supports wildcard patterns using * for flexible matching:
      • delete* - ignores all tools starting with "delete"
      • *account - ignores all tools ending with "account"
      • exactTool - ignores only the tool named exactly "exactTool"
  2. Bidirectional Filtering

    • Response Filtering: Automatically filters ignored tools from tools/list responses
    • Request Blocking: Prevents tools/call requests for ignored tools and returns appropriate error responses
  3. Pattern Matching Engine

    • Implemented patternToRegex() function to convert glob patterns to regular expressions
    • Added shouldIncludeTool() function for efficient pattern matching
    • Case-insensitive matching for better usability

🔧 Implementation Details

Command Line Parsing (src/lib/utils.ts)

  • Extended parseCommandLineArgs() to parse multiple --ignore-tool flags
  • Added validation and logging for ignored tool patterns
  • Returns ignoredTools array in the configuration object

Message Transformation (src/lib/utils.ts)

  • Created createMessageTransformer() utility for intercepting and transforming MCP messages
  • Implemented request interception to block calls to ignored tools
  • Implemented response transformation to filter tools from tools/list responses
  • Added proper error handling with JSON-RPC compliant error responses

Proxy Integration (src/proxy.ts)

  • Updated runProxy() function to accept and pass through ignoredTools parameter
  • Integrated tool filtering into the MCP proxy workflow

MCP Proxy Enhancement (src/lib/utils.ts)

  • Enhanced mcpProxy() function to support tool filtering
  • Added message blocking mechanism using symbols for type safety
  • Proper error response generation for blocked tool calls

📚 Documentation Updates

Updated README.md with comprehensive usage examples:

  • Basic usage with single and multiple --ignore-tool flags
  • Wildcard pattern examples with clear explanations
  • JSON configuration examples for MCP client setups

🧪 Comprehensive Test Suite

Added extensive test coverage in src/lib/utils.test.ts:

Command Line Parsing Tests (18 scenarios)

  • Basic server URL parsing
  • Port and transport strategy parsing
  • Custom headers and host configuration
  • Single and multiple ignored tool parsing
  • Mixed argument parsing validation

Tool Filtering Tests (6 scenarios)

  • Wildcard pattern matching (create*, *account)
  • Multiple pattern support
  • Exact match scenarios
  • Empty pattern handling
  • Non-matching pattern behavior

MCP Proxy Tests (8 scenarios)

  • Message proxying between client and server
  • Transport lifecycle management
  • Tool filtering in tools/list responses
  • Request blocking for ignored tools
  • Error response generation

Usage Examples

Basic Usage

npx mcp-remote https://remote.mcp.server/sse --ignore-tool deleteTask

Multiple Patterns

npx mcp-remote https://remote.mcp.server/sse --ignore-tool "delete*" --ignore-tool "remove*"

MCP Client Configuration

{
  "args": [
    "mcp-remote",
    "https://remote.mcp.server/sse",
    "--ignore-tool",
    "delete*",
    "--ignore-tool", 
    "remove*"
  ]
}

Manual Testing Checklist

✅ VS Code Integration

  • Test MCP client configuration with ignored tools
  • Verify filtered tools don't appear in tool lists
  • Confirm blocked tools return appropriate errors
  • Validate wildcard patterns work correctly

✅ Command Line Interface

  • Test single --ignore-tool flag parsing
  • Test multiple --ignore-tool flags parsing
  • Test wildcard patterns (delete*, *account, exact)
  • Test mixed arguments with other flags
  • Verify logging output shows ignored tools

Test Execution

Run the unit tests to validate all functionality:

pnpm run test:unit

Expected results:

  • ✅ All command line parsing tests pass (18/18)
  • ✅ All tool filtering tests pass (6/6)
  • ✅ All MCP proxy tests pass (8/8)
  • ✅ Total: 32 test scenarios passing

Breaking Changes

None. This is a backward-compatible addition that doesn't affect existing functionality when no --ignore-tool flags are provided.

Security Considerations

  • Tool filtering happens at the proxy level, providing an additional security layer
  • Blocked tools return proper JSON-RPC error responses instead of potentially leaking information
  • Pattern matching uses escaped regex to prevent injection attacks

@JonDotsoy JonDotsoy changed the title feat: ignore tool flag Add tool filtering support with --ignore-tool flag Aug 8, 2025
@JonDotsoy JonDotsoy marked this pull request as ready for review August 8, 2025 18:00
@geelen geelen force-pushed the feat/ignore-tool-flag branch from 59f8082 to 0c3357c Compare August 27, 2025 01:06
Copy link

pkg-pr-new bot commented Aug 27, 2025

Open in StackBlitz

npx https://pkg.pr.new/mcp-remote@137

commit: 94059ce

@geelen
Copy link
Owner

geelen commented Aug 27, 2025

Nice idea! I'm on parental leave at the moment so I don't have much capacity to test this out, but given that it's additive, I'm going to merge & release it as-is

@geelen geelen merged commit 8e7b804 into geelen:main Aug 27, 2025
3 checks passed
@geelen
Copy link
Owner

geelen commented Aug 27, 2025

Released as v0.1.22

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants