@7h3/protocol 0.4.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (248) hide show
  1. package/README.md +6 -323
  2. package/agentAdapter.d.ts +26 -0
  3. package/auditLog.d.ts +54 -0
  4. package/capability.d.ts +67 -0
  5. package/cborCodec.d.ts +34 -0
  6. package/conformanceVectors.d.ts +20 -0
  7. package/encryption.d.ts +85 -0
  8. package/envelopeCbor.d.ts +33 -0
  9. package/frameworkAdapters.d.ts +72 -0
  10. package/gateway.d.ts +53 -0
  11. package/grpcBinding.d.ts +29 -0
  12. package/httpBinding.d.ts +50 -0
  13. package/index.d.ts +39 -0
  14. package/index.js +3662 -0
  15. package/keyInfra.d.ts +64 -0
  16. package/keyRegistry.d.ts +9 -0
  17. package/keyRotation.d.ts +20 -0
  18. package/mcpGateway.d.ts +37 -0
  19. package/mcpTransports.d.ts +62 -0
  20. package/mcpWrapper.d.ts +83 -0
  21. package/otel.d.ts +65 -0
  22. package/package.json +13 -79
  23. package/policyEnforcer.d.ts +50 -0
  24. package/policyTelemetryFeedback.d.ts +11 -0
  25. package/protocol.d.ts +66 -0
  26. package/protocolAgent.d.ts +58 -0
  27. package/protocolBinary.d.ts +8 -0
  28. package/protocolCapabilities.d.ts +24 -0
  29. package/protocolReplay.d.ts +35 -0
  30. package/protocolTransport.d.ts +73 -0
  31. package/queueBinding.d.ts +43 -0
  32. package/rateLimiter.d.ts +18 -0
  33. package/{src/redisClient.ts → redisClient.d.ts} +25 -75
  34. package/replayStores.d.ts +98 -0
  35. package/revocation.d.ts +71 -0
  36. package/routePolicy.d.ts +30 -0
  37. package/runtimePolicy.d.ts +24 -0
  38. package/runtimePolicyManager.d.ts +15 -0
  39. package/runtimePolicyPresets.d.ts +11 -0
  40. package/signedResponse.d.ts +21 -0
  41. package/stream.d.ts +59 -0
  42. package/telemetry.d.ts +79 -0
  43. package/webhookBinding.d.ts +30 -0
  44. package/wsBinding.d.ts +52 -0
  45. package/.dockerignore +0 -19
  46. package/.github/dependabot.yml +0 -32
  47. package/.github/workflows/ci.yml +0 -31
  48. package/.github/workflows/publish.yml +0 -59
  49. package/.github/workflows/scorecard.yml +0 -37
  50. package/7h3.example.yaml +0 -125
  51. package/CHANGELOG.md +0 -92
  52. package/CONTRIBUTING.md +0 -82
  53. package/Dockerfile +0 -73
  54. package/GOVERNANCE.md +0 -62
  55. package/SECURITY.md +0 -70
  56. package/bench-results/replay-cache-full-1777891033256.json +0 -10
  57. package/bench-results/replay-cache-full-1777896317488.json +0 -10
  58. package/bench-results/replay-cache-full-1777900993184.json +0 -10
  59. package/bench-results/replay-cache-full-1777901019285.json +0 -10
  60. package/bench-results/replay-cache-quick-1777870170126.json +0 -10
  61. package/bench-results/signature-profiles-quick-1775875160079.json +0 -85
  62. package/bench-results/signature-profiles-quick-1775983539716.json +0 -85
  63. package/bench-results/signature-profiles-quick-1776237913190.json +0 -85
  64. package/bench-results/wire-codecs-full-1777891019803.json +0 -93
  65. package/bench-results/wire-codecs-full-1777896260964.json +0 -93
  66. package/bench-results/wire-codecs-full-1777901004247.json +0 -93
  67. package/bench-results/wire-codecs-quick-1775972879056.json +0 -93
  68. package/bench-results/wire-codecs-quick-1775983541111.json +0 -93
  69. package/bench-results/wire-codecs-quick-1776237914299.json +0 -93
  70. package/bench-results/wire-codecs-quick-1777841285236.json +0 -93
  71. package/bench-results/wire-codecs-quick-1777841321772.json +0 -93
  72. package/bench-results/wire-codecs-quick-1777841330408.json +0 -93
  73. package/bench-results/wire-codecs-quick-1777852886082.json +0 -93
  74. package/bench-results/wire-codecs-quick-1777852988773.json +0 -93
  75. package/bench-results/wire-codecs-quick-1777870188095.json +0 -93
  76. package/bench-results/wire-codecs-quick-1777870263918.json +0 -93
  77. package/bench-results/wire-codecs-quick-1777870455034.json +0 -93
  78. package/bench-results/wire-codecs-quick-1778816163081.json +0 -93
  79. package/bench-results/wire-codecs-quick-1778843936130.json +0 -93
  80. package/bin/7h3.ts +0 -385
  81. package/conformance/7h3_v0_1.json +0 -77
  82. package/conformance/7h3_v0_1_binary.json +0 -20
  83. package/conformance/aip_v0_1_binary.json +0 -20
  84. package/docker-compose.yaml +0 -77
  85. package/docs/ADOPTION_PLAN.md +0 -120
  86. package/docs/AGENTS.md +0 -77
  87. package/docs/AIP_RFC_v0.1.md +0 -97
  88. package/docs/AI_DECISION_CARD.md +0 -122
  89. package/docs/AI_RUNTIME_POLICY.json +0 -126
  90. package/docs/AI_RUNTIME_POLICY.yaml +0 -110
  91. package/docs/BACKPRESSURE_TUNING.md +0 -65
  92. package/docs/BENCHMARK_CLAIM_MATRIX.md +0 -42
  93. package/docs/BENCHMARK_REPORT_TEMPLATE.md +0 -169
  94. package/docs/BINARY_CODEC_BENCH.md +0 -23
  95. package/docs/CLEAN_CLONE_RUNBOOK.md +0 -36
  96. package/docs/CLOCK_SKEW_POLICY.md +0 -30
  97. package/docs/DISTRIBUTED_REPLAY.md +0 -142
  98. package/docs/FUZZ_CAMPAIGN.md +0 -121
  99. package/docs/GATEWAY.md +0 -195
  100. package/docs/KEY_MANAGEMENT_POLICY.md +0 -53
  101. package/docs/KEY_REVOCATION.md +0 -69
  102. package/docs/MCP_WRAPPER.md +0 -159
  103. package/docs/MIGRATION_GUIDE.md +0 -40
  104. package/docs/OPERATORS.md +0 -184
  105. package/docs/PERF_REGRESSION_POLICY.md +0 -34
  106. package/docs/PROJECT_EXAMINATION_2026-05-31.md +0 -219
  107. package/docs/RELEASE_BENCHMARK_REPORT_2026-05-15.md +0 -135
  108. package/docs/RELEASE_GATE.md +0 -25
  109. package/docs/RELEASE_NOTES_v0.1.0.md +0 -54
  110. package/docs/SECURITY_REVIEW_2026-06-05.md +0 -165
  111. package/docs/TELEMETRY.md +0 -41
  112. package/docs/THREAT_MODEL.md +0 -89
  113. package/docs/VERSIONING_POLICY.md +0 -30
  114. package/docs/assets/banner.png +0 -0
  115. package/eslint.config.js +0 -15
  116. package/fuzz/ts/harness-decode.ts +0 -136
  117. package/fuzz/ts/harness-verify.ts +0 -121
  118. package/fuzz/ts/run.ts +0 -35
  119. package/mcp-server/README.md +0 -38
  120. package/mcp-server/package-lock.json +0 -1187
  121. package/mcp-server/package.json +0 -35
  122. package/mcp-server/src/index.ts +0 -236
  123. package/mcp-server/tsconfig.json +0 -14
  124. package/scripts/aip-framework-quickstart.ts +0 -110
  125. package/scripts/aip-mcp-gateway.ts +0 -38
  126. package/scripts/aip-mcp-wrap-demo.ts +0 -72
  127. package/scripts/aip-quickstart.ts +0 -60
  128. package/scripts/bench-diff.ts +0 -118
  129. package/scripts/bench-protocol-e2e.ts +0 -937
  130. package/scripts/bench-protocol-openloop.ts +0 -1397
  131. package/scripts/bench-replay-cache.ts +0 -76
  132. package/scripts/bench-signature-profiles.ts +0 -180
  133. package/scripts/bench-wire-codecs.ts +0 -161
  134. package/scripts/build-binary-conformance.ts +0 -36
  135. package/scripts/build-release-dashboard.ts +0 -175
  136. package/scripts/canary-rollout.ts +0 -38
  137. package/scripts/mcpGatewayCli.test.ts +0 -116
  138. package/scripts/prepare-aip-package.ts +0 -88
  139. package/scripts/regen-conformance-sigs.ts +0 -18
  140. package/scripts/release-gate.ts +0 -19
  141. package/scripts/validate-runtime-policy.ts +0 -18
  142. package/sdk/browser/index.test.ts +0 -162
  143. package/sdk/browser/index.ts +0 -257
  144. package/sdk/browser/package.json +0 -13
  145. package/sdk/go/go.mod +0 -3
  146. package/sdk/go/http.go +0 -135
  147. package/sdk/go/protocol.go +0 -324
  148. package/sdk/go/protocol_test.go +0 -334
  149. package/sdk/go/webhook.go +0 -136
  150. package/sdk/python/README.md +0 -18
  151. package/sdk/python/protocol_7h3/__init__.py +0 -46
  152. package/sdk/python/protocol_7h3/http.py +0 -212
  153. package/sdk/python/protocol_7h3/keys.py +0 -149
  154. package/sdk/python/protocol_7h3/protocol.py +0 -525
  155. package/sdk/python/protocol_7h3/queue.py +0 -118
  156. package/sdk/python/protocol_7h3/webhook.py +0 -116
  157. package/sdk/python/pyproject.toml +0 -40
  158. package/sdk/python/tests/test_conformance.py +0 -110
  159. package/sdk/python/tests/test_http.py +0 -305
  160. package/sdk/python/tests/test_keys.py +0 -417
  161. package/sdk/python/tests/test_queue.py +0 -120
  162. package/sdk/python/tests/test_webhook.py +0 -345
  163. package/sdk/rust/Cargo.lock +0 -371
  164. package/sdk/rust/Cargo.toml +0 -25
  165. package/sdk/rust/README.md +0 -31
  166. package/sdk/rust/fuzz/Cargo.toml +0 -29
  167. package/sdk/rust/fuzz/fuzz_targets/fuzz_canonicalize.rs +0 -46
  168. package/sdk/rust/fuzz/fuzz_targets/fuzz_decode.rs +0 -11
  169. package/sdk/rust/src/bin/aip_mcp_gateway.rs +0 -59
  170. package/sdk/rust/src/http.rs +0 -145
  171. package/sdk/rust/src/keys.rs +0 -161
  172. package/sdk/rust/src/lib.rs +0 -688
  173. package/sdk/rust/src/queue.rs +0 -79
  174. package/sdk/rust/src/webhook.rs +0 -86
  175. package/sdk/rust/tests/conformance.rs +0 -148
  176. package/sdk/rust/tests/gateway.rs +0 -130
  177. package/sdk/rust/tests/http_webhook_queue.rs +0 -201
  178. package/sdk/rust/tests/keys.rs +0 -189
  179. package/src/agentAdapter.test.ts +0 -48
  180. package/src/agentAdapter.ts +0 -56
  181. package/src/auditLog.test.ts +0 -145
  182. package/src/auditLog.ts +0 -147
  183. package/src/conformance.test.ts +0 -136
  184. package/src/conformanceVectors.ts +0 -99
  185. package/src/frameworkAdapters.test.ts +0 -290
  186. package/src/frameworkAdapters.ts +0 -261
  187. package/src/gateway.test.ts +0 -343
  188. package/src/gateway.ts +0 -171
  189. package/src/grpcBinding.test.ts +0 -211
  190. package/src/grpcBinding.ts +0 -103
  191. package/src/httpBinding.test.ts +0 -376
  192. package/src/httpBinding.ts +0 -163
  193. package/src/index.ts +0 -32
  194. package/src/keyInfra.test.ts +0 -278
  195. package/src/keyInfra.ts +0 -228
  196. package/src/keyRegistry.ts +0 -59
  197. package/src/keyRotation.test.ts +0 -78
  198. package/src/keyRotation.ts +0 -72
  199. package/src/mcpGateway.test.ts +0 -129
  200. package/src/mcpGateway.ts +0 -250
  201. package/src/mcpTransports.test.ts +0 -92
  202. package/src/mcpTransports.ts +0 -169
  203. package/src/mcpWrapper.test.ts +0 -179
  204. package/src/mcpWrapper.ts +0 -206
  205. package/src/policyEnforcer.test.ts +0 -99
  206. package/src/policyEnforcer.ts +0 -169
  207. package/src/policyTelemetryFeedback.test.ts +0 -25
  208. package/src/policyTelemetryFeedback.ts +0 -38
  209. package/src/protocol.bench.ts +0 -37
  210. package/src/protocol.test.ts +0 -155
  211. package/src/protocol.ts +0 -413
  212. package/src/protocolAgent.test.ts +0 -105
  213. package/src/protocolAgent.ts +0 -169
  214. package/src/protocolBinary.test.ts +0 -165
  215. package/src/protocolBinary.ts +0 -312
  216. package/src/protocolCapabilities.ts +0 -70
  217. package/src/protocolFuzz.advanced.test.ts +0 -235
  218. package/src/protocolFuzz.test.ts +0 -111
  219. package/src/protocolNegative.test.ts +0 -97
  220. package/src/protocolReplay.test.ts +0 -71
  221. package/src/protocolReplay.ts +0 -194
  222. package/src/protocolTransport.test.ts +0 -556
  223. package/src/protocolTransport.ts +0 -483
  224. package/src/queueBinding.test.ts +0 -130
  225. package/src/queueBinding.ts +0 -102
  226. package/src/rateLimiter.test.ts +0 -96
  227. package/src/rateLimiter.ts +0 -46
  228. package/src/redisIntegration.test.ts +0 -134
  229. package/src/replayStores.test.ts +0 -141
  230. package/src/replayStores.ts +0 -82
  231. package/src/revocation.test.ts +0 -98
  232. package/src/revocation.ts +0 -0
  233. package/src/routePolicy.test.ts +0 -87
  234. package/src/routePolicy.ts +0 -72
  235. package/src/runtimePolicy.test.ts +0 -49
  236. package/src/runtimePolicy.ts +0 -81
  237. package/src/runtimePolicyManager.test.ts +0 -29
  238. package/src/runtimePolicyManager.ts +0 -50
  239. package/src/runtimePolicyPresets.ts +0 -43
  240. package/src/signedResponse.test.ts +0 -111
  241. package/src/signedResponse.ts +0 -83
  242. package/src/webhookBinding.test.ts +0 -144
  243. package/src/webhookBinding.ts +0 -115
  244. package/src/wsBinding.test.ts +0 -221
  245. package/src/wsBinding.ts +0 -100
  246. package/tsconfig.json +0 -15
  247. package/tsconfig.lib.json +0 -23
  248. package/vite.lib.config.ts +0 -16
package/keyInfra.d.ts ADDED
@@ -0,0 +1,64 @@
1
+ import type { KeyRegistry } from './keyRegistry';
2
+ export interface KeyEntry {
3
+ id: string;
4
+ algorithm: 'Ed25519';
5
+ publicKey: string;
6
+ created: number;
7
+ expires?: number;
8
+ revoked?: boolean;
9
+ revokedAt?: number;
10
+ }
11
+ export interface WellKnownKeysDocument {
12
+ version: '7h3/0.1';
13
+ updated: number;
14
+ keys: KeyEntry[];
15
+ }
16
+ export declare function serveWellKnownKeys(doc: WellKnownKeysDocument): string;
17
+ export declare function parseWellKnownKeys(json: string): WellKnownKeysDocument;
18
+ export declare function fetchWellKnownKeys(baseUrl: string, opts?: {
19
+ timeout?: number;
20
+ }): Promise<WellKnownKeysDocument>;
21
+ export declare function createHttpKeyRegistry(opts?: {
22
+ timeout?: number;
23
+ cacheMs?: number;
24
+ }): KeyRegistry;
25
+ export interface ManagedKeyPair {
26
+ id: string;
27
+ publicKey: string;
28
+ privateKey: string;
29
+ createdAt: number;
30
+ expiresAt?: number;
31
+ }
32
+ export interface KeyRotationOptions {
33
+ maxAgeMs: number;
34
+ overlapMs?: number;
35
+ }
36
+ export declare class KeyRotationManager {
37
+ private readonly maxAgeMs;
38
+ private readonly overlapMs;
39
+ private keys;
40
+ constructor(opts: KeyRotationOptions);
41
+ addKey(pair: ManagedKeyPair): void;
42
+ getCurrentKey(): ManagedKeyPair | null;
43
+ rotateIfNeeded(): Promise<ManagedKeyPair | null>;
44
+ getWellKnownDocument(): WellKnownKeysDocument;
45
+ toKeyRegistry(): KeyRegistry;
46
+ }
47
+ export interface RevocationEntry {
48
+ id: string;
49
+ revokedAt: number;
50
+ reason?: string;
51
+ }
52
+ export interface RevocationList {
53
+ version: '7h3/0.1';
54
+ updated: number;
55
+ revokedKeys: RevocationEntry[];
56
+ }
57
+ export declare class RevocationRegistry {
58
+ private revoked;
59
+ revoke(keyId: string, reason?: string): void;
60
+ isRevoked(keyId: string): boolean;
61
+ getList(): RevocationList;
62
+ importList(list: RevocationList): void;
63
+ wrapRegistry(inner: KeyRegistry): KeyRegistry;
64
+ }
@@ -0,0 +1,9 @@
1
+ export interface KeyRegistry {
2
+ getPublicKey(senderId: string): Promise<string | null>;
3
+ getSharedSecret?(keyId: string): Promise<string | null>;
4
+ }
5
+ export declare function createStaticKeyRegistry(keys: Record<string, string>): KeyRegistry;
6
+ export declare function createCompositeKeyRegistry(...registries: KeyRegistry[]): KeyRegistry;
7
+ export declare function createCachingKeyRegistry(inner: KeyRegistry, opts?: {
8
+ ttlMs?: number;
9
+ }): KeyRegistry;
@@ -0,0 +1,20 @@
1
+ import type { ProtocolEnvelope, SignatureVerificationMaterial } from './protocol';
2
+ export type KeyStatus = 'active' | 'verify-only' | 'revoked';
3
+ export interface KeyRecord {
4
+ sender: string;
5
+ keyId: string;
6
+ alg: SignatureVerificationMaterial['alg'];
7
+ status: KeyStatus;
8
+ notBeforeMs: number;
9
+ notAfterMs: number;
10
+ material: SignatureVerificationMaterial;
11
+ }
12
+ export declare class RollingKeyring {
13
+ private readonly records;
14
+ constructor(records?: KeyRecord[]);
15
+ add(record: KeyRecord): void;
16
+ revoke(sender: string, keyId: string, revokedAtMs?: number): boolean;
17
+ selectSigningKey(sender: string, alg: SignatureVerificationMaterial['alg'], nowMs?: number): KeyRecord | null;
18
+ resolveVerificationMaterial(signature: NonNullable<ProtocolEnvelope['signature']>, sender: string, nowMs?: number): SignatureVerificationMaterial | undefined;
19
+ }
20
+ export declare function createKeyringSignatureResolver(keyring: RollingKeyring, nowMsProvider?: () => number): (signature: NonNullable<ProtocolEnvelope["signature"]>, sender: string) => Promise<SignatureVerificationMaterial | undefined>;
@@ -0,0 +1,37 @@
1
+ import { type BootstrapRuntimePolicyEnforcerOptions } from './policyEnforcer';
2
+ import { type JsonRpcPolicyEvent, type JsonRpcBridgeOptions } from './frameworkAdapters';
3
+ export interface AipMcpGatewayOptions {
4
+ sharedSecret: string;
5
+ gatewayAgentId?: string;
6
+ workerAgentId?: string;
7
+ allowedMethods?: readonly string[];
8
+ workerCapabilityPrefix?: string;
9
+ methodToCapability?: JsonRpcBridgeOptions['methodToCapability'];
10
+ authorizeMethod?: JsonRpcBridgeOptions['authorizeMethod'];
11
+ rateLimitKey?: JsonRpcBridgeOptions['rateLimitKey'];
12
+ rateLimiter?: JsonRpcBridgeOptions['rateLimiter'];
13
+ onAuditEvent?: (event: AipMcpGatewayAuditEvent) => void | Promise<void>;
14
+ gatewayWireFormat?: 'json' | 'compact' | 'binary';
15
+ workerWireFormat?: 'json' | 'compact' | 'binary';
16
+ runtimePolicy?: {
17
+ enabled?: boolean;
18
+ options?: BootstrapRuntimePolicyEnforcerOptions;
19
+ concurrencyHint?: number;
20
+ latencySensitive?: boolean;
21
+ compatibilityFirst?: boolean;
22
+ };
23
+ }
24
+ export interface AipMcpGatewayRuntime {
25
+ handleLine: (line: string) => Promise<string | null>;
26
+ }
27
+ export interface AipMcpGatewayAuditEvent {
28
+ phase: 'request_received' | 'policy' | 'verification_failed' | 'request_success' | 'request_error';
29
+ id?: string | number | null;
30
+ method?: string;
31
+ code?: number;
32
+ message?: string;
33
+ policy?: JsonRpcPolicyEvent;
34
+ timestampMs: number;
35
+ }
36
+ export declare function createAipMcpGatewayRuntime(options: AipMcpGatewayOptions): AipMcpGatewayRuntime;
37
+ export declare function createAipMcpGatewayRuntimeWithPolicy(options: AipMcpGatewayOptions): Promise<AipMcpGatewayRuntime>;
@@ -0,0 +1,62 @@
1
+ import type { IncomingMessage, ServerResponse } from 'node:http';
2
+ import type { Readable, Writable } from 'node:stream';
3
+ import type { WireEnvelope } from './protocolTransport';
4
+ /**
5
+ * Transport adapters for the MCP hardening wrapper.
6
+ *
7
+ * The wrapper (`wrapMcpServer` / `wrapMcpClient`) is transport-agnostic: it
8
+ * operates on `WireEnvelope`s. These adapters connect it to real transports —
9
+ * newline-delimited stdio and HTTP — so a secured server is deployable as-is.
10
+ *
11
+ * Note: the stdio adapter frames one envelope per line and therefore supports
12
+ * the string wire formats (`json`, `compact`). For `binary`, use HTTP.
13
+ */
14
+ type WireHandler = (raw: WireEnvelope) => Promise<WireEnvelope>;
15
+ export interface StdioServerHandle {
16
+ close(): void;
17
+ }
18
+ /**
19
+ * Serves a wrapped MCP handler over newline-delimited streams (default
20
+ * `process.stdin` / `process.stdout`). Lines are processed sequentially so
21
+ * responses are emitted in request order.
22
+ */
23
+ export declare function serveMcpOverStdio(handle: WireHandler, options?: {
24
+ input?: Readable;
25
+ output?: Writable;
26
+ onError?: (error: unknown) => void;
27
+ }): StdioServerHandle;
28
+ export interface StdioClientHandle {
29
+ send: WireHandler;
30
+ close(): void;
31
+ }
32
+ /**
33
+ * Creates a stdio client `send` function for `wrapMcpClient`. Requests and
34
+ * responses are correlated by order over the single pipe (one envelope per line).
35
+ */
36
+ export declare function createStdioMcpClient(options: {
37
+ input: Readable;
38
+ output: Writable;
39
+ }): StdioClientHandle;
40
+ /**
41
+ * Creates a `node:http`-compatible request handler that runs the wrapped MCP
42
+ * handler. The request body is the wire envelope; the response body is the
43
+ * signed reply envelope.
44
+ */
45
+ export declare function createHttpMcpHandler(handle: WireHandler, options?: {
46
+ binary?: boolean;
47
+ }): (req: IncomingMessage, res: ServerResponse) => void;
48
+ export interface HttpClientHandle {
49
+ send: WireHandler;
50
+ }
51
+ /**
52
+ * Creates an HTTP client `send` function for `wrapMcpClient`. POSTs the request
53
+ * envelope to `url` and returns the response envelope. Uses the global `fetch`
54
+ * by default; inject `fetchImpl` for tests or custom agents.
55
+ */
56
+ export declare function createHttpMcpClient(options: {
57
+ url: string;
58
+ fetchImpl?: typeof fetch;
59
+ binary?: boolean;
60
+ headers?: Record<string, string>;
61
+ }): HttpClientHandle;
62
+ export {};
@@ -0,0 +1,83 @@
1
+ import { type ProtocolEnvelope } from './protocol';
2
+ import { type ReceiveEnvelopeOptions, type WireEnvelope, type WireFormat } from './protocolTransport';
3
+ /**
4
+ * MCP hardening middleware.
5
+ *
6
+ * MCP messages are plain JSON-RPC 2.0 and, as shipped, carry no signature or
7
+ * replay protection. These wrappers put an AIP envelope around each message on
8
+ * the wire — signing, TTL-bounding, and replay-checking every request and
9
+ * response — while the underlying MCP handler still receives plain JSON-RPC.
10
+ * "Zero app changes": your handler signature does not change.
11
+ *
12
+ * The wrappers enforce four bindings beyond signature verification:
13
+ * - recipient binding: the server only runs the handler when the envelope is
14
+ * addressed to it (`recipient === selfAgentId`) — defeats cross-server relay.
15
+ * - sender binding: the client only accepts responses from its expected peer
16
+ * (`sender === peerAgentId`) — defeats response spoofing.
17
+ * - correlation binding: the client only accepts a response whose
18
+ * `correlationId` matches the request it sent — defeats response substitution.
19
+ * - replay protection on by default (an `InMemoryReplayCache` if none supplied).
20
+ *
21
+ * Verification reuses the transport pipeline (`receiveEnvelope`), so it composes
22
+ * directly with a Redis-backed replay cache (`createRedisReplayStore`) and
23
+ * fleet-wide revocation (`withRevocationCheck`) via the `receive` options.
24
+ */
25
+ export type JsonRpcId = string | number | null;
26
+ export interface JsonRpcRequest {
27
+ jsonrpc: '2.0';
28
+ id: JsonRpcId;
29
+ method: string;
30
+ params?: unknown;
31
+ }
32
+ export interface JsonRpcResponse {
33
+ jsonrpc: '2.0';
34
+ id: JsonRpcId;
35
+ result?: unknown;
36
+ error?: {
37
+ code: number;
38
+ message: string;
39
+ data?: unknown;
40
+ };
41
+ }
42
+ export type McpJsonRpcHandler = (request: JsonRpcRequest) => JsonRpcResponse | Promise<JsonRpcResponse>;
43
+ export interface McpSecurityOptions {
44
+ /** This peer's agent id, used as the envelope `sender` and enforced as the inbound `recipient`. */
45
+ selfAgentId: string;
46
+ /** The peer's agent id: set as the envelope `recipient` (client) and enforced as the response `sender`. */
47
+ peerAgentId?: string;
48
+ /** Signs an unsigned envelope, e.g. `(e) => signEnvelopeHmac(e, secret, keyId)`. */
49
+ sign: (envelope: Omit<ProtocolEnvelope, 'signature'>) => Promise<ProtocolEnvelope>;
50
+ /** Inbound verification options: signature material resolver, replay cache, clock skew, etc. */
51
+ receive?: ReceiveEnvelopeOptions;
52
+ /** Wire format for the envelope on the wire. Default `compact`. */
53
+ wireFormat?: WireFormat;
54
+ /** Envelope TTL in ms. Default 60_000. */
55
+ ttlMs?: number;
56
+ }
57
+ export interface EncodedRequest {
58
+ raw: WireEnvelope;
59
+ /** The envelope `messageId` generated for this request; bind the response's `correlationId` to it. */
60
+ messageId: string;
61
+ }
62
+ export interface DecodeResponseOptions {
63
+ /** When set, require the response `correlationId` to equal this value (the request's messageId). */
64
+ expectCorrelationId?: string;
65
+ }
66
+ /**
67
+ * Wraps an existing MCP JSON-RPC handler so every inbound message is verified
68
+ * (signature + replay + TTL + recipient binding) before the handler runs, and
69
+ * every response is signed. Returns a function over the AIP wire envelope.
70
+ */
71
+ export declare function wrapMcpServer(handler: McpJsonRpcHandler, options: McpSecurityOptions): (raw: WireEnvelope) => Promise<WireEnvelope>;
72
+ export interface McpClientCodec {
73
+ encodeRequest(request: JsonRpcRequest): Promise<EncodedRequest>;
74
+ decodeResponse(raw: WireEnvelope, options?: DecodeResponseOptions): Promise<JsonRpcResponse>;
75
+ }
76
+ /** Signs outbound requests and verifies inbound responses (with sender + correlation binding). */
77
+ export declare function createMcpClientCodec(options: McpSecurityOptions): McpClientCodec;
78
+ /**
79
+ * Wraps a transport send function (`stdio`, HTTP, ...) so callers issue and
80
+ * receive plain JSON-RPC while AIP signing, verification, and request/response
81
+ * correlation happen on the wire.
82
+ */
83
+ export declare function wrapMcpClient(send: (raw: WireEnvelope) => Promise<WireEnvelope>, options: McpSecurityOptions): (request: JsonRpcRequest) => Promise<JsonRpcResponse>;
package/otel.d.ts ADDED
@@ -0,0 +1,65 @@
1
+ /**
2
+ * otel.ts — Optional OpenTelemetry integration for 7h3 Protocol
3
+ *
4
+ * Pass your @opentelemetry/api Tracer provider via setOtelProvider.
5
+ * No hard dependency — works with any OTel-compatible SDK or without any OTel at all.
6
+ *
7
+ * Duck-typed interfaces: any object matching the shape will work, including
8
+ * the real @opentelemetry/api TracerProvider.
9
+ *
10
+ * Usage:
11
+ * import { setOtelProvider } from '@7h3/protocol/otel'
12
+ * import { trace } from '@opentelemetry/api'
13
+ *
14
+ * setOtelProvider(trace.getTracerProvider())
15
+ *
16
+ * Without OTel configured, withVerificationSpan and withAuditSpan call fn(null)
17
+ * with zero overhead.
18
+ */
19
+ /**
20
+ * A minimal OTel-compatible span interface.
21
+ * Compatible with @opentelemetry/api Span.
22
+ */
23
+ export interface OtelSpan {
24
+ setAttribute(key: string, value: string | number | boolean): void;
25
+ end(): void;
26
+ recordException(error: Error): void;
27
+ }
28
+ /**
29
+ * A minimal OTel-compatible tracer interface.
30
+ * Compatible with @opentelemetry/api Tracer.
31
+ */
32
+ export interface OtelTracer {
33
+ startSpan(name: string, attrs?: Record<string, string | number>): OtelSpan;
34
+ }
35
+ /**
36
+ * A minimal OTel-compatible provider interface.
37
+ * Compatible with @opentelemetry/api TracerProvider.
38
+ */
39
+ export interface OtelProvider {
40
+ getTracer(name: string): OtelTracer;
41
+ }
42
+ /**
43
+ * Register an OTel provider. Call once at application startup.
44
+ * Subsequent calls replace the previous provider.
45
+ */
46
+ export declare function setOtelProvider(provider: OtelProvider): void;
47
+ /**
48
+ * Returns the active OTel tracer for '7h3/protocol', or null if no provider
49
+ * has been registered.
50
+ */
51
+ export declare function getOtelTracer(): OtelTracer | null;
52
+ /**
53
+ * Creates a span named '7h3.verify' if OTel is configured, calls fn(span),
54
+ * records any exception, and ends the span on completion.
55
+ *
56
+ * If no OTel provider is registered, calls fn(null) directly with zero overhead.
57
+ */
58
+ export declare function withVerificationSpan<T>(fn: (span: OtelSpan | null) => Promise<T>, attrs?: Record<string, string | number>): Promise<T>;
59
+ /**
60
+ * Creates a span named '7h3.audit.write' if OTel is configured, calls fn(span),
61
+ * records any exception, and ends the span on completion.
62
+ *
63
+ * If no OTel provider is registered, calls fn(null) directly with zero overhead.
64
+ */
65
+ export declare function withAuditSpan<T>(fn: (span: OtelSpan | null) => Promise<T>): Promise<T>;
package/package.json CHANGED
@@ -1,90 +1,24 @@
1
1
  {
2
2
  "name": "@7h3/protocol",
3
- "version": "0.4.0",
4
- "description": "7h3 Protocol: deterministic, signed, replay-safe AI-to-AI messaging with TypeScript/Python/Rust parity. Wire version 7h3/0.1.",
3
+ "version": "0.5.1",
4
+ "description": "7h3 Protocol: deterministic, signed, replay-safe AI-to-AI message envelopes (wire 7h3/0.1).",
5
5
  "type": "module",
6
- "license": "MIT",
7
- "main": "./dist/protocol/index.js",
8
- "types": "./dist/protocol/index.d.ts",
6
+ "main": "./index.js",
7
+ "types": "./index.d.ts",
9
8
  "exports": {
10
9
  ".": {
11
- "types": "./dist/protocol/index.d.ts",
12
- "import": "./dist/protocol/index.js"
13
- },
14
- "./protocol": {
15
- "types": "./dist/protocol/index.d.ts",
16
- "import": "./dist/protocol/index.js"
17
- },
18
- "./http": { "types": "./dist/protocol/httpBinding.d.ts", "import": "./dist/protocol/httpBinding.js" },
19
- "./webhook": { "types": "./dist/protocol/webhookBinding.d.ts", "import": "./dist/protocol/webhookBinding.js" },
20
- "./queue": { "types": "./dist/protocol/queueBinding.d.ts", "import": "./dist/protocol/queueBinding.js" },
21
- "./ws": { "types": "./dist/protocol/wsBinding.d.ts", "import": "./dist/protocol/wsBinding.js" },
22
- "./grpc": { "types": "./dist/protocol/grpcBinding.d.ts", "import": "./dist/protocol/grpcBinding.js" },
23
- "./keys": { "types": "./dist/protocol/keyInfra.d.ts", "import": "./dist/protocol/keyInfra.js" },
24
- "./key-registry": { "types": "./dist/protocol/keyRegistry.d.ts", "import": "./dist/protocol/keyRegistry.js" },
25
- "./gateway": { "types": "./dist/protocol/gateway.d.ts", "import": "./dist/protocol/gateway.js" },
26
- "./audit-log": { "types": "./dist/protocol/auditLog.d.ts", "import": "./dist/protocol/auditLog.js" },
27
- "./signed-response": { "types": "./dist/protocol/signedResponse.d.ts", "import": "./dist/protocol/signedResponse.js" },
28
- "./rate-limiter": { "types": "./dist/protocol/rateLimiter.d.ts", "import": "./dist/protocol/rateLimiter.js" },
29
- "./route-policy": { "types": "./dist/protocol/routePolicy.d.ts", "import": "./dist/protocol/routePolicy.js" }
10
+ "import": "./index.js",
11
+ "types": "./index.d.ts"
12
+ }
30
13
  },
14
+ "files": [
15
+ "index.js",
16
+ "*.d.ts",
17
+ "README.md"
18
+ ],
19
+ "license": "MIT",
31
20
  "repository": {
32
21
  "type": "git",
33
22
  "url": "https://github.com/IceMasterT/7h3-protocol-aip.git"
34
- },
35
- "bin": {
36
- "7h3": "./bin/7h3.js"
37
- },
38
- "scripts": {
39
- "cli:help": "tsx bin/7h3.ts help",
40
- "cli:keygen": "tsx bin/7h3.ts keygen",
41
- "lint": "eslint .",
42
- "test": "vitest run",
43
- "build:protocol": "tsc -p tsconfig.lib.json && vite build --config vite.lib.config.ts",
44
- "package:protocol": "npm run build:protocol && tsx scripts/prepare-aip-package.ts",
45
- "bench:protocol": "vitest bench src/protocol.bench.ts",
46
- "bench:e2e:quick": "tsx scripts/bench-protocol-e2e.ts --profile quick",
47
- "bench:e2e:full": "tsx scripts/bench-protocol-e2e.ts --profile full",
48
- "aip:quickstart": "tsx scripts/aip-quickstart.ts",
49
- "aip:framework:quickstart": "tsx scripts/aip-framework-quickstart.ts",
50
- "aip:mcp:gateway": "tsx scripts/aip-mcp-gateway.ts",
51
- "aip:mcp:wrap": "tsx scripts/aip-mcp-wrap-demo.ts",
52
- "bench:openloop:quick": "tsx scripts/bench-protocol-openloop.ts --profile quick --adaptive --p99-threshold 200 --drop-threshold-pct 2",
53
- "bench:openloop:full": "tsx scripts/bench-protocol-openloop.ts --profile full --adaptive --p99-threshold 200 --drop-threshold-pct 2 --adaptive-max-doublings 6 --adaptive-binary-steps 8",
54
- "bench:openloop:stress:quick": "tsx scripts/bench-protocol-openloop.ts --profile quick",
55
- "bench:openloop:stress:full": "tsx scripts/bench-protocol-openloop.ts --profile full",
56
- "bench:signatures:quick": "tsx scripts/bench-signature-profiles.ts --profile quick",
57
- "bench:signatures:full": "tsx scripts/bench-signature-profiles.ts --profile full",
58
- "bench:wire:quick": "tsx scripts/bench-wire-codecs.ts --profile quick",
59
- "bench:wire:full": "tsx scripts/bench-wire-codecs.ts --profile full",
60
- "bench:replay:quick": "tsx scripts/bench-replay-cache.ts --profile quick",
61
- "bench:replay:full": "tsx scripts/bench-replay-cache.ts --profile full",
62
- "conformance:binary": "tsx scripts/build-binary-conformance.ts",
63
- "release:gate": "tsx scripts/release-gate.ts",
64
- "policy:validate": "tsx scripts/validate-runtime-policy.ts",
65
- "canary:plan": "tsx scripts/canary-rollout.ts",
66
- "release:dashboard": "tsx scripts/build-release-dashboard.ts",
67
- "bench:diff": "tsx scripts/bench-diff.ts",
68
- "conformance:python": "PYTHONPATH=sdk/python python3 -m unittest discover -s sdk/python/tests -v",
69
- "conformance:rust": "cargo test --manifest-path sdk/rust/Cargo.toml",
70
- "fuzz:ts": "tsx fuzz/ts/run.ts",
71
- "fuzz:ts:decode": "tsx fuzz/ts/harness-decode.ts",
72
- "fuzz:ts:verify": "tsx fuzz/ts/harness-verify.ts",
73
- "fuzz:rust:setup": "cargo install cargo-fuzz 2>/dev/null || true",
74
- "fuzz:rust:decode": "cargo +nightly fuzz run fuzz_decode --manifest-path sdk/rust/fuzz/Cargo.toml -- -max_total_time=60",
75
- "fuzz:rust:canonicalize": "cargo +nightly fuzz run fuzz_canonicalize --manifest-path sdk/rust/fuzz/Cargo.toml -- -max_total_time=60"
76
- },
77
- "devDependencies": {
78
- "@eslint/js": "^10.0.1",
79
- "@types/node": "^25.9.3",
80
- "eslint": "^10.5.0",
81
- "fast-check": "^4.8.0",
82
- "globals": "^17.6.0",
83
- "tsx": "^4.20.6",
84
- "typescript": "~6.0.3",
85
- "typescript-eslint": "^8.61.0",
86
- "vite": "^8.0.16",
87
- "vitest": "^4.1.8",
88
- "ws": "^8.18.3"
89
23
  }
90
24
  }
@@ -0,0 +1,50 @@
1
+ import { type LoadRuntimePolicyOptions, type RuntimePolicy } from './runtimePolicy';
2
+ export type RuntimeMode = 'http' | 'ws' | 'http-binary' | 'http-batch' | 'http-binary-batch' | 'ws-batch' | 'ws-binary' | 'ws-binary-batch';
3
+ export interface ModeSelectionInput {
4
+ concurrency: number;
5
+ latencySensitive?: boolean;
6
+ compatibilityFirst?: boolean;
7
+ }
8
+ export interface TuningInput {
9
+ trafficClass: 'low' | 'medium' | 'high';
10
+ p99Ms?: number;
11
+ dropPct?: number;
12
+ }
13
+ export interface TuningDecision {
14
+ batchSizeRange: [number, number];
15
+ inflightCapRange: [number, number];
16
+ retryBackoffMsRange: [number, number];
17
+ actions: string[];
18
+ }
19
+ export interface RetryInput {
20
+ error: unknown;
21
+ attempt: number;
22
+ }
23
+ export interface InvariantConfig {
24
+ signatureVerification: boolean;
25
+ canonicalization: boolean;
26
+ replayDefense: boolean;
27
+ ttlClockSkewEnforcement: boolean;
28
+ }
29
+ export declare function createPolicyEnforcer(policy: RuntimePolicy): {
30
+ selectMode: (input: ModeSelectionInput) => RuntimeMode;
31
+ tune: (input: TuningInput) => TuningDecision;
32
+ shouldRetry: (input: RetryInput) => boolean;
33
+ assertInvariant: (config: InvariantConfig) => void;
34
+ metadata: {
35
+ policyName: string;
36
+ policyVersion: string;
37
+ policyStatus: string;
38
+ enforcedSlo: {
39
+ dropPctMax: number;
40
+ p99MsMax: number;
41
+ };
42
+ };
43
+ };
44
+ export type PolicyEnforcer = ReturnType<typeof createPolicyEnforcer>;
45
+ export type BootstrapRuntimePolicyEnforcerOptions = LoadRuntimePolicyOptions;
46
+ export declare function bootstrapRuntimePolicyEnforcer(options?: BootstrapRuntimePolicyEnforcerOptions): Promise<{
47
+ policy: RuntimePolicy;
48
+ enforcer: PolicyEnforcer;
49
+ }>;
50
+ export declare function isRuntimeMode(value: string): value is RuntimeMode;
@@ -0,0 +1,11 @@
1
+ export interface TelemetryFeedbackInput {
2
+ mode: string;
3
+ p99Ms: number;
4
+ dropPct: number;
5
+ concurrency: number;
6
+ }
7
+ export interface TelemetryFeedbackRecommendation {
8
+ severity: 'normal' | 'warn' | 'critical';
9
+ actions: string[];
10
+ }
11
+ export declare function recommendPolicyAdjustments(input: TelemetryFeedbackInput): TelemetryFeedbackRecommendation;
package/protocol.d.ts ADDED
@@ -0,0 +1,66 @@
1
+ export type ProtocolVersion = '7h3/0.1';
2
+ export type IntentKind = 'PING' | 'PONG' | 'CAPS' | 'TASK' | 'RESULT' | 'ERROR' | 'ENCRYPTED';
3
+ export interface ProtocolHeader {
4
+ version: ProtocolVersion;
5
+ messageId: string;
6
+ timestampMs: number;
7
+ ttlMs: number;
8
+ sender: string;
9
+ recipient?: string;
10
+ nonce: string;
11
+ }
12
+ export interface ProtocolBody {
13
+ intent: IntentKind;
14
+ content: string;
15
+ capability?: string;
16
+ correlationId?: string;
17
+ }
18
+ export interface ProtocolSignature {
19
+ alg: 'HS256' | 'ED25519';
20
+ keyId: string;
21
+ value: string;
22
+ }
23
+ export interface ProtocolEnvelope {
24
+ header: ProtocolHeader;
25
+ body: ProtocolBody;
26
+ signature?: ProtocolSignature;
27
+ }
28
+ export interface ProtocolDiagnostic {
29
+ level: 'error' | 'warning';
30
+ message: string;
31
+ }
32
+ export type SignatureVerificationMaterial = {
33
+ alg: 'HS256';
34
+ secret: string;
35
+ } | {
36
+ alg: 'ED25519';
37
+ publicKey: string;
38
+ };
39
+ export declare function canonicalizeEnvelope(envelope: Omit<ProtocolEnvelope, 'signature'>): string;
40
+ export declare function signCanonicalPayloadHmac(payload: string, secret: string): Promise<string>;
41
+ export declare function verifyCanonicalPayloadHmac(payload: string, signature: string, secret: string): Promise<boolean>;
42
+ export declare function generateEd25519KeypairBase64Url(): Promise<{
43
+ publicKey: string;
44
+ privateKey: string;
45
+ }>;
46
+ export declare function signCanonicalPayloadEd25519(payload: string, privateKeyPkcs8Base64Url: string): Promise<string>;
47
+ export declare function verifyCanonicalPayloadEd25519(payload: string, signature: string, publicKeySpkiBase64Url: string): Promise<boolean>;
48
+ export declare function signEnvelopeHmac(envelope: Omit<ProtocolEnvelope, 'signature'>, secret: string, keyId?: string): Promise<ProtocolEnvelope>;
49
+ export declare function verifyEnvelopeHmac(envelope: ProtocolEnvelope, secret: string): Promise<boolean>;
50
+ export declare function signEnvelopeEd25519(envelope: Omit<ProtocolEnvelope, 'signature'>, privateKeyPkcs8Base64Url: string, keyId?: string): Promise<ProtocolEnvelope>;
51
+ export declare function verifyEnvelopeEd25519(envelope: ProtocolEnvelope, publicKeySpkiBase64Url: string): Promise<boolean>;
52
+ export declare function verifyEnvelopeSignature(envelope: ProtocolEnvelope, material: SignatureVerificationMaterial): Promise<boolean>;
53
+ export declare function verifyCanonicalPayloadSignature(payload: string, signature: ProtocolSignature | undefined, material: SignatureVerificationMaterial): Promise<boolean>;
54
+ export declare function validateEnvelope(envelope: ProtocolEnvelope, nowMs?: number): ProtocolDiagnostic[];
55
+ export declare function createEnvelope(input: {
56
+ sender: string;
57
+ recipient?: string;
58
+ intent: IntentKind;
59
+ content: string;
60
+ capability?: string;
61
+ correlationId?: string;
62
+ ttlMs?: number;
63
+ messageId?: string;
64
+ nonce?: string;
65
+ nowMs?: number;
66
+ }): Omit<ProtocolEnvelope, 'signature'>;
@@ -0,0 +1,58 @@
1
+ import { type IntentKind, type ProtocolDiagnostic, type ProtocolEnvelope } from './protocol';
2
+ import { type AckMode } from './protocolCapabilities';
3
+ import { InMemoryReplayCache } from './protocolReplay';
4
+ import { type WireEnvelope, type WireFormat } from './protocolTransport';
5
+ export interface TaskHandlerResult {
6
+ intent?: 'RESULT' | 'ERROR';
7
+ content: string;
8
+ capability?: string;
9
+ }
10
+ export interface AgentSessionConfig {
11
+ agentId: string;
12
+ outboundSecret: string;
13
+ keyId?: string;
14
+ capabilities?: string[];
15
+ supportedWireFormats?: WireFormat[];
16
+ maxBatchSize?: number;
17
+ ackModes?: AckMode[];
18
+ requireSignature?: boolean;
19
+ replayCache?: InMemoryReplayCache;
20
+ sharedSecrets?: Record<string, string>;
21
+ resolveInboundSecret?: (keyId: string, sender: string) => string | undefined | Promise<string | undefined>;
22
+ onTask?: (envelope: ProtocolEnvelope) => TaskHandlerResult | Promise<TaskHandlerResult>;
23
+ }
24
+ export interface AgentReceiveResult {
25
+ ok: boolean;
26
+ diagnostics: ProtocolDiagnostic[];
27
+ received: ProtocolEnvelope | null;
28
+ response: ProtocolEnvelope | null;
29
+ }
30
+ export declare class AgentSession {
31
+ private readonly agentId;
32
+ private readonly outboundSecret;
33
+ private readonly keyId;
34
+ private readonly capabilities;
35
+ private readonly supportedWireFormats;
36
+ private readonly maxBatchSize;
37
+ private readonly ackModes;
38
+ private readonly requireSignature;
39
+ private readonly replayCache;
40
+ private readonly sharedSecrets;
41
+ private readonly resolveInboundSecret?;
42
+ private readonly onTask?;
43
+ constructor(config: AgentSessionConfig);
44
+ private inboundSecretResolver;
45
+ createSignedIntent(input: {
46
+ recipient?: string;
47
+ intent: IntentKind;
48
+ content: string;
49
+ capability?: string;
50
+ correlationId?: string;
51
+ ttlMs?: number;
52
+ messageId?: string;
53
+ nonce?: string;
54
+ nowMs?: number;
55
+ }): Promise<ProtocolEnvelope>;
56
+ receiveAndRespond(input: WireEnvelope | ProtocolEnvelope, nowMs?: number): Promise<AgentReceiveResult>;
57
+ private buildAutoResponse;
58
+ }
@@ -0,0 +1,8 @@
1
+ import type { ProtocolEnvelope } from './protocol';
2
+ import type { ReceiveEnvelopeResult } from './protocolTransport';
3
+ export declare const DEFAULT_MAX_BINARY_ENVELOPE_BYTES: number;
4
+ export interface BinaryDecodeOptions {
5
+ maxFrameBytes?: number;
6
+ }
7
+ export declare function encodeEnvelopeBinary(envelope: ProtocolEnvelope): Uint8Array;
8
+ export declare function decodeEnvelopeBinary(payload: Uint8Array, options?: BinaryDecodeOptions): ReceiveEnvelopeResult;