Skip to content

feat(mcp): MCP Server skeleton + Bearer Token auth + get_proxy_status tool#12

Open
jayeliu wants to merge 4 commits into
inkdust2021:mainfrom
jayeliu:feature/mcp-server
Open

feat(mcp): MCP Server skeleton + Bearer Token auth + get_proxy_status tool#12
jayeliu wants to merge 4 commits into
inkdust2021:mainfrom
jayeliu:feature/mcp-server

Conversation

@jayeliu

@jayeliu jayeliu commented Jun 15, 2026

Copy link
Copy Markdown

Summary

  • 实现 MCP Server 骨架,支持 Streamable HTTP 协议(JSON-RPC 2.0)
  • Bearer Token 认证中间件,token 自动生成并持久化到 ~/.vibeguard/mcp_token
  • get_proxy_status 工具返回代理版本、启动时间、请求统计、拦截模式等
  • POST /mcp 路由接入 proxy.go HTTP 路由
  • 12 个测试覆盖全部行为

Closes #11

Test plan

  • go test ./internal/mcp/ -v — 12 个测试全部通过
  • go test ./... — 全量回归测试通过
  • 手动验证:启动 vibeguard → curl -X POST http://localhost:8080/mcp -H "Authorization: Bearer <token>" -d '{"jsonrpc":"2.0","method":"initialize","id":1}'

…xy_status tool

Implements Slice 1 of the MCP plugin: Streamable HTTP endpoint at POST /mcp
with JSON-RPC 2.0 protocol, Bearer Token authentication (auto-generated
random token persisted to ~/.vibeguard/mcp_token), and the get_proxy_status
tool that returns proxy runtime stats and configuration.

- JSON-RPC 2.0: initialize, tools/list, tools/call with proper error codes
- Auth middleware: 401 for missing/invalid Bearer Token
- Route wiring in proxy: POST /mcp intercepted before proxy fallback
- 12 tests covering protocol, auth, and tool behaviors
Jae Chen added 3 commits June 16, 2026 23:40
实现 Slice 2/3/4,覆盖 #2, #3, #4:

Slice 2 (Engine.Detect 只读检测 + detect_sensitive / preview_redacted):
- 新增 Engine.Detect 方法:扫描敏感数据但不生成占位符、不注册到 session
- detect_sensitive 工具:默认返回分类/位置,include_originals=true 时返回原始值
- preview_redacted 工具:返回带临时占位符的脱敏文本预览
- proxy.Server 暴露 GetRedactEngine() 供 MCP 访问

Slice 3 (规则查询工具):
- list_keywords 工具:返回当前加载的关键词规则(user/builtin/engine 来源)
- list_rule_lists 工具:返回规则列表元数据(id/name/enabled/source)
- Engine 新增 ListKeywords() 和 ListRegexCategories() 方法

Slice 4 (CLI mcp setup 向导):
- vibeguard mcp setup 命令:生成 Claude Code 兼容的 MCP 配置
- --format=json (默认) 输出完整 mcpServers JSON 配置
- --format=env 输出可 source 的环境变量
- token 不存在时给出友好提示

测试:
- internal/redact/engine_test.go: 6 个测试覆盖 Detect 只读性
- internal/mcp/server_test.go: 新增 7 个测试覆盖 5 个新工具
- 全量回归测试通过
启动编译后的二进制进程,验证 MCP 端到端通信路径。

覆盖行为:
- 进程启动后 /mcp 端点可达,返回有效 JSON-RPC 响应
- token 自动生成机制在真实文件系统上工作
- HTTP 路由正确分流(/mcp 不被 admin 或 proxy 拦截)
- 5 个工具的端到端响应(initialize, tools/list, get_proxy_status,
  detect_sensitive, preview_redacted, list_keywords, list_rule_lists)
- 认证:无 token 和无效 token 均返回 401
- detect_sensitive 默认不暴露原始值,include_originals=true 时返回

技术要点:
- 二进制只编译一次(sync.Once 共享)
- 随机端口避免冲突
- 临时 HOME 隔离每个测试的 .vibeguard 目录
- 显式禁用 rule_lists 让代理走 raw Engine 模式(detect 工具可用)
新增 docs/MCP.md,覆盖协议端点、Bearer Token 认证、CLI
一键配置、5 个工具的完整参数与返回字段、错误码、curl 示例、
Claude Code 集成步骤和故障排查。
@inkdust2021

Copy link
Copy Markdown
Owner

Thanks for the contribution! I pulled PR #12 locally and did a review plus runtime verification. My current recommendation is: do not merge yet.

The issue is not that the main proxy breaks, but that several core paths introduced by this MCP feature are reproducibly broken.

Findings:

  1. vibeguard mcp setup fails on first-use flow
    The MCP token is currently generated lazily on the first /mcp request, not when the service starts. In a clean environment, this flow fails:

    • vibeguard start
    • vibeguard mcp setup

    Error:

    Error: cannot read MCP token, make sure VibeGuard is running
    

    This does not match the documented setup flow.

  2. vibeguard mcp setup --config ... ignores the provided config file
    I tested with a custom config listening on 127.0.0.1:28792, but mcp setup --config custom.yaml still emitted the default URL:

    "url": "http://127.0.0.1:28657/mcp"
    

    The setup command should load the config path passed through the CLI instead of creating a fresh default config.NewManager().Load().

  3. MCP detect_sensitive / preview_redacted keep using a stale engine after config hot reload
    mcp.NewServer(..., s.GetRedactEngine(), ...) captures the engine pointer at startup. After ReloadFromConfig() updates runtime config, the MCP server still uses the old engine.

    I reproduced this by changing rules from alpha to beta: list_keywords saw the new config keyword beta, but detect_sensitive still matched the old engine keyword alpha.

  4. preview_redacted produces corrupted placeholders for overlapping matches
    Example: keyword secret plus regex secret123 produces:

    x __VG_KW_PREVIEW__EG_PREVIEW__ y
    

    Suggest reusing/extracting the existing overlap-safe redaction logic, or sorting/deduplicating/merging overlapping match ranges before applying temporary placeholders.

Please fix at least the first 3 blockers and add regression tests. The overlap issue in preview_redacted also seems small enough to fix in the same pass.

Thanks again for the PR. MCP support is a great direction for VibeGuard, and I’d be happy to review another revision after these issues are addressed.

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.

feat: MCP Server skeleton + Bearer Token auth + get_proxy_status tool

2 participants