filter_func
Filter network traffic from Charles Proxy captures using host, method, or content criteria to isolate specific requests for debugging and monitoring.
Instructions
Filter traffic from a fixed capture window or the latest saved history package.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| capture_seconds | Yes | 录制持续时长,单位为秒。绝对不是 Unix 时间戳(如 1700000000)也不是毫秒时间戳(如 1700000000000)。0 表示读取最新历史流量包。 | |
| host_contains | No | 按 host 子串过滤(包含匹配)。例如:api.example.com | |
| http_method | No | HTTP 方法过滤。仅允许标准 HTTP 方法。必须是方法名,不是正则表达式,不是路径。 | |
| keyword_regex | No | 用于搜索请求/响应内容的 Python 正则表达式。建议使用短表达式,避免灾难性回溯。 | |
| keep_request | No | ||
| keep_response | No |
Implementation Reference
- charles_mcp/tools/legacy.py:44-115 (handler)The filter_func function is the handler that implements the traffic filtering logic based on provided criteria such as time, host, HTTP method, and regex keywords.
async def filter_func( capture_seconds: RecordSeconds, ctx: ToolContext, host_contains: HostContains = None, http_method: HttpMethodFilter = None, keyword_regex: KeywordRegex = None, keep_request: bool = True, keep_response: bool = True, ) -> list[dict]: """Filter traffic from a fixed capture window or the latest saved history package.""" logger.info( "Tool called: filter_func(capture_seconds=%s, host_contains=%s, http_method=%s)", capture_seconds, host_contains, http_method, ) deps = get_tool_dependencies(ctx) error_payload = seconds_input_error( parameter="capture_seconds", value=capture_seconds, max_allowed=deps.config.max_stoptime, retry_example='filter_func(capture_seconds=30, host_contains="api.example.com")', ) if error_payload: return error_payload host_contains_normalized = normalize_text_filter(host_contains) method_normalized, method_error = normalize_http_method(http_method) if method_error: return method_error if keyword_regex: valid, error_msg = validate_regex(keyword_regex) if not valid: return build_tool_guidance_error( parameter="keyword_regex", received=keyword_regex, reason=f"invalid regex: {error_msg}", valid_input="Provide a valid Python regular expression.", retry_example='filter_func(capture_seconds=0, keyword_regex="token|session")', ) raw_data = await get_proxy_data(capture_seconds, ctx, deps=deps) if not isinstance(raw_data, list): return raw_data return deps.history_service.filter_entries( raw_data, host_contains=host_contains_normalized, method_normalized=method_normalized, keyword_regex=keyword_regex, keep_request=keep_request, keep_response=keep_response, )