Files
zeekayandHanzo Dev 896036e87d feat(go): MCP over ZAP, the sibling of zap-proto/http
The schema has described this wire since the repo opened and TypeScript has
spoken it since v1.0.0, but nothing in Go did — so every Go service reaching a
tool server still went over HTTP+SSE or stdio. MCP therefore sat BESIDE the ZAP
stack rather than on it, and the properties the wire already provides (PQ
confidentiality, mutual auth, zero-copy parse) had to be re-argued per
deployment.

Built as the sibling of zap-proto/http: same seams (Server/ListenAndServe/
Serve, Dial/Transport/Do), same offset discipline in the codec, same
relationship to the ZAP core. Neither module depends on the other, which is
why each carries its own framing — a protocol that had to import a sibling to
find its frame boundaries would make the wire's shape a property of whichever
sibling defined it first.

One value, two projections. MCP is JSON-RPC 2.0 and ZAP is binary, so the
obvious shape is two message types kept in step by hand. Instead there is one
Frame with two renderings: Marshal for the wire, json.Marshal for JSON-RPC. A
server answers frames and never learns which door one came in — which is what
lets a handler serve ZAP natively and have HTTP be an adapter over it rather
than a peer of it.

The schema's Payload union is encoded the way the sibling encodes its frame
type: the tag rides the message header flags, so a reader dispatches on the
envelope without decoding the body. The three arms share one object shape
because they overlap almost entirely and an unused field is a zeroed slot that
costs no bytes.

Two gates worth naming, both mutation-checked:

  - TestCodec_MatchesBuilder pins the encoder against the reference zap.Builder
    path byte for byte. Without it the hand-rolled offsets are only checked
    against themselves, and a decoder agreeing with a wrong encoder passes
    every round trip here while being unreadable by every other language.
  - TestServe_CloseBeforeServe covers a real deadlock this found: a Close that
    beat the accept loop to the lock saw no listener, closed nothing, and left
    Serve blocked in Accept forever. Serve now refuses to start once closed, so
    either half of the window stops the server.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-05 18:33:12 -07:00

33 lines
1.1 KiB
Go

package zapmcp
import (
"context"
"net"
)
// connKey carries the connection a frame arrived on into the handler's context.
type connKey struct{}
// with binds a connection to the context its frames are answered under.
func with(ctx context.Context, c net.Conn) context.Context {
return context.WithValue(ctx, connKey{}, c)
}
// Conn is the connection this frame arrived on, or nil when there is none —
// a frame handed to a handler in-process, or a test.
//
// It is the connection itself rather than a digest of it because what a caller
// wants to know differs by deployment and none of it belongs here: the remote
// address, the kernel's peer credential over a unix socket (which is the one
// thing about a caller the caller does not get to state), the transport's key
// material. A codec that pre-chewed those into a struct would have to guess
// which ones matter and would be wrong somewhere.
//
// if c := zapmcp.Conn(ctx); c != nil {
// cred, err := unix.GetsockoptUcred(...) // whatever this deployment needs
// }
func Conn(ctx context.Context) net.Conn {
c, _ := ctx.Value(connKey{}).(net.Conn)
return c
}