by heizaheiza
Charles Proxy MCP server for AI agents with live capture, structured traffic analysis, and agent-friendly tool contracts
# Add to your Claude Code skills
git clone https://github.com/heizaheiza/Charles-mcpEnglish README | Tool Contract
Charles MCP Server 用于把 Charles Proxy 接入 MCP 客户端,让 agent 可以稳定地读取实时流量、分析历史录包,并在需要时再展开单条请求细节。
它解决的核心问题只有三个:
在 Charles 中依次进入:Proxy -> Web Interface Settings
请确认:
Enable web interfaceadmin123456菜单位置示意:

设置窗口示意:

无需 clone 仓库,无需手动创建虚拟环境。需要先安装 uv。
claude mcp add-json charles '{
"type": "stdio",
"command": "uvx",
"args": ["charles-mcp"],
"env": {
"CHARLES_USER": "admin",
"CHARLES_PASS": "123456",
"CHARLES_MANAGE_LIFECYCLE": "false"
}
}'
No comments yet. Be the first to share your thoughts!
{
"mcpServers": {
"charles": {
"command": "uvx",
"args": ["charles-mcp"],
"env": {
"CHARLES_USER": "admin",
"CHARLES_PASS": "123456",
"CHARLES_MANAGE_LIFECYCLE": "false"
}
}
}
}
[mcp_servers.charles]
command = "uvx"
args = ["charles-mcp"]
[mcp_servers.charles.env]
CHARLES_USER = "admin"
CHARLES_PASS = "123456"
CHARLES_MANAGE_LIFECYCLE = "false"
将以下提示词复制粘贴给任意 AI agent(Claude Code、ChatGPT、Gemini CLI、Cursor Agent 等),agent 会自动完成安装和配置:
Install the "charles-mcp" MCP server and configure it for my MCP client. Follow these steps exactly:
Step 1 — Detect OS:
Determine if this machine runs Windows, macOS, or Linux.
Step 2 — Ensure uv is installed:
Run: uv --version
If the command fails (uv not found):
- macOS/Linux: run: curl -LsSf https://astral.sh/uv/install.sh | sh
- Windows: run: powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"
After installing, verify uv works: uv --version
Step 3 — Detect which MCP client is installed:
Check in this order and use the FIRST match:
a) Claude Code — run: claude --version
If it succeeds, run this command and skip to Step 5:
claude mcp add-json charles '{"type":"stdio","command":"uvx","args":["charles-mcp"],"env":{"CHARLES_USER":"admin","CHARLES_PASS":"123456","CHARLES_MANAGE_LIFECYCLE":"false"}}'
b) Claude Desktop — check if config file exists:
- macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
- Windows: %APPDATA%\Claude\claude_desktop_config.json
- Linux: ~/.config/Claude/claude_desktop_config.json
c) Cursor — check if any of these exist:
- ~/.cursor/mcp.json
- .cursor/mcp.json (in current project)
d) Windsurf — check if exists:
- ~/.codeium/windsurf/mcp_config.json
If none detected, ask the user which client they use.
Step 4 — Write config (for clients b/c/d):
The config entry to add is:
"charles": {
"command": "uvx",
"args": ["charles-mcp"],
"env": {
"CHARLES_USER": "admin",
"CHARLES_PASS": "123456",
"CHARLES_MANAGE_LIFECYCLE": "false"
}
}
IMPORTANT: If the config file already exists, read it first, parse the JSON,
add the "charles" key inside "mcpServers" (create "mcpServers" if absent),
and write back. Do NOT overwrite other existing MCP server entries.
If the file does not exist, create it with:
{ "mcpServers": { "charles": { ... } } }
Step 5 — Verify:
Run: uvx charles-mcp
Wait 3 seconds then terminate the process.
If it starts without import errors, the installation is successful.
Step 6 — Report:
Print: "charles-mcp installed successfully. Please restart your MCP client to load the new server."
Also mention that Charles Proxy must be running with Web Interface enabled (Proxy → Web Interface Settings, username: admin, password: 123456).
127.0.0.1:8888推荐默认保持 CHARLES_MANAGE_LIFECYCLE=false。除非你明确希望 MCP server 接管 Charles 生命周期,否则不要让它在退出时关闭你的 Charles 进程。
| 变量 | 默认值 | 说明 |
| --- | --- | --- |
| CHARLES_USER | admin | Charles Web Interface 用户名 |
| CHARLES_PASS | 123456 | Charles Web Interface 密码 |
| CHARLES_PROXY_HOST | 127.0.0.1 | Charles 代理主机 |
| CHARLES_PROXY_PORT | 8888 | Charles 代理端口 |
| CHARLES_CONFIG_PATH | 自动探测 | Charles 配置文件路径 |
| CHARLES_REQUEST_TIMEOUT | 10 | 控制面 HTTP 超时秒数 |
| CHARLES_MAX_STOPTIME | 3600 | 有界录制最大时长 |
| CHARLES_MANAGE_LIFECYCLE | false | 是否由 MCP server 管理 Charles 启停 |
start_live_capturegroup_capture_analysisquery_live_capture_entriesget_traffic_entry_detailstop_live_capture这条路径的目标是先用最少 token 找到热点,再按需展开单条请求。
list_recordingsanalyze_recorded_trafficgroup_capture_analysis(source="history")get_traffic_entry_detail这条路径适合先浏览录包,再对结构化 summary 做筛选和 drill-down。
read_live_capture 与 peek_live_capture 现在只返回路由级摘要字段,例如 host、method、path、status,不再直接抛出完整原始 Charles entry,避免在实时轮询时快速打满上下文。query_live_capture_entries 改为只读分析入口,不会推进 live cursor。你可以基于同一个 capture_id 反复换过滤条件查询,而不会把历史增量“消费掉”。analyze_recorded_traffic 和 query_live_capture_entries 的 summary 项会显式返回 matched_fields 与 match_reasons,便于 agent 解释“为什么这条流量被选中”。get_traffic_entry_detail 默认 include_full_body=false、max_body_chars=2048。如果 detail 估算输出超过约 12,000 字符,会在 warnings 中提示缩小范围或关闭 full body。null 值,并隐藏 header_map、parsed_json、parsed_form、lower_name 等内部字段;需要查看头信息时请使用 headers 列表。README 只覆盖推荐使用的主流程工具。兼容保留入口不在本文档中展开。
| 工具 | 作用 | 何时使用 |
| --- | --- | --- |
| start_live_capture | 启动或接管当前 live capture,并返回 capture_id | 开始实时观察前 |
| read_live_capture | 按 cursor 增量读取 live capture,并只返回紧凑路由摘要 | 连续消费新增流量、只想先看 host/path/status 时 |
| peek_live_capture | 预览新增流量但不推进 cursor,并只返回紧凑路由摘要 | 想先看一眼而不改变读取进度时 |
| stop_live_capture | 结束 capture,并在需要时持久化快照 | 收尾或导出本次实时抓包时 |
| query_live_capture_entries | 对 live capture 输出结构化 summary,且不推进 cursor | 想从实时流量里反复筛关键请求时 |
| 工具 | 作用 | 何时使用 |
| --- | --- | --- |
| group_capture_analysis | 对 live 或 history 结果聚合分组 | 先看热点 host、path、status 时 |
| get_capture_analysis_stats | 返回分类统计结果 | 想快速知道 API、静态资源、错误流量占比时 |
| get_traffic_entry_detail | 读取单条 entry 的 detail,并在响应过大时给出 warnings | 已经拿到目标 entry_id,准备 drill-down 时 |
| analyze_recorded_traffic | 对指定录包或最新录包输出结构化 summary,并附带匹配原因 | 想分析历史 .chlsj 时 |
| 工具 | 作用 | 何时使用 |
| --- | --- | --- |
| list_recordings | 列出当前已保存的录包文件 | 想先知道有哪些历史录包时 |
| get_recording_snapshot | 读取某个录包的原始快照 | 需要完整查看某个保存快照时 |
| query_recorded_traffic | 直接对最新录包做轻量过滤 | 需要快速查找 host、method、regex 命中时 |
| 工具 | 作用 | 何时使用 |
| --- | --- | --- |
| charles_status | 查看 Charles 连接状态与当前 live capture 状态 | 怀疑连接异常或想确认 capture 是否仍在活动时 |
| throttling | 设置 Charles 弱网预设 | 需要模拟 3G / 4G / 5G / off 等网络条件时 |
| reset_environment | 恢复 Charles 配置并清理当前环境 | 需要回到干净环境时 |
所有工具默认返回完整原始内容,不做脱敏处理。如果上层需要 masking,应由 MCP 客户端或 agent 自行处理。
推荐先用 group_capture_analysis、query_live_capture_entries 或 analyze_recorded_traffic 确认目标,再调用 get_traffic_entry_detail。
默认不要一开始就请求 include_full_body=true。
所有 summary 和 detail 输出都经过了序列化瘦身:
header_map、parsed_json、parsed_form、lower_name 等内部字段不再出现在输出中null 的字段在序列化时自动剥离full_text 存在时,冗余的 preview_text 会被移除默认参数已调低以保护上下文窗口:
| 参数 | 旧默认 | 新默认 |
| --- | --- | --- |
| max_items | 20 | 10 |
| max_preview_chars | 256 | 128 |
| max_headers_per_side | 8 | 6 |
| max_body_chars | 4096 | 2048 |
如需更大范围查看,仍可手动传入更高的值。
history summary 会返回 recording_path,live summary 会返回 capture_id。
对 get_traffic_entry_detail:
recording_pathcapture_idstop_live_capture 的失败是可恢复的stop_live_capture 有两个稳定结束态:
status="stopped":真正关闭完成status="stop_failed":短重试后仍失败,但 capture 仍保留当返回 stop_failed 时,应同时关注:
recoverableactive_capture_preserved如果结果是:
{
"status": "stop_failed",
"recoverable": true,
"active_capture_preserved": true
}
说明当前 capture 仍然可继续读取、诊断、再次 stop,而不是已经被关闭。
运行测试:
python -m pytest -q
常用本地检查:
python charles-mcp-server.py
python -c "from charles_mcp.main import main; main()"
本项目受到 tianhetonghua/Charles-mcp-server 的启发,先向前作致谢。它证明了把 Charles Proxy 接入 MCP、交给 AI 使用,这条方向本身是成立的。
但对我想解决的问题来说,前作并不够用,所以我没有在它的基础上继续打补丁,而是从零重写了 charles-mcp。前作更偏向逆向工程 / 安全研究场景,核心能力围绕 harvest、keyword interlock、加密检测和任务级缓存展开;而这个项目的目标是让通用 AI agent 在 Claude Code、Codex、Cursor 等 MCP 客户端里,稳定、低 token、可重复地分析实时流量和历史录包。
我重写它,主要是因为需要这些前作没有完整覆盖的能力:
capture_id、cursor、recording_path 语义,让 agent 可以反复查询而不会把实时数据“读没了”所以,这不是简单分叉,也不是小修小补,而是一次面向 AI Agent 生态的完整重构。前作给了我出发点,而这个仓库则是在不同目标下,重新做出一个更稳定、更结构化、也更适合 agent 的实现。
如果这个项目对你有帮助,欢迎请我喝杯咖啡,支持后续维护与迭代。