@crediolabs/policy-builder-mcp 0.2.0 → 0.3.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/README.md +3 -3
- package/dist/src/schemas.d.ts +45 -346
- package/dist/src/schemas.js +21 -38
- package/dist/src/server.js +5 -6
- package/dist/src/transports/http.js +32 -34
- package/dist-cjs/package.json +3 -0
- package/dist-cjs/src/index.d.ts +7 -0
- package/dist-cjs/src/index.js +20 -0
- package/dist-cjs/src/schemas.d.ts +871 -0
- package/dist-cjs/src/schemas.js +121 -0
- package/dist-cjs/src/server.d.ts +6 -0
- package/dist-cjs/src/server.js +42 -0
- package/dist-cjs/src/tools/result.d.ts +24 -0
- package/dist-cjs/src/tools/result.js +38 -0
- package/dist-cjs/src/transports/http.d.ts +21 -0
- package/dist-cjs/src/transports/http.js +181 -0
- package/dist-cjs/src/transports/stdio.d.ts +1 -0
- package/dist-cjs/src/transports/stdio.js +18 -0
- package/package.json +11 -7
- package/src/schemas.ts +19 -50
- package/src/server.ts +5 -14
- package/src/transports/http.ts +32 -35
package/src/schemas.ts
CHANGED
|
@@ -26,18 +26,13 @@ import {
|
|
|
26
26
|
GetInterpreterInfoInputSchema,
|
|
27
27
|
InstallPolicyInputSchema,
|
|
28
28
|
InterpreterOptionsSchema,
|
|
29
|
-
MandateSpecSchema,
|
|
30
29
|
NetworkSchema,
|
|
31
|
-
OraclePriceFixtureSchema,
|
|
32
|
-
OzAdapterConfigSchema,
|
|
33
30
|
PredicateNodeSchema,
|
|
34
31
|
RecordedTransactionSchema,
|
|
35
32
|
RecordTransactionInputSchema,
|
|
36
33
|
RevokePolicyInputSchema,
|
|
37
|
-
SimulatePolicyInputSchema,
|
|
38
34
|
SynthesizePolicyInputSchema,
|
|
39
35
|
ToolErrorSchema,
|
|
40
|
-
VerifyPolicyInputSchema,
|
|
41
36
|
} from '@crediolabs/policy-synth/run'
|
|
42
37
|
import { z } from 'zod'
|
|
43
38
|
|
|
@@ -51,18 +46,13 @@ export {
|
|
|
51
46
|
GetInterpreterInfoInputSchema,
|
|
52
47
|
InstallPolicyInputSchema,
|
|
53
48
|
InterpreterOptionsSchema,
|
|
54
|
-
MandateSpecSchema,
|
|
55
49
|
NetworkSchema,
|
|
56
|
-
OraclePriceFixtureSchema,
|
|
57
|
-
OzAdapterConfigSchema,
|
|
58
50
|
PredicateNodeSchema,
|
|
59
51
|
RecordedTransactionSchema,
|
|
60
52
|
RecordTransactionInputSchema,
|
|
61
53
|
RevokePolicyInputSchema,
|
|
62
|
-
SimulatePolicyInputSchema,
|
|
63
54
|
SynthesizePolicyInputSchema,
|
|
64
55
|
ToolErrorSchema,
|
|
65
|
-
VerifyPolicyInputSchema,
|
|
66
56
|
}
|
|
67
57
|
|
|
68
58
|
/** Flat ZodRawShape used for the MCP SDK tool registration. The body
|
|
@@ -76,17 +66,15 @@ export const RecordTransactionToolShape = {
|
|
|
76
66
|
} as const
|
|
77
67
|
|
|
78
68
|
/** Flat ZodRawShape used for MCP tool registration. Every field is optional
|
|
79
|
-
* so the JSON-Schema the SDK exposes to clients
|
|
80
|
-
*
|
|
69
|
+
* so the JSON-Schema the SDK exposes to clients stays permissive; the body
|
|
70
|
+
* re-validates against the strict schema. */
|
|
81
71
|
export const SynthesizePolicyToolShape = {
|
|
82
|
-
source: z.
|
|
83
|
-
mandate: MandateSpecSchema.optional(),
|
|
72
|
+
source: z.literal('recording').optional(),
|
|
84
73
|
recordedTx: RecordedTransactionSchema.optional(),
|
|
85
74
|
network: NetworkSchema.optional(),
|
|
86
75
|
userResponses: ComposeUserResponsesSchema.optional(),
|
|
87
76
|
confidenceOverride: z.object({ threshold: z.number().min(0).max(1) }).optional(),
|
|
88
77
|
interpreter: InterpreterOptionsSchema.optional(),
|
|
89
|
-
ozConfig: OzAdapterConfigSchema.optional(),
|
|
90
78
|
// Without this the tool chain has no join. A ProposedPolicy carries
|
|
91
79
|
// `policyDocuments[].encodedPredicate` (canonical ScVal bytes), while
|
|
92
80
|
// `simulate_policy` and `verify_policy` both want the PredicateNode TREE,
|
|
@@ -97,35 +85,18 @@ export const SynthesizePolicyToolShape = {
|
|
|
97
85
|
explain: z.boolean().optional(),
|
|
98
86
|
} as const
|
|
99
87
|
|
|
100
|
-
/**
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
* malformed predicate. `predicate` is nullable here (vs required for
|
|
106
|
-
* verify_policy) so the SDK-emitted JSON Schema mirrors the engine's
|
|
107
|
-
* "OZ-only / no interpreter predicate" contract. */
|
|
108
|
-
export const SimulatePolicyToolShape = {
|
|
109
|
-
predicate: z.unknown().nullable().optional(),
|
|
88
|
+
/** `simulate_policy` and `verify_policy` share their input: the predicate tree
|
|
89
|
+
* (from `synthesize_policy` under `explain`) plus the recording it was
|
|
90
|
+
* synthesised from. */
|
|
91
|
+
const PolicyCheckToolShape = {
|
|
92
|
+
predicate: PredicateNodeSchema,
|
|
110
93
|
permitTx: RecordedTransactionSchema,
|
|
111
94
|
validUntilLedger: z.number().int().positive().optional(),
|
|
112
|
-
oraclePricesByAsset: z.record(z.string(), OraclePriceFixtureSchema).optional(),
|
|
113
95
|
} as const
|
|
114
96
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
* required at the strict-schema level (`VerifyPolicyInputSchema`) so
|
|
118
|
-
* the body fails closed on a missing predicate. */
|
|
119
|
-
export const VerifyPolicyToolShape = {
|
|
120
|
-
predicate: z.unknown(),
|
|
121
|
-
permitTx: RecordedTransactionSchema,
|
|
122
|
-
validUntilLedger: z.number().int().positive().optional(),
|
|
123
|
-
oraclePricesByAsset: z.record(z.string(), OraclePriceFixtureSchema).optional(),
|
|
124
|
-
} as const
|
|
97
|
+
export const SimulatePolicyToolShape = { ...PolicyCheckToolShape } as const
|
|
98
|
+
export const VerifyPolicyToolShape = { ...PolicyCheckToolShape } as const
|
|
125
99
|
|
|
126
|
-
// Re-export the strict input schemas so MCP consumers (and downstream
|
|
127
|
-
// tests) can import the canonical wire shapes from the same module
|
|
128
|
-
// that owns the tool-shape glue.
|
|
129
100
|
export type {
|
|
130
101
|
GetInterpreterInfoInput,
|
|
131
102
|
InstallPolicyInput,
|
|
@@ -135,13 +106,20 @@ export type {
|
|
|
135
106
|
} from '@crediolabs/policy-synth/run'
|
|
136
107
|
|
|
137
108
|
/** Common base for `install_policy` and `revoke_policy`: smartAccount,
|
|
138
|
-
* sourceAccount, optional RPC URL, optional base fee. Both
|
|
139
|
-
* same smart-account context, so the SDK-emitted JSON Schema stays
|
|
109
|
+
* sourceAccount, target network, optional RPC URL, optional base fee. Both
|
|
110
|
+
* share the same smart-account context, so the SDK-emitted JSON Schema stays
|
|
140
111
|
* identical for those fields. The body re-validates against the strict
|
|
141
112
|
* schemas in `@crediolabs/policy-synth/run`. */
|
|
142
113
|
const SmartAccountToolShape = {
|
|
143
114
|
smartAccount: z.string().min(1).optional(),
|
|
144
115
|
sourceAccount: z.string().min(1).optional(),
|
|
116
|
+
/** Omitting this from the shape made both tools testnet-ONLY. The input
|
|
117
|
+
* schema defaults `network` to `testnet` and expects a mainnet caller to
|
|
118
|
+
* set it, but a field absent from the tool shape is STRIPPED before the
|
|
119
|
+
* body runs, so an MCP client could not reach the mainnet pin at all. The
|
|
120
|
+
* failure presented as a deliberate testnet pin rather than as a missing
|
|
121
|
+
* parameter, which sent integrators to build the install by hand. */
|
|
122
|
+
network: NetworkSchema.optional(),
|
|
145
123
|
rpcUrl: z.string().url().optional(),
|
|
146
124
|
baseFee: z.number().int().positive().optional(),
|
|
147
125
|
}
|
|
@@ -167,15 +145,6 @@ export const RevokePolicyToolShape = {
|
|
|
167
145
|
interpreterAddress: z.string().optional(),
|
|
168
146
|
} as const
|
|
169
147
|
|
|
170
|
-
/** Flat ZodRawShape for `merge_policy`: the tightening remedy when two rules
|
|
171
|
-
* our interpreter polices can serve the same calls. Two steps, in order. */
|
|
172
|
-
export const MergePolicyToolShape = {
|
|
173
|
-
...SmartAccountToolShape,
|
|
174
|
-
ruleId: z.number().int().nonnegative(),
|
|
175
|
-
incomingPredicateBlobBase64: z.string().min(1),
|
|
176
|
-
step: z.enum(['detach', 'reinstall']),
|
|
177
|
-
} as const
|
|
178
|
-
|
|
179
148
|
/** Flat ZodRawShape for `get_interpreter_info`. `verifyLive` triggers an
|
|
180
149
|
* optional RPC `grammar_version()` call so the caller can verify the
|
|
181
150
|
* deployed contract matches the pin. */
|
package/src/server.ts
CHANGED
|
@@ -15,7 +15,6 @@ import type { ToolResponse } from '@crediolabs/policy-synth'
|
|
|
15
15
|
import {
|
|
16
16
|
runGetInterpreterInfo,
|
|
17
17
|
runInstallPolicy,
|
|
18
|
-
runMergePolicy,
|
|
19
18
|
runRecordTransaction,
|
|
20
19
|
runRevokePolicy,
|
|
21
20
|
runSimulatePolicy,
|
|
@@ -27,7 +26,6 @@ import type { CallToolResult } from '@modelcontextprotocol/sdk/types.js'
|
|
|
27
26
|
import {
|
|
28
27
|
GetInterpreterInfoToolShape,
|
|
29
28
|
InstallPolicyToolShape,
|
|
30
|
-
MergePolicyToolShape,
|
|
31
29
|
RecordTransactionToolShape,
|
|
32
30
|
RevokePolicyToolShape,
|
|
33
31
|
SimulatePolicyToolShape,
|
|
@@ -66,23 +64,23 @@ export function registerTools(server: McpServer): void {
|
|
|
66
64
|
|
|
67
65
|
server.tool(
|
|
68
66
|
'synthesize_policy',
|
|
69
|
-
'Synthesize a ProposedPolicy from
|
|
67
|
+
'Synthesize a ProposedPolicy from a RecordedTransaction (`source: recording`).',
|
|
70
68
|
SynthesizePolicyToolShape,
|
|
71
69
|
(args) => runSynthesizePolicy(args).then(toCallToolResult)
|
|
72
70
|
)
|
|
73
71
|
|
|
74
72
|
server.tool(
|
|
75
73
|
'simulate_policy',
|
|
76
|
-
'
|
|
74
|
+
'Evaluate a predicate against one recorded call and report permit/deny with the deny reason. The evaluator is a second implementation of the on-chain semantics, cross-checked against the Rust interpreter by the conformance harness, so a verdict here is a claim about what the contract would do. Pass the `predicate` returned by `synthesize_policy` under `explain`.',
|
|
77
75
|
SimulatePolicyToolShape,
|
|
78
|
-
(args) => runSimulatePolicy(args)
|
|
76
|
+
(args) => toCallToolResult(runSimulatePolicy(args))
|
|
79
77
|
)
|
|
80
78
|
|
|
81
79
|
server.tool(
|
|
82
80
|
'verify_policy',
|
|
83
|
-
'
|
|
81
|
+
'Check a predicate against the transaction it was synthesised from, plus a generated deny case per dimension. Reports `ok` only when the permit case is permitted AND every deny case is denied - a denied permit case means the policy is too strict, a permitted deny case means it is too loose.',
|
|
84
82
|
VerifyPolicyToolShape,
|
|
85
|
-
(args) => runVerifyPolicy(args)
|
|
83
|
+
(args) => toCallToolResult(runVerifyPolicy(args))
|
|
86
84
|
)
|
|
87
85
|
|
|
88
86
|
server.tool(
|
|
@@ -99,13 +97,6 @@ export function registerTools(server: McpServer): void {
|
|
|
99
97
|
(args) => runRevokePolicy(args).then(toCallToolResult)
|
|
100
98
|
)
|
|
101
99
|
|
|
102
|
-
server.tool(
|
|
103
|
-
'merge_policy',
|
|
104
|
-
"Tighten a rule by replacing its predicate with the conjunction of the installed one and a new one. This is the remedy for the overlap `install_policy` reports between two rules the interpreter polices: because OpenZeppelin enforces only the rule a signer names, adding a second, tighter rule restricts nothing, so the restriction has to become one predicate. TWO transactions, in order: call with step 'detach' to remove the current attachment, wait for it to confirm, then call with step 'reinstall'. Detaching uninstalls the policy, which RESETS every counter on the rule, and the rule is unpoliced in between - both are reported in `warnings`.",
|
|
105
|
-
MergePolicyToolShape,
|
|
106
|
-
(args) => runMergePolicy(args).then(toCallToolResult)
|
|
107
|
-
)
|
|
108
|
-
|
|
109
100
|
server.tool(
|
|
110
101
|
'get_interpreter_info',
|
|
111
102
|
'Read-only fingerprint lookup for the policy interpreter contract: returns the pinned address, grammar version, and wasm sha256 (from the pinned constants + SELF_VERSION). When `verifyLive=true`, performs an additional `grammar_version()` RPC call against the pinned address and reports whether the deployed contract matches the pin - a live mismatch check is more useful than a fabricated audit field.',
|
package/src/transports/http.ts
CHANGED
|
@@ -2,13 +2,10 @@
|
|
|
2
2
|
//
|
|
3
3
|
// Streamable HTTP transport (hosted). Uses the Node http module directly so
|
|
4
4
|
// the package stays thin - no express / hono dep. We run in STATELESS mode
|
|
5
|
-
// (no
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
// a transport inside the request handler, connect it, dispatch the request,
|
|
10
|
-
// then close it (close() resets the McpServer's transport slot so the next
|
|
11
|
-
// request can reconnect cleanly).
|
|
5
|
+
// (no sessionIdGenerator: the SDK disables session management when it is not
|
|
6
|
+
// provided) so each POST /mcp is its own transaction: this matches the
|
|
7
|
+
// brief's "stateless across calls" invariant. Stateless means a fresh server
|
|
8
|
+
// and transport per request - see the handler for why the SDK requires it.
|
|
12
9
|
//
|
|
13
10
|
// Single endpoint: POST /mcp (the SDK also accepts GET for SSE streaming, but
|
|
14
11
|
// the T1 surface does not emit server-initiated messages so we omit it).
|
|
@@ -66,12 +63,6 @@ export async function startHttpServer(opts: StartHttpServerOptions): Promise<Run
|
|
|
66
63
|
`startHttpServer: refusing to bind host ${host}: the MCP surface is unauthenticated, so only loopback (127.0.0.1, ::1, localhost) is permitted by default. Pass \`allowExternalHost: true\` to opt in to a non-loopback bind.`
|
|
67
64
|
)
|
|
68
65
|
}
|
|
69
|
-
// The McpServer is stateless and per-process; we keep ONE instance alive
|
|
70
|
-
// for the lifetime of the listener, but each request creates + connects a
|
|
71
|
-
// fresh StreamableHTTPServerTransport (SDK 1.26+ refuses to reuse a
|
|
72
|
-
// stateless transport across requests).
|
|
73
|
-
const server = createMcpServer()
|
|
74
|
-
|
|
75
66
|
const httpServer: Server = createServer(async (req, res) => {
|
|
76
67
|
if (!req.url) {
|
|
77
68
|
sendJson(res, 400, { error: 'missing url' })
|
|
@@ -118,22 +109,33 @@ export async function startHttpServer(opts: StartHttpServerOptions): Promise<Run
|
|
|
118
109
|
return
|
|
119
110
|
}
|
|
120
111
|
|
|
121
|
-
//
|
|
122
|
-
//
|
|
123
|
-
//
|
|
124
|
-
//
|
|
125
|
-
//
|
|
126
|
-
//
|
|
127
|
-
//
|
|
128
|
-
//
|
|
129
|
-
//
|
|
130
|
-
//
|
|
131
|
-
|
|
112
|
+
// A stateless transport handles exactly ONE request: the SDK refuses to
|
|
113
|
+
// reuse one ("Stateless transport cannot be reused across requests"),
|
|
114
|
+
// because a shared instance would let concurrent clients collide on
|
|
115
|
+
// JSON-RPC message ids. So the server and its transport are built here,
|
|
116
|
+
// per request, and torn down when the response closes.
|
|
117
|
+
// No `sessionIdGenerator`: the SDK disables session management when the
|
|
118
|
+
// option is absent, which is the stateless mode this surface wants.
|
|
119
|
+
// Passing an explicit `undefined` means the same thing to the SDK but is
|
|
120
|
+
// not assignable under `exactOptionalPropertyTypes`, so omission is both
|
|
121
|
+
// the type-correct and the documented spelling.
|
|
122
|
+
const server = createMcpServer()
|
|
132
123
|
const transport = new StreamableHTTPServerTransport()
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
124
|
+
// Registered before dispatch, not after: once the response has closed the
|
|
125
|
+
// event is gone, so a listener attached afterwards would never fire and
|
|
126
|
+
// the pair would leak.
|
|
127
|
+
res.on('close', () => {
|
|
128
|
+
void transport.close().catch(() => {})
|
|
129
|
+
void server.close().catch(() => {})
|
|
130
|
+
})
|
|
136
131
|
try {
|
|
132
|
+
// The SDK declares `Transport.onclose` as an optional `() => void`, but
|
|
133
|
+
// exposes it on this class as an accessor pair typed `(() => void) |
|
|
134
|
+
// undefined`. Those are not assignable under `exactOptionalPropertyTypes`.
|
|
135
|
+
// The widening is upstream and structural only - the runtime object does
|
|
136
|
+
// satisfy `Transport` - so the assertion is narrowed to this one call
|
|
137
|
+
// rather than relaxing the compiler flag for the whole package.
|
|
138
|
+
await server.connect(transport as unknown as Transport)
|
|
137
139
|
// `handleRequest` writes the response and returns once the message has
|
|
138
140
|
// been dispatched. No shared state across calls in stateless mode.
|
|
139
141
|
await transport.handleRequest(req as IncomingMessage & { auth?: never }, res, body)
|
|
@@ -151,13 +153,6 @@ export async function startHttpServer(opts: StartHttpServerOptions): Promise<Run
|
|
|
151
153
|
}
|
|
152
154
|
sendJson(res, 500, { error })
|
|
153
155
|
}
|
|
154
|
-
} finally {
|
|
155
|
-
// Closing the transport fires its `onclose`, which resets
|
|
156
|
-
// `server._transport` to undefined so the next request can connect
|
|
157
|
-
// a fresh transport. We swallow the close error: a request that
|
|
158
|
-
// already wrote its response does not care whether the close path
|
|
159
|
-
// threw.
|
|
160
|
-
await transport.close().catch(() => {})
|
|
161
156
|
}
|
|
162
157
|
})
|
|
163
158
|
|
|
@@ -173,9 +168,11 @@ export async function startHttpServer(opts: StartHttpServerOptions): Promise<Run
|
|
|
173
168
|
port: opts.port,
|
|
174
169
|
host,
|
|
175
170
|
path,
|
|
171
|
+
// Nothing outlives a request, so closing the listener is the whole
|
|
172
|
+
// shutdown: each request's server and transport are already torn down by
|
|
173
|
+
// the `close` handler on its own response.
|
|
176
174
|
close: async () => {
|
|
177
175
|
await new Promise<void>((resolve) => httpServer.close(() => resolve()))
|
|
178
|
-
await server.close().catch(() => {})
|
|
179
176
|
},
|
|
180
177
|
}
|
|
181
178
|
}
|