@workerstack/clients 0.0.0-stage → 1.0.0-rc.23
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.
- package/LICENSE +21 -0
- package/README.md +619 -2
- package/bin/honey-clients.js +94 -0
- package/dist/cli.d.ts +102 -0
- package/dist/cli.js +190 -0
- package/dist/emit/cli/channels.d.ts +73 -0
- package/dist/emit/cli/channels.js +197 -0
- package/dist/emit/cli/commands.d.ts +27 -0
- package/dist/emit/cli/commands.js +187 -0
- package/dist/emit/cli/discovery.d.ts +35 -0
- package/dist/emit/cli/discovery.js +202 -0
- package/dist/emit/cli/flags.d.ts +59 -0
- package/dist/emit/cli/flags.js +186 -0
- package/dist/emit/cli/index.d.ts +1 -0
- package/dist/emit/cli/index.js +1 -0
- package/dist/emit/cli/install.d.ts +37 -0
- package/dist/emit/cli/install.js +88 -0
- package/dist/emit/cli/layout.d.ts +23 -0
- package/dist/emit/cli/layout.js +118 -0
- package/dist/emit/cli/module.d.ts +30 -0
- package/dist/emit/cli/module.js +186 -0
- package/dist/emit/cli/release.d.ts +40 -0
- package/dist/emit/cli/release.js +183 -0
- package/dist/emit/cli/skills.d.ts +11 -0
- package/dist/emit/cli/skills.js +56 -0
- package/dist/emit/csharp/errors.d.ts +7 -0
- package/dist/emit/csharp/errors.js +69 -0
- package/dist/emit/csharp/index.d.ts +1 -0
- package/dist/emit/csharp/index.js +1 -0
- package/dist/emit/csharp/json.d.ts +13 -0
- package/dist/emit/csharp/json.js +113 -0
- package/dist/emit/csharp/realtime.d.ts +10 -0
- package/dist/emit/csharp/realtime.js +33 -0
- package/dist/emit/csharp/sdk-answer.d.ts +48 -0
- package/dist/emit/csharp/sdk-answer.js +85 -0
- package/dist/emit/csharp/sdk-body.d.ts +10 -0
- package/dist/emit/csharp/sdk-body.js +87 -0
- package/dist/emit/csharp/sdk-client.d.ts +32 -0
- package/dist/emit/csharp/sdk-client.js +177 -0
- package/dist/emit/csharp/sdk-csproj.d.ts +36 -0
- package/dist/emit/csharp/sdk-csproj.js +136 -0
- package/dist/emit/csharp/sdk-doc.d.ts +12 -0
- package/dist/emit/csharp/sdk-doc.js +35 -0
- package/dist/emit/csharp/sdk-method.d.ts +26 -0
- package/dist/emit/csharp/sdk-method.js +187 -0
- package/dist/emit/csharp/sdk-plan.d.ts +38 -0
- package/dist/emit/csharp/sdk-plan.js +1 -0
- package/dist/emit/csharp/sdk.d.ts +40 -0
- package/dist/emit/csharp/sdk.js +105 -0
- package/dist/emit/csharp/sse.d.ts +18 -0
- package/dist/emit/csharp/sse.js +29 -0
- package/dist/emit/csharp/transport.d.ts +11 -0
- package/dist/emit/csharp/transport.js +67 -0
- package/dist/emit/csharp/types-enum.d.ts +19 -0
- package/dist/emit/csharp/types-enum.js +63 -0
- package/dist/emit/csharp/types-names.d.ts +33 -0
- package/dist/emit/csharp/types-names.js +182 -0
- package/dist/emit/csharp/types-record.d.ts +25 -0
- package/dist/emit/csharp/types-record.js +65 -0
- package/dist/emit/csharp/types-union.d.ts +27 -0
- package/dist/emit/csharp/types-union.js +113 -0
- package/dist/emit/csharp/types.d.ts +36 -0
- package/dist/emit/csharp/types.js +126 -0
- package/dist/emit/csharp/ws.d.ts +20 -0
- package/dist/emit/csharp/ws.js +14 -0
- package/dist/emit/go/errors.d.ts +7 -0
- package/dist/emit/go/errors.js +44 -0
- package/dist/emit/go/http.d.ts +11 -0
- package/dist/emit/go/http.js +50 -0
- package/dist/emit/go/index.d.ts +1 -0
- package/dist/emit/go/index.js +1 -0
- package/dist/emit/go/module-path.d.ts +2 -0
- package/dist/emit/go/module-path.js +27 -0
- package/dist/emit/go/sdk-answer.d.ts +28 -0
- package/dist/emit/go/sdk-answer.js +41 -0
- package/dist/emit/go/sdk-body.d.ts +24 -0
- package/dist/emit/go/sdk-body.js +76 -0
- package/dist/emit/go/sdk-client.d.ts +29 -0
- package/dist/emit/go/sdk-client.js +141 -0
- package/dist/emit/go/sdk-method.d.ts +68 -0
- package/dist/emit/go/sdk-method.js +172 -0
- package/dist/emit/go/sdk-mirror.d.ts +23 -0
- package/dist/emit/go/sdk-mirror.js +97 -0
- package/dist/emit/go/sdk-opts.d.ts +55 -0
- package/dist/emit/go/sdk-opts.js +100 -0
- package/dist/emit/go/sdk.d.ts +26 -0
- package/dist/emit/go/sdk.js +111 -0
- package/dist/emit/go/sse.d.ts +16 -0
- package/dist/emit/go/sse.js +26 -0
- package/dist/emit/go/types-codec.d.ts +15 -0
- package/dist/emit/go/types-codec.js +171 -0
- package/dist/emit/go/types-names.d.ts +14 -0
- package/dist/emit/go/types-names.js +64 -0
- package/dist/emit/go/types-struct.d.ts +29 -0
- package/dist/emit/go/types-struct.js +65 -0
- package/dist/emit/go/types-union.d.ts +17 -0
- package/dist/emit/go/types-union.js +84 -0
- package/dist/emit/go/types.d.ts +32 -0
- package/dist/emit/go/types.js +177 -0
- package/dist/emit/go/ws.d.ts +12 -0
- package/dist/emit/go/ws.js +70 -0
- package/dist/emit/java/gradle.d.ts +57 -0
- package/dist/emit/java/gradle.js +221 -0
- package/dist/emit/java/index.d.ts +2 -0
- package/dist/emit/java/index.js +2 -0
- package/dist/emit/java/json.d.ts +13 -0
- package/dist/emit/java/json.js +84 -0
- package/dist/emit/java/layout-ast.d.ts +146 -0
- package/dist/emit/java/layout-ast.js +102 -0
- package/dist/emit/java/layout-decl.d.ts +20 -0
- package/dist/emit/java/layout-decl.js +155 -0
- package/dist/emit/java/layout-doc.d.ts +55 -0
- package/dist/emit/java/layout-doc.js +166 -0
- package/dist/emit/java/layout-dot.d.ts +18 -0
- package/dist/emit/java/layout-dot.js +127 -0
- package/dist/emit/java/layout-strings.d.ts +8 -0
- package/dist/emit/java/layout-strings.js +76 -0
- package/dist/emit/java/layout-types.d.ts +11 -0
- package/dist/emit/java/layout-types.js +95 -0
- package/dist/emit/java/layout-visitor.d.ts +38 -0
- package/dist/emit/java/layout-visitor.js +232 -0
- package/dist/emit/java/layout.d.ts +61 -0
- package/dist/emit/java/layout.js +121 -0
- package/dist/emit/java/models-enum.d.ts +17 -0
- package/dist/emit/java/models-enum.js +51 -0
- package/dist/emit/java/models-names.d.ts +29 -0
- package/dist/emit/java/models-names.js +97 -0
- package/dist/emit/java/models-record.d.ts +24 -0
- package/dist/emit/java/models-record.js +79 -0
- package/dist/emit/java/models-union.d.ts +27 -0
- package/dist/emit/java/models-union.js +86 -0
- package/dist/emit/java/models.d.ts +28 -0
- package/dist/emit/java/models.js +118 -0
- package/dist/emit/java/realtime.d.ts +50 -0
- package/dist/emit/java/realtime.js +69 -0
- package/dist/emit/java/sdk-client.d.ts +66 -0
- package/dist/emit/java/sdk-client.js +207 -0
- package/dist/emit/java/sdk-decl.d.ts +11 -0
- package/dist/emit/java/sdk-decl.js +63 -0
- package/dist/emit/java/sdk-errors.d.ts +13 -0
- package/dist/emit/java/sdk-errors.js +141 -0
- package/dist/emit/java/sdk-headers.d.ts +12 -0
- package/dist/emit/java/sdk-headers.js +74 -0
- package/dist/emit/java/sdk-mirror.d.ts +14 -0
- package/dist/emit/java/sdk-mirror.js +107 -0
- package/dist/emit/java/sdk-models.d.ts +16 -0
- package/dist/emit/java/sdk-models.js +68 -0
- package/dist/emit/java/sdk-op.d.ts +57 -0
- package/dist/emit/java/sdk-op.js +216 -0
- package/dist/emit/java/sdk-params.d.ts +38 -0
- package/dist/emit/java/sdk-params.js +133 -0
- package/dist/emit/java/sdk-plan.d.ts +52 -0
- package/dist/emit/java/sdk-plan.js +1 -0
- package/dist/emit/java/sdk-record.d.ts +16 -0
- package/dist/emit/java/sdk-record.js +99 -0
- package/dist/emit/java/sdk.d.ts +49 -0
- package/dist/emit/java/sdk.js +216 -0
- package/dist/emit/java/sse.d.ts +35 -0
- package/dist/emit/java/sse.js +57 -0
- package/dist/emit/java/ws.d.ts +27 -0
- package/dist/emit/java/ws.js +31 -0
- package/dist/emit/mcp/annotations.d.ts +15 -0
- package/dist/emit/mcp/annotations.js +15 -0
- package/dist/emit/mcp/files.d.ts +18 -0
- package/dist/emit/mcp/files.js +42 -0
- package/dist/emit/mcp/index.d.ts +3 -0
- package/dist/emit/mcp/index.js +3 -0
- package/dist/emit/mcp/literal.d.ts +2 -0
- package/dist/emit/mcp/literal.js +33 -0
- package/dist/emit/mcp/mounted.d.ts +21 -0
- package/dist/emit/mcp/mounted.js +89 -0
- package/dist/emit/mcp/output-schema.d.ts +18 -0
- package/dist/emit/mcp/output-schema.js +38 -0
- package/dist/emit/mcp/registry.d.ts +23 -0
- package/dist/emit/mcp/registry.js +83 -0
- package/dist/emit/mcp/runtime.d.ts +10 -0
- package/dist/emit/mcp/runtime.js +47 -0
- package/dist/emit/mcp/schema.d.ts +19 -0
- package/dist/emit/mcp/schema.js +88 -0
- package/dist/emit/mcp/server.d.ts +30 -0
- package/dist/emit/mcp/server.js +246 -0
- package/dist/emit/mcp/tools.d.ts +51 -0
- package/dist/emit/mcp/tools.js +153 -0
- package/dist/emit/python/errors.d.ts +8 -0
- package/dist/emit/python/errors.js +68 -0
- package/dist/emit/python/index.d.ts +1 -0
- package/dist/emit/python/index.js +1 -0
- package/dist/emit/python/realtime.d.ts +10 -0
- package/dist/emit/python/realtime.js +33 -0
- package/dist/emit/python/sdk-answer.d.ts +49 -0
- package/dist/emit/python/sdk-answer.js +89 -0
- package/dist/emit/python/sdk-body.d.ts +11 -0
- package/dist/emit/python/sdk-body.js +40 -0
- package/dist/emit/python/sdk-client.d.ts +36 -0
- package/dist/emit/python/sdk-client.js +163 -0
- package/dist/emit/python/sdk-doc.d.ts +9 -0
- package/dist/emit/python/sdk-doc.js +32 -0
- package/dist/emit/python/sdk-imports.d.ts +14 -0
- package/dist/emit/python/sdk-imports.js +38 -0
- package/dist/emit/python/sdk-method.d.ts +29 -0
- package/dist/emit/python/sdk-method.js +183 -0
- package/dist/emit/python/sdk-plan.d.ts +33 -0
- package/dist/emit/python/sdk-plan.js +1 -0
- package/dist/emit/python/sdk-pyproject.d.ts +27 -0
- package/dist/emit/python/sdk-pyproject.js +103 -0
- package/dist/emit/python/sdk.d.ts +22 -0
- package/dist/emit/python/sdk.js +154 -0
- package/dist/emit/python/sse.d.ts +12 -0
- package/dist/emit/python/sse.js +23 -0
- package/dist/emit/python/transport.d.ts +15 -0
- package/dist/emit/python/transport.js +60 -0
- package/dist/emit/python/types-dict.d.ts +31 -0
- package/dist/emit/python/types-dict.js +45 -0
- package/dist/emit/python/types-names.d.ts +16 -0
- package/dist/emit/python/types-names.js +52 -0
- package/dist/emit/python/types-union.d.ts +32 -0
- package/dist/emit/python/types-union.js +46 -0
- package/dist/emit/python/types.d.ts +36 -0
- package/dist/emit/python/types.js +128 -0
- package/dist/emit/python/ws.d.ts +14 -0
- package/dist/emit/python/ws.js +16 -0
- package/dist/emit/rust/errors.d.ts +7 -0
- package/dist/emit/rust/errors.js +107 -0
- package/dist/emit/rust/http.d.ts +13 -0
- package/dist/emit/rust/http.js +67 -0
- package/dist/emit/rust/index.d.ts +1 -0
- package/dist/emit/rust/index.js +1 -0
- package/dist/emit/rust/realtime.d.ts +11 -0
- package/dist/emit/rust/realtime.js +29 -0
- package/dist/emit/rust/sdk-body.d.ts +19 -0
- package/dist/emit/rust/sdk-body.js +53 -0
- package/dist/emit/rust/sdk-cargo.d.ts +43 -0
- package/dist/emit/rust/sdk-cargo.js +178 -0
- package/dist/emit/rust/sdk-client.d.ts +25 -0
- package/dist/emit/rust/sdk-client.js +108 -0
- package/dist/emit/rust/sdk-method.d.ts +15 -0
- package/dist/emit/rust/sdk-method.js +119 -0
- package/dist/emit/rust/sdk-opts.d.ts +58 -0
- package/dist/emit/rust/sdk-opts.js +117 -0
- package/dist/emit/rust/sdk-plan.d.ts +33 -0
- package/dist/emit/rust/sdk-plan.js +1 -0
- package/dist/emit/rust/sdk.d.ts +23 -0
- package/dist/emit/rust/sdk.js +111 -0
- package/dist/emit/rust/sse.d.ts +19 -0
- package/dist/emit/rust/sse.js +20 -0
- package/dist/emit/rust/types-enum.d.ts +13 -0
- package/dist/emit/rust/types-enum.js +62 -0
- package/dist/emit/rust/types-names.d.ts +19 -0
- package/dist/emit/rust/types-names.js +66 -0
- package/dist/emit/rust/types-struct.d.ts +21 -0
- package/dist/emit/rust/types-struct.js +59 -0
- package/dist/emit/rust/types-union.d.ts +24 -0
- package/dist/emit/rust/types-union.js +97 -0
- package/dist/emit/rust/types.d.ts +34 -0
- package/dist/emit/rust/types.js +105 -0
- package/dist/emit/rust/ws.d.ts +21 -0
- package/dist/emit/rust/ws.js +20 -0
- package/dist/emit/shared/escape.d.ts +55 -0
- package/dist/emit/shared/escape.js +144 -0
- package/dist/emit/shared/format.d.ts +24 -0
- package/dist/emit/shared/format.js +23 -0
- package/dist/emit/shared/gofmt.d.ts +3 -0
- package/dist/emit/shared/gofmt.js +26 -0
- package/dist/emit/shared/lang.d.ts +60 -0
- package/dist/emit/shared/lang.js +233 -0
- package/dist/emit/shared/literals.d.ts +4 -0
- package/dist/emit/shared/literals.js +31 -0
- package/dist/emit/shared/metadata.d.ts +106 -0
- package/dist/emit/shared/metadata.js +130 -0
- package/dist/emit/shared/mirror.d.ts +22 -0
- package/dist/emit/shared/mirror.js +26 -0
- package/dist/emit/shared/ruff.d.ts +3 -0
- package/dist/emit/shared/ruff.js +31 -0
- package/dist/emit/shared/rustfmt.d.ts +3 -0
- package/dist/emit/shared/rustfmt.js +32 -0
- package/dist/emit/shared/status-keys.d.ts +7 -0
- package/dist/emit/shared/status-keys.js +67 -0
- package/dist/emit/shared/type-emitter.d.ts +19 -0
- package/dist/emit/shared/type-emitter.js +100 -0
- package/dist/emit/ts/app-client.d.ts +4 -0
- package/dist/emit/ts/app-client.js +68 -0
- package/dist/emit/ts/http.d.ts +20 -0
- package/dist/emit/ts/http.js +74 -0
- package/dist/emit/ts/package.d.ts +36 -0
- package/dist/emit/ts/package.js +126 -0
- package/dist/emit/ts/route-tree.d.ts +68 -0
- package/dist/emit/ts/route-tree.js +240 -0
- package/dist/emit/ts/sdk-types-preamble.d.ts +14 -0
- package/dist/emit/ts/sdk-types-preamble.js +123 -0
- package/dist/emit/ts/sdk-types.d.ts +13 -0
- package/dist/emit/ts/sdk-types.js +213 -0
- package/dist/emit/ts/sdk.d.ts +21 -0
- package/dist/emit/ts/sdk.js +173 -0
- package/dist/emit/ts/service-map.d.ts +40 -0
- package/dist/emit/ts/service-map.js +171 -0
- package/dist/emit/ts/tanstack-query.d.ts +12 -0
- package/dist/emit/ts/tanstack-query.js +156 -0
- package/dist/emit/ts/type-gen.d.ts +26 -0
- package/dist/emit/ts/type-gen.js +255 -0
- package/dist/emit/ts/webhooks.d.ts +19 -0
- package/dist/emit/ts/webhooks.js +81 -0
- package/dist/extract/chain.d.ts +34 -0
- package/dist/extract/chain.js +84 -0
- package/dist/extract/extract.d.ts +41 -0
- package/dist/extract/extract.js +191 -0
- package/dist/extract/serialize.d.ts +34 -0
- package/dist/extract/serialize.js +295 -0
- package/dist/extract/walk.d.ts +47 -0
- package/dist/extract/walk.js +109 -0
- package/dist/gen/args.d.ts +28 -0
- package/dist/gen/args.js +133 -0
- package/dist/gen/formatters.d.ts +62 -0
- package/dist/gen/formatters.js +146 -0
- package/dist/gen/process.d.ts +13 -0
- package/dist/gen/process.js +35 -0
- package/dist/gen/shipped.d.ts +17 -0
- package/dist/gen/shipped.js +55 -0
- package/dist/gen/targets.d.ts +93 -0
- package/dist/gen/targets.js +171 -0
- package/dist/gen/write.d.ts +33 -0
- package/dist/gen/write.js +118 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +15 -0
- package/dist/ir/build.d.ts +37 -0
- package/dist/ir/build.js +152 -0
- package/dist/ir/load.d.ts +11 -0
- package/dist/ir/load.js +112 -0
- package/dist/ir/model.d.ts +206 -0
- package/dist/ir/model.js +5 -0
- package/dist/ir/resolve-refs.d.ts +11 -0
- package/dist/ir/resolve-refs.js +66 -0
- package/dist/ir/resource-tree.d.ts +9 -0
- package/dist/ir/resource-tree.js +53 -0
- package/dist/ir/route-tree-intern.d.ts +18 -0
- package/dist/ir/route-tree-intern.js +174 -0
- package/dist/ir/schema.d.ts +11 -0
- package/dist/ir/schema.js +192 -0
- package/dist/ir/sdk-model.d.ts +86 -0
- package/dist/ir/sdk-model.js +204 -0
- package/dist/ir/slots.d.ts +42 -0
- package/dist/ir/slots.js +245 -0
- package/dist/runtime/cli/body.go +176 -0
- package/dist/runtime/cli/config.go +243 -0
- package/dist/runtime/cli/discovery.go +147 -0
- package/dist/runtime/cli/format.go +174 -0
- package/dist/runtime/cli/log.go +117 -0
- package/dist/runtime/cli/output.go +247 -0
- package/dist/runtime/cli/params.go +197 -0
- package/dist/runtime/cli/run.go +226 -0
- package/dist/runtime/cli/socket.go +60 -0
- package/dist/runtime/cli/stream.go +217 -0
- package/dist/runtime/cli/target.go +117 -0
- package/dist/runtime/csharp/Adapters.cs +183 -0
- package/dist/runtime/csharp/Auth.cs +56 -0
- package/dist/runtime/csharp/Body.cs +135 -0
- package/dist/runtime/csharp/Config.cs +161 -0
- package/dist/runtime/csharp/Envelope.cs +149 -0
- package/dist/runtime/csharp/Errors.cs +194 -0
- package/dist/runtime/csharp/Events.cs +155 -0
- package/dist/runtime/csharp/Exchange.cs +247 -0
- package/dist/runtime/csharp/Handler.cs +103 -0
- package/dist/runtime/csharp/Hooks.cs +128 -0
- package/dist/runtime/csharp/Invalidation.cs +102 -0
- package/dist/runtime/csharp/Json.cs +144 -0
- package/dist/runtime/csharp/Log.cs +48 -0
- package/dist/runtime/csharp/Operation.cs +33 -0
- package/dist/runtime/csharp/Path.cs +65 -0
- package/dist/runtime/csharp/Query.cs +72 -0
- package/dist/runtime/csharp/Realtime.cs +118 -0
- package/dist/runtime/csharp/RealtimeLoop.cs +236 -0
- package/dist/runtime/csharp/Redirect.cs +53 -0
- package/dist/runtime/csharp/Result.cs +57 -0
- package/dist/runtime/csharp/Sse.cs +122 -0
- package/dist/runtime/csharp/Stale.cs +151 -0
- package/dist/runtime/csharp/Transport.cs +143 -0
- package/dist/runtime/csharp/TransportLongPoll.cs +63 -0
- package/dist/runtime/csharp/TransportSse.cs +54 -0
- package/dist/runtime/csharp/TransportWs.cs +40 -0
- package/dist/runtime/csharp/Wire.cs +109 -0
- package/dist/runtime/csharp/Ws.cs +300 -0
- package/dist/runtime/csharp/WsDial.cs +127 -0
- package/dist/runtime/go/auth.go +127 -0
- package/dist/runtime/go/body.go +129 -0
- package/dist/runtime/go/config.go +75 -0
- package/dist/runtime/go/errors.go +185 -0
- package/dist/runtime/go/export.go +37 -0
- package/dist/runtime/go/hooks.go +53 -0
- package/dist/runtime/go/invalidation.go +112 -0
- package/dist/runtime/go/log.go +92 -0
- package/dist/runtime/go/nullable.go +64 -0
- package/dist/runtime/go/options.go +118 -0
- package/dist/runtime/go/path.go +156 -0
- package/dist/runtime/go/query.go +147 -0
- package/dist/runtime/go/realtime.go +239 -0
- package/dist/runtime/go/realtime_loop.go +208 -0
- package/dist/runtime/go/redirect.go +124 -0
- package/dist/runtime/go/request.go +222 -0
- package/dist/runtime/go/result.go +86 -0
- package/dist/runtime/go/sse.go +186 -0
- package/dist/runtime/go/stale.go +229 -0
- package/dist/runtime/go/stream.go +105 -0
- package/dist/runtime/go/transport.go +242 -0
- package/dist/runtime/go/transport_longpoll.go +121 -0
- package/dist/runtime/go/transport_sse.go +175 -0
- package/dist/runtime/go/ws.go +205 -0
- package/dist/runtime/java/Answer.java +107 -0
- package/dist/runtime/java/ApiException.java +110 -0
- package/dist/runtime/java/BinaryBody.java +36 -0
- package/dist/runtime/java/Body.java +105 -0
- package/dist/runtime/java/Call.java +23 -0
- package/dist/runtime/java/CallLog.java +51 -0
- package/dist/runtime/java/ClientConfig.java +233 -0
- package/dist/runtime/java/ConnectionState.java +12 -0
- package/dist/runtime/java/Credential.java +58 -0
- package/dist/runtime/java/Deadline.java +38 -0
- package/dist/runtime/java/Decode.java +181 -0
- package/dist/runtime/java/Envelope.java +141 -0
- package/dist/runtime/java/ErrorEnvelope.java +23 -0
- package/dist/runtime/java/EventStream.java +129 -0
- package/dist/runtime/java/Exchange.java +62 -0
- package/dist/runtime/java/FilePart.java +5 -0
- package/dist/runtime/java/Flight.java +306 -0
- package/dist/runtime/java/HoneyException.java +14 -0
- package/dist/runtime/java/Hooks.java +47 -0
- package/dist/runtime/java/Hops.java +133 -0
- package/dist/runtime/java/InvalidationConfig.java +73 -0
- package/dist/runtime/java/Json.java +23 -0
- package/dist/runtime/java/Keepalive.java +60 -0
- package/dist/runtime/java/LogEntry.java +14 -0
- package/dist/runtime/java/LongPollAdapter.java +68 -0
- package/dist/runtime/java/Operation.java +21 -0
- package/dist/runtime/java/Opt.java +121 -0
- package/dist/runtime/java/Pump.java +160 -0
- package/dist/runtime/java/RawResponse.java +48 -0
- package/dist/runtime/java/Realtime.java +152 -0
- package/dist/runtime/java/RealtimeLoop.java +371 -0
- package/dist/runtime/java/RedirectPolicy.java +13 -0
- package/dist/runtime/java/Redirects.java +126 -0
- package/dist/runtime/java/RequestContext.java +78 -0
- package/dist/runtime/java/RequestHook.java +9 -0
- package/dist/runtime/java/RequestOptions.java +91 -0
- package/dist/runtime/java/ResponseContext.java +70 -0
- package/dist/runtime/java/ResponseHook.java +12 -0
- package/dist/runtime/java/Result.java +32 -0
- package/dist/runtime/java/ResumableConnection.java +165 -0
- package/dist/runtime/java/SdkException.java +11 -0
- package/dist/runtime/java/Socket.java +301 -0
- package/dist/runtime/java/Sse.java +171 -0
- package/dist/runtime/java/SseAdapter.java +61 -0
- package/dist/runtime/java/SseEvent.java +10 -0
- package/dist/runtime/java/StaleMeta.java +16 -0
- package/dist/runtime/java/StaleTracker.java +287 -0
- package/dist/runtime/java/Transport.java +16 -0
- package/dist/runtime/java/TransportAdapter.java +14 -0
- package/dist/runtime/java/TransportConnection.java +15 -0
- package/dist/runtime/java/TransportKind.java +12 -0
- package/dist/runtime/java/TransportMessage.java +9 -0
- package/dist/runtime/java/TransportOptions.java +138 -0
- package/dist/runtime/java/TypedWebSocket.java +83 -0
- package/dist/runtime/java/UrlPath.java +49 -0
- package/dist/runtime/java/UrlQuery.java +99 -0
- package/dist/runtime/java/WsAdapter.java +52 -0
- package/dist/runtime/java/WsClose.java +47 -0
- package/dist/runtime/java/WsDial.java +148 -0
- package/dist/runtime/java/WsMessage.java +18 -0
- package/dist/runtime/mcp/call.ts +167 -0
- package/dist/runtime/mcp/files.ts +183 -0
- package/dist/runtime/mcp/guard.ts +176 -0
- package/dist/runtime/mcp/http.ts +159 -0
- package/dist/runtime/mcp/jsonrpc.ts +50 -0
- package/dist/runtime/mcp/listen.ts +76 -0
- package/dist/runtime/mcp/mount.ts +123 -0
- package/dist/runtime/mcp/protocol.ts +219 -0
- package/dist/runtime/mcp/reader.ts +57 -0
- package/dist/runtime/mcp/result.ts +85 -0
- package/dist/runtime/mcp/stdio.ts +79 -0
- package/dist/runtime/mcp/stream.ts +62 -0
- package/dist/runtime/python/__init__.py +2 -0
- package/dist/runtime/python/auth.py +67 -0
- package/dist/runtime/python/body.py +83 -0
- package/dist/runtime/python/config.py +139 -0
- package/dist/runtime/python/errors.py +213 -0
- package/dist/runtime/python/events.py +152 -0
- package/dist/runtime/python/exchange.py +298 -0
- package/dist/runtime/python/hooks.py +86 -0
- package/dist/runtime/python/invalidation.py +57 -0
- package/dist/runtime/python/log.py +60 -0
- package/dist/runtime/python/operation.py +25 -0
- package/dist/runtime/python/path.py +41 -0
- package/dist/runtime/python/query.py +48 -0
- package/dist/runtime/python/realtime.py +244 -0
- package/dist/runtime/python/redirect.py +93 -0
- package/dist/runtime/python/result.py +58 -0
- package/dist/runtime/python/send_async.py +189 -0
- package/dist/runtime/python/send_sync.py +105 -0
- package/dist/runtime/python/sse.py +81 -0
- package/dist/runtime/python/stale.py +94 -0
- package/dist/runtime/python/transport.py +157 -0
- package/dist/runtime/python/transport_longpoll.py +60 -0
- package/dist/runtime/python/transport_sse.py +60 -0
- package/dist/runtime/python/transport_ws.py +57 -0
- package/dist/runtime/python/ws.py +276 -0
- package/dist/runtime/rust/Cargo.lock +1763 -0
- package/dist/runtime/rust/answer.rs +120 -0
- package/dist/runtime/rust/auth.rs +80 -0
- package/dist/runtime/rust/body.rs +150 -0
- package/dist/runtime/rust/call.rs +200 -0
- package/dist/runtime/rust/config.rs +254 -0
- package/dist/runtime/rust/envelope.rs +84 -0
- package/dist/runtime/rust/errors.rs +235 -0
- package/dist/runtime/rust/events.rs +177 -0
- package/dist/runtime/rust/exchange.rs +287 -0
- package/dist/runtime/rust/hooks.rs +63 -0
- package/dist/runtime/rust/invalidation.rs +84 -0
- package/dist/runtime/rust/log.rs +88 -0
- package/dist/runtime/rust/nullable.rs +13 -0
- package/dist/runtime/rust/path.rs +83 -0
- package/dist/runtime/rust/query.rs +84 -0
- package/dist/runtime/rust/realtime.rs +182 -0
- package/dist/runtime/rust/realtime_loop.rs +177 -0
- package/dist/runtime/rust/redirect.rs +121 -0
- package/dist/runtime/rust/request.rs +178 -0
- package/dist/runtime/rust/result.rs +101 -0
- package/dist/runtime/rust/sse.rs +104 -0
- package/dist/runtime/rust/stale.rs +209 -0
- package/dist/runtime/rust/transport.rs +221 -0
- package/dist/runtime/rust/transport_longpoll.rs +108 -0
- package/dist/runtime/rust/transport_sse.rs +91 -0
- package/dist/runtime/rust/transport_ws.rs +75 -0
- package/dist/runtime/rust/ws.rs +322 -0
- package/dist/runtime/ts/app-types.ts +200 -0
- package/dist/runtime/ts/app.ts +104 -0
- package/dist/runtime/ts/config.ts +124 -0
- package/dist/runtime/ts/defaults.ts +48 -0
- package/dist/runtime/ts/error.ts +113 -0
- package/dist/runtime/ts/hooks.ts +92 -0
- package/dist/runtime/ts/http.ts +228 -0
- package/dist/runtime/ts/media-type.ts +36 -0
- package/dist/runtime/ts/path.ts +152 -0
- package/dist/runtime/ts/realtime-connection.ts +296 -0
- package/dist/runtime/ts/realtime-transports.ts +149 -0
- package/dist/runtime/ts/realtime.ts +293 -0
- package/dist/runtime/ts/redirect-policy.ts +17 -0
- package/dist/runtime/ts/redirects.ts +74 -0
- package/dist/runtime/ts/request.ts +239 -0
- package/dist/runtime/ts/response.ts +219 -0
- package/dist/runtime/ts/sdk-realtime.ts +10 -0
- package/dist/runtime/ts/sdk-sse.ts +55 -0
- package/dist/runtime/ts/sdk-ws.ts +21 -0
- package/dist/runtime/ts/sdk.ts +242 -0
- package/dist/runtime/ts/sse.ts +175 -0
- package/dist/runtime/ts/stale.ts +137 -0
- package/dist/runtime/ts/stream.ts +87 -0
- package/dist/runtime/ts/webhook.ts +116 -0
- package/dist/runtime/ts/ws.ts +155 -0
- package/dist/runtime/ts-tanstack-query/invalidate.ts +152 -0
- package/dist/runtime/ts-tanstack-query/query.ts +261 -0
- package/package.json +84 -5
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A file argument's value (bit 21 c, g): the io.modelcontextprotocol/files draft's FileInput (PR head 6101d34), its data
|
|
3
|
+
* canonical base64 of its size and digest, a call's inline bytes capped; a path a reading transport reads; a URL a
|
|
4
|
+
* mount's fetch reads. The extension's wire shape lives here alone: its identifier, its advertisement, its upload flow
|
|
5
|
+
* (a mount's uploads, a 2026-07-28 request declaring it): an input request of one `omit` target a data-less file, and
|
|
6
|
+
* on the retry each file's bytes taken once by the token its requestState echoes.
|
|
7
|
+
*/
|
|
8
|
+
export const FILES = "io.modelcontextprotocol/files"
|
|
9
|
+
|
|
10
|
+
/** The extension as advertised: the inline bytes one request may carry. */
|
|
11
|
+
export const filesCapability = (maxInlineBytes: number) => ({ [FILES]: { maxInlineBytes } })
|
|
12
|
+
|
|
13
|
+
/** Reads a file by its path, rejecting with the rule it breaks. */
|
|
14
|
+
export type Read = (path: string) => Promise<Uint8Array>
|
|
15
|
+
|
|
16
|
+
/** Fetches a file by its URL: its bytes, type and name; rejecting with the rule it breaks. */
|
|
17
|
+
export type FetchFile = (
|
|
18
|
+
url: string,
|
|
19
|
+
signal: AbortSignal,
|
|
20
|
+
) => Promise<{ bytes: Uint8Array; type: string; name?: string }>
|
|
21
|
+
|
|
22
|
+
/* an upload's call (credential headers, tool, arguments) and file (id, size, SHA-256) */
|
|
23
|
+
type Scope = { credential: string; tool: string; args: Record<string, unknown> }
|
|
24
|
+
type Declared = { id: string; size: number; digest: string }
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A mount's uploads for one request: targets under `base/`, a file's size cap, a token, and the retry's bytes, taken
|
|
28
|
+
* once; `take` rejects a token the call does not hold and resolves a missing upload's rule.
|
|
29
|
+
*/
|
|
30
|
+
export type Uploads = {
|
|
31
|
+
readonly base: string
|
|
32
|
+
readonly max: number
|
|
33
|
+
readonly issue: (scope: Scope, file: Declared) => Promise<string>
|
|
34
|
+
readonly take: (scope: Scope, file: Declared, token: string) => Promise<Uint8Array | string>
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** A call's files: a path reader, a URL fetch, uploads and their credential; a modern request's params and client. */
|
|
38
|
+
export type Files = {
|
|
39
|
+
readonly read?: Read
|
|
40
|
+
readonly fetch?: FetchFile
|
|
41
|
+
readonly uploads?: Uploads
|
|
42
|
+
readonly credential?: string
|
|
43
|
+
readonly asked?: Record<string, unknown>
|
|
44
|
+
readonly client?: { readonly extensions?: Record<string, unknown> }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/* canonical base64: whole quads, the last padded, its unused bits zero */
|
|
48
|
+
const BASE64 = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/][AQgw]==|[A-Za-z0-9+/]{2}[AEIMQUYcgkosw048]=)?$/
|
|
49
|
+
|
|
50
|
+
/** `text`'s bytes when it is canonical base64. */
|
|
51
|
+
export const decoded = (text: string): Uint8Array<ArrayBuffer> | undefined =>
|
|
52
|
+
BASE64.test(text) ? Uint8Array.from(atob(text), (char) => char.charCodeAt(0)) : undefined
|
|
53
|
+
|
|
54
|
+
/** `bytes` as base64, 32 KiB at a time (no Buffer, so a Worker runs it). */
|
|
55
|
+
export function encoded(bytes: Uint8Array): string {
|
|
56
|
+
let text = ""
|
|
57
|
+
for (let at = 0; at < bytes.length; at += 32_768) text += String.fromCharCode(...bytes.subarray(at, at + 32_768))
|
|
58
|
+
return btoa(text)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** `bytes`' SHA-256, base64url unpadded, as a FileInput's digest is. */
|
|
62
|
+
export const digestOf = async (bytes: Uint8Array<ArrayBuffer>) =>
|
|
63
|
+
encoded(new Uint8Array(await crypto.subtle.digest("SHA-256", bytes)))
|
|
64
|
+
.replace(/=+$/, "")
|
|
65
|
+
.replaceAll("+", "-")
|
|
66
|
+
.replaceAll("/", "_")
|
|
67
|
+
|
|
68
|
+
/** An inline FileInput's bytes, `budget.left` the inline bytes still allowed; else the rule it breaks. */
|
|
69
|
+
async function inlined(file: Record<string, unknown>, budget: { left: number; cap: number }) {
|
|
70
|
+
if (typeof file.data !== "string") return "has no data, and no upload is served here"
|
|
71
|
+
const bytes = decoded(file.data)
|
|
72
|
+
if (bytes === undefined) return "data is not base64"
|
|
73
|
+
if ((budget.left -= bytes.length) < 0) return `is past the ${budget.cap} inline bytes a call may carry`
|
|
74
|
+
if (bytes.length !== file.size) return "data does not decode to its size"
|
|
75
|
+
if ((await digestOf(bytes)) !== (file.digest as { value: unknown }).value) return "data does not match its digest"
|
|
76
|
+
return { bytes, ...named(file) }
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** A FileInput's type, and its name when it gives one. */
|
|
80
|
+
const named = (file: Record<string, unknown>) => ({
|
|
81
|
+
type: String(file.mimeType),
|
|
82
|
+
...(typeof file.name === "string" && { name: file.name }),
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
/** A FileInput as its upload binds it. */
|
|
86
|
+
const declared = (file: Record<string, unknown>): Declared => ({
|
|
87
|
+
digest: String((file.digest as { value: unknown }).value),
|
|
88
|
+
id: String(file.id),
|
|
89
|
+
size: Number(file.size),
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
type Pending = readonly (readonly [label: string, value: Record<string, unknown>])[]
|
|
93
|
+
|
|
94
|
+
/** The first call's input request: one `omit` target a pending file, the tokens its requestState. */
|
|
95
|
+
async function asked(uploads: Uploads, scope: Scope, pending: Pending) {
|
|
96
|
+
/* a whole size within the cap, a SHA-256's 43 base64url characters: no `.` in a token's fields */
|
|
97
|
+
const wrong = pending.find(([, { size, ...value }]) => {
|
|
98
|
+
const fits = Number.isSafeInteger(size) && (size as number) >= 0 && (size as number) <= uploads.max
|
|
99
|
+
return !(fits && /^[\w-]{43}$/.test(declared(value).digest))
|
|
100
|
+
})
|
|
101
|
+
if (wrong)
|
|
102
|
+
return `Invalid arguments: ${wrong[0]} needs a whole size within the ${uploads.max}-byte upload cap and a SHA-256`
|
|
103
|
+
const tokens = await Promise.all(pending.map(([, value]) => uploads.issue(scope, declared(value))))
|
|
104
|
+
const files = pending.map(([, value], at) => ({
|
|
105
|
+
credentials: "omit",
|
|
106
|
+
fileId: value.id,
|
|
107
|
+
url: `${uploads.base}/${tokens[at]}`,
|
|
108
|
+
}))
|
|
109
|
+
return {
|
|
110
|
+
inputRequired: {
|
|
111
|
+
inputRequests: { uploads: { method: `${FILES}/upload`, params: { files } } },
|
|
112
|
+
requestState: tokens.join(" "),
|
|
113
|
+
},
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** The retry's bytes: one outcome a pending file, the state one token a file in its order, each file taken once. */
|
|
118
|
+
async function retried(uploads: Uploads, scope: Scope, pending: Pending, params: Record<string, unknown>) {
|
|
119
|
+
const answered = (params.inputResponses as { uploads?: { resultType?: unknown; files?: unknown } } | undefined)
|
|
120
|
+
?.uploads
|
|
121
|
+
const listed = Array.isArray(answered?.files) ? (answered.files as ({ fileId?: unknown } | null)[]) : []
|
|
122
|
+
const outcomes = new Map(listed.map((each) => [each?.fileId, (each as { status?: unknown } | null)?.status]))
|
|
123
|
+
const tokens = typeof params.requestState === "string" ? params.requestState.split(" ") : []
|
|
124
|
+
const answers = (status: string) => pending.filter(([, value]) => outcomes.get(value.id) === status)
|
|
125
|
+
const [completed, failed] = [answers("completed"), answers("failed")]
|
|
126
|
+
if (
|
|
127
|
+
answered?.resultType !== "complete" ||
|
|
128
|
+
tokens.length !== pending.length ||
|
|
129
|
+
listed.length !== pending.length ||
|
|
130
|
+
completed.length + failed.length !== pending.length
|
|
131
|
+
)
|
|
132
|
+
return { invalid: "requestState and inputResponses.uploads answer no upload this call asked for" }
|
|
133
|
+
if (failed[0]) return `Invalid arguments: ${failed[0][0]} failed to upload`
|
|
134
|
+
const taken = new Map<object, Uint8Array>()
|
|
135
|
+
for (const [at, [label, value]] of pending.entries()) {
|
|
136
|
+
let bytes: Uint8Array | string
|
|
137
|
+
try {
|
|
138
|
+
bytes = await uploads.take(scope, declared(value), tokens[at] as string)
|
|
139
|
+
/* a token this call does not hold is the state's refusal */
|
|
140
|
+
} catch (error) {
|
|
141
|
+
return { invalid: (error as Error).message }
|
|
142
|
+
}
|
|
143
|
+
if (typeof bytes === "string") return `Invalid arguments: ${label} ${bytes}`
|
|
144
|
+
taken.set(value, bytes)
|
|
145
|
+
}
|
|
146
|
+
return taken
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** The data-less FileInputs among `values` (`[label, value]`): asked for on the call, taken on its retry. */
|
|
150
|
+
export function staged(tool: string, args: Record<string, unknown>, values: Pending, files: Files | undefined) {
|
|
151
|
+
const { asked: params, client, credential = "", uploads } = files ?? {}
|
|
152
|
+
const pending = values.filter(([, value]) => Object.hasOwn(value, "id") && value.data === undefined)
|
|
153
|
+
if (uploads === undefined || pending.length === 0) return new Map<object, Uint8Array>()
|
|
154
|
+
const extension = client?.extensions?.[FILES]
|
|
155
|
+
if (typeof extension !== "object" || extension === null)
|
|
156
|
+
return `Invalid arguments: ${pending[0]?.[0]} has no data, and an upload needs a 2026-07-28 request declaring ${FILES}`
|
|
157
|
+
const ids = values.flatMap(([, value]) => (Object.hasOwn(value, "id") ? [value.id] : []))
|
|
158
|
+
if (new Set(ids).size !== ids.length) return "Invalid arguments: a file id repeats"
|
|
159
|
+
const scope = { args, credential, tool }
|
|
160
|
+
return params?.requestState === undefined ? asked(uploads, scope, pending) : retried(uploads, scope, pending, params)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** A file argument its inputSchema took: its bytes, name and type; else the rule it breaks. */
|
|
164
|
+
export async function fileOf(
|
|
165
|
+
file: Record<string, unknown>,
|
|
166
|
+
files: Files & { readonly taken: Map<object, Uint8Array>; readonly signal: AbortSignal },
|
|
167
|
+
budget: { left: number; cap: number },
|
|
168
|
+
) {
|
|
169
|
+
const uploaded = files.taken.get(file)
|
|
170
|
+
if (Object.hasOwn(file, "id")) return uploaded ? { bytes: uploaded, ...named(file) } : inlined(file, budget)
|
|
171
|
+
const { fetch, read } = files
|
|
172
|
+
const url = Object.hasOwn(file, "url")
|
|
173
|
+
if (url ? fetch === undefined : read === undefined)
|
|
174
|
+
return url ? "is a URL, which this server fetches none of" : "is a path, which only the stdio server reads"
|
|
175
|
+
try {
|
|
176
|
+
if (url) return await (fetch as FetchFile)(String(file.url), files.signal)
|
|
177
|
+
const path = String(file.path)
|
|
178
|
+
return { bytes: await (read as Read)(path), name: path.split(/[/\\]/).pop(), type: "application/octet-stream" }
|
|
179
|
+
/* a reader's or a fetch's refusal is its rule */
|
|
180
|
+
} catch (error) {
|
|
181
|
+
return (error as Error).message
|
|
182
|
+
}
|
|
183
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A tool call's guards (bit 18), run before anything is sent, each refusal naming the argument: a `__proto__`,
|
|
3
|
+
* `constructor` or `prototype` key at any depth; a value its inputSchema rejects; a path value holding `/` or `\`, or
|
|
4
|
+
* `.` or `..`, as given or after any number of percent-decodings (H18.11). And the API key, as given, URL-encoded or
|
|
5
|
+
* form-encoded, scrubbed from every string of a result, keys included.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** A tool's JSON Schema: the keywords schema.ts emits that constrain a value, every one checked here. */
|
|
9
|
+
type Schema = {
|
|
10
|
+
readonly type?: string
|
|
11
|
+
readonly enum?: readonly unknown[]
|
|
12
|
+
readonly const?: unknown
|
|
13
|
+
readonly anyOf?: readonly Schema[]
|
|
14
|
+
readonly allOf?: readonly Schema[]
|
|
15
|
+
readonly properties?: Readonly<Record<string, Schema>>
|
|
16
|
+
readonly required?: readonly string[]
|
|
17
|
+
readonly additionalProperties?: Schema | false
|
|
18
|
+
readonly items?: Schema | false
|
|
19
|
+
readonly prefixItems?: readonly Schema[]
|
|
20
|
+
readonly minItems?: number
|
|
21
|
+
readonly maxItems?: number
|
|
22
|
+
readonly minLength?: number
|
|
23
|
+
readonly maxLength?: number
|
|
24
|
+
readonly minimum?: number
|
|
25
|
+
readonly maximum?: number
|
|
26
|
+
readonly exclusiveMinimum?: number
|
|
27
|
+
readonly exclusiveMaximum?: number
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** `key` under the path `at`. */
|
|
31
|
+
const under = (at: string, key: string) => (at ? `${at}.${key}` : key)
|
|
32
|
+
|
|
33
|
+
/** The first key in `value` naming a prototype, as its path under `at`. */
|
|
34
|
+
function prototypeKey(value: unknown, at: string): string | undefined {
|
|
35
|
+
if (typeof value !== "object" || value === null) return undefined
|
|
36
|
+
for (const [key, item] of Object.entries(value)) {
|
|
37
|
+
const path = Array.isArray(value) ? `${at}[${key}]` : under(at, key)
|
|
38
|
+
const found =
|
|
39
|
+
!Array.isArray(value) && /^(?:__proto__|constructor|prototype)$/.test(key) ? path : prototypeKey(item, path)
|
|
40
|
+
if (found) return found
|
|
41
|
+
}
|
|
42
|
+
return undefined
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const typeOf = (value: unknown) =>
|
|
46
|
+
value === null ? "null" : Array.isArray(value) ? "array" : Number.isInteger(value) ? "integer" : typeof value
|
|
47
|
+
|
|
48
|
+
const same = (a: unknown, b: unknown) => JSON.stringify(a) === JSON.stringify(b)
|
|
49
|
+
|
|
50
|
+
/** Why the array `list`, at `at`, fails `schema`'s items. */
|
|
51
|
+
function itemsInvalid(schema: Schema, list: readonly unknown[], at: string): string | undefined {
|
|
52
|
+
if (list.length < (schema.minItems ?? 0)) return `${at} has too few items`
|
|
53
|
+
if (list.length > (schema.maxItems ?? Infinity)) return `${at} has too many items`
|
|
54
|
+
return list
|
|
55
|
+
.map((item, index) => {
|
|
56
|
+
const of = schema.prefixItems?.[index] ?? schema.items
|
|
57
|
+
return of === false ? `${at}[${index}] is past the last item` : of && invalid(of, item, `${at}[${index}]`)
|
|
58
|
+
})
|
|
59
|
+
.find(Boolean)
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Why the object `object`, at `at`, fails `schema`'s properties. */
|
|
63
|
+
function fieldsInvalid(schema: Schema, object: Record<string, unknown>, at: string): string | undefined {
|
|
64
|
+
const missing = schema.required?.find((name) => !Object.hasOwn(object, name))
|
|
65
|
+
if (missing) return `${under(at, missing)} is required`
|
|
66
|
+
const { additionalProperties, properties = {} } = schema
|
|
67
|
+
return Object.entries(object)
|
|
68
|
+
.map(([name, item]) => {
|
|
69
|
+
const of = Object.hasOwn(properties, name) ? properties[name] : additionalProperties
|
|
70
|
+
return of === false ? `${under(at, name)} is not a declared property` : of && invalid(of, item, under(at, name))
|
|
71
|
+
})
|
|
72
|
+
.find(Boolean)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Why `value`, of `type`, fails `schema`'s own keywords: its type, enum, const and anyOf. */
|
|
76
|
+
function ownInvalid(schema: Schema, value: unknown, type: string, at: string): string | undefined {
|
|
77
|
+
const where = at || "arguments"
|
|
78
|
+
if (schema.type && schema.type !== type && !(schema.type === "number" && type === "integer"))
|
|
79
|
+
return `${where} must be ${schema.type}`
|
|
80
|
+
if (schema.enum && !schema.enum.some((option) => same(option, value)))
|
|
81
|
+
return `${where} must be one of ${JSON.stringify(schema.enum)}`
|
|
82
|
+
if ("const" in schema && !same(schema.const, value)) return `${where} must be ${JSON.stringify(schema.const)}`
|
|
83
|
+
if (schema.anyOf && !schema.anyOf.some((option) => !invalid(option, value, at)))
|
|
84
|
+
return `${where} matches none of its schemas`
|
|
85
|
+
return undefined
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Why the number `value` falls outside `schema`'s bounds, or the string `value` outside its length (code points). */
|
|
89
|
+
function rangeInvalid(schema: Schema, value: unknown, where: string): string | undefined {
|
|
90
|
+
if (typeof value === "string") {
|
|
91
|
+
const length = [...value].length
|
|
92
|
+
if (length < (schema.minLength ?? 0)) return `${where} is too short`
|
|
93
|
+
return length > (schema.maxLength ?? Infinity) ? `${where} is too long` : undefined
|
|
94
|
+
}
|
|
95
|
+
if (typeof value !== "number") return undefined
|
|
96
|
+
const { exclusiveMaximum: below, exclusiveMinimum: above, maximum, minimum } = schema
|
|
97
|
+
if (minimum !== undefined && value < minimum) return `${where} must be at least ${minimum}`
|
|
98
|
+
if (maximum !== undefined && value > maximum) return `${where} must be at most ${maximum}`
|
|
99
|
+
if (above !== undefined && value <= above) return `${where} must be more than ${above}`
|
|
100
|
+
return below !== undefined && value >= below ? `${where} must be less than ${below}` : undefined
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Why `value`, at `at`, fails `schema`; undefined when it conforms. */
|
|
104
|
+
function invalid(schema: Schema, value: unknown, at: string): string | undefined {
|
|
105
|
+
const type = typeOf(value)
|
|
106
|
+
const where = at || "arguments"
|
|
107
|
+
const own =
|
|
108
|
+
ownInvalid(schema, value, type, at) ??
|
|
109
|
+
rangeInvalid(schema, value, at || "arguments") ??
|
|
110
|
+
schema.allOf?.map((one) => invalid(one, value, at)).find(Boolean)
|
|
111
|
+
if (own || (type !== "array" && type !== "object")) return own
|
|
112
|
+
return type === "array"
|
|
113
|
+
? itemsInvalid(schema, value as unknown[], where)
|
|
114
|
+
: fieldsInvalid(schema, value as Record<string, unknown>, at)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Whether a path value could leave its segment, as given or after any number of percent-decodings. */
|
|
118
|
+
function leavesSegment(value: string): boolean {
|
|
119
|
+
for (let text = value, last = ""; text !== last;) {
|
|
120
|
+
if (/[/\\]|^\.\.?$/.test(text)) return true
|
|
121
|
+
last = text
|
|
122
|
+
text = text.replace(/%([\da-f]{2})/gi, (_, hex: string) => String.fromCharCode(Number.parseInt(hex, 16)))
|
|
123
|
+
}
|
|
124
|
+
return false
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Why `args` are refused, naming the argument; `paths` are the path parameters' properties. */
|
|
128
|
+
export function refusal(
|
|
129
|
+
inputSchema: Schema,
|
|
130
|
+
paths: readonly string[],
|
|
131
|
+
args: Record<string, unknown>,
|
|
132
|
+
): string | undefined {
|
|
133
|
+
const prototype = prototypeKey(args, "")
|
|
134
|
+
const leaving = paths.find((property) => args[property] !== undefined && leavesSegment(String(args[property])))
|
|
135
|
+
const why = prototype
|
|
136
|
+
? `${prototype} names a prototype`
|
|
137
|
+
: (invalid(inputSchema, args, "") ?? (leaving && `${leaving} must stay within one path segment`))
|
|
138
|
+
return why && `Invalid arguments: ${why}`
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** A scrubber of `secrets` from every string in a value, keys included, in any letter case (a hex escape's either). */
|
|
142
|
+
export function redactor(secrets: readonly string[]): (value: unknown) => unknown {
|
|
143
|
+
const forms = secrets
|
|
144
|
+
.filter(Boolean)
|
|
145
|
+
.flatMap((s) => [s, encodeURIComponent(s), new URLSearchParams({ s }).toString().slice(2)])
|
|
146
|
+
if (forms.length === 0) return (value) => value
|
|
147
|
+
const pattern = new RegExp(forms.map((form) => form.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|"), "gi")
|
|
148
|
+
const scrub = (value: unknown): unknown =>
|
|
149
|
+
typeof value === "string"
|
|
150
|
+
? value.replace(pattern, "[redacted]")
|
|
151
|
+
: typeof value !== "object" || value === null
|
|
152
|
+
? value
|
|
153
|
+
: Array.isArray(value)
|
|
154
|
+
? value.map(scrub)
|
|
155
|
+
: Object.fromEntries(Object.entries(value).map(([key, item]) => [scrub(key) as string, scrub(item)]))
|
|
156
|
+
return scrub
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** `$<PREFIX>_HEADERS`, each header passing the SDK's `check`; else why not, quoting no value. */
|
|
160
|
+
export function configHeaders(
|
|
161
|
+
text: string | undefined,
|
|
162
|
+
name: string,
|
|
163
|
+
check: (header: string, value: string) => unknown,
|
|
164
|
+
) {
|
|
165
|
+
const shape = `Invalid $${name}: a JSON object of string values`
|
|
166
|
+
try {
|
|
167
|
+
const headers: unknown = JSON.parse(text || "{}")
|
|
168
|
+
if (typeof headers !== "object" || headers === null || Array.isArray(headers)) return shape
|
|
169
|
+
const entries = Object.entries(headers)
|
|
170
|
+
if (entries.some(([, value]) => typeof value !== "string")) return shape
|
|
171
|
+
for (const [header, value] of entries) check(header, value as string)
|
|
172
|
+
return headers as Record<string, string>
|
|
173
|
+
} catch (err) {
|
|
174
|
+
return err instanceof SyntaxError ? shape : `Invalid $${name}: ${(err as Error).message}`
|
|
175
|
+
}
|
|
176
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP's Streamable HTTP transport (bit 21 d), web-standard (no node API): an Origin not its own nor listed 403, POST
|
|
3
|
+
* alone 405, Accept naming JSON and SSE 406, JSON 415, a body past `max` as it streams 413. MCP-Protocol-Version picks
|
|
4
|
+
* the era (absent: 2025-03-26); a modern request's headers match its body, else 400 -32020. No session id. JSON, or SSE
|
|
5
|
+
* from a call's first progress. In 2026-07-28 a closed request or stream cancels its call. No path is read.
|
|
6
|
+
*/
|
|
7
|
+
import { decoded, type Files } from "./files.ts"
|
|
8
|
+
import { ErrorCode, failure, isId, type Reply } from "./jsonrpc.ts"
|
|
9
|
+
import { BATCHED, claimed, isObject, MODERN, type Protocol, PROTOCOL_VERSIONS, unserved, VERSION } from "./protocol.ts"
|
|
10
|
+
|
|
11
|
+
/** What the transport serves beside its own origin, its body cap in bytes, and its calls' files (a mount's). */
|
|
12
|
+
export type HttpOptions = { readonly origins?: readonly string[]; readonly max?: number; readonly files?: Files }
|
|
13
|
+
|
|
14
|
+
const SSE = { "cache-control": "no-cache", "content-type": "text/event-stream", "x-accel-buffering": "no" }
|
|
15
|
+
|
|
16
|
+
/** A response of `status`, `reply` its JSON body when given. */
|
|
17
|
+
const answered = (status: number, reply?: unknown) =>
|
|
18
|
+
reply === undefined
|
|
19
|
+
? new Response(null, { status })
|
|
20
|
+
: new Response(JSON.stringify(reply), { headers: { "content-type": "application/json" }, status })
|
|
21
|
+
|
|
22
|
+
/** A header's value as sent: a `=?base64?…?=` one decoded as UTF-8; undefined when absent or malformed. */
|
|
23
|
+
function valueOf(value: string | null): string | undefined {
|
|
24
|
+
const encoded = /^=\?base64\?(.*)\?=$/.exec(value ?? "")?.[1]
|
|
25
|
+
if (encoded === undefined) return value !== null && /^[\t\x20-\x7e]*$/.test(value) ? value : undefined
|
|
26
|
+
const bytes = decoded(encoded)
|
|
27
|
+
try {
|
|
28
|
+
return bytes && new TextDecoder("utf-8", { fatal: true }).decode(bytes)
|
|
29
|
+
/* bytes that are no UTF-8 are malformed */
|
|
30
|
+
} catch {
|
|
31
|
+
return undefined
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** `request`'s body as text, at most `max` bytes counted as they stream; undefined past it. */
|
|
36
|
+
async function bodyOf(request: Request, max: number): Promise<string | undefined> {
|
|
37
|
+
const chunks: Uint8Array<ArrayBuffer>[] = []
|
|
38
|
+
let size = 0
|
|
39
|
+
const reader = request.body?.getReader()
|
|
40
|
+
for (let read = await reader?.read(); read && !read.done; read = await reader?.read()) {
|
|
41
|
+
if ((size += read.value.length) > max) return undefined
|
|
42
|
+
chunks.push(read.value)
|
|
43
|
+
}
|
|
44
|
+
return new Blob(chunks).text()
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The refusal of a request this endpoint takes no body from: its Origin, method, Accept or Content-Type. */
|
|
48
|
+
function unfit({ headers, method, url }: Request, origins: readonly string[]): Response | undefined {
|
|
49
|
+
const origin = headers.get("origin")
|
|
50
|
+
if (origin !== null && origin !== new URL(url).origin && !origins.includes(origin)) return answered(403)
|
|
51
|
+
if (method !== "POST") return new Response(null, { headers: { allow: "POST" }, status: 405 })
|
|
52
|
+
const accepts = (headers.get("accept") ?? "").split(",").map((type) => type.split(";")[0]?.trim().toLowerCase())
|
|
53
|
+
if (!accepts.includes("application/json") || !accepts.includes("text/event-stream")) return answered(406)
|
|
54
|
+
if (!/^application\/json *(;|$)/i.test(headers.get("content-type") ?? "")) return answered(415)
|
|
55
|
+
return undefined
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The header of a modern request's that disagrees with its body; undefined when none does. */
|
|
59
|
+
function mismatched(headers: Headers, method: unknown, params: Record<string, unknown>, version?: string) {
|
|
60
|
+
if (claimed(params)?.[VERSION] !== version) return "MCP-Protocol-Version"
|
|
61
|
+
if (headers.get("mcp-method") !== method) return "Mcp-Method"
|
|
62
|
+
/* the body field Mcp-Name mirrors: a resource's uri, a tool's or a prompt's name */
|
|
63
|
+
const field = method === "resources/read" ? "uri" : method === "tools/call" || method === "prompts/get" ? "name" : ""
|
|
64
|
+
const named = params[field]
|
|
65
|
+
return typeof named === "string" && valueOf(headers.get("mcp-name")) !== named ? "Mcp-Name" : undefined
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** A message's params, `{}` when it has none. */
|
|
69
|
+
const paramsOf = (message: Record<string, unknown>) => (isObject(message.params) ? message.params : {})
|
|
70
|
+
|
|
71
|
+
/** Whether `message` is a modern request: one with an id, its `_meta` claiming a revision or its header the modern. */
|
|
72
|
+
const modernOf = (message: Record<string, unknown>, version?: string) =>
|
|
73
|
+
isId(message.id) && (claimed(paramsOf(message)) !== undefined || version === MODERN)
|
|
74
|
+
|
|
75
|
+
/** The refusal of a message its era rejects: a client's response in 2026-07-28; a modern request's header or `_meta`. */
|
|
76
|
+
function misheaded(message: Record<string, unknown>, headers: Headers, version?: string): Response | undefined {
|
|
77
|
+
/* a client's response: taken before 2026-07-28, which forbids sending one */
|
|
78
|
+
if (!("method" in message) && ("result" in message || "error" in message))
|
|
79
|
+
return version === MODERN
|
|
80
|
+
? answered(400, failure(null, ErrorCode.InvalidRequest, "Invalid Request: a client sends no response"))
|
|
81
|
+
: answered(202)
|
|
82
|
+
if (!modernOf(message, version)) return undefined
|
|
83
|
+
const id = message.id as string | number
|
|
84
|
+
const mismatch = mismatched(headers, message.method, paramsOf(message), version)
|
|
85
|
+
if (mismatch) return answered(400, failure(id, ErrorCode.HeaderMismatch, `Header mismatch: ${mismatch}`))
|
|
86
|
+
const meta = claimed(paramsOf(message))
|
|
87
|
+
const refusal = meta && unserved(id, meta)
|
|
88
|
+
return refusal ? answered(400, refusal) : undefined
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** A reply's status: a modern unknown method 404, a message JSON-RPC cannot read 400, else 200. */
|
|
92
|
+
function statusOf(reply: Reply | Reply[], modern: boolean): number {
|
|
93
|
+
const code = "error" in reply ? reply.error.code : 0
|
|
94
|
+
if (code === ErrorCode.MethodNotFound && modern) return 404
|
|
95
|
+
return code === ErrorCode.ParseError || code === ErrorCode.InvalidRequest ? 400 : 200
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* `protocol`'s answer to `body` under `version` as a Response: JSON, or an event stream once the call reports progress.
|
|
100
|
+
* A modern request's call is cancelled by `closed` or its stream's close; a legacy one's by neither. Its calls get `files`.
|
|
101
|
+
*/
|
|
102
|
+
function streamed(
|
|
103
|
+
protocol: Protocol,
|
|
104
|
+
body: unknown,
|
|
105
|
+
version: string,
|
|
106
|
+
{ closed, files }: { closed?: AbortSignal; files?: Files },
|
|
107
|
+
) {
|
|
108
|
+
const call = new AbortController()
|
|
109
|
+
const signal = closed ? AbortSignal.any([closed, call.signal]) : call.signal
|
|
110
|
+
return new Promise<Response>((settle) => {
|
|
111
|
+
let stream: ReadableStreamDefaultController<Uint8Array> | undefined
|
|
112
|
+
let open = true
|
|
113
|
+
const write = (message: unknown) =>
|
|
114
|
+
open && stream?.enqueue(new TextEncoder().encode(`event: message\ndata: ${JSON.stringify(message)}\n\n`))
|
|
115
|
+
const notify = (message: object) => {
|
|
116
|
+
if (stream === undefined) {
|
|
117
|
+
const cancel = () => void ((open = false), closed && call.abort())
|
|
118
|
+
settle(new Response(new ReadableStream({ cancel, start: (each) => void (stream = each) }), { headers: SSE }))
|
|
119
|
+
}
|
|
120
|
+
write(message)
|
|
121
|
+
}
|
|
122
|
+
const answer = protocol.answer(body, { connection: { version }, files, notify, signal })
|
|
123
|
+
void Promise.resolve(answer).then((reply) => {
|
|
124
|
+
if (stream !== undefined) return void (write(reply), open && stream.close())
|
|
125
|
+
settle(reply === undefined ? answered(202) : answered(statusOf(reply, closed !== undefined), reply))
|
|
126
|
+
})
|
|
127
|
+
})
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** The Streamable HTTP endpoint serving `protocol`: its own origin and `origins`, a body at most `max` bytes. */
|
|
131
|
+
export function serveHttp(protocol: Protocol, { files, max = 4_194_304, origins = [] }: HttpOptions = {}) {
|
|
132
|
+
return async (request: Request): Promise<Response> => {
|
|
133
|
+
const unfitting = unfit(request, origins)
|
|
134
|
+
if (unfitting) return unfitting
|
|
135
|
+
const text = await bodyOf(request, max)
|
|
136
|
+
if (text === undefined) return answered(413)
|
|
137
|
+
let body: unknown
|
|
138
|
+
try {
|
|
139
|
+
body = JSON.parse(text)
|
|
140
|
+
/* a body that is no JSON is JSON-RPC's parse error */
|
|
141
|
+
} catch {
|
|
142
|
+
return answered(400, failure(null, ErrorCode.ParseError, "Parse error"))
|
|
143
|
+
}
|
|
144
|
+
const version = request.headers.get("mcp-protocol-version") ?? undefined
|
|
145
|
+
if (version !== undefined && !(PROTOCOL_VERSIONS as readonly string[]).includes(version))
|
|
146
|
+
return answered(
|
|
147
|
+
400,
|
|
148
|
+
failure(null, ErrorCode.UnsupportedProtocolVersion, "Unsupported protocol version", {
|
|
149
|
+
requested: version,
|
|
150
|
+
supported: PROTOCOL_VERSIONS,
|
|
151
|
+
}),
|
|
152
|
+
)
|
|
153
|
+
const message = isObject(body) ? body : {}
|
|
154
|
+
const refusal = misheaded(message, request.headers, version)
|
|
155
|
+
if (refusal) return refusal
|
|
156
|
+
const closed = modernOf(message, version) ? request.signal : undefined
|
|
157
|
+
return streamed(protocol, body, version ?? BATCHED, { closed, files })
|
|
158
|
+
}
|
|
159
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON-RPC 2.0 as MCP frames it (bit 18): a message, the error codes a server answers with, and the two reply shapes.
|
|
3
|
+
* A request carries an id; a notification does not and is never answered. An error may carry data (bit 21 b). Bit 21 d:
|
|
4
|
+
* -32020, the HTTP headers disagreeing with the body.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** One message as it arrives: anything may be missing or of the wrong type until checked. */
|
|
8
|
+
export type Message = {
|
|
9
|
+
readonly jsonrpc?: unknown
|
|
10
|
+
readonly id?: unknown
|
|
11
|
+
readonly method?: unknown
|
|
12
|
+
readonly params?: unknown
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** A request's id: a string or a number (JSON-RPC 2.0; MCP forbids null). */
|
|
16
|
+
export type Id = string | number
|
|
17
|
+
|
|
18
|
+
/** The error codes this server answers with. */
|
|
19
|
+
export const ErrorCode = {
|
|
20
|
+
HeaderMismatch: -32020,
|
|
21
|
+
InternalError: -32603,
|
|
22
|
+
InvalidParams: -32602,
|
|
23
|
+
InvalidRequest: -32600,
|
|
24
|
+
MethodNotFound: -32601,
|
|
25
|
+
ParseError: -32700,
|
|
26
|
+
UnsupportedProtocolVersion: -32022,
|
|
27
|
+
} as const
|
|
28
|
+
|
|
29
|
+
/** A reply: a request's result, or its error. */
|
|
30
|
+
export type Reply =
|
|
31
|
+
| { readonly jsonrpc: "2.0"; readonly id: Id; readonly result: unknown }
|
|
32
|
+
| {
|
|
33
|
+
readonly jsonrpc: "2.0"
|
|
34
|
+
readonly id: Id | null
|
|
35
|
+
readonly error: { readonly code: number; readonly message: string; readonly data?: unknown }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The result reply to the request `id`. */
|
|
39
|
+
export const result = (id: Id, value: unknown): Reply => ({ id, jsonrpc: "2.0", result: value })
|
|
40
|
+
|
|
41
|
+
/** The error reply to the request `id` (null when it could not be read), its `data` when given. */
|
|
42
|
+
export const failure = (id: Id | null, code: number, message: string, data?: unknown): Reply => ({
|
|
43
|
+
error: { code, data, message },
|
|
44
|
+
id,
|
|
45
|
+
jsonrpc: "2.0",
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
/** Whether `value` is an id a request may carry. */
|
|
49
|
+
export const isId = (value: unknown): value is Id =>
|
|
50
|
+
typeof value === "string" || (typeof value === "number" && Number.isFinite(value))
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The standalone Streamable HTTP server (bit 21 d): node's http adapting http.ts's handler at /mcp alone. Unless exposed,
|
|
3
|
+
* a Host not a loopback name with the bound port is 421 (DNS rebinding). With a token, a request carries it as its
|
|
4
|
+
* bearer, compared in constant time, else 401. A response closed early aborts its request's signal.
|
|
5
|
+
*/
|
|
6
|
+
import { createHash, timingSafeEqual } from "node:crypto"
|
|
7
|
+
import { createServer, type IncomingMessage, type Server } from "node:http"
|
|
8
|
+
import type { AddressInfo } from "node:net"
|
|
9
|
+
import { pipeline, Readable } from "node:stream"
|
|
10
|
+
|
|
11
|
+
/** Where the server listens, its token when it has one, and whether it answers for any Host. */
|
|
12
|
+
export type Listen = {
|
|
13
|
+
readonly host: string
|
|
14
|
+
readonly port: number
|
|
15
|
+
readonly token?: string
|
|
16
|
+
readonly exposed?: boolean
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const digest = (text: string) => createHash("sha256").update(text).digest()
|
|
20
|
+
|
|
21
|
+
/** Whether `authorization` carries `token` as its bearer: their digests compared in constant time. */
|
|
22
|
+
export const bearer = (authorization: string | null, token: string): boolean =>
|
|
23
|
+
timingSafeEqual(digest(/^Bearer (.+)$/i.exec(authorization ?? "")?.[1] ?? ""), digest(token))
|
|
24
|
+
|
|
25
|
+
/** `incoming` as a Request to `url`, every header kept as sent (a repeated one joined), `closed` its signal. */
|
|
26
|
+
function requestOf(incoming: IncomingMessage, headers: Headers, url: string, closed: AbortSignal) {
|
|
27
|
+
const post = incoming.method === "POST"
|
|
28
|
+
const body = post ? (Readable.toWeb(incoming) as ReadableStream) : null
|
|
29
|
+
return new Request(url, { body, duplex: "half", headers, method: incoming.method, signal: closed } as RequestInit)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The answer to `incoming` at `port`: a Host not served 421, no token 401, another path 404, else `handler`'s, its
|
|
34
|
+
* request's signal `closed`.
|
|
35
|
+
*/
|
|
36
|
+
async function answerOf(
|
|
37
|
+
incoming: IncomingMessage,
|
|
38
|
+
handler: (request: Request) => Promise<Response>,
|
|
39
|
+
{ exposed, port, token }: Listen,
|
|
40
|
+
closed: AbortSignal,
|
|
41
|
+
): Promise<Response> {
|
|
42
|
+
const headers = new Headers()
|
|
43
|
+
for (let at = 0; at < incoming.rawHeaders.length; at += 2)
|
|
44
|
+
headers.append(incoming.rawHeaders[at] as string, incoming.rawHeaders[at + 1] as string)
|
|
45
|
+
const host = headers.get("host")?.toLowerCase()
|
|
46
|
+
if (!exposed && !["localhost", "127.0.0.1", "[::1]"].some((name) => host === `${name}:${port}`))
|
|
47
|
+
return new Response(null, { status: 421 })
|
|
48
|
+
if (token !== undefined && !bearer(headers.get("authorization"), token))
|
|
49
|
+
return new Response(null, { headers: { "www-authenticate": "Bearer" }, status: 401 })
|
|
50
|
+
if (incoming.url?.split("?")[0] !== "/mcp") return new Response(null, { status: 404 })
|
|
51
|
+
try {
|
|
52
|
+
return await handler(requestOf(incoming, headers, `http://${host}/mcp`, closed))
|
|
53
|
+
/* a Request node cannot make (a Host no URL holds, a method fetch forbids) or a body cut mid-read */
|
|
54
|
+
} catch {
|
|
55
|
+
return new Response(null, { status: 400 })
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** `handler` served at /mcp on `listen`'s host and port; settles once it listens. */
|
|
60
|
+
export function listenHttp(handler: (request: Request) => Promise<Response>, listen: Listen): Promise<Server> {
|
|
61
|
+
const server = createServer(async (incoming, outgoing) => {
|
|
62
|
+
const closed = new AbortController()
|
|
63
|
+
outgoing.once("close", () => outgoing.writableFinished || closed.abort())
|
|
64
|
+
const { port } = server.address() as AddressInfo
|
|
65
|
+
const response = await answerOf(incoming, handler, { ...listen, port }, closed.signal)
|
|
66
|
+
outgoing.writeHead(response.status, Object.fromEntries(response.headers))
|
|
67
|
+
/* a response closed early destroys its source, which cancels the body's stream; closed already, it is cancelled */
|
|
68
|
+
if (response.body && !outgoing.destroyed)
|
|
69
|
+
pipeline(Readable.fromWeb(response.body as never), outgoing, () => undefined)
|
|
70
|
+
else void (response.body?.cancel(), outgoing.end())
|
|
71
|
+
})
|
|
72
|
+
return new Promise((settle, reject) => {
|
|
73
|
+
server.once("error", reject)
|
|
74
|
+
server.listen(listen.port, listen.host, () => settle(server))
|
|
75
|
+
})
|
|
76
|
+
}
|