Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and

### Added

- MCP `2026-07-28` stateless request metadata, mandatory `server/discover`, modern result decoration, and dual-era protocol tests.
- JSON Schema 2020-12 tool validation with deterministic tool discovery and modern-only cache metadata.
- Standard `resources/templates/list` discovery with modern concrete-resource separation and legacy catalog compatibility.
- Wire-level request ID presence and type validation across stdio and HTTP transports.
- Distributed worker-to-orchestrator callbacks for step events, lease renewals, and terminal results.
- Redis-backed distributed ownership leases with persisted `traces.current_attempt` and `traces.terminal_reason` state.
- Worker-side session attachment and stable distributed callback propagation for existing Appium sessions.
Expand All @@ -22,6 +26,10 @@ The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and

### Changed

- Streamable HTTP now validates modern `MCP-Protocol-Version`, `Mcp-Method`, and `Mcp-Name` headers while retaining `2025-11-25` and `2025-06-18` initialize compatibility.
- Tool call results now include native `structuredContent`, and modern HTTP no longer exposes GET streams or accepts client response envelopes.
- Modern Streamable HTTP rejects client notifications and uses request connection closure for cancellation.
- Resource reads now use current MCP `-32602` not-found and `-32603` internal error semantics instead of generic legacy server errors.
- Distributed dispatch now reserves an execution attempt before RPC dispatch and promotes that reserved attempt to `running` only after worker acceptance.
- Watchdog handling now distinguishes plan timeout from ownership-lease expiry and prevents stale callbacks from overwriting active or terminal traces.
- Worker registration now uses a routable advertise address instead of hard-coded `localhost`.
Expand Down
90 changes: 90 additions & 0 deletions MCP_2026_07_28_ALIGNMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# MCP 2026-07-28 升级说明

本仓库已按官方 Current 版本 `2026-07-28` 完成核心 server/transport 对齐,并继续兼容 initialize-based legacy 客户端。官方参考:

- [MCP 2026-07-28 Specification](https://modelcontextprotocol.io/specification/2026-07-28)
- [2026-07-28 Changelog](https://modelcontextprotocol.io/specification/2026-07-28/changelog)
- [Versioning and Compatibility](https://modelcontextprotocol.io/specification/2026-07-28/basic/versioning)
- [Streamable HTTP](https://modelcontextprotocol.io/specification/2026-07-28/basic/transports/streamable-http)
- [Server Discovery](https://modelcontextprotocol.io/specification/2026-07-28/server/discover)
- [Base Protocol](https://modelcontextprotocol.io/specification/2026-07-28/basic/index)
- [Resources](https://modelcontextprotocol.io/specification/2026-07-28/server/resources)
- [Tools](https://modelcontextprotocol.io/specification/2026-07-28/server/tools)

## 协议变化摘要

| 变化 | `2025-11-25` 及更早 | `2026-07-28` | 本仓库实现 |
| --- | --- | --- | --- |
| 生命周期 | `initialize` + `notifications/initialized` | 无握手、逐请求无状态 | 双时代兼容 |
| 版本/能力 | 初始化时协商 | 每个 request 的 `params._meta` | 强制验证两个必填 namespaced 字段 |
| 服务发现 | 依赖 initialize capabilities | `server/discover` 为服务端必选 | 已实现并返回全部支持版本 |
| 成功结果 | `resultType` 可缺失 | 每个结果必须声明 `resultType` | 现代结果统一补 `complete` |
| Request ID | 旧集成可能混淆 `null` 与 omitted | 仅 string/integer,且不得为 `null` | 三个 transport 统一保留 presence 并校验类型 |
| 服务身份 | initialize 的 `serverInfo` | 推荐每个 result 的 `_meta` | 现代结果统一补 namespaced `serverInfo` |
| HTTP | POST,可选 GET/SSE 和 session | 仅 POST,无 GET stream/session id | `/mcp` GET 返回 405,POST 无 session 状态 |
| HTTP 镜像头 | 仅协议版本 | 另需 `Mcp-Method`;指定方法需 `Mcp-Name` | 校验缺失、编码和 header/body 一致性 |
| 工具/资源缓存 | 无统一字段 | list/template-list/read 支持 `ttlMs`、`cacheScope` | 目录使用 public,授权相关 resource read 使用 private |
| 资源模板 | 可被旧集成混入 `resources/list` | 参数化 URI 由 `resources/templates/list` 返回 | Modern 分离 concrete resources 与 URI templates,legacy 保持原结果 |
| Tool schema | dialect 行为不统一 | 默认且至少支持 JSON Schema 2020-12 | 显式声明并使用 2020-12 校验器 |
| Tool result | structured object 限制较多 | `structuredContent` 可为任意 JSON | 同时返回 text 与原生 structured JSON |

## 当前支持矩阵

- Modern:`2026-07-28`,通过每个 request 的 `_meta` 选择,无 initialize。
- Legacy:`2025-11-25` 和 `2025-06-18`,继续通过 initialize 选择。
- `server/discover` 返回上述三个版本,顺序为 modern 优先。
- Modern 方法:`server/discover`、`tools/list`、`tools/call`、`resources/list`、`resources/templates/list`、`resources/read`,以及 stdio cancellation 处理。
- Modern `resources/list` 当前返回空 concrete resource 集合;三个参数化 Appium URI 由 `resources/templates/list` 返回。
- Legacy 的 `ping`、`notifications/initialized` 与现有兼容方法保持可用,但在 modern `_meta` 下返回 method not found。
- Modern Streamable HTTP 不接收 client notification;取消正在执行的 HTTP 请求应关闭该请求的响应流,`notifications/cancelled` 仅保留给 stdio。
- `/jsonrpc` 继续作为非标准 compatibility endpoint;标准 MCP HTTP endpoint 是 `/mcp`。

## Modern Request 要求

每个 JSON-RPC request 的 `params._meta` 必须包含:

```json
{
"io.modelcontextprotocol/protocolVersion": "2026-07-28",
"io.modelcontextprotocol/clientCapabilities": {}
}
```

`io.modelcontextprotocol/clientInfo` 可选,但建议每次提供。HTTP 同时要求:

- `Content-Type: application/json`
- `Accept: application/json, text/event-stream`
- `MCP-Protocol-Version` 与 body protocol version 一致
- `Mcp-Method` 与 JSON-RPC `method` 一致
- `tools/call`、`resources/read`、`prompts/get` 的 `Mcp-Name` 与 `params.name` / `params.uri` 一致
- 不安全 ASCII、非 ASCII 或首尾空白的 `Mcp-Name` 使用 `=?base64?{Base64}?=` sentinel

关键错误映射:

- Header 缺失、格式错误或与 body 不一致:HTTP 400 / JSON-RPC `-32020`
- Modern 必填 `_meta` 字段缺失:HTTP 400 / JSON-RPC `-32602`
- 请求版本不支持:HTTP 400 / JSON-RPC `-32022`,data 包含 `supported` 和 `requested`
- Modern RPC 未实现:HTTP 404 / JSON-RPC `-32601`
- Resource 不存在或对调用方不可见:JSON-RPC `-32602`;后端读取失败:JSON-RPC `-32603`

## 实现位置

- `internal/gateway/mcp/protocol.go`:版本、逐请求 metadata、discover、现代 result/cache decoration 与标准错误。
- `internal/gateway/jsonrpc/modern.go`:modern/legacy 分类与 modern 方法边界。
- `internal/gateway/mcphttp/handler.go`:双时代 Streamable HTTP、标准头和状态码。
- `internal/gateway/mcp/tools.go`:确定性排序和 JSON Schema 2020-12 预编译校验。
- `internal/gateway/mcp/handler.go`:legacy-compatible 基础结果与 `structuredContent`。

## 验证

```bash
go test ./internal/gateway/mcp ./internal/gateway/jsonrpc ./internal/gateway/mcphttp -count=1
go test ./... -count=1
go vet ./...
```

测试覆盖 modern discovery、逐请求 metadata、unsupported-version retry data、legacy 回归、request ID 校验、HTTP header mismatch、Base64 sentinel、modern 404、HTTP notification rejection、resource/template 分离、resource error codes、结果装饰、确定性 tool ordering,以及 `unevaluatedProperties` 的 JSON Schema 2020-12 行为。

## 非阻塞扩展项

Prompts、subscriptions/listen、sampling、elicitation、MCP Apps、Tasks 等仍是未声明的可选 capability,不影响当前 tools/resources server 对 `2026-07-28` 核心协议的支持。后续增加任何 capability 时,应同时加入 discover capability 声明、逐请求 client capability 检查及对应 conformance tests。
68 changes: 68 additions & 0 deletions MCP_ALIGNMENT_HANDOFF.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# MCP Alignment Handoff

Updated: 2026-08-06

This handoff records the current MCP alignment state in `/Users/stellajen/Documents/workspace/mcp_for_appium_v1` on branch `v1-work`.

## Current Status

The original P0 legacy alignment is complete, including request/notification classification, silent notification responses, cancellation, legacy `ping`, and `/mcp` Streamable HTTP support.

The follow-up implementation now targets the official Current MCP revision `2026-07-28` while preserving dual-era compatibility:

- Modern `2026-07-28` requests use stateless per-request `_meta` and do not initialize.
- Legacy `2025-11-25` and `2025-06-18` clients continue to use initialize semantics.
- `server/discover` advertises all three supported versions and the implemented tools/resources capabilities.
- Successful modern results receive `resultType: complete` and namespaced server identity metadata.
- Modern Streamable HTTP validates `MCP-Protocol-Version`, `Mcp-Method`, and method-specific `Mcp-Name`, including Base64 sentinel decoding.
- Modern HTTP removes GET streams and session IDs, maps header mismatch to `400/-32020`, unsupported versions to `400/-32022`, and unknown methods to `404/-32601`.
- Modern HTTP rejects client notifications because cancellation is represented by closing the request response stream; stdio cancellation remains supported.
- Modern tool and resource results receive method-specific cache metadata while legacy result shapes remain unchanged; tool ordering is deterministic.
- Modern parameterized resources are exposed through `resources/templates/list`; `resources/list` reports the currently enumerable concrete set while legacy callers retain the historical catalog shape.
- Resource reads map missing or caller-invisible URIs to `-32602` and backend failures to `-32603`, replacing the old generic `-32000` path.
- Shared JSON-RPC decoding distinguishes omitted IDs from explicit JSON null and accepts only string or integer request IDs.
- Tool schemas are explicitly advertised and validated as JSON Schema 2020-12.
- Tool calls now expose native results through `structuredContent` in addition to text content.

The detailed protocol and compatibility summary is in `MCP_2026_07_28_ALIGNMENT.md`.

## Primary Implementation Areas

- `internal/gateway/mcp/protocol.go`
- `internal/gateway/mcp/handler.go`
- `internal/gateway/mcp/tools.go`
- `internal/gateway/jsonrpc/modern.go`
- `internal/gateway/jsonrpc/handler.go`
- `internal/gateway/mcphttp/handler.go`
- `README.md`

## Test Coverage

Targeted tests cover:

- mandatory modern discovery over shared JSON-RPC and Streamable HTTP
- required request metadata and unsupported-version retry data
- result decoration and server identity
- legacy initialize, ping, notifications, and client response compatibility
- modern HTTP method/name/version header consistency
- Base64 sentinel decoding
- modern HTTP client response and notification rejection
- modern method-not-found HTTP 404 behavior
- modern concrete-resource and resource-template separation
- current resource-not-found and internal error codes
- explicit-null and non-integral request ID rejection
- cache metadata and deterministic tool order
- JSON Schema 2020-12 `unevaluatedProperties` behavior

## Remaining Optional Work

The server does not declare optional Prompts, subscriptions/listen, sampling, elicitation, MCP Apps, or Tasks capabilities. Adding one later requires capability advertisement, request-local client capability checks where applicable, transport behavior, and conformance tests.

Tool `title`, annotations, icons, and outputSchema remain optional metadata improvements. They are not required for the implemented tools/resources core server surface.

## Validation Commands

```bash
GOCACHE=/tmp/mcp-for-appium-go-cache go test ./... -count=1
GOCACHE=/tmp/mcp-for-appium-go-cache go vet ./...
```
Loading
Loading