@toolgrain/mcp 0.0.0-stage → 0.1.0-rc.0
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 +86 -2
- package/dist/auth/abort.d.ts +3 -0
- package/dist/auth/abort.d.ts.map +1 -0
- package/dist/auth/abort.js +14 -0
- package/dist/auth/abort.js.map +1 -0
- package/dist/auth/audience.d.ts +3 -0
- package/dist/auth/audience.d.ts.map +1 -0
- package/dist/auth/audience.js +17 -0
- package/dist/auth/audience.js.map +1 -0
- package/dist/auth/authorization.d.ts +7 -0
- package/dist/auth/authorization.d.ts.map +1 -0
- package/dist/auth/authorization.js +58 -0
- package/dist/auth/authorization.js.map +1 -0
- package/dist/auth/context.d.ts +8 -0
- package/dist/auth/context.d.ts.map +1 -0
- package/dist/auth/context.js +37 -0
- package/dist/auth/context.js.map +1 -0
- package/dist/auth/errors.d.ts +14 -0
- package/dist/auth/errors.d.ts.map +1 -0
- package/dist/auth/errors.js +23 -0
- package/dist/auth/errors.js.map +1 -0
- package/dist/auth/types.d.ts +28 -0
- package/dist/auth/types.d.ts.map +1 -0
- package/dist/auth/types.js +2 -0
- package/dist/auth/types.js.map +1 -0
- package/dist/auth/url.d.ts +3 -0
- package/dist/auth/url.d.ts.map +1 -0
- package/dist/auth/url.js +18 -0
- package/dist/auth/url.js.map +1 -0
- package/dist/connection.d.ts +12 -0
- package/dist/connection.d.ts.map +1 -0
- package/dist/connection.js +151 -0
- package/dist/connection.js.map +1 -0
- package/dist/context/access.d.ts +7 -0
- package/dist/context/access.d.ts.map +1 -0
- package/dist/context/access.js +22 -0
- package/dist/context/access.js.map +1 -0
- package/dist/context/definition.d.ts +24 -0
- package/dist/context/definition.d.ts.map +1 -0
- package/dist/context/definition.js +57 -0
- package/dist/context/definition.js.map +1 -0
- package/dist/context/registry.d.ts +5 -0
- package/dist/context/registry.d.ts.map +1 -0
- package/dist/context/registry.js +15 -0
- package/dist/context/registry.js.map +1 -0
- package/dist/context/scope.d.ts +5 -0
- package/dist/context/scope.d.ts.map +1 -0
- package/dist/context/scope.js +56 -0
- package/dist/context/scope.js.map +1 -0
- package/dist/diagnostics.d.ts +10 -0
- package/dist/diagnostics.d.ts.map +1 -0
- package/dist/diagnostics.js +13 -0
- package/dist/diagnostics.js.map +1 -0
- package/dist/dispatcher.d.ts +19 -0
- package/dist/dispatcher.d.ts.map +1 -0
- package/dist/dispatcher.js +23 -0
- package/dist/dispatcher.js.map +1 -0
- package/dist/execute-request.d.ts +7 -0
- package/dist/execute-request.d.ts.map +1 -0
- package/dist/execute-request.js +20 -0
- package/dist/execute-request.js.map +1 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/legacy/connection.d.ts +26 -0
- package/dist/legacy/connection.d.ts.map +1 -0
- package/dist/legacy/connection.js +73 -0
- package/dist/legacy/connection.js.map +1 -0
- package/dist/legacy/dispatcher.d.ts +10 -0
- package/dist/legacy/dispatcher.d.ts.map +1 -0
- package/dist/legacy/dispatcher.js +41 -0
- package/dist/legacy/dispatcher.js.map +1 -0
- package/dist/legacy/handlers.d.ts +12 -0
- package/dist/legacy/handlers.d.ts.map +1 -0
- package/dist/legacy/handlers.js +44 -0
- package/dist/legacy/handlers.js.map +1 -0
- package/dist/legacy/messages.d.ts +31 -0
- package/dist/legacy/messages.d.ts.map +1 -0
- package/dist/legacy/messages.js +27 -0
- package/dist/legacy/messages.js.map +1 -0
- package/dist/legacy/responses.d.ts +5 -0
- package/dist/legacy/responses.d.ts.map +1 -0
- package/dist/legacy/responses.js +8 -0
- package/dist/legacy/responses.js.map +1 -0
- package/dist/legacy/results.d.ts +5 -0
- package/dist/legacy/results.d.ts.map +1 -0
- package/dist/legacy/results.js +19 -0
- package/dist/legacy/results.js.map +1 -0
- package/dist/modern/dispatcher.d.ts +7 -0
- package/dist/modern/dispatcher.d.ts.map +1 -0
- package/dist/modern/dispatcher.js +37 -0
- package/dist/modern/dispatcher.js.map +1 -0
- package/dist/modern/handlers.d.ts +9 -0
- package/dist/modern/handlers.d.ts.map +1 -0
- package/dist/modern/handlers.js +36 -0
- package/dist/modern/handlers.js.map +1 -0
- package/dist/modern/messages.d.ts +142 -0
- package/dist/modern/messages.d.ts.map +1 -0
- package/dist/modern/messages.js +14 -0
- package/dist/modern/messages.js.map +1 -0
- package/dist/modern/metadata.d.ts +45 -0
- package/dist/modern/metadata.d.ts.map +1 -0
- package/dist/modern/metadata.js +49 -0
- package/dist/modern/metadata.js.map +1 -0
- package/dist/modern/responses.d.ts +3 -0
- package/dist/modern/responses.d.ts.map +1 -0
- package/dist/modern/responses.js +15 -0
- package/dist/modern/responses.js.map +1 -0
- package/dist/modern/results.d.ts +4 -0
- package/dist/modern/results.d.ts.map +1 -0
- package/dist/modern/results.js +4 -0
- package/dist/modern/results.js.map +1 -0
- package/dist/pending-requests.d.ts +12 -0
- package/dist/pending-requests.d.ts.map +1 -0
- package/dist/pending-requests.js +30 -0
- package/dist/pending-requests.js.map +1 -0
- package/dist/protocol/decode.d.ts +21 -0
- package/dist/protocol/decode.d.ts.map +1 -0
- package/dist/protocol/decode.js +40 -0
- package/dist/protocol/decode.js.map +1 -0
- package/dist/protocol/icon.d.ts +11 -0
- package/dist/protocol/icon.d.ts.map +1 -0
- package/dist/protocol/icon.js +8 -0
- package/dist/protocol/icon.js.map +1 -0
- package/dist/protocol/messages.d.ts +19 -0
- package/dist/protocol/messages.d.ts.map +1 -0
- package/dist/protocol/messages.js +21 -0
- package/dist/protocol/messages.js.map +1 -0
- package/dist/protocol/responses.d.ts +9 -0
- package/dist/protocol/responses.d.ts.map +1 -0
- package/dist/protocol/responses.js +20 -0
- package/dist/protocol/responses.js.map +1 -0
- package/dist/protocol/version-metadata.d.ts +7 -0
- package/dist/protocol/version-metadata.d.ts.map +1 -0
- package/dist/protocol/version-metadata.js +21 -0
- package/dist/protocol/version-metadata.js.map +1 -0
- package/dist/server.d.ts +35 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +101 -0
- package/dist/server.js.map +1 -0
- package/dist/tools/adapter.d.ts +12 -0
- package/dist/tools/adapter.d.ts.map +1 -0
- package/dist/tools/adapter.js +45 -0
- package/dist/tools/adapter.js.map +1 -0
- package/dist/tools/call.d.ts +8 -0
- package/dist/tools/call.d.ts.map +1 -0
- package/dist/tools/call.js +9 -0
- package/dist/tools/call.js.map +1 -0
- package/dist/tools/content.d.ts +181 -0
- package/dist/tools/content.d.ts.map +1 -0
- package/dist/tools/content.js +54 -0
- package/dist/tools/content.js.map +1 -0
- package/dist/tools/context.d.ts +36 -0
- package/dist/tools/context.d.ts.map +1 -0
- package/dist/tools/context.js +50 -0
- package/dist/tools/context.js.map +1 -0
- package/dist/tools/definition.d.ts +55 -0
- package/dist/tools/definition.d.ts.map +1 -0
- package/dist/tools/definition.js +44 -0
- package/dist/tools/definition.js.map +1 -0
- package/dist/tools/errors.d.ts +11 -0
- package/dist/tools/errors.d.ts.map +1 -0
- package/dist/tools/errors.js +19 -0
- package/dist/tools/errors.js.map +1 -0
- package/dist/tools/json-value.d.ts +15 -0
- package/dist/tools/json-value.d.ts.map +1 -0
- package/dist/tools/json-value.js +42 -0
- package/dist/tools/json-value.js.map +1 -0
- package/dist/tools/name.d.ts +6 -0
- package/dist/tools/name.d.ts.map +1 -0
- package/dist/tools/name.js +13 -0
- package/dist/tools/name.js.map +1 -0
- package/dist/tools/registry.d.ts +14 -0
- package/dist/tools/registry.d.ts.map +1 -0
- package/dist/tools/registry.js +31 -0
- package/dist/tools/registry.js.map +1 -0
- package/dist/tools/results.d.ts +14 -0
- package/dist/tools/results.d.ts.map +1 -0
- package/dist/tools/results.js +71 -0
- package/dist/tools/results.js.map +1 -0
- package/dist/tools/rich-result.d.ts +44 -0
- package/dist/tools/rich-result.d.ts.map +1 -0
- package/dist/tools/rich-result.js +24 -0
- package/dist/tools/rich-result.js.map +1 -0
- package/dist/tools/schema.d.ts +18 -0
- package/dist/tools/schema.d.ts.map +1 -0
- package/dist/tools/schema.js +70 -0
- package/dist/tools/schema.js.map +1 -0
- package/dist/transport-ownership.d.ts +4 -0
- package/dist/transport-ownership.d.ts.map +1 -0
- package/dist/transport-ownership.js +13 -0
- package/dist/transport-ownership.js.map +1 -0
- package/dist/transport.d.ts +24 -0
- package/dist/transport.d.ts.map +1 -0
- package/dist/transport.js +4 -0
- package/dist/transport.js.map +1 -0
- package/package.json +65 -4
- package/src/auth/abort.ts +14 -0
- package/src/auth/audience.ts +22 -0
- package/src/auth/authorization.ts +81 -0
- package/src/auth/context.ts +43 -0
- package/src/auth/errors.ts +35 -0
- package/src/auth/types.ts +34 -0
- package/src/auth/url.ts +22 -0
- package/src/connection.ts +207 -0
- package/src/context/access.ts +36 -0
- package/src/context/definition.ts +100 -0
- package/src/context/registry.ts +25 -0
- package/src/context/scope.ts +82 -0
- package/src/diagnostics.ts +25 -0
- package/src/dispatcher.ts +70 -0
- package/src/execute-request.ts +35 -0
- package/src/index.ts +41 -0
- package/src/legacy/connection.ts +101 -0
- package/src/legacy/dispatcher.ts +72 -0
- package/src/legacy/handlers.ts +76 -0
- package/src/legacy/messages.ts +27 -0
- package/src/legacy/responses.ts +20 -0
- package/src/legacy/results.ts +20 -0
- package/src/modern/dispatcher.ts +77 -0
- package/src/modern/handlers.ts +55 -0
- package/src/modern/messages.ts +14 -0
- package/src/modern/metadata.ts +49 -0
- package/src/modern/responses.ts +25 -0
- package/src/modern/results.ts +6 -0
- package/src/pending-requests.ts +30 -0
- package/src/protocol/decode.ts +69 -0
- package/src/protocol/icon.ts +8 -0
- package/src/protocol/messages.ts +29 -0
- package/src/protocol/responses.ts +27 -0
- package/src/protocol/version-metadata.ts +42 -0
- package/src/server.ts +164 -0
- package/src/tools/adapter.ts +76 -0
- package/src/tools/call.ts +18 -0
- package/src/tools/content.ts +57 -0
- package/src/tools/context.ts +119 -0
- package/src/tools/definition.ts +144 -0
- package/src/tools/errors.ts +21 -0
- package/src/tools/json-value.ts +81 -0
- package/src/tools/name.ts +13 -0
- package/src/tools/registry.ts +52 -0
- package/src/tools/results.ts +100 -0
- package/src/tools/rich-result.ts +70 -0
- package/src/tools/schema.ts +86 -0
- package/src/transport-ownership.ts +17 -0
- package/src/transport.ts +34 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const key = Symbol.for("@toolgrain/mcp/context-access/v1");
|
|
2
|
+
/** Keep own auth properties and shared request data on dependency-aware views. */
|
|
3
|
+
export function withContextAccess(ctx, access) {
|
|
4
|
+
return Object.freeze(Object.create(Object.getPrototypeOf(ctx), {
|
|
5
|
+
...Object.getOwnPropertyDescriptors(ctx),
|
|
6
|
+
[key]: { value: access },
|
|
7
|
+
}));
|
|
8
|
+
}
|
|
9
|
+
export function readContext(ctx, identity, name) {
|
|
10
|
+
try {
|
|
11
|
+
const access = Object.getOwnPropertyDescriptor(ctx, key)?.value;
|
|
12
|
+
if (!access)
|
|
13
|
+
throw new Error("Context access requires a Toolgrain-created context");
|
|
14
|
+
ctx.signal.throwIfAborted();
|
|
15
|
+
return access(identity, name);
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
// Accessors always return promises, including invalid contexts and aborts.
|
|
19
|
+
return Promise.reject(error);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=access.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"access.js","sourceRoot":"","sources":["../../src/context/access.ts"],"names":[],"mappings":"AAEA,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC,kCAAkC,CAAC,CAAC;AAG3D,kFAAkF;AAClF,MAAM,UAAU,iBAAiB,CAC/B,GAAgB,EAChB,MAAc;IAEd,OAAO,MAAM,CAAC,MAAM,CAClB,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,cAAc,CAAC,GAAG,CAAC,EAAE;QACxC,GAAG,MAAM,CAAC,yBAAyB,CAAC,GAAG,CAAC;QACxC,CAAC,GAAG,CAAC,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE;KACzB,CAAgB,CAClB,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,WAAW,CACzB,GAAgB,EAChB,QAAgB,EAChB,IAAY;IAEZ,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,MAAM,CAAC,wBAAwB,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,KAE7C,CAAC;QACd,IAAI,CAAC,MAAM;YACT,MAAM,IAAI,KAAK,CAAC,qDAAqD,CAAC,CAAC;QACzE,GAAG,CAAC,MAAM,CAAC,cAAc,EAAE,CAAC;QAC5B,OAAO,MAAM,CAAC,QAAQ,EAAE,IAAI,CAAmB,CAAC;IAClD,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,2EAA2E;QAC3E,OAAO,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC/B,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ToolContext } from "../tools/context.js";
|
|
2
|
+
export interface ContextBinding {
|
|
3
|
+
readonly identity: symbol;
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly resolve: (ctx: ToolContext) => unknown;
|
|
6
|
+
}
|
|
7
|
+
/** Erase values for heterogeneous registrations without exposing unsafe overrides. */
|
|
8
|
+
export declare abstract class RegisteredContextProvider {
|
|
9
|
+
abstract readonly name: string;
|
|
10
|
+
abstract readonly use: (ctx: ToolContext) => Promise<unknown>;
|
|
11
|
+
"~toolgrain"(): "context@1";
|
|
12
|
+
}
|
|
13
|
+
/** An opaque, invariant value contract; use createContext() and provide() for bindings. */
|
|
14
|
+
export declare abstract class ContextProvider<Value> extends RegisteredContextProvider {
|
|
15
|
+
abstract readonly use: (ctx: ToolContext) => Promise<Value>;
|
|
16
|
+
abstract readonly provide: (resolve: (ctx: ToolContext) => Value | PromiseLike<Value>) => ContextProvider<Value>;
|
|
17
|
+
}
|
|
18
|
+
/** Infer the resolved value type, including asynchronous factories. */
|
|
19
|
+
export declare function createContext<Value>(options: {
|
|
20
|
+
name: string;
|
|
21
|
+
resolve: (ctx: ToolContext) => Value;
|
|
22
|
+
}): ContextProvider<Awaited<Value>>;
|
|
23
|
+
export declare function contextBinding(provider: unknown): ContextBinding;
|
|
24
|
+
//# sourceMappingURL=definition.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"definition.d.ts","sourceRoot":"","sources":["../../src/context/definition.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAMvD,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,WAAW,KAAK,OAAO,CAAC;CACjD;AAED,sFAAsF;AACtF,8BAAsB,yBAAyB;IAC7C,QAAQ,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,WAAW,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;IAG9D,YAAY,IAAI,WAAW,CAE1B;CACF;AAKD,2FAA2F;AAC3F,8BAAsB,eAAe,CAAC,KAAK,CAAE,SAAQ,yBAAyB;IAC5E,QAAQ,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,WAAW,KAAK,OAAO,CAAC,KAAK,CAAC,CAAC;IAE5D,QAAQ,CAAC,QAAQ,CAAC,OAAO,EAAE,CACzB,OAAO,EAAE,CAAC,GAAG,EAAE,WAAW,KAAK,KAAK,GAAG,WAAW,CAAC,KAAK,CAAC,KACtD,eAAe,CAAC,KAAK,CAAC,CAAC;CAC7B;AA6BD,uEAAuE;AACvE,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,EAAE;IAC5C,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,CAAC,GAAG,EAAE,WAAW,KAAK,KAAK,CAAC;CACtC,GAAG,eAAe,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAelC;AAED,wBAAgB,cAAc,CAAC,QAAQ,EAAE,OAAO,GAAG,cAAc,CAgBhE"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { readContext } from "./access.js";
|
|
2
|
+
const brand = Symbol.for("@toolgrain/mcp/context-provider/v1");
|
|
3
|
+
const bindingKey = Symbol.for("@toolgrain/mcp/context-binding/v1");
|
|
4
|
+
/** Erase values for heterogeneous registrations without exposing unsafe overrides. */
|
|
5
|
+
export class RegisteredContextProvider {
|
|
6
|
+
// Structural branding keeps compatible package copies interoperable.
|
|
7
|
+
"~toolgrain"() {
|
|
8
|
+
return "context@1";
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
Object.defineProperty(RegisteredContextProvider.prototype, brand, {
|
|
12
|
+
value: true,
|
|
13
|
+
});
|
|
14
|
+
/** An opaque, invariant value contract; use createContext() and provide() for bindings. */
|
|
15
|
+
export class ContextProvider extends RegisteredContextProvider {
|
|
16
|
+
}
|
|
17
|
+
class Provider extends ContextProvider {
|
|
18
|
+
name;
|
|
19
|
+
use;
|
|
20
|
+
constructor(binding, use = (ctx) => readContext(ctx, binding.identity, binding.name)) {
|
|
21
|
+
super();
|
|
22
|
+
this.name = binding.name;
|
|
23
|
+
this.use = use;
|
|
24
|
+
Object.defineProperty(this, bindingKey, { value: Object.freeze(binding) });
|
|
25
|
+
Object.freeze(this);
|
|
26
|
+
}
|
|
27
|
+
provide = (resolve) => {
|
|
28
|
+
if (typeof resolve !== "function")
|
|
29
|
+
throw new TypeError("Context provider overrides require a resolver function");
|
|
30
|
+
return new Provider({ ...contextBinding(this), resolve }, this.use);
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/** Infer the resolved value type, including asynchronous factories. */
|
|
34
|
+
export function createContext(options) {
|
|
35
|
+
if (!options ||
|
|
36
|
+
typeof options.name !== "string" ||
|
|
37
|
+
!options.name.trim() ||
|
|
38
|
+
typeof options.resolve !== "function")
|
|
39
|
+
throw new TypeError("Context providers require a nonempty name and a resolver function");
|
|
40
|
+
return new Provider({
|
|
41
|
+
identity: Symbol(options.name),
|
|
42
|
+
name: options.name,
|
|
43
|
+
resolve: options.resolve,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
export function contextBinding(provider) {
|
|
47
|
+
if (typeof provider !== "object" ||
|
|
48
|
+
provider === null ||
|
|
49
|
+
provider[brand] !== true)
|
|
50
|
+
throw new TypeError("Context providers must be created with createContext()");
|
|
51
|
+
const binding = Object.getOwnPropertyDescriptor(provider, bindingKey)
|
|
52
|
+
?.value;
|
|
53
|
+
if (!binding)
|
|
54
|
+
throw new TypeError("Context providers must be created with createContext()");
|
|
55
|
+
return binding;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=definition.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"definition.js","sourceRoot":"","sources":["../../src/context/definition.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,oCAAoC,CAAC,CAAC;AAC/D,MAAM,UAAU,GAAG,MAAM,CAAC,GAAG,CAAC,mCAAmC,CAAC,CAAC;AAQnE,sFAAsF;AACtF,MAAM,OAAgB,yBAAyB;IAI7C,qEAAqE;IACrE,YAAY;QACV,OAAO,WAAW,CAAC;IACrB,CAAC;CACF;AACD,MAAM,CAAC,cAAc,CAAC,yBAAyB,CAAC,SAAS,EAAE,KAAK,EAAE;IAChE,KAAK,EAAE,IAAI;CACZ,CAAC,CAAC;AAEH,2FAA2F;AAC3F,MAAM,OAAgB,eAAuB,SAAQ,yBAAyB;CAM7E;AAED,MAAM,QAAgB,SAAQ,eAAsB;IACzC,IAAI,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AiB;IAC9C,IACE,OAAO,QAAQ,KAAK,QAAQ;QAC5B,QAAQ,KAAK,IAAI;QAChB,QAAkC,CAAC,KAAK,CAAC,KAAK,IAAI;QAEnD,MAAM,IAAI,SAAS,CACjB,wDAAwD,CACzD,CAAC;IACJ,MAAM,OAAO,GAAG,MAAM,CAAC,wBAAwB,CAAC,QAAQ,EAAE,UAAU,CAAC;QACnE,EAAE,KAAmC,CAAC;IACxC,IAAI,CAAC,OAAO;QACV,MAAM,IAAI,SAAS,CACjB,wDAAwD,CACzD,CAAC;IACJ,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type ContextBinding, type RegisteredContextProvider } from "./definition.js";
|
|
2
|
+
export type ContextRegistry = ReadonlyMap<symbol, ContextBinding>;
|
|
3
|
+
/** Snapshot definitions once; provider names are labels, not registration keys. */
|
|
4
|
+
export declare function createContextRegistry(providers?: readonly RegisteredContextProvider[]): ContextRegistry;
|
|
5
|
+
//# sourceMappingURL=registry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/context/registry.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,cAAc,EAEnB,KAAK,yBAAyB,EAC/B,MAAM,iBAAiB,CAAC;AAEzB,MAAM,MAAM,eAAe,GAAG,WAAW,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;AAElE,mFAAmF;AACnF,wBAAgB,qBAAqB,CACnC,SAAS,GAAE,SAAS,yBAAyB,EAAO,GACnD,eAAe,CAajB"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { contextBinding, } from "./definition.js";
|
|
2
|
+
/** Snapshot definitions once; provider names are labels, not registration keys. */
|
|
3
|
+
export function createContextRegistry(providers = []) {
|
|
4
|
+
if (!Array.isArray(providers))
|
|
5
|
+
throw new TypeError("Context registration must be an array of providers");
|
|
6
|
+
const registry = new Map();
|
|
7
|
+
for (const provider of providers) {
|
|
8
|
+
const binding = contextBinding(provider);
|
|
9
|
+
if (registry.has(binding.identity))
|
|
10
|
+
throw new TypeError(`Context provider ${JSON.stringify(binding.name)} is registered more than once`);
|
|
11
|
+
registry.set(binding.identity, binding);
|
|
12
|
+
}
|
|
13
|
+
return registry;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=registry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/context/registry.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,cAAc,GAEf,MAAM,iBAAiB,CAAC;AAIzB,mFAAmF;AACnF,MAAM,UAAU,qBAAqB,CACnC,SAAS,GAAyC,EAAE;IAEpD,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC;QAC3B,MAAM,IAAI,SAAS,CAAC,oDAAoD,CAAC,CAAC;IAC5E,MAAM,QAAQ,GAAG,IAAI,GAAG,EAA0B,CAAC;IACnD,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;QACjC,MAAM,OAAO,GAAG,cAAc,CAAC,QAAQ,CAAC,CAAC;QACzC,IAAI,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,CAAC;YAChC,MAAM,IAAI,SAAS,CACjB,oBAAoB,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,+BAA+B,CAChF,CAAC;QACJ,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IAC1C,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ToolContext } from "../tools/context.js";
|
|
2
|
+
import type { ContextRegistry } from "./registry.js";
|
|
3
|
+
/** One cache and pending dependency graph per tool context, independent of request IDs. */
|
|
4
|
+
export declare function createContextScope(ctx: ToolContext, registry: ContextRegistry): ToolContext;
|
|
5
|
+
//# sourceMappingURL=scope.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scope.d.ts","sourceRoot":"","sources":["../../src/context/scope.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAEvD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AASrD,2FAA2F;AAC3F,wBAAgB,kBAAkB,CAChC,GAAG,EAAE,WAAW,EAChB,QAAQ,EAAE,eAAe,GACxB,WAAW,CAmDb"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { withContextAccess } from "./access.js";
|
|
2
|
+
/** One cache and pending dependency graph per tool context, independent of request IDs. */
|
|
3
|
+
export function createContextScope(ctx, registry) {
|
|
4
|
+
const resolutions = new Map();
|
|
5
|
+
const resolve = (identity, name, caller) => {
|
|
6
|
+
const binding = registry.get(identity);
|
|
7
|
+
if (!binding)
|
|
8
|
+
throw new Error(`Context provider ${JSON.stringify(name)} is not registered`);
|
|
9
|
+
let resolution = resolutions.get(identity);
|
|
10
|
+
if (!resolution) {
|
|
11
|
+
const current = {
|
|
12
|
+
name: binding.name,
|
|
13
|
+
pending: true,
|
|
14
|
+
dependencies: new Set(),
|
|
15
|
+
// Publish the promise before running user code, including synchronous factories.
|
|
16
|
+
promise: Promise.resolve()
|
|
17
|
+
.then(() => {
|
|
18
|
+
ctx.signal.throwIfAborted();
|
|
19
|
+
return binding.resolve(withContextAccess(ctx, (key, label) => resolve(key, label, current)));
|
|
20
|
+
})
|
|
21
|
+
.then((value) => {
|
|
22
|
+
ctx.signal.throwIfAborted();
|
|
23
|
+
return value;
|
|
24
|
+
})
|
|
25
|
+
.finally(() => {
|
|
26
|
+
current.pending = false;
|
|
27
|
+
current.dependencies.clear();
|
|
28
|
+
}),
|
|
29
|
+
};
|
|
30
|
+
resolutions.set(identity, current);
|
|
31
|
+
resolution = current;
|
|
32
|
+
}
|
|
33
|
+
if (caller?.pending && resolution.pending) {
|
|
34
|
+
const path = dependencyPath(resolution, caller, new Set());
|
|
35
|
+
if (path)
|
|
36
|
+
throw new Error(`Context dependency cycle: ${[caller, ...path].map((item) => item.name).join(" -> ")}`);
|
|
37
|
+
caller.dependencies.add(resolution);
|
|
38
|
+
}
|
|
39
|
+
return resolution.promise;
|
|
40
|
+
};
|
|
41
|
+
return withContextAccess(ctx, (identity, name) => resolve(identity, name));
|
|
42
|
+
}
|
|
43
|
+
/** Follow only unfinished resolutions; shared pending dependencies are not cycles. */
|
|
44
|
+
function dependencyPath(from, target, visited) {
|
|
45
|
+
if (from === target)
|
|
46
|
+
return [from];
|
|
47
|
+
if (!from.pending || visited.has(from))
|
|
48
|
+
return;
|
|
49
|
+
visited.add(from);
|
|
50
|
+
for (const dependency of from.dependencies) {
|
|
51
|
+
const path = dependencyPath(dependency, target, visited);
|
|
52
|
+
if (path)
|
|
53
|
+
return [from, ...path];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=scope.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scope.js","sourceRoot":"","sources":["../../src/context/scope.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAUhD,2FAA2F;AAC3F,MAAM,UAAU,kBAAkB,CAChC,GAAgB,EAChB,QAAyB;IAEzB,MAAM,WAAW,GAAG,IAAI,GAAG,EAAsB,CAAC;IAClD,MAAM,OAAO,GAAG,CACd,QAAgB,EAChB,IAAY,EACZ,MAAmB,EACD,EAAE;QACpB,MAAM,OAAO,GAAG,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QACvC,IAAI,CAAC,OAAO;YACV,MAAM,IAAI,KAAK,CACb,oBAAoB,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,oBAAoB,CAC7D,CAAC;QACJ,IAAI,UAAU,GAAG,WAAW,CAAC,GAAG,CAAC,QAAQ,CAAC,CA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CAAC,EAAE,CACvF,CAAC;YACJ,MAAM,CAAC,YAAY,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;QACtC,CAAC;QACD,OAAO,UAAU,CAAC,OAAO,CAAC;IAC5B,CAAC,CAAC;IACF,OAAO,iBAAiB,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,IAAI,EAAE,EAAE,CAAC,OAAO,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC,CAAC;AAC7E,CAAC;AAED,sFAAsF;AACtF,SAAS,cAAc,CACrB,IAAgB,EAChB,MAAkB,EAClB,OAAwB;IAExB,IAAI,IAAI,KAAK,MAAM;QAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IACnC,IAAI,CAAC,IAAI,CAAC,OAAO,IAAI,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC;QAAE,OAAO;IAC/C,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAClB,KAAK,MAAM,UAAU,IAAI,IAAI,CAAC,YAAY,EAAE,CAAC;QAC3C,MAAM,IAAI,GAAG,cAAc,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;QACzD,IAAI,IAAI;YAAE,OAAO,CAAC,IAAI,EAAE,GAAG,IAAI,CAAC,CAAC;IACnC,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ToolRequest } from "./tools/context.js";
|
|
2
|
+
/** Private diagnostics for an unexpected tool failure; no arguments or auth data are added. */
|
|
3
|
+
export interface ToolErrorDetails {
|
|
4
|
+
readonly toolName: string;
|
|
5
|
+
readonly request: ToolRequest;
|
|
6
|
+
}
|
|
7
|
+
export type ErrorHandler = (error: unknown, details?: ToolErrorDetails) => void;
|
|
8
|
+
/** Diagnostics cannot interrupt a response or cleanup, including rejected async callbacks. */
|
|
9
|
+
export declare function reportError(onError: ErrorHandler | undefined, error: unknown, details?: ToolErrorDetails): void;
|
|
10
|
+
//# sourceMappingURL=diagnostics.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"diagnostics.d.ts","sourceRoot":"","sources":["../src/diagnostics.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEtD,+FAA+F;AAC/F,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC;CAC/B;AAED,MAAM,MAAM,YAAY,GAAG,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,gBAAgB,KAAK,IAAI,CAAC;AAEhF,8FAA8F;AAC9F,wBAAgB,WAAW,CACzB,OAAO,EAAE,YAAY,GAAG,SAAS,EACjC,KAAK,EAAE,OAAO,EACd,OAAO,CAAC,EAAE,gBAAgB,GACzB,IAAI,CASN"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Diagnostics cannot interrupt a response or cleanup, including rejected async callbacks. */
|
|
2
|
+
export function reportError(onError, error, details) {
|
|
3
|
+
if (!onError)
|
|
4
|
+
return;
|
|
5
|
+
try {
|
|
6
|
+
const result = details === undefined ? onError(error) : onError(error, details);
|
|
7
|
+
void Promise.resolve(result).catch(() => { });
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
/* Diagnostics must not replace the original failure. */
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=diagnostics.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"diagnostics.js","sourceRoot":"","sources":["../src/diagnostics.ts"],"names":[],"mappings":"AAUA,8FAA8F;AAC9F,MAAM,UAAU,WAAW,CACzB,OAAiC,EACjC,KAAc,EACd,OAA0B;IAE1B,IAAI,CAAC,OAAO;QAAE,OAAO;IACrB,IAAI,CAAC;QACH,MAAM,MAAM,GACV,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QACnE,KAAK,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;IAC/C,CAAC;IAAC,MAAM,CAAC;QACP,wDAAwD;IAC1D,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type Implementation, type JSONRPCErrorResponse, type ProtocolResponse, type SupportedProtocolVersion } from "@toolgrain/protocol";
|
|
2
|
+
import type { AuthIdentity } from "./auth/types.js";
|
|
3
|
+
import type { ContextRegistry } from "./context/registry.js";
|
|
4
|
+
import type { Request } from "./protocol/messages.js";
|
|
5
|
+
import type { ToolAdapter } from "./tools/adapter.js";
|
|
6
|
+
import type { ToolClient } from "./tools/context.js";
|
|
7
|
+
/** Per-request execution state supplied by the connection or independent caller. */
|
|
8
|
+
export interface RequestScope {
|
|
9
|
+
signal: AbortSignal;
|
|
10
|
+
/** Client details negotiated by legacy initialization, when the connection has them. */
|
|
11
|
+
client?: ToolClient | undefined;
|
|
12
|
+
auth?: AuthIdentity;
|
|
13
|
+
}
|
|
14
|
+
export type Dispatcher = (request: Request, scope: RequestScope, protocolVersion?: SupportedProtocolVersion) => Promise<ProtocolResponse | undefined>;
|
|
15
|
+
/** Compose the two protocol adapters around one execution registry. */
|
|
16
|
+
export declare function createDispatcher(tools: ToolAdapter, identity: Implementation, instructions?: string, providers?: ContextRegistry): Dispatcher;
|
|
17
|
+
/** Envelope errors have no negotiated connection when processing independent calls. */
|
|
18
|
+
export declare function formatErrorResponse(response: JSONRPCErrorResponse, protocolVersion?: SupportedProtocolVersion): ProtocolResponse;
|
|
19
|
+
//# sourceMappingURL=dispatcher.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dispatcher.d.ts","sourceRoot":"","sources":["../src/dispatcher.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,cAAc,EACnB,KAAK,oBAAoB,EAEzB,KAAK,gBAAgB,EACrB,KAAK,wBAAwB,EAC9B,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AACpD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAO7D,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAEtD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAErD,oFAAoF;AACpF,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,WAAW,CAAC;IACpB,wFAAwF;IACxF,MAAM,CAAC,EAAE,UAAU,GAAG,SAAS,CAAC;IAChC,IAAI,CAAC,EAAE,YAAY,CAAC;CACrB;AAED,MAAM,MAAM,UAAU,GAAG,CACvB,OAAO,EAAE,OAAO,EAChB,KAAK,EAAE,YAAY,EACnB,eAAe,CAAC,EAAE,wBAAwB,KACvC,OAAO,CAAC,gBAAgB,GAAG,SAAS,CAAC,CAAC;AAE3C,uEAAuE;AACvE,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,WAAW,EAClB,QAAQ,EAAE,cAAc,EACxB,YAAY,CAAC,EAAE,MAAM,EACrB,SAAS,CAAC,EAAE,eAAe,GAC1B,UAAU,CAmBZ;AAED,uFAAuF;AACvF,wBAAgB,mBAAmB,CACjC,QAAQ,EAAE,oBAAoB,EAC9B,eAAe,CAAC,EAAE,wBAAwB,GACzC,gBAAgB,CAIlB"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { LEGACY_PROTOCOL_VERSION, } from "@toolgrain/protocol";
|
|
2
|
+
import { createLegacyDispatcher, usesLegacyProtocol, } from "./legacy/dispatcher.js";
|
|
3
|
+
import { errorResponse as legacyErrorResponse } from "./legacy/responses.js";
|
|
4
|
+
import { createModernDispatcher } from "./modern/dispatcher.js";
|
|
5
|
+
import { validateVersionContext } from "./protocol/version-metadata.js";
|
|
6
|
+
/** Compose the two protocol adapters around one execution registry. */
|
|
7
|
+
export function createDispatcher(tools, identity, instructions, providers) {
|
|
8
|
+
const legacy = createLegacyDispatcher(tools, identity, instructions, providers);
|
|
9
|
+
const modern = createModernDispatcher(tools, identity, instructions, providers);
|
|
10
|
+
return (request, scope, protocolVersion) => {
|
|
11
|
+
validateVersionContext(request, protocolVersion);
|
|
12
|
+
return usesLegacyProtocol(request, protocolVersion)
|
|
13
|
+
? legacy(request, scope)
|
|
14
|
+
: modern(request, scope);
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/** Envelope errors have no negotiated connection when processing independent calls. */
|
|
18
|
+
export function formatErrorResponse(response, protocolVersion) {
|
|
19
|
+
return protocolVersion === LEGACY_PROTOCOL_VERSION
|
|
20
|
+
? legacyErrorResponse(response)
|
|
21
|
+
: response;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=dispatcher.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dispatcher.js","sourceRoot":"","sources":["../src/dispatcher.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,uBAAuB,GAGxB,MAAM,qBAAqB,CAAC;AAG7B,OAAO,EACL,sBAAsB,EACtB,kBAAkB,GACnB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,aAAa,IAAI,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AAC7E,OAAO,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AAEhE,OAAO,EAAE,sBAAsB,EAAE,MAAM,gCAAgC,CAAC;AAkBxE,uEAAuE;AACvE,MAAM,UAAU,gBAAgB,CAC9B,KAAkB,EAClB,QAAwB,EACxB,YAAqB,EACrB,SAA2B;IAE3B,MAAM,MAAM,GAAG,sBAAsB,CACnC,KAAK,EACL,QAAQ,EACR,YAAY,EACZ,SAAS,CACV,CAAC;IACF,MAAM,MAAM,GAAG,sBAAsB,CACnC,KAAK,EACL,QAAQ,EACR,YAAY,EACZ,SAAS,CACV,CAAC;IACF,OAAO,CAAC,OAAO,EAAE,KAAK,EAAE,eAAe,EAAE,EAAE;QACzC,sBAAsB,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC;QACjD,OAAO,kBAAkB,CAAC,OAAO,EAAE,eAAe,CAAC;YACjD,CAAC,CAAC,MAAM,CAAC,OAAO,EAAE,KAAK,CAAC;YACxB,CAAC,CAAC,MAAM,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;IAC7B,CAAC,CAAC;AACJ,CAAC;AAED,uFAAuF;AACvF,MAAM,UAAU,mBAAmB,CACjC,QAA8B,EAC9B,eAA0C;IAE1C,OAAO,eAAe,KAAK,uBAAuB;QAChD,CAAC,CAAC,mBAAmB,CAAC,QAAQ,CAAC;QAC/B,CAAC,CAAC,QAAQ,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type ProtocolResponse, type SupportedProtocolVersion } from "@toolgrain/protocol";
|
|
2
|
+
import { type ErrorHandler } from "./diagnostics.js";
|
|
3
|
+
import type { Dispatcher, RequestScope } from "./dispatcher.js";
|
|
4
|
+
import type { Request } from "./protocol/messages.js";
|
|
5
|
+
/** Shared dispatch boundary for connection-scoped and independent requests. */
|
|
6
|
+
export declare function executeRequest(dispatch: Dispatcher, request: Request, scope: RequestScope, onError?: ErrorHandler, protocolVersion?: SupportedProtocolVersion): Promise<ProtocolResponse | undefined>;
|
|
7
|
+
//# sourceMappingURL=execute-request.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"execute-request.d.ts","sourceRoot":"","sources":["../src/execute-request.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,gBAAgB,EACrB,KAAK,wBAAwB,EAC9B,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,KAAK,YAAY,EAAe,MAAM,kBAAkB,CAAC;AAClE,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAChE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAC;AAGtD,+EAA+E;AAC/E,wBAAsB,cAAc,CAClC,QAAQ,EAAE,UAAU,EACpB,OAAO,EAAE,OAAO,EAChB,KAAK,EAAE,YAAY,EACnB,OAAO,CAAC,EAAE,YAAY,EACtB,eAAe,CAAC,EAAE,wBAAwB,GACzC,OAAO,CAAC,gBAAgB,GAAG,SAAS,CAAC,CAiBvC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { INTERNAL_ERROR, } from "@toolgrain/protocol";
|
|
2
|
+
import { reportError } from "./diagnostics.js";
|
|
3
|
+
import { ProtocolError, protocolError } from "./protocol/responses.js";
|
|
4
|
+
/** Shared dispatch boundary for connection-scoped and independent requests. */
|
|
5
|
+
export async function executeRequest(dispatch, request, scope, onError, protocolVersion) {
|
|
6
|
+
const isActive = () => !scope.signal.aborted;
|
|
7
|
+
try {
|
|
8
|
+
const response = await dispatch(request, scope, protocolVersion);
|
|
9
|
+
return isActive() ? response : undefined;
|
|
10
|
+
}
|
|
11
|
+
catch (error) {
|
|
12
|
+
if (!isActive())
|
|
13
|
+
return;
|
|
14
|
+
if (error instanceof ProtocolError)
|
|
15
|
+
return protocolError(error.code, error.message, request.id, error.data);
|
|
16
|
+
reportError(onError, error);
|
|
17
|
+
return protocolError(INTERNAL_ERROR, "Internal error", request.id, undefined);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=execute-request.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"execute-request.js","sourceRoot":"","sources":["../src/execute-request.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,GAGf,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAqB,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAGlE,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAEvE,+EAA+E;AAC/E,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,QAAoB,EACpB,OAAgB,EAChB,KAAmB,EACnB,OAAsB,EACtB,eAA0C;IAE1C,MAAM,QAAQ,GAAG,GAAG,EAAE,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC;IAC7C,IAAI,CAAC;QACH,MAAM,QAAQ,GAAG,MAAM,QAAQ,CAAC,OAAO,EAAE,KAAK,EAAE,eAAe,CAAC,CAAC;QACjE,OAAO,QAAQ,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC;IAC3C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAI,CAAC,QAAQ,EAAE;YAAE,OAAO;QACxB,IAAI,KAAK,YAAY,aAAa;YAChC,OAAO,aAAa,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,EAAE,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC;QAC1E,WAAW,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;QAC5B,OAAO,aAAa,CAClB,cAAc,EACd,gBAAgB,EAChB,OAAO,CAAC,EAAE,EACV,SAAS,CACV,CAAC;IACJ,CAAC;AACH,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export { validateTokenAudience } from "./auth/audience.js";
|
|
2
|
+
export { createAuthAccessor } from "./auth/context.js";
|
|
3
|
+
export type { AuthErrorCode } from "./auth/errors.js";
|
|
4
|
+
export { AuthError, isAuthError } from "./auth/errors.js";
|
|
5
|
+
export type { AuthMetadata, AuthProvider, AuthReadonly, AuthScope, AuthVerifyOptions, } from "./auth/types.js";
|
|
6
|
+
export { authUrl as validateAuthUrl } from "./auth/url.js";
|
|
7
|
+
export type { ContextProvider } from "./context/definition.js";
|
|
8
|
+
export { createContext } from "./context/definition.js";
|
|
9
|
+
export type { ToolErrorDetails } from "./diagnostics.js";
|
|
10
|
+
export type { HandleOptions, Server, ServerOptions } from "./server.js";
|
|
11
|
+
export { createServer } from "./server.js";
|
|
12
|
+
export type { ToolClient, ToolContext, ToolContextOptions, ToolRequest, } from "./tools/context.js";
|
|
13
|
+
export { toolContext } from "./tools/context.js";
|
|
14
|
+
export type { Tool, ToolDefinition, ToolMetadata } from "./tools/definition.js";
|
|
15
|
+
export { tool } from "./tools/definition.js";
|
|
16
|
+
export { ToolNameError } from "./tools/name.js";
|
|
17
|
+
export type { ToolMap } from "./tools/registry.js";
|
|
18
|
+
export type { McpErrorResult, McpResult, McpResultOptions, McpStructuredResult, } from "./tools/rich-result.js";
|
|
19
|
+
export { mcpResult } from "./tools/rich-result.js";
|
|
20
|
+
export { ToolSchemaError } from "./tools/schema.js";
|
|
21
|
+
export type { RequestTransport, Transport, TransportCallbacks, } from "./transport.js";
|
|
22
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACvD,YAAY,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC1D,YAAY,EACV,YAAY,EACZ,YAAY,EACZ,YAAY,EACZ,SAAS,EACT,iBAAiB,GAClB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,OAAO,IAAI,eAAe,EAAE,MAAM,eAAe,CAAC;AAC3D,YAAY,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,YAAY,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACzD,YAAY,EAAE,aAAa,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AACxE,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,YAAY,EACV,UAAU,EACV,WAAW,EACX,kBAAkB,EAClB,WAAW,GACZ,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,YAAY,EAAE,IAAI,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAChF,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,YAAY,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AACnD,YAAY,EACV,cAAc,EACd,SAAS,EACT,gBAAgB,EAChB,mBAAmB,GACpB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,YAAY,EACV,gBAAgB,EAChB,SAAS,EACT,kBAAkB,GACnB,MAAM,gBAAgB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { validateTokenAudience } from "./auth/audience.js";
|
|
2
|
+
export { createAuthAccessor } from "./auth/context.js";
|
|
3
|
+
export { AuthError, isAuthError } from "./auth/errors.js";
|
|
4
|
+
export { authUrl as validateAuthUrl } from "./auth/url.js";
|
|
5
|
+
export { createContext } from "./context/definition.js";
|
|
6
|
+
export { createServer } from "./server.js";
|
|
7
|
+
export { toolContext } from "./tools/context.js";
|
|
8
|
+
export { tool } from "./tools/definition.js";
|
|
9
|
+
export { ToolNameError } from "./tools/name.js";
|
|
10
|
+
export { mcpResult } from "./tools/rich-result.js";
|
|
11
|
+
export { ToolSchemaError } from "./tools/schema.js";
|
|
12
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAEvD,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAQ1D,OAAO,EAAE,OAAO,IAAI,eAAe,EAAE,MAAM,eAAe,CAAC;AAE3D,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAGxD,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAO3C,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEjD,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAQhD,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type JSONRPCErrorResponse, type ProtocolResponse, type RequestId, type SupportedProtocolVersion } from "@toolgrain/protocol";
|
|
2
|
+
import type { Notification, Request } from "../protocol/messages.js";
|
|
3
|
+
import { type ToolClient } from "../tools/context.js";
|
|
4
|
+
/** Owns the initialization lifecycle for one attached transport. */
|
|
5
|
+
export declare function createLegacyConnection(): {
|
|
6
|
+
prepare(request: Request): {
|
|
7
|
+
error?: never;
|
|
8
|
+
readonly protocolVersion?: never;
|
|
9
|
+
} | {
|
|
10
|
+
error: JSONRPCErrorResponse;
|
|
11
|
+
readonly protocolVersion?: never;
|
|
12
|
+
} | {
|
|
13
|
+
error?: never;
|
|
14
|
+
readonly protocolVersion: "2025-06-18";
|
|
15
|
+
};
|
|
16
|
+
begin(request: Request, protocolVersion?: SupportedProtocolVersion): {
|
|
17
|
+
respond(response: ProtocolResponse | undefined): void;
|
|
18
|
+
finish(): void;
|
|
19
|
+
} | undefined;
|
|
20
|
+
notification(notification: Notification): void;
|
|
21
|
+
/** Details from a completed legacy initialization on this connection. */
|
|
22
|
+
client(): ToolClient | undefined;
|
|
23
|
+
canCancel(id: RequestId): boolean;
|
|
24
|
+
errorResponse(response: JSONRPCErrorResponse): JSONRPCErrorResponse | import("@toolgrain/protocol").LegacyJSONRPCErrorResponse;
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=connection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection.d.ts","sourceRoot":"","sources":["../../src/legacy/connection.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,oBAAoB,EAEzB,KAAK,gBAAgB,EACrB,KAAK,SAAS,EACd,KAAK,wBAAwB,EAC9B,MAAM,qBAAqB,CAAC;AAK7B,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAMrE,OAAO,EAAE,KAAK,UAAU,EAAc,MAAM,qBAAqB,CAAC;AAIlE,oEAAoE;AACpE,wBAAgB,sBAAsB;IAKlC,OAAO,UAAU,OAAO;;;;;;;;;;IA0BxB,KAAK,UAAU,OAAO,oBAAoB,wBAAwB;0BAS5C,gBAAgB,GAAG,SAAS;;;IAkBlD,YAAY,eAAe,YAAY;IAQvC,yEAAyE;IACzE,MAAM;IAGN,SAAS,KAAK,SAAS;IAGvB,aAAa,WAAW,oBAAoB;EAI/C"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { INVALID_REQUEST, LEGACY_PROTOCOL_VERSION, } from "@toolgrain/protocol";
|
|
2
|
+
import { protocolError } from "../protocol/responses.js";
|
|
3
|
+
import { hasProtocolVersionMetadata, metadataMatchesVersion, } from "../protocol/version-metadata.js";
|
|
4
|
+
import { toolClient } from "../tools/context.js";
|
|
5
|
+
import { commonParams, initializeParams } from "./messages.js";
|
|
6
|
+
import { errorResponse } from "./responses.js";
|
|
7
|
+
/** Owns the initialization lifecycle for one attached transport. */
|
|
8
|
+
export function createLegacyConnection() {
|
|
9
|
+
let state;
|
|
10
|
+
let initializeId;
|
|
11
|
+
let client;
|
|
12
|
+
return {
|
|
13
|
+
prepare(request) {
|
|
14
|
+
const legacy = (state !== undefined &&
|
|
15
|
+
metadataMatchesVersion(request, LEGACY_PROTOCOL_VERSION)) ||
|
|
16
|
+
(!hasProtocolVersionMetadata(request) &&
|
|
17
|
+
(state !== undefined ||
|
|
18
|
+
request.method === "initialize" ||
|
|
19
|
+
request.method === "ping"));
|
|
20
|
+
if (!legacy)
|
|
21
|
+
return {};
|
|
22
|
+
const initialize = request.method === "initialize";
|
|
23
|
+
if (initialize
|
|
24
|
+
? state !== undefined
|
|
25
|
+
: state !== "ready" && request.method !== "ping")
|
|
26
|
+
return {
|
|
27
|
+
error: protocolError(INVALID_REQUEST, initialize
|
|
28
|
+
? "Server is already initialized"
|
|
29
|
+
: "Server is not initialized", request.id),
|
|
30
|
+
};
|
|
31
|
+
return { protocolVersion: LEGACY_PROTOCOL_VERSION };
|
|
32
|
+
},
|
|
33
|
+
begin(request, protocolVersion) {
|
|
34
|
+
if (protocolVersion !== LEGACY_PROTOCOL_VERSION ||
|
|
35
|
+
request.method !== "initialize")
|
|
36
|
+
return;
|
|
37
|
+
state = "initializing";
|
|
38
|
+
initializeId = request.id;
|
|
39
|
+
return {
|
|
40
|
+
respond(response) {
|
|
41
|
+
state =
|
|
42
|
+
response && "result" in response
|
|
43
|
+
? "awaiting-initialized"
|
|
44
|
+
: undefined;
|
|
45
|
+
// The response succeeded only if these parameters already validated.
|
|
46
|
+
const params = initializeParams.safeParse(request.params);
|
|
47
|
+
if (state && params.success)
|
|
48
|
+
client = toolClient(params.data.clientInfo, params.data.capabilities);
|
|
49
|
+
},
|
|
50
|
+
finish() {
|
|
51
|
+
initializeId = undefined;
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
},
|
|
55
|
+
notification(notification) {
|
|
56
|
+
if (notification.method === "notifications/initialized" &&
|
|
57
|
+
commonParams.safeParse(notification.params ?? {}).success &&
|
|
58
|
+
state === "awaiting-initialized")
|
|
59
|
+
state = "ready";
|
|
60
|
+
},
|
|
61
|
+
/** Details from a completed legacy initialization on this connection. */
|
|
62
|
+
client() {
|
|
63
|
+
return client;
|
|
64
|
+
},
|
|
65
|
+
canCancel(id) {
|
|
66
|
+
return id !== initializeId;
|
|
67
|
+
},
|
|
68
|
+
errorResponse(response) {
|
|
69
|
+
return state === undefined ? response : errorResponse(response);
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=connection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"connection.js","sourceRoot":"","sources":["../../src/legacy/connection.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EAEf,uBAAuB,GAIxB,MAAM,qBAAqB,CAAC;AAM7B,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EACL,0BAA0B,EAC1B,sBAAsB,GACvB,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAmB,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAClE,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAE/C,oEAAoE;AACpE,MAAM,UAAU,sBAAsB;IACpC,IAAI,KAAoE,CAAC;IACzE,IAAI,YAAmC,CAAC;IACxC,IAAI,MAA8B,CAAC;IACnC,OAAO;QACL,OAAO,CAAC,OAAgB;YACtB,MAAM,MAAM,GACV,CAAC,KAAK,KAAK,SAAS;gBAClB,sBAAsB,CAAC,OAAO,EAAE,uBAAuB,CAAC,CAAC;gBAC3D,CAAC,CAAC,0BAA0B,CAAC,OAAO,CAAC;oBACnC,CAA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B,CAAC;aACF,CAAC;QACJ,CAAC;QACD,YAAY,CAAC,YAA0B;YACrC,IACE,YAAY,CAAC,MAAM,KAAK,2BAA2B;gBACnD,YAAY,CAAC,SAAS,CAAC,YAAY,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,OAAO;gBACzD,KAAK,KAAK,sBAAsB;gBAEhC,KAAK,GAAG,OAAO,CAAC;QACpB,CAAC;QACD,yEAAyE;QACzE,MAAM;YACJ,OAAO,MAAM,CAAC;QAChB,CAAC;QACD,SAAS,CAAC,EAAa;YACrB,OAAO,EAAE,KAAK,YAAY,CAAC;QAC7B,CAAC;QACD,aAAa,CAAC,QAA8B;YAC1C,OAAO,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;QAClE,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type Implementation, type SupportedProtocolVersion } from "@toolgrain/protocol";
|
|
2
|
+
import type { JSONRPCResponse } from "@toolgrain/protocol/2025-06-18";
|
|
3
|
+
import type { ContextRegistry } from "../context/registry.js";
|
|
4
|
+
import type { RequestScope } from "../dispatcher.js";
|
|
5
|
+
import type { Request } from "../protocol/messages.js";
|
|
6
|
+
import type { ToolAdapter } from "../tools/adapter.js";
|
|
7
|
+
/** Initialization selects this adapter only without an explicit modern context. */
|
|
8
|
+
export declare function usesLegacyProtocol(request: Request, protocolVersion?: SupportedProtocolVersion): boolean;
|
|
9
|
+
export declare function createLegacyDispatcher(tools: ToolAdapter, identity: Implementation, instructions?: string, providers?: ContextRegistry): (request: Request, scope: RequestScope) => Promise<JSONRPCResponse | undefined>;
|
|
10
|
+
//# sourceMappingURL=dispatcher.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dispatcher.d.ts","sourceRoot":"","sources":["../../src/legacy/dispatcher.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,cAAc,EAGnB,KAAK,wBAAwB,EAC9B,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EAAE,eAAe,EAAU,MAAM,gCAAgC,CAAC;AAC9E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAC9D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAGvD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAKvD,mFAAmF;AACnF,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,OAAO,EAChB,eAAe,CAAC,EAAE,wBAAwB,GACzC,OAAO,CAOT;AAED,wBAAgB,sBAAsB,CACpC,KAAK,EAAE,WAAW,EAClB,QAAQ,EAAE,cAAc,EACxB,YAAY,CAAC,EAAE,MAAM,EACrB,SAAS,CAAC,EAAE,eAAe,aAGhB,OAAO,SACT,YAAY,KAClB,OAAO,CAAC,eAAe,GAAG,SAAS,CAAC,CAgCxC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { LEGACY_PROTOCOL_VERSION, METHOD_NOT_FOUND, } from "@toolgrain/protocol";
|
|
2
|
+
import { ProtocolError } from "../protocol/responses.js";
|
|
3
|
+
import { hasProtocolVersionMetadata } from "../protocol/version-metadata.js";
|
|
4
|
+
import { createToolContext } from "../tools/context.js";
|
|
5
|
+
import { callTool, initialize, listTools, ping } from "./handlers.js";
|
|
6
|
+
import { successResponse } from "./responses.js";
|
|
7
|
+
/** Initialization selects this adapter only without an explicit modern context. */
|
|
8
|
+
export function usesLegacyProtocol(request, protocolVersion) {
|
|
9
|
+
return (protocolVersion === LEGACY_PROTOCOL_VERSION ||
|
|
10
|
+
(protocolVersion === undefined &&
|
|
11
|
+
!hasProtocolVersionMetadata(request) &&
|
|
12
|
+
request.method === "initialize"));
|
|
13
|
+
}
|
|
14
|
+
export function createLegacyDispatcher(tools, identity, instructions, providers) {
|
|
15
|
+
return async (request, scope) => {
|
|
16
|
+
let result;
|
|
17
|
+
switch (request.method) {
|
|
18
|
+
case "initialize":
|
|
19
|
+
result = initialize(request.params, identity, instructions);
|
|
20
|
+
break;
|
|
21
|
+
case "ping":
|
|
22
|
+
result = ping(request.params);
|
|
23
|
+
break;
|
|
24
|
+
case "tools/list":
|
|
25
|
+
result = listTools(request.params, tools);
|
|
26
|
+
break;
|
|
27
|
+
case "tools/call":
|
|
28
|
+
result = await callTool(request.params, tools, (params) => createToolContext(scope.signal, {
|
|
29
|
+
id: request.id,
|
|
30
|
+
protocolVersion: LEGACY_PROTOCOL_VERSION,
|
|
31
|
+
...(scope.client === undefined ? {} : { client: scope.client }),
|
|
32
|
+
meta: params._meta ?? {},
|
|
33
|
+
}, scope.auth, providers));
|
|
34
|
+
break;
|
|
35
|
+
default:
|
|
36
|
+
throw new ProtocolError(METHOD_NOT_FOUND, "Method not found");
|
|
37
|
+
}
|
|
38
|
+
return result ? successResponse(request.id, result) : undefined;
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=dispatcher.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dispatcher.js","sourceRoot":"","sources":["../../src/legacy/dispatcher.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,uBAAuB,EACvB,gBAAgB,GAEjB,MAAM,qBAAqB,CAAC;AAK7B,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EAAE,0BAA0B,EAAE,MAAM,iCAAiC,CAAC;AAE7E,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AACtE,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEjD,mFAAmF;AACnF,MAAM,UAAU,kBAAkB,CAChC,OAA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EAAE,kBAAkB,CAAC,CAAC;QAClE,CAAC;QACD,OAAO,MAAM,CAAC,CAAC,CAAC,eAAe,CAAC,OAAO,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAClE,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type Implementation } from "@toolgrain/protocol";
|
|
2
|
+
import type { InitializeResult, ListToolsResult } from "@toolgrain/protocol/2025-06-18";
|
|
3
|
+
import type { ToolAdapter } from "../tools/adapter.js";
|
|
4
|
+
import type { ToolContext } from "../tools/context.js";
|
|
5
|
+
/** Initialization can only negotiate the initialization-based revision. */
|
|
6
|
+
export declare function initialize(params: unknown, identity: Implementation, instructions?: string): InitializeResult;
|
|
7
|
+
export declare function ping(params: unknown): Record<string, never>;
|
|
8
|
+
export declare function listTools(params: unknown, tools: ToolAdapter): ListToolsResult;
|
|
9
|
+
export declare function callTool(params: unknown, tools: ToolAdapter, ctx: (params: {
|
|
10
|
+
_meta?: Record<string, unknown> | undefined;
|
|
11
|
+
}) => ToolContext): Promise<import("@toolgrain/protocol/2025-06-18").CallToolResult | undefined>;
|
|
12
|
+
//# sourceMappingURL=handlers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"handlers.d.ts","sourceRoot":"","sources":["../../src/legacy/handlers.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,cAAc,EAGpB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EACV,gBAAgB,EAChB,eAAe,EAChB,MAAM,gCAAgC,CAAC;AAExC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAEvD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AASvD,2EAA2E;AAC3E,wBAAgB,UAAU,CACxB,MAAM,EAAE,OAAO,EACf,QAAQ,EAAE,cAAc,EACxB,YAAY,CAAC,EAAE,MAAM,GACpB,gBAAgB,CAYlB;AAED,wBAAgB,IAAI,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAI3D;AAED,wBAAgB,SAAS,CACvB,MAAM,EAAE,OAAO,EACf,KAAK,EAAE,WAAW,GACjB,eAAe,CAcjB;AAED,wBAAsB,QAAQ,CAC5B,MAAM,EAAE,OAAO,EACf,KAAK,EAAE,WAAW,EAClB,GAAG,EAAE,CAAC,MAAM,EAAE;IAAE,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAA;CAAE,KAAK,WAAW,gFAO9E"}
|