AI Engineering from Scratch(英文原版)
Given a list of MCP server transports, produce a client harness that prefers MCP 2026-07-28 and isolates legacy compatibility.
Produce:
- Peer configuration. Map a stable server name to a pinned command or endpoint, arguments, environment allowlist, authorization context, transport kind, and an explicit
allow_legacyflag that defaults to false. - Modern request builder. Stamp protocol version, current client capabilities, and recommended client identity into every
params._metaimmediately before serialization. - stdio era probe. Send
server/discoverfirst. Accept a valid DiscoverResult, retry-32022at a mutually supported modern version, and treat-32020plus-32021as correctable modern errors. - Legacy compatibility probe. Treat an unrecognized error, timeout, connection close, or empty response as ambiguous. Send one deadline-bound
initializeonly when that exact peer hasallow_legacy: true. Select legacy only after a correlated JSON-RPC success containing a configured legacy revision, object capabilities, and non-empty server identity. Otherwise fail closed. - Tool cache. Honor
ttlMsandcacheScopein the negotiated authorization context. Treat absent legacyresultTypeas"complete". - Namespace merge. Sort peers and tools. Prefix or reject collisions. Forbid silent overwrite.
- Router. Map canonical tool names to peer plus local name, create a new request id, send the era-correct request, and verify the response id.
- Recovery. On transport loss, fail in-flight work, restart or reconnect, repeat discovery and lists, re-open subscriptions, and retry only operations allowed by the safety policy.
Hard rejects:
- Sending modern requests without current
_meta. - Falling back to initialization after a recognized modern error.
- Sending
initializeto a peer that is not explicitly allowlisted for legacy compatibility. - Treating a timeout, connection close, empty response, unrecognized error, malformed result, or unsupported revision as proof of legacy behavior.
- Treating a process, connection, or
Mcp-Session-Idas modern protocol state. - Sharing a private cached list across authorization contexts.
- Silently overwriting a duplicate tool name.
- Accepting a modern success without
resultType.
Refusal rules:
- Refuse to spawn a command outside a pinned allowlist.
- Refuse to route a tool when the owner is ambiguous.
- Refuse to retry a non-idempotent call automatically without an application idempotency key or user decision.
Output a complete Python harness, at least six conformance tests, and a startup report listing peer, selected era, selected version, cache scope, and canonical tool names.