test_regex
Validate a regex pattern by testing it against all defined test cases to ensure it meets requirements.
Instructions
Test a regex pattern against all current test cases to see if it satisfies the requirements.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| pattern | Yes | The regex pattern to test | |
| flags | No | Optional regex flags (e.g., 'i' for case-insensitive, 'm' for multiline, 's' for dotall). Default is no flags. |
Implementation Reference
- src/mcpgex/server.py:55-73 (schema)Schema definition for test_regex tool: registers the tool with name 'test_regex', description, and input schema requiring 'pattern' (string) and optional 'flags' (string).
types.Tool( name="test_regex", description="Test a regex pattern against all current test cases to see if it satisfies the requirements.", inputSchema={ "type": "object", "properties": { "pattern": { "type": "string", "description": "The regex pattern to test" }, "flags": { "type": "string", "description": "Optional regex flags (e.g., 'i' for case-insensitive, 'm' for multiline, 's' for dotall). Default is no flags.", "default": "" } }, "required": ["pattern"] } ), - src/mcpgex/server.py:131-230 (handler)Handler logic for test_regex tool: compiles the regex pattern with optional flags, iterates over all test cases, performs re.search on each input string, compares matched groups against expected matches, and returns a detailed results report.
elif name == "test_regex": pattern = arguments.get("pattern", "") flags_str = arguments.get("flags", "") if not test_cases: return [ types.TextContent( type="text", text="No test cases defined. Please add test cases first using add_test_case." ) ] # Convert flags string to regex flags flags = 0 if flags_str: if 'i' in flags_str.lower(): flags |= re.IGNORECASE if 'm' in flags_str.lower(): flags |= re.MULTILINE if 's' in flags_str.lower(): flags |= re.DOTALL if 'x' in flags_str.lower(): flags |= re.VERBOSE try: compiled_pattern = re.compile(pattern, flags) except re.error as e: return [ types.TextContent( type="text", text=f"Invalid regex pattern: {e}" ) ] results = [] passed = 0 failed = 0 no_match = 0 results.append(f"Testing regex pattern: {pattern}") if flags_str: results.append(f"Flags: {flags_str}") results.append("-" * 50) for i, test_case in enumerate(test_cases, 1): input_str = test_case["input_string"] groups = test_case["groups"] expected_matches = test_case["expected_matches"] description = test_case.get("description", "") # Try to find the expected match in the input string match = compiled_pattern.search(input_str) if match: # Check if the match contains the expected substring matched_texts = [match.group(g) for g in groups] if groups else [match.group(0)] # Where groups = e.g. [1,2] if sorted(matched_texts) == sorted(expected_matches): results.append(f"✅ Test case {i}: PASSED") results.append(f" Input: '{input_str}'") results.append(f" Expected: {expected_matches}") results.append(f" Groups: '{groups}'") results.append(f" Matched: {matched_texts}") if description: results.append(f" Description: {description}") passed += 1 else: results.append(f"🛑 Test case {i}: FAILED") results.append(f" Input: '{input_str}'") results.append(f" Expected: '{expected_matches}'") results.append(f" Groups: '{groups}'") results.append(f" Matched: '{matched_texts}' (doesn't match expected)") if description: results.append(f" Description: {description}") failed += 1 else: results.append(f"0️⃣ Test case {i}: NO MATCHES FOUND") results.append(f" Input: '{input_str}'") results.append(f" Expected: '{expected_matches}'") results.append(f" Groups: '{groups}'") results.append(f" Matched: '{[]}'") if description: results.append(f" Description: {description}") no_match += 1 results.append("") results.append("-" * 50) results.append(f"Summary: {passed} passed, {failed} failed, {no_match} with no match") if failed == 0: results.append("🎉 All test cases passed! The regex pattern is working correctly.") else: results.append("💡 Some test cases failed. Consider adjusting the regex pattern.") return [ types.TextContent( type="text", text="\n".join(results) ) ] - src/mcpgex/server.py:55-73 (registration)Registration of test_regex as a tool via the @server.list_tools() decorator, returning it as a Tool object in the list of available tools.
types.Tool( name="test_regex", description="Test a regex pattern against all current test cases to see if it satisfies the requirements.", inputSchema={ "type": "object", "properties": { "pattern": { "type": "string", "description": "The regex pattern to test" }, "flags": { "type": "string", "description": "Optional regex flags (e.g., 'i' for case-insensitive, 'm' for multiline, 's' for dotall). Default is no flags.", "default": "" } }, "required": ["pattern"] } ),