@gjermundgaraba/effect-actions 0.40.0 → 0.41.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ActionMcp.d.mts +1 -0
- package/dist/ActionMcp.mjs +15 -8
- package/dist/ActionToolkit.mjs +18 -7
- package/dist/contract/lift.mjs +25 -5
- package/dist/contract/media.d.mts +3 -2
- package/dist/contract/media.mjs +3 -2
- package/dist/mcp/client.d.mts +1 -1
- package/dist/mcp/client.mjs +3 -3
- package/dist/mcp/protocol.mjs +19 -9
- package/docs/ActionMcp.md +1 -1
- package/package.json +1 -1
package/dist/ActionMcp.d.mts
CHANGED
|
@@ -122,6 +122,7 @@ type Inferring<D, A extends Pick<Any$1, "caller">> = [D] extends [undefined] ? [
|
|
|
122
122
|
*
|
|
123
123
|
* - A success is `isError: false` with `structuredContent` its JSON and the same JSON as one text block, both `null` for an action without `success`; an `Action.Image` field is left out of `structuredContent` and its text, its block following them, and a success of images alone has no `structuredContent`.
|
|
124
124
|
* - A success whose schema encodes only to a string, such as `Schema.String`, a string literal or `Schema.DateTimeUtc`, is its raw text as one text block on every revision, with no `structuredContent` and no `outputSchema`, since Claude Code shows a model serialized `structuredContent` in place of every text block: return file contents or command output as such a string, never as a field of an object, which reaches a model JSON-escaped.
|
|
125
|
+
* - A union success of `Action.Image`, or an array of it, and members that encode only to a string, such as `Schema.Union([Schema.String, Action.Image])`, answers a string as its raw text block alone and media as its image blocks alone, with no `structuredContent` and no `outputSchema`; a union of media and a member that encodes to anything else is refused as the endpoint is made.
|
|
125
126
|
* - A union holding a string, such as `Schema.NullOr(Schema.String)` or an optional string, is sent as any other success, so a 2026-07-28 client gets the string as `structuredContent` and JSON-quoted text, which reaches a Claude Code model escaped: return `""` for nothing, not `null`.
|
|
126
127
|
* - A declared error is an `isError: true` tool result in a 200 whose one text block is its `message`, or the error itself when it is a string, or its JSON when it has no `message` or an empty one, with its JSON in `_meta["io.github.gjermundgaraba.effect-actions/error"]`, from which `client` decodes it, Effect's `McpSchema.ToolErrorInfo` `{ kind: "failure" }` in `_meta["website.effect/toolError"]`, with `tag` its `_tag` where it has a non-empty one, and no `structuredContent`: write each `message` for the model, since its other fields reach a model only through it; a refusal is answered as `Action.Unauthenticated` and `Action.Forbidden` say.
|
|
127
128
|
* - An action answering a stream, `Action.Stream`, answers its elements collected into one array once the stream ends, a success of that array.
|
package/dist/ActionMcp.mjs
CHANGED
|
@@ -25,7 +25,14 @@ const assertToolsServable = (lifted) => {
|
|
|
25
25
|
const inputs = lifted.map(([action]) => action).flatMap(({ input, name }) => isObjectSchema(toolJsonSchema(Schema.toCodecJson(input), true)) ? [] : [name]);
|
|
26
26
|
if (inputs.length > 0) throw new Error(`MCP tool input must be one object with keys, such as a struct: ${inputs.join(", ")} (wrap a union, an array or a scalar in a field, omit input for no arguments, or leave the action out of actions)`);
|
|
27
27
|
const misplaced = lifted.flatMap(([action, lift]) => isMisplaced(action, lift) ? [action.name] : []);
|
|
28
|
-
if (misplaced.length > 0) throw new Error(`MCP media must be the success or an array of it, or a top-level field of a struct success, one, optional or a required array: ${misplaced.join(", ")} (make the image such a field, or give an optional array as a required one, empty when absent; an image in input or an error is not served over MCP, so leave that action out of actions)`);
|
|
28
|
+
if (misplaced.length > 0) throw new Error(`MCP media must be the success or an array of it, a member of a union success whose other members encode to a string, or a top-level field of a struct success, one, optional or a required array: ${misplaced.join(", ")} (make the image such a field, or give an optional array as a required one, empty when absent; an image in input or an error is not served over MCP, so leave that action out of actions)`);
|
|
29
|
+
};
|
|
30
|
+
const structuredOf = (lift) => {
|
|
31
|
+
switch (lift._tag) {
|
|
32
|
+
case "Plain": return lift.success;
|
|
33
|
+
case "Fields": return lift.rest;
|
|
34
|
+
case "Either": return;
|
|
35
|
+
}
|
|
29
36
|
};
|
|
30
37
|
const internalErrorResult = new McpSchema.CallToolResult({
|
|
31
38
|
isError: true,
|
|
@@ -89,9 +96,9 @@ const registerTools = Effect.fnUntraced(function* (apps, authentication) {
|
|
|
89
96
|
const toolSuccess = toolSuccessOf(action.success);
|
|
90
97
|
const run = answeringWhole(action, handler);
|
|
91
98
|
const lift = liftOf(toolSuccess);
|
|
92
|
-
const
|
|
93
|
-
const
|
|
94
|
-
const
|
|
99
|
+
const structuredSuccess = structuredOf(lift);
|
|
100
|
+
const text = lift._tag === "Plain" && isText(toolSuccess);
|
|
101
|
+
const isOtherwise = lift._tag === "Either" ? Schema.is(lift.otherwise) : () => false;
|
|
95
102
|
const structured = structuredSuccess === void 0 || text ? void 0 : Schema.toCodecJson(structuredSuccess);
|
|
96
103
|
const outputSchema = structured === void 0 ? void 0 : yield* Schema.decodeUnknownEffect(McpSchema.ToolOutputJson)(toolJsonSchema(structured, false)).pipe(Effect.orDie);
|
|
97
104
|
const tool = yield* toolOf(action, outputSchema).pipe(Effect.orDie);
|
|
@@ -100,21 +107,21 @@ const registerTools = Effect.fnUntraced(function* (apps, authentication) {
|
|
|
100
107
|
const encodeFailure = encoder(Schema.Union(projectedErrors(action)));
|
|
101
108
|
const reported = (input) => action.progress === void 0 ? run(input) : Effect.flatMap(progressNotifier(registry.notifications, action.progress), (sink) => run(input, sink));
|
|
102
109
|
const call = (input) => asCaller(authentication, action, reported(input));
|
|
103
|
-
const liftedFieldNames = new Set(
|
|
110
|
+
const liftedFieldNames = new Set(lift._tag === "Plain" ? [] : lift.fields.map(({ name }) => name));
|
|
104
111
|
const resultOf = (encoded, success) => {
|
|
105
|
-
if (text) return new McpSchema.CallToolResult({
|
|
112
|
+
if ((text || isOtherwise(success)) && Predicate.isString(encoded)) return new McpSchema.CallToolResult({
|
|
106
113
|
isError: false,
|
|
107
114
|
content: [{
|
|
108
115
|
type: "text",
|
|
109
116
|
text: encoded
|
|
110
117
|
}]
|
|
111
118
|
});
|
|
112
|
-
if (
|
|
119
|
+
if (lift._tag === "Plain") return new McpSchema.CallToolResult({
|
|
113
120
|
isError: false,
|
|
114
121
|
structuredContent: encoded,
|
|
115
122
|
content: nativeJsonTextOf(encoded)
|
|
116
123
|
});
|
|
117
|
-
const blocks = liftedFrom(
|
|
124
|
+
const blocks = liftedFrom(lift.fields, success).map(contentBlockOf);
|
|
118
125
|
if (structured === void 0) return new McpSchema.CallToolResult({
|
|
119
126
|
isError: false,
|
|
120
127
|
content: blocks
|
package/dist/ActionToolkit.mjs
CHANGED
|
@@ -6,7 +6,7 @@ import { assertOnce, heldOnce } from "./contract/names.mjs";
|
|
|
6
6
|
import { progressSink } from "./contract/progress.mjs";
|
|
7
7
|
import { provideServed } from "./contract/serving.mjs";
|
|
8
8
|
import { toolSuccessOf } from "./contract/stream.mjs";
|
|
9
|
-
import { Effect, Schema, SchemaGetter } from "effect";
|
|
9
|
+
import { Effect, Predicate, Schema, SchemaGetter } from "effect";
|
|
10
10
|
import { Prompt, Tool, Toolkit } from "effect/ai";
|
|
11
11
|
// src/toolkit/ActionToolkit.ts
|
|
12
12
|
const annotateIfGiven = (tool, key, value) => value === void 0 ? tool : tool.annotate(key, value);
|
|
@@ -30,14 +30,25 @@ const ProgressOf = (progress) => Schema.TaggedStruct("Progress", { value: progre
|
|
|
30
30
|
value: reported.value
|
|
31
31
|
}))
|
|
32
32
|
}));
|
|
33
|
+
const jsonTextPartOf = (encoded) => Prompt.textPart({ text: Predicate.isString(encoded) ? encoded : JSON.stringify(encoded) });
|
|
34
|
+
const filePartsOf = (lifted) => lifted.map(({ value }) => Prompt.filePart({
|
|
35
|
+
mediaType: value.mimeType,
|
|
36
|
+
data: value.data
|
|
37
|
+
}));
|
|
33
38
|
const modelOutputOf = (success) => {
|
|
34
39
|
const lift = liftOf(success);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
40
|
+
switch (lift._tag) {
|
|
41
|
+
case "Plain": return;
|
|
42
|
+
case "Either": {
|
|
43
|
+
const isOtherwise = Schema.is(lift.otherwise);
|
|
44
|
+
const encodeOtherwise = Schema.encodeUnknownSync(Schema.toCodecJson(lift.otherwise));
|
|
45
|
+
return (result) => isOtherwise(result) ? [jsonTextPartOf(encodeOtherwise(result))] : filePartsOf(liftedFrom(lift.fields, result));
|
|
46
|
+
}
|
|
47
|
+
case "Fields": {
|
|
48
|
+
const encodeRest = lift.rest === void 0 ? void 0 : Schema.encodeUnknownSync(Schema.toCodecJson(lift.rest));
|
|
49
|
+
return (result) => [...encodeRest === void 0 ? [] : [Prompt.textPart({ text: JSON.stringify(encodeRest(result)) })], ...filePartsOf(liftedFrom(lift.fields, result))];
|
|
50
|
+
}
|
|
51
|
+
}
|
|
41
52
|
};
|
|
42
53
|
const jsonToolOf = (action) => {
|
|
43
54
|
const success = toolSuccessOf(action.success);
|
package/dist/contract/lift.mjs
CHANGED
|
@@ -37,15 +37,34 @@ const liftOf = (success) => {
|
|
|
37
37
|
const ast = unsuspended(success.ast);
|
|
38
38
|
const whole = wholeSuccessMediaOf(ast);
|
|
39
39
|
if (whole !== void 0) return {
|
|
40
|
+
_tag: "Fields",
|
|
40
41
|
fields: [{
|
|
41
42
|
name: void 0,
|
|
42
43
|
...whole
|
|
43
44
|
}],
|
|
44
45
|
rest: void 0
|
|
45
46
|
};
|
|
47
|
+
if (SchemaAST.isUnion(ast) && ast.encoding === void 0) {
|
|
48
|
+
const members = ast.types.map((member) => [member, wholeSuccessMediaOf(member)]);
|
|
49
|
+
const media = members.flatMap(([, lifted]) => lifted === void 0 ? [] : [{
|
|
50
|
+
name: void 0,
|
|
51
|
+
...lifted
|
|
52
|
+
}]);
|
|
53
|
+
const others = members.flatMap(([member, lifted]) => lifted === void 0 ? [Schema.make(member)] : []);
|
|
54
|
+
const [only, ...more] = others;
|
|
55
|
+
if (media.length > 0) return only === void 0 ? {
|
|
56
|
+
_tag: "Fields",
|
|
57
|
+
fields: media,
|
|
58
|
+
rest: void 0
|
|
59
|
+
} : {
|
|
60
|
+
_tag: "Either",
|
|
61
|
+
fields: media,
|
|
62
|
+
otherwise: more.length === 0 ? only : Schema.Union(others)
|
|
63
|
+
};
|
|
64
|
+
}
|
|
46
65
|
if (!SchemaAST.isObjects(ast) || ast.encoding !== void 0) return {
|
|
47
|
-
|
|
48
|
-
|
|
66
|
+
_tag: "Plain",
|
|
67
|
+
success
|
|
49
68
|
};
|
|
50
69
|
const fields = ast.propertySignatures.flatMap(({ name, type }) => {
|
|
51
70
|
const field = mediaFieldOf(type);
|
|
@@ -55,18 +74,19 @@ const liftOf = (success) => {
|
|
|
55
74
|
}];
|
|
56
75
|
});
|
|
57
76
|
if (fields.length === 0) return {
|
|
58
|
-
|
|
59
|
-
|
|
77
|
+
_tag: "Plain",
|
|
78
|
+
success
|
|
60
79
|
};
|
|
61
80
|
const names = new Set(fields.map(({ name }) => name));
|
|
62
81
|
const rest = ast.propertySignatures.filter(({ name }) => !names.has(name));
|
|
63
82
|
const { identifier: _identifierOfWholeSuccess, ...annotationsOfRest } = ast.annotations ?? {};
|
|
64
83
|
return {
|
|
84
|
+
_tag: "Fields",
|
|
65
85
|
fields,
|
|
66
86
|
rest: rest.length === 0 && ast.indexSignatures.length === 0 ? void 0 : Schema.make(new SchemaAST.Objects(rest, ast.indexSignatures, annotationsOfRest))
|
|
67
87
|
};
|
|
68
88
|
};
|
|
69
|
-
const liftedFrom = (
|
|
89
|
+
const liftedFrom = (fields, success) => fields.flatMap(({ name, kind, many }) => {
|
|
70
90
|
const field = name !== void 0 && Predicate.hasProperty(success, name) ? success[name] : void 0;
|
|
71
91
|
const value = name === void 0 ? success : field;
|
|
72
92
|
const listed = isMediaArray(value) ? value : [];
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Schema, SchemaAST } from "effect";
|
|
2
2
|
/**
|
|
3
3
|
* An image, a media field of a success: `{ data: Uint8Array, mimeType: string }`, `mimeType` such
|
|
4
|
-
* as `image/png`. A top-level field of a struct success, one, optional, or a required array,
|
|
5
|
-
* whole success, one or an array
|
|
4
|
+
* as `image/png`. A top-level field of a struct success, one, optional, or a required array, the
|
|
5
|
+
* whole success, one or an array, or a member of a union success whose other members encode to a
|
|
6
|
+
* string, such as a tool answering a file's text or its image: an MCP tool lifts it into an image block of its own, a toolkit tool
|
|
6
7
|
* into a file part of what its model reads, and every other surface sends it as JSON, the bytes in
|
|
7
8
|
* base64. `ActionMcp` refuses it anywhere else, and a toolkit sends it there in the JSON.
|
|
8
9
|
*
|
package/dist/contract/media.mjs
CHANGED
|
@@ -7,8 +7,9 @@ const Media = Schema.Struct({
|
|
|
7
7
|
});
|
|
8
8
|
/**
|
|
9
9
|
* An image, a media field of a success: `{ data: Uint8Array, mimeType: string }`, `mimeType` such
|
|
10
|
-
* as `image/png`. A top-level field of a struct success, one, optional, or a required array,
|
|
11
|
-
* whole success, one or an array
|
|
10
|
+
* as `image/png`. A top-level field of a struct success, one, optional, or a required array, the
|
|
11
|
+
* whole success, one or an array, or a member of a union success whose other members encode to a
|
|
12
|
+
* string, such as a tool answering a file's text or its image: an MCP tool lifts it into an image block of its own, a toolkit tool
|
|
12
13
|
* into a file part of what its model reads, and every other surface sends it as JSON, the bytes in
|
|
13
14
|
* base64. `ActionMcp` refuses it anywhere else, and a toolkit sends it there in the JSON.
|
|
14
15
|
*
|
package/dist/mcp/client.d.mts
CHANGED
|
@@ -52,7 +52,7 @@ type Watching<Listed extends Any$2, D, E, R> = [Listed] extends [never] ? unknow
|
|
|
52
52
|
* encoded with the action's schema, and the success decoded from its structured content, each
|
|
53
53
|
* media field from the blocks whose `_meta` names it, a success that is media, or whose
|
|
54
54
|
* every field is, from its blocks alone, and a success whose schema encodes only to a string
|
|
55
|
-
* from its text block. A declared error the tool returns, the action's own or a refusal, is its
|
|
55
|
+
* from its text block; a union of the two from its image blocks when it has any, else its text. A declared error the tool returns, the action's own or a refusal, is its
|
|
56
56
|
* decoded value, read from the result's `_meta`, and so is a refusal the endpoint's authentication answers
|
|
57
57
|
* with: a test telling a 401 or 403 from a tool result asserts the status of a request `request`
|
|
58
58
|
* builds. The argument may be omitted when `{}` is a valid input. Where `contracts` lists event
|
package/dist/mcp/client.mjs
CHANGED
|
@@ -48,12 +48,12 @@ const callTool = (client, url, action, success, refusedWith, input, sink) => {
|
|
|
48
48
|
const declared = result._meta?.[errorKey];
|
|
49
49
|
return yield* declared === void 0 ? Effect.fail(undeclared) : failWith(projectedErrors(action), JSON.stringify(declared), undeclared);
|
|
50
50
|
}
|
|
51
|
-
if (lift.
|
|
51
|
+
if (lift._tag === "Plain" ? isText(success) : lift._tag === "Either" && result.content.every((block) => block.type === "text")) {
|
|
52
52
|
if (firstText === void 0 || result.structuredContent !== void 0) return yield* callError(method, name, new UnexpectedContent({ body: text }));
|
|
53
53
|
return yield* Schema.decodeEffect(Schema.toCodecJson(success))(firstText);
|
|
54
54
|
}
|
|
55
|
-
if (
|
|
56
|
-
return yield* Schema.decodeUnknownEffect(Schema.toCodecJson(success))(unliftedFrom(lift, result));
|
|
55
|
+
if ((lift._tag === "Plain" || lift._tag === "Fields" && lift.rest !== void 0) && result.structuredContent === void 0) return yield* callError(method, name, new UnexpectedContent({ body: text }));
|
|
56
|
+
return yield* Schema.decodeUnknownEffect(Schema.toCodecJson(success))(unliftedFrom(lift._tag === "Plain" ? [] : lift.fields, result));
|
|
57
57
|
});
|
|
58
58
|
};
|
|
59
59
|
function client(contracts, { url = defaultPath, transformClient, authentication } = {}) {
|
package/dist/mcp/protocol.mjs
CHANGED
|
@@ -68,11 +68,21 @@ const holdsMediaAnywhere = (ast) => {
|
|
|
68
68
|
}
|
|
69
69
|
return false;
|
|
70
70
|
};
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
71
|
+
const servedBeside = (lift) => {
|
|
72
|
+
switch (lift._tag) {
|
|
73
|
+
case "Plain": return lift.success;
|
|
74
|
+
case "Fields": return lift.rest;
|
|
75
|
+
case "Either": return lift.otherwise;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
const isMisplaced = (action, lift) => {
|
|
79
|
+
const served = servedBeside(lift);
|
|
80
|
+
return [
|
|
81
|
+
action.input,
|
|
82
|
+
...action.error,
|
|
83
|
+
...served === void 0 ? [] : [served]
|
|
84
|
+
].some(({ ast }) => holdsMediaAnywhere(ast)) || lift._tag === "Either" && !isText(lift.otherwise);
|
|
85
|
+
};
|
|
76
86
|
const mediaOf = ({ name, kind, many }, result) => {
|
|
77
87
|
const values = result.content.flatMap((block) => block.type === kind && block._meta?.["io.github.gjermundgaraba.effect-actions/field"] === name ? [{
|
|
78
88
|
data: Base64.encode(block.data),
|
|
@@ -80,13 +90,13 @@ const mediaOf = ({ name, kind, many }, result) => {
|
|
|
80
90
|
}] : []);
|
|
81
91
|
return many || values.length > 1 ? values : values[0];
|
|
82
92
|
};
|
|
83
|
-
const unliftedFrom = (
|
|
84
|
-
const [whole] =
|
|
93
|
+
const unliftedFrom = (fields, result) => {
|
|
94
|
+
const [whole] = fields;
|
|
85
95
|
if (whole !== void 0 && whole.name === void 0) return mediaOf(whole, result);
|
|
86
96
|
const { structuredContent = {} } = result;
|
|
87
97
|
if (!Predicate.isObject(structuredContent)) return structuredContent;
|
|
88
|
-
const lifted = new Set(
|
|
89
|
-
return Object.fromEntries([...Object.entries(structuredContent).filter(([key]) => !lifted.has(key)), ...
|
|
98
|
+
const lifted = new Set(fields.map(({ name }) => name));
|
|
99
|
+
return Object.fromEntries([...Object.entries(structuredContent).filter(([key]) => !lifted.has(key)), ...fields.flatMap((field) => {
|
|
90
100
|
const value = mediaOf(field, result);
|
|
91
101
|
return field.name === void 0 || value === void 0 ? [] : [[field.name, value]];
|
|
92
102
|
})]);
|
package/docs/ActionMcp.md
CHANGED
|
@@ -72,7 +72,7 @@ A tool is its contract ([Action.md](Action.md#define-a-contract)): a model knows
|
|
|
72
72
|
|
|
73
73
|
## Return images and other media
|
|
74
74
|
|
|
75
|
-
Give the success an `Action.Image` field and return its bytes and MIME type from the handler: `get_camera_snapshot` in [mcp-tools/src/printers/Printers.ts](../examples/mcp-tools/src/printers/Printers.ts); `ActionMcp.client` decodes it back. Where `ActionMcp` refuses one is on `Action.Image`: [kitchen-sink/src/Actions.ts](../examples/kitchen-sink/src/Actions.ts).
|
|
75
|
+
Give the success an `Action.Image` field and return its bytes and MIME type from the handler: `get_camera_snapshot` in [mcp-tools/src/printers/Printers.ts](../examples/mcp-tools/src/printers/Printers.ts); `ActionMcp.client` decodes it back. A tool that answers text or an image, such as one reading a file that may be a picture, takes `Schema.Union([Schema.String, Action.Image])` as its success and returns either. Where `ActionMcp` refuses one is on `Action.Image`: [kitchen-sink/src/Actions.ts](../examples/kitchen-sink/src/Actions.ts).
|
|
76
76
|
|
|
77
77
|
## Protect tools
|
|
78
78
|
|
package/package.json
CHANGED