@alveus-ai/std 0.1.57 → 0.1.59

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/index.d.ts CHANGED
@@ -22,4 +22,5 @@ export * from './memory/index.js';
22
22
  export * from './patterns/index.js';
23
23
  export * from './composition/index.js';
24
24
  export * from './utils/index.js';
25
+ export * from './mcp.js';
25
26
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAGH,cAAc,mBAAmB,CAAC;AAGlC,cAAc,mBAAmB,CAAC;AAGlC,cAAc,qBAAqB,CAAC;AAGpC,cAAc,wBAAwB,CAAC;AAGvC,cAAc,kBAAkB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAGH,cAAc,mBAAmB,CAAC;AAGlC,cAAc,mBAAmB,CAAC;AAGlC,cAAc,qBAAqB,CAAC;AAGpC,cAAc,wBAAwB,CAAC;AAGvC,cAAc,kBAAkB,CAAC;AAGjC,cAAc,UAAU,CAAC"}
package/dist/index.js CHANGED
@@ -27,4 +27,6 @@ export * from './patterns/index.js';
27
27
  export * from './composition/index.js';
28
28
  // Re-export all utilities
29
29
  export * from './utils/index.js';
30
+ // MCP tools — call tools on connected external MCP servers
31
+ export * from './mcp.js';
30
32
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,uBAAuB;AACvB,cAAc,mBAAmB,CAAC;AAElC,iCAAiC;AACjC,cAAc,mBAAmB,CAAC;AAElC,yBAAyB;AACzB,cAAc,qBAAqB,CAAC;AAEpC,sCAAsC;AACtC,cAAc,wBAAwB,CAAC;AAEvC,0BAA0B;AAC1B,cAAc,kBAAkB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,uBAAuB;AACvB,cAAc,mBAAmB,CAAC;AAElC,iCAAiC;AACjC,cAAc,mBAAmB,CAAC;AAElC,yBAAyB;AACzB,cAAc,qBAAqB,CAAC;AAEpC,sCAAsC;AACtC,cAAc,wBAAwB,CAAC;AAEvC,0BAA0B;AAC1B,cAAc,kBAAkB,CAAC;AAEjC,2DAA2D;AAC3D,cAAc,UAAU,CAAC"}
package/dist/mcp.d.ts ADDED
@@ -0,0 +1,53 @@
1
+ /**
2
+ * MCP tools — call tools exposed by connected external MCP servers from an agent.
3
+ */
4
+ import type { ExecutionContext, MCPServerRef, MCPToolResult } from '@alveus-ai/core';
5
+ /**
6
+ * Specification of a tool on an MCP server. Provide exactly one of `connection`
7
+ * (a server configured on your account, the usual case) or `server` (an inline
8
+ * reference for a server that needs no authorization or uses a static token).
9
+ */
10
+ export interface MCPToolSpec {
11
+ /** Name of a connection configured on your account. */
12
+ connection?: string;
13
+ /** An inline server reference (no-auth or static token). */
14
+ server?: MCPServerRef;
15
+ /** The tool name to invoke. */
16
+ tool: string;
17
+ /** The tool's input schema, recorded as call evidence when provided. */
18
+ inputSchema?: Record<string, unknown>;
19
+ }
20
+ /** A reusable handle to a tool on an MCP server. */
21
+ export interface MCPToolHandle {
22
+ (ctx: ExecutionContext, args?: Record<string, unknown>): Promise<MCPToolResult>;
23
+ /** The connection name this handle calls, if configured by name. */
24
+ readonly connection?: string;
25
+ /** The inline server this handle calls, if configured inline. */
26
+ readonly server?: MCPServerRef;
27
+ /** The tool this handle invokes. */
28
+ readonly tool: string;
29
+ }
30
+ /**
31
+ * Define a reusable handle to a tool on an MCP server.
32
+ *
33
+ * Call the returned handle inside an agent with the execution context and the
34
+ * tool's arguments. The call runs through the durable runtime and returns the
35
+ * tool result with reconstructable evidence of exactly what was invoked.
36
+ *
37
+ * @example
38
+ * ```ts
39
+ * // A server connected on your account (authorization handled for you):
40
+ * const search = mcpTool({ connection: 'my-search', tool: 'search' });
41
+ *
42
+ * // Or an inline server that needs no authorization:
43
+ * const ping = mcpTool({ server: { url: 'https://example.com/mcp' }, tool: 'ping' });
44
+ *
45
+ * // inside an agent handler:
46
+ * const result = await search(ctx, { query: 'alveus' });
47
+ * if (!result.isError) {
48
+ * // use result.content / result.structuredContent
49
+ * }
50
+ * ```
51
+ */
52
+ export declare function mcpTool(spec: MCPToolSpec): MCPToolHandle;
53
+ //# sourceMappingURL=mcp.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mcp.d.ts","sourceRoot":"","sources":["../src/mcp.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,KAAK,EAAE,gBAAgB,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAErF;;;;GAIG;AACH,MAAM,WAAW,WAAW;IAC1B,uDAAuD;IACvD,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,4DAA4D;IAC5D,MAAM,CAAC,EAAE,YAAY,CAAC;IACtB,+BAA+B;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,wEAAwE;IACxE,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACvC;AAED,oDAAoD;AACpD,MAAM,WAAW,aAAa;IAC5B,CAAC,GAAG,EAAE,gBAAgB,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;IAChF,oEAAoE;IACpE,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B,iEAAiE;IACjE,QAAQ,CAAC,MAAM,CAAC,EAAE,YAAY,CAAC;IAC/B,oCAAoC;IACpC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,WAAW,GAAG,aAAa,CAuBxD"}
package/dist/mcp.js ADDED
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Define a reusable handle to a tool on an MCP server.
3
+ *
4
+ * Call the returned handle inside an agent with the execution context and the
5
+ * tool's arguments. The call runs through the durable runtime and returns the
6
+ * tool result with reconstructable evidence of exactly what was invoked.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * // A server connected on your account (authorization handled for you):
11
+ * const search = mcpTool({ connection: 'my-search', tool: 'search' });
12
+ *
13
+ * // Or an inline server that needs no authorization:
14
+ * const ping = mcpTool({ server: { url: 'https://example.com/mcp' }, tool: 'ping' });
15
+ *
16
+ * // inside an agent handler:
17
+ * const result = await search(ctx, { query: 'alveus' });
18
+ * if (!result.isError) {
19
+ * // use result.content / result.structuredContent
20
+ * }
21
+ * ```
22
+ */
23
+ export function mcpTool(spec) {
24
+ if ((spec.connection && spec.server) || (!spec.connection && !spec.server)) {
25
+ throw new Error('mcpTool requires exactly one of `connection` or `server`.');
26
+ }
27
+ const handle = (async (ctx, args) => {
28
+ if (!ctx.mcp) {
29
+ throw new Error('MCP tools require the durable runtime; ctx.mcp is not available in this environment.');
30
+ }
31
+ return ctx.mcp.callTool({
32
+ connection: spec.connection,
33
+ server: spec.server,
34
+ tool: spec.tool,
35
+ arguments: args ?? {},
36
+ inputSchema: spec.inputSchema,
37
+ });
38
+ });
39
+ return Object.assign(handle, {
40
+ connection: spec.connection,
41
+ server: spec.server,
42
+ tool: spec.tool,
43
+ });
44
+ }
45
+ //# sourceMappingURL=mcp.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mcp.js","sourceRoot":"","sources":["../src/mcp.ts"],"names":[],"mappings":"AAgCA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,OAAO,CAAC,IAAiB;IACvC,IAAI,CAAC,IAAI,CAAC,UAAU,IAAI,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,UAAU,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;QAC3E,MAAM,IAAI,KAAK,CAAC,2DAA2D,CAAC,CAAC;IAC/E,CAAC;IACD,MAAM,MAAM,GAAG,CAAC,KAAK,EAAE,GAAqB,EAAE,IAA8B,EAAE,EAAE;QAC9E,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC;YACb,MAAM,IAAI,KAAK,CACb,sFAAsF,CACvF,CAAC;QACJ,CAAC;QACD,OAAO,GAAG,CAAC,GAAG,CAAC,QAAQ,CAAC;YACtB,UAAU,EAAE,IAAI,CAAC,UAAU;YAC3B,MAAM,EAAE,IAAI,CAAC,MAAM;YACnB,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,SAAS,EAAE,IAAI,IAAI,EAAE;YACrB,WAAW,EAAE,IAAI,CAAC,WAAW;SAC9B,CAAC,CAAC;IACL,CAAC,CAAkB,CAAC;IACpB,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE;QAC3B,UAAU,EAAE,IAAI,CAAC,UAAU;QAC3B,MAAM,EAAE,IAAI,CAAC,MAAM;QACnB,IAAI,EAAE,IAAI,CAAC,IAAI;KAChB,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,161 @@
1
+ /**
2
+ * Action Gate Demo: a verified refund
3
+ *
4
+ * A support agent proposes a refund. Before anything runs, the proposal is
5
+ * frozen and checked against a refund policy by a pinned scorer. The refund
6
+ * executes ONLY when every check passes, with the exact payload that was
7
+ * checked, and a postcondition check then confirms the refund took effect.
8
+ * An over-limit refund is revised once (lowered to the limit) and re-checked.
9
+ *
10
+ * Policy:
11
+ * - refunds up to $500
12
+ * - the transaction is within the 30-day refund window
13
+ * - a reason is given
14
+ * - refunds over $200 need an approval on record
15
+ *
16
+ * Every terminal run yields an Action Receipt written by the platform, bound to
17
+ * hashes of the exact code and payload, which the agent can't forge.
18
+ *
19
+ * Usage:
20
+ * alveus deploy
21
+ * alveus run @yourscope/action-gate-demo --input '{"amount": 120, "daysSincePurchase": 5, "reason": "Duplicate charge"}'
22
+ * alveus run @yourscope/action-gate-demo --input '{"amount": 800, "daysSincePurchase": 10, "reason": "Item arrived damaged", "approvalRecorded": true}'
23
+ * alveus run @yourscope/action-gate-demo --input '{"amount": 90, "daysSincePurchase": 45, "reason": "Changed my mind"}'
24
+ * alveus receipt <execution-id>
25
+ */
26
+
27
+ import { agent$ } from '@alveus-ai/core';
28
+ import type { ExecutionContext, ScorerInput, ScorerOutput } from '@alveus-ai/core';
29
+ import { actionGate } from '@alveus-ai/std';
30
+
31
+ const REFUND_LIMIT = 500;
32
+ const REFUND_WINDOW_DAYS = 30;
33
+ const APPROVAL_OVER = 200;
34
+
35
+ interface RefundInput {
36
+ amount: number;
37
+ daysSincePurchase: number;
38
+ reason?: string;
39
+ /** A recorded approval flag the gate checks. Not a live approval step. */
40
+ approvalRecorded?: boolean;
41
+ }
42
+
43
+ /**
44
+ * Scorer: deterministic policy checks over the frozen proposal. It only
45
+ * measures; it never decides whether the refund runs.
46
+ */
47
+ const policyScorer = agent$(async (_state, input: ScorerInput, _ctx: ExecutionContext) => {
48
+ const proposal = input.subject as { input?: RefundInput };
49
+ const refund = proposal?.input ?? ({} as RefundInput);
50
+ const amount = refund.amount;
51
+
52
+ const check = (id: string): { verdict: boolean; reasoning: string } => {
53
+ if (id === 'within_limit') {
54
+ const ok = typeof amount === 'number' && amount > 0 && amount <= REFUND_LIMIT;
55
+ return {
56
+ verdict: ok,
57
+ reasoning: ok
58
+ ? `$${amount} is within the $${REFUND_LIMIT} limit`
59
+ : `$${amount} is over the $${REFUND_LIMIT} limit`,
60
+ };
61
+ }
62
+ if (id === 'transaction_eligible') {
63
+ const days = refund.daysSincePurchase;
64
+ const ok = typeof days === 'number' && days >= 0 && days <= REFUND_WINDOW_DAYS;
65
+ return {
66
+ verdict: ok,
67
+ reasoning: ok
68
+ ? `Purchased ${days} days ago, inside the ${REFUND_WINDOW_DAYS}-day window`
69
+ : `Purchased ${days} days ago, outside the ${REFUND_WINDOW_DAYS}-day window`,
70
+ };
71
+ }
72
+ if (id === 'reason_given') {
73
+ const ok = typeof refund.reason === 'string' && refund.reason.trim().length > 0;
74
+ return { verdict: ok, reasoning: ok ? 'A reason is on the proposal' : 'No reason was given' };
75
+ }
76
+ if (id === 'approval_recorded') {
77
+ const needed = typeof amount === 'number' && amount > APPROVAL_OVER;
78
+ const ok = !needed || refund.approvalRecorded === true;
79
+ return {
80
+ verdict: ok,
81
+ reasoning: !needed
82
+ ? `No approval needed at $${APPROVAL_OVER} or less`
83
+ : ok
84
+ ? 'An approval is on record'
85
+ : `Over $${APPROVAL_OVER} needs an approval on record`,
86
+ };
87
+ }
88
+ return { verdict: false, reasoning: 'Unknown check' };
89
+ };
90
+
91
+ const output: ScorerOutput = {
92
+ results: input.criteria.map((c) => ({
93
+ criterionId: c.criterionId,
94
+ ...check(c.criterionId),
95
+ method: 'deterministic',
96
+ })),
97
+ };
98
+ return output;
99
+ });
100
+
101
+ /**
102
+ * Execute step: issues the refund. It runs ONLY when every check passed, with
103
+ * the exact frozen payload that was checked. In production this would call a
104
+ * payments API via `ctx.fetch`; keep it idempotent so a retry never refunds twice.
105
+ */
106
+ const issueRefund = agent$(
107
+ async (_state, payload: { action: string; input: RefundInput }, _ctx: ExecutionContext) => {
108
+ return { refunded: true, amount: payload.input.amount, currency: 'USD' };
109
+ },
110
+ );
111
+
112
+ /**
113
+ * Revise step: lowers an over-limit refund to the limit so it can be
114
+ * re-checked. It cannot change the purchase date, add a reason or record an
115
+ * approval, so those proposals are re-checked unchanged and denied.
116
+ */
117
+ const lowerToLimit = agent$(
118
+ async (_state, event: { action: string; input: RefundInput; feedback: string[] }) => {
119
+ return { input: { ...event.input, amount: Math.min(event.input.amount, REFUND_LIMIT) } };
120
+ },
121
+ );
122
+
123
+ /** Postcondition: confirms the refund was recorded for the amount that was approved. */
124
+ const refundRecorded = agent$(async (_state, input: ScorerInput, _ctx: ExecutionContext) => {
125
+ const subject = input.subject as {
126
+ input?: RefundInput;
127
+ result?: { refunded?: boolean; amount?: number };
128
+ };
129
+ const ok = subject?.result?.refunded === true && subject.result.amount === subject.input?.amount;
130
+ const output: ScorerOutput = {
131
+ results: input.criteria.map((c) => ({
132
+ criterionId: c.criterionId,
133
+ verdict: ok,
134
+ reasoning: ok
135
+ ? `A refund of $${subject?.result?.amount} is on record`
136
+ : 'No matching refund is on record',
137
+ method: 'deterministic',
138
+ })),
139
+ };
140
+ return output;
141
+ });
142
+
143
+ const gate = actionGate({
144
+ criteria: [
145
+ { id: 'within_limit' },
146
+ { id: 'transaction_eligible' },
147
+ { id: 'reason_given' },
148
+ { id: 'approval_recorded' },
149
+ ],
150
+ scorer: policyScorer,
151
+ execute: issueRefund,
152
+ revise: lowerToLimit,
153
+ maxRevisions: 1,
154
+ verifyCriteria: [{ id: 'refund_recorded' }],
155
+ verify: refundRecorded,
156
+ });
157
+
158
+ /** Deployable root: proposes the refund to the gate, labelled by its amount. */
159
+ export const verifiedRefund = agent$((_state, input: RefundInput, ctx: ExecutionContext) =>
160
+ ctx.call(gate, { action: 'issue-refund', input }, { label: `refund $${input?.amount}` }),
161
+ );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alveus-ai/std",
3
- "version": "0.1.57",
3
+ "version": "0.1.59",
4
4
  "description": "Standard library of reusable agents and utilities for Alveus",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",