@crediolabs/policy-builder-mcp 0.1.17 → 0.2.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/README.md +42 -52
- package/dist/bin/server.js +1 -1
- package/dist/src/index.js +1 -1
- package/dist/src/schemas.d.ts +122 -5
- package/dist/src/schemas.js +22 -9
- package/dist/src/server.js +16 -35
- package/dist/src/tools/result.js +8 -9
- package/dist/src/transports/http.d.ts +6 -0
- package/dist/src/transports/http.js +57 -14
- package/dist/src/transports/stdio.js +1 -1
- package/package.json +8 -12
- package/src/index.ts +1 -1
- package/src/schemas.ts +24 -9
- package/src/server.ts +27 -34
- package/src/tools/result.ts +6 -8
- package/src/transports/http.ts +67 -13
- package/src/transports/stdio.ts +1 -1
- package/dist-cjs/package.json +0 -3
- package/dist-cjs/src/index.d.ts +0 -7
- package/dist-cjs/src/index.js +0 -20
- package/dist-cjs/src/schemas.d.ts +0 -1055
- package/dist-cjs/src/schemas.js +0 -130
- package/dist-cjs/src/server.d.ts +0 -6
- package/dist-cjs/src/server.js +0 -62
- package/dist-cjs/src/tools/result.d.ts +0 -24
- package/dist-cjs/src/tools/result.js +0 -39
- package/dist-cjs/src/transports/http.d.ts +0 -15
- package/dist-cjs/src/transports/http.js +0 -140
- package/dist-cjs/src/transports/stdio.d.ts +0 -1
- package/dist-cjs/src/transports/stdio.js +0 -18
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crediolabs/policy-builder-mcp",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "MCP server exposing the OZ policy-synth core (record_transaction + synthesize_policy) over stdio and Streamable HTTP transports.",
|
|
6
6
|
"type": "module",
|
|
@@ -12,12 +12,12 @@
|
|
|
12
12
|
},
|
|
13
13
|
"repository": {
|
|
14
14
|
"type": "git",
|
|
15
|
-
"url": "https://github.com/untangledfinance/
|
|
16
|
-
"directory": "
|
|
15
|
+
"url": "https://github.com/untangledfinance/octogate.git",
|
|
16
|
+
"directory": "packages/policy-builder-mcp"
|
|
17
17
|
},
|
|
18
|
-
"homepage": "https://github.com/untangledfinance/
|
|
18
|
+
"homepage": "https://github.com/untangledfinance/octogate#readme",
|
|
19
19
|
"bugs": {
|
|
20
|
-
"url": "https://github.com/untangledfinance/
|
|
20
|
+
"url": "https://github.com/untangledfinance/octogate/issues"
|
|
21
21
|
},
|
|
22
22
|
"keywords": [
|
|
23
23
|
"stellar",
|
|
@@ -37,7 +37,6 @@
|
|
|
37
37
|
"types": "./dist/src/index.d.ts",
|
|
38
38
|
"bun": "./src/index.ts",
|
|
39
39
|
"import": "./dist/src/index.js",
|
|
40
|
-
"require": "./dist-cjs/src/index.js",
|
|
41
40
|
"default": "./dist/src/index.js"
|
|
42
41
|
},
|
|
43
42
|
"./package.json": "./package.json"
|
|
@@ -47,7 +46,6 @@
|
|
|
47
46
|
},
|
|
48
47
|
"files": [
|
|
49
48
|
"dist",
|
|
50
|
-
"dist-cjs",
|
|
51
49
|
"src",
|
|
52
50
|
"!src/**/*.test.ts"
|
|
53
51
|
],
|
|
@@ -56,15 +54,13 @@
|
|
|
56
54
|
},
|
|
57
55
|
"scripts": {
|
|
58
56
|
"test": "bun test",
|
|
59
|
-
"build": "tsc -p tsconfig.build.json
|
|
60
|
-
"build:esm": "tsc -p tsconfig.build.json",
|
|
61
|
-
"build:cjs": "tsc -p tsconfig.build.cjs.json && node scripts/write-cjs-package-json.cjs",
|
|
57
|
+
"build": "tsc -p tsconfig.build.json",
|
|
62
58
|
"prepublishOnly": "bun run build && bun test",
|
|
63
59
|
"prepack": "bun run build"
|
|
64
60
|
},
|
|
65
61
|
"dependencies": {
|
|
66
|
-
"@crediolabs/policy-synth": "0.
|
|
67
|
-
"@modelcontextprotocol/sdk": "1.
|
|
62
|
+
"@crediolabs/policy-synth": "0.2.0",
|
|
63
|
+
"@modelcontextprotocol/sdk": "1.26.0",
|
|
68
64
|
"@stellar/stellar-sdk": "14.4.0",
|
|
69
65
|
"zod": "3.25.76"
|
|
70
66
|
},
|
package/src/index.ts
CHANGED
package/src/schemas.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//
|
|
1
|
+
// packages/policy-builder-mcp/src/schemas.ts
|
|
2
2
|
//
|
|
3
3
|
// MCP-only tool shapes. The CORE input/output schemas and the MCP server
|
|
4
4
|
// registrations all live on `@crediolabs/policy-synth` (the tool-body glue
|
|
@@ -134,6 +134,18 @@ export type {
|
|
|
134
134
|
VerifyPolicyInput,
|
|
135
135
|
} from '@crediolabs/policy-synth/run'
|
|
136
136
|
|
|
137
|
+
/** Common base for `install_policy` and `revoke_policy`: smartAccount,
|
|
138
|
+
* sourceAccount, optional RPC URL, optional base fee. Both share the
|
|
139
|
+
* same smart-account context, so the SDK-emitted JSON Schema stays
|
|
140
|
+
* identical for those fields. The body re-validates against the strict
|
|
141
|
+
* schemas in `@crediolabs/policy-synth/run`. */
|
|
142
|
+
const SmartAccountToolShape = {
|
|
143
|
+
smartAccount: z.string().min(1).optional(),
|
|
144
|
+
sourceAccount: z.string().min(1).optional(),
|
|
145
|
+
rpcUrl: z.string().url().optional(),
|
|
146
|
+
baseFee: z.number().int().positive().optional(),
|
|
147
|
+
}
|
|
148
|
+
|
|
137
149
|
/** Flat ZodRawShape for `install_policy`. `rule` is typed as `z.unknown()`
|
|
138
150
|
* at the tool boundary because the rule schema is a discriminated union
|
|
139
151
|
* the SDK does not accept at registration; the body re-validates
|
|
@@ -142,23 +154,26 @@ export type {
|
|
|
142
154
|
* (the policy already carries the encoded predicate); the run-layer
|
|
143
155
|
* extracts them from there. */
|
|
144
156
|
export const InstallPolicyToolShape = {
|
|
145
|
-
|
|
146
|
-
sourceAccount: z.string().min(1).optional(),
|
|
157
|
+
...SmartAccountToolShape,
|
|
147
158
|
rule: z.unknown().optional(),
|
|
148
159
|
installNonce: z.number().int().positive().optional(),
|
|
149
160
|
interpreterAddress: z.string().optional(),
|
|
150
|
-
rpcUrl: z.string().url().optional(),
|
|
151
|
-
baseFee: z.number().int().positive().optional(),
|
|
152
161
|
} as const
|
|
153
162
|
|
|
154
163
|
/** Flat ZodRawShape for `revoke_policy`. */
|
|
155
164
|
export const RevokePolicyToolShape = {
|
|
156
|
-
|
|
157
|
-
sourceAccount: z.string().min(1).optional(),
|
|
165
|
+
...SmartAccountToolShape,
|
|
158
166
|
ruleId: z.number().int().nonnegative().optional(),
|
|
159
167
|
interpreterAddress: z.string().optional(),
|
|
160
|
-
|
|
161
|
-
|
|
168
|
+
} as const
|
|
169
|
+
|
|
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']),
|
|
162
177
|
} as const
|
|
163
178
|
|
|
164
179
|
/** Flat ZodRawShape for `get_interpreter_info`. `verifyLive` triggers an
|
package/src/server.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//
|
|
1
|
+
// packages/policy-builder-mcp/src/server.ts
|
|
2
2
|
//
|
|
3
3
|
// Registers the T1 tool surface on a fresh McpServer instance. The
|
|
4
4
|
// registration uses the official MCP SDK's `tool()` API with ZodRawShape
|
|
@@ -11,9 +11,11 @@
|
|
|
11
11
|
// shared mutable state across calls; nothing here caches, queues, or holds
|
|
12
12
|
// key material.
|
|
13
13
|
|
|
14
|
+
import type { ToolResponse } from '@crediolabs/policy-synth'
|
|
14
15
|
import {
|
|
15
16
|
runGetInterpreterInfo,
|
|
16
17
|
runInstallPolicy,
|
|
18
|
+
runMergePolicy,
|
|
17
19
|
runRecordTransaction,
|
|
18
20
|
runRevokePolicy,
|
|
19
21
|
runSimulatePolicy,
|
|
@@ -25,6 +27,7 @@ import type { CallToolResult } from '@modelcontextprotocol/sdk/types.js'
|
|
|
25
27
|
import {
|
|
26
28
|
GetInterpreterInfoToolShape,
|
|
27
29
|
InstallPolicyToolShape,
|
|
30
|
+
MergePolicyToolShape,
|
|
28
31
|
RecordTransactionToolShape,
|
|
29
32
|
RevokePolicyToolShape,
|
|
30
33
|
SimulatePolicyToolShape,
|
|
@@ -33,6 +36,14 @@ import {
|
|
|
33
36
|
} from './schemas.ts'
|
|
34
37
|
import { mcpResultFromCore } from './tools/result.ts'
|
|
35
38
|
|
|
39
|
+
/** Our envelope types `structuredContent` precisely (T / ToolError); the SDK's
|
|
40
|
+
* CallToolResult widens it to Record<string, unknown>, so the nominal types
|
|
41
|
+
* do not overlap. The runtime shapes match, so we assert through `unknown`
|
|
42
|
+
* at the transport boundary - in one place, used by every tool. */
|
|
43
|
+
const toCallToolResult = <T>(
|
|
44
|
+
res: ToolResponse<T> | (ToolResponse<T> & Record<string, unknown>)
|
|
45
|
+
): CallToolResult => mcpResultFromCore(res as ToolResponse<T>) as unknown as CallToolResult
|
|
46
|
+
|
|
36
47
|
/** Build a fresh, stateless MCP server. The caller owns the returned object
|
|
37
48
|
* and connects it to a single transport (stdio or Streamable HTTP). */
|
|
38
49
|
export function createMcpServer(): McpServer {
|
|
@@ -50,73 +61,55 @@ export function registerTools(server: McpServer): void {
|
|
|
50
61
|
'record_transaction',
|
|
51
62
|
'Decode a Soroban transaction (on-chain hash OR base64 envelope XDR) into a RecordedTransaction. Returns a machine-readable ToolError on validation failure.',
|
|
52
63
|
RecordTransactionToolShape,
|
|
53
|
-
|
|
54
|
-
const res = await runRecordTransaction(args)
|
|
55
|
-
// Our envelope types `structuredContent` precisely (T / ToolError); the
|
|
56
|
-
// SDK's CallToolResult widens it to Record<string, unknown>, so the
|
|
57
|
-
// nominal types do not overlap. The runtime shapes match, so assert
|
|
58
|
-
// through `unknown` at this transport boundary.
|
|
59
|
-
return mcpResultFromCore(res) as unknown as CallToolResult
|
|
60
|
-
}
|
|
64
|
+
(args) => runRecordTransaction(args).then(toCallToolResult)
|
|
61
65
|
)
|
|
62
66
|
|
|
63
67
|
server.tool(
|
|
64
68
|
'synthesize_policy',
|
|
65
69
|
'Synthesize a ProposedPolicy from either a deterministic MandateSpec (`source: mandate`) or a RecordedTransaction (`source: recording`). The discriminated `source` field selects the front-end.',
|
|
66
70
|
SynthesizePolicyToolShape,
|
|
67
|
-
|
|
68
|
-
const res = await runSynthesizePolicy(args)
|
|
69
|
-
return mcpResultFromCore(res) as unknown as CallToolResult
|
|
70
|
-
}
|
|
71
|
+
(args) => runSynthesizePolicy(args).then(toCallToolResult)
|
|
71
72
|
)
|
|
72
73
|
|
|
73
74
|
server.tool(
|
|
74
75
|
'simulate_policy',
|
|
75
76
|
'Replay a RecordedTransaction against a proposed PredicateNode (or null for an OZ-only policy) and emit the SimulationResult envelope (permit verdict + deny-case battery). Returns a SIMULATION_ERROR ToolError on runtime evaluation failure.',
|
|
76
77
|
SimulatePolicyToolShape,
|
|
77
|
-
|
|
78
|
-
const res = await runSimulatePolicy(args)
|
|
79
|
-
return mcpResultFromCore(res) as unknown as CallToolResult
|
|
80
|
-
}
|
|
78
|
+
(args) => runSimulatePolicy(args).then(toCallToolResult)
|
|
81
79
|
)
|
|
82
80
|
|
|
83
81
|
server.tool(
|
|
84
82
|
'verify_policy',
|
|
85
83
|
'Run the static minimality check on a proposed PredicateNode against a RecordedTransaction (no conjunct is load-bearing-free). Returns VERIFICATION_FAILED with the dropped-constraint fingerprints when the predicate is over-broad.',
|
|
86
84
|
VerifyPolicyToolShape,
|
|
87
|
-
|
|
88
|
-
const res = await runVerifyPolicy(args)
|
|
89
|
-
return mcpResultFromCore(res) as unknown as CallToolResult
|
|
90
|
-
}
|
|
85
|
+
(args) => runVerifyPolicy(args).then(toCallToolResult)
|
|
91
86
|
)
|
|
92
87
|
|
|
93
88
|
server.tool(
|
|
94
89
|
'install_policy',
|
|
95
90
|
'Build an UNSIGNED Soroban transaction XDR for `account.add_context_rule(...)` that installs a new policy rule on the given smart account. The wallet signs the returned XDR - the signature IS the user-confirmation step (this server is stateless and holds no key material, so there is no two-call confirm pair). Only CALL 1 is emitted; the interpreter `install` follow-up needs the rule id the account assigns in call 1 and is documented in `followUp` in the response.',
|
|
96
91
|
InstallPolicyToolShape,
|
|
97
|
-
|
|
98
|
-
const res = await runInstallPolicy(args)
|
|
99
|
-
return mcpResultFromCore(res) as unknown as CallToolResult
|
|
100
|
-
}
|
|
92
|
+
(args) => runInstallPolicy(args).then(toCallToolResult)
|
|
101
93
|
)
|
|
102
94
|
|
|
103
95
|
server.tool(
|
|
104
96
|
'revoke_policy',
|
|
105
97
|
'Build an UNSIGNED Soroban transaction XDR for `account.remove_context_rule(ruleId)` that removes a policy rule from the given smart account. The smart account handles uninstalling each attached policy itself. Auth is master-only - the source account MUST be the master signer set; delegated signers cannot uninstall.',
|
|
106
98
|
RevokePolicyToolShape,
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
99
|
+
(args) => runRevokePolicy(args).then(toCallToolResult)
|
|
100
|
+
)
|
|
101
|
+
|
|
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)
|
|
111
107
|
)
|
|
112
108
|
|
|
113
109
|
server.tool(
|
|
114
110
|
'get_interpreter_info',
|
|
115
|
-
'Read-only fingerprint lookup for the policy interpreter contract: returns the pinned address, grammar version, and wasm sha256 (from
|
|
111
|
+
'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.',
|
|
116
112
|
GetInterpreterInfoToolShape,
|
|
117
|
-
|
|
118
|
-
const res = await runGetInterpreterInfo(args)
|
|
119
|
-
return mcpResultFromCore(res) as unknown as CallToolResult
|
|
120
|
-
}
|
|
113
|
+
(args) => runGetInterpreterInfo(args).then(toCallToolResult)
|
|
121
114
|
)
|
|
122
115
|
}
|
package/src/tools/result.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//
|
|
1
|
+
// packages/policy-builder-mcp/src/tools/result.ts
|
|
2
2
|
//
|
|
3
3
|
// Transport-layer mapping between the core's ToolResponse<T> envelope and the
|
|
4
4
|
// MCP result envelope. This is the ONLY place the two envelopes meet; no
|
|
@@ -32,14 +32,12 @@ export type McpToolResult<T> = McpToolSuccess<T> | McpToolError
|
|
|
32
32
|
/** Map a core ToolResponse<T> to the MCP result envelope. The handler in
|
|
33
33
|
* src/server.ts wraps this in the SDK's `{ content, isError }` shape. */
|
|
34
34
|
export function mcpResultFromCore<T>(res: ToolResponse<T>): McpToolResult<T> {
|
|
35
|
-
if (res.ok)
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
35
|
+
if (!res.ok) return mcpErrorFromCore(res.error)
|
|
36
|
+
return {
|
|
37
|
+
isError: false,
|
|
38
|
+
content: [{ type: 'text', text: JSON.stringify(res.data) }],
|
|
39
|
+
structuredContent: res.data,
|
|
41
40
|
}
|
|
42
|
-
return mcpErrorFromCore(res.error)
|
|
43
41
|
}
|
|
44
42
|
|
|
45
43
|
/** Map a core ToolError directly (for inputs that already failed validation
|
package/src/transports/http.ts
CHANGED
|
@@ -1,24 +1,48 @@
|
|
|
1
|
-
//
|
|
1
|
+
// packages/policy-builder-mcp/src/transports/http.ts
|
|
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
|
-
// (sessionIdGenerator
|
|
6
|
-
//
|
|
5
|
+
// (no `sessionIdGenerator` option), and the SDK 1.26+ requires a FRESH
|
|
6
|
+
// transport per request in stateless mode: reusing one across requests lets
|
|
7
|
+
// concurrent clients share internal message-id state, which is exactly the
|
|
8
|
+
// cross-client data leak GHSA-345p-7cg4-v4c7 warns about. We therefore build
|
|
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).
|
|
7
12
|
//
|
|
8
13
|
// Single endpoint: POST /mcp (the SDK also accepts GET for SSE streaming, but
|
|
9
14
|
// the T1 surface does not emit server-initiated messages so we omit it).
|
|
10
|
-
// Listens on 127.0.0.1 by default
|
|
15
|
+
// Listens on 127.0.0.1 by default.
|
|
16
|
+
//
|
|
17
|
+
// SECURITY BOUNDARY: the MCP server has NO auth - any request is processed.
|
|
18
|
+
// Binding to a non-loopback interface (e.g. `0.0.0.0`, `::`, an external
|
|
19
|
+
// NIC) would expose that unauthenticated surface to every reachable host
|
|
20
|
+
// (LAN peers, public cloud metadata, the open internet on a misconfigured
|
|
21
|
+
// VPS). The host boundary is therefore FAIL-CLOSED: only loopback
|
|
22
|
+
// (`127.0.0.1`, `::1`, `localhost`) is accepted by default. A caller that
|
|
23
|
+
// KNOWS they want to expose the server MUST pass `allowExternalHost: true`
|
|
24
|
+
// explicitly - the flag is the auditable intent. Test runners and the
|
|
25
|
+
// in-process CLI client do not need it; they bind 127.0.0.1 already.
|
|
11
26
|
|
|
12
27
|
import { createServer, type IncomingMessage, type Server, type ServerResponse } from 'node:http'
|
|
13
28
|
import type { ToolError } from '@crediolabs/policy-synth'
|
|
14
29
|
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js'
|
|
30
|
+
import type { Transport } from '@modelcontextprotocol/sdk/shared/transport.js'
|
|
15
31
|
import { createMcpServer } from '../server.ts'
|
|
16
32
|
|
|
33
|
+
const LOOPBACK_HOSTS = new Set(['127.0.0.1', '::1', 'localhost'])
|
|
34
|
+
|
|
17
35
|
export interface StartHttpServerOptions {
|
|
18
36
|
port: number
|
|
19
37
|
host?: string
|
|
20
38
|
/** Path the server mounts the MCP endpoint at. Default `/mcp`. */
|
|
21
39
|
path?: string
|
|
40
|
+
/** Opt-in to binding a NON-loopback host (e.g. `0.0.0.0` to expose the
|
|
41
|
+
* server on a LAN / public NIC). The MCP surface is unauthenticated, so
|
|
42
|
+
* this is gated behind a flag: the default refuse-then-opt-in shape
|
|
43
|
+
* keeps the security boundary auditable in code review. A caller that
|
|
44
|
+
* sets this is taking responsibility for downstream auth / firewall. */
|
|
45
|
+
allowExternalHost?: boolean
|
|
22
46
|
}
|
|
23
47
|
|
|
24
48
|
export interface RunningHttpServer {
|
|
@@ -33,13 +57,20 @@ export interface RunningHttpServer {
|
|
|
33
57
|
export async function startHttpServer(opts: StartHttpServerOptions): Promise<RunningHttpServer> {
|
|
34
58
|
const host = opts.host ?? '127.0.0.1'
|
|
35
59
|
const path = opts.path ?? '/mcp'
|
|
60
|
+
// Default-deny: refuse to bind a non-loopback host unless the caller has
|
|
61
|
+
// explicitly opted in. The MCP server has no auth, so the only thing
|
|
62
|
+
// standing between this binary and an open attack surface on `0.0.0.0` is
|
|
63
|
+
// this check; we would rather fail loudly here than silently expose it.
|
|
64
|
+
if (!LOOPBACK_HOSTS.has(host) && opts.allowExternalHost !== true) {
|
|
65
|
+
throw new Error(
|
|
66
|
+
`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
|
+
)
|
|
68
|
+
}
|
|
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).
|
|
36
73
|
const server = createMcpServer()
|
|
37
|
-
// One transport per server (the SDK reuses the transport for every request
|
|
38
|
-
// in stateless mode). We connect it once at startup and reuse it.
|
|
39
|
-
const transport = new StreamableHTTPServerTransport({
|
|
40
|
-
sessionIdGenerator: undefined,
|
|
41
|
-
})
|
|
42
|
-
await server.connect(transport)
|
|
43
74
|
|
|
44
75
|
const httpServer: Server = createServer(async (req, res) => {
|
|
45
76
|
if (!req.url) {
|
|
@@ -87,6 +118,21 @@ export async function startHttpServer(opts: StartHttpServerOptions): Promise<Run
|
|
|
87
118
|
return
|
|
88
119
|
}
|
|
89
120
|
|
|
121
|
+
// One transport per request. Omitting `sessionIdGenerator` selects
|
|
122
|
+
// stateless mode in SDK 1.26+ (the previous `sessionIdGenerator:
|
|
123
|
+
// undefined` form is rejected because the option type no longer
|
|
124
|
+
// includes `undefined` under `exactOptionalPropertyTypes`).
|
|
125
|
+
//
|
|
126
|
+
// The cast `as unknown as Transport` is narrowly scoped to the SDK
|
|
127
|
+
// boundary: the Node wrapper exposes `onclose` via getter/setter with
|
|
128
|
+
// type `(() => void) | undefined`, while the `Transport` interface
|
|
129
|
+
// declares `onclose?: () => void`. Under `exactOptionalPropertyTypes:
|
|
130
|
+
// true` those are not structurally assignable. The runtime contract is
|
|
131
|
+
// the same (the setter accepts `() => void`), so we cast once here.
|
|
132
|
+
const transport = new StreamableHTTPServerTransport()
|
|
133
|
+
const sdkTransport = transport as unknown as Transport
|
|
134
|
+
await server.connect(sdkTransport)
|
|
135
|
+
|
|
90
136
|
try {
|
|
91
137
|
// `handleRequest` writes the response and returns once the message has
|
|
92
138
|
// been dispatched. No shared state across calls in stateless mode.
|
|
@@ -94,7 +140,9 @@ export async function startHttpServer(opts: StartHttpServerOptions): Promise<Run
|
|
|
94
140
|
} catch {
|
|
95
141
|
// The SDK normally writes structured errors itself; this is a belt +
|
|
96
142
|
// braces guard so a thrown error does not leave the socket hanging.
|
|
97
|
-
if (
|
|
143
|
+
if (res.headersSent) {
|
|
144
|
+
res.end()
|
|
145
|
+
} else {
|
|
98
146
|
const error: ToolError = {
|
|
99
147
|
code: 'SYNTHESIS_ERROR',
|
|
100
148
|
message: 'internal server error',
|
|
@@ -102,7 +150,14 @@ export async function startHttpServer(opts: StartHttpServerOptions): Promise<Run
|
|
|
102
150
|
retryable: false,
|
|
103
151
|
}
|
|
104
152
|
sendJson(res, 500, { error })
|
|
105
|
-
}
|
|
153
|
+
}
|
|
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(() => {})
|
|
106
161
|
}
|
|
107
162
|
})
|
|
108
163
|
|
|
@@ -120,7 +175,6 @@ export async function startHttpServer(opts: StartHttpServerOptions): Promise<Run
|
|
|
120
175
|
path,
|
|
121
176
|
close: async () => {
|
|
122
177
|
await new Promise<void>((resolve) => httpServer.close(() => resolve()))
|
|
123
|
-
await transport.close().catch(() => {})
|
|
124
178
|
await server.close().catch(() => {})
|
|
125
179
|
},
|
|
126
180
|
}
|
package/src/transports/stdio.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//
|
|
1
|
+
// packages/policy-builder-mcp/src/transports/stdio.ts
|
|
2
2
|
//
|
|
3
3
|
// stdio transport (Claude Desktop / local agents). The MCP SDK reads JSON-RPC
|
|
4
4
|
// from stdin and writes to stdout. Each process serves ONE client and exits
|
package/dist-cjs/package.json
DELETED
package/dist-cjs/src/index.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export { type RunRecordTransactionInput, type RunSynthesizePolicyInput, runRecordTransaction, runSynthesizePolicy, } from '@crediolabs/policy-synth/run';
|
|
2
|
-
export { RecordTransactionToolShape, SynthesizePolicyToolShape, } from './schemas.ts';
|
|
3
|
-
export { createMcpServer, registerTools } from './server.ts';
|
|
4
|
-
export type { McpToolError, McpToolResult } from './tools/result.ts';
|
|
5
|
-
export { mcpErrorFromCore, mcpResultFromCore } from './tools/result.ts';
|
|
6
|
-
export { startHttpServer } from './transports/http.ts';
|
|
7
|
-
export { startStdioServer } from './transports/stdio.ts';
|
package/dist-cjs/src/index.js
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
// apps/policy-builder-mcp/src/index.ts - public re-exports for the MCP server package.
|
|
3
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.startStdioServer = exports.startHttpServer = exports.mcpResultFromCore = exports.mcpErrorFromCore = exports.registerTools = exports.createMcpServer = exports.SynthesizePolicyToolShape = exports.RecordTransactionToolShape = exports.runSynthesizePolicy = exports.runRecordTransaction = void 0;
|
|
5
|
-
var run_1 = require("@crediolabs/policy-synth/run");
|
|
6
|
-
Object.defineProperty(exports, "runRecordTransaction", { enumerable: true, get: function () { return run_1.runRecordTransaction; } });
|
|
7
|
-
Object.defineProperty(exports, "runSynthesizePolicy", { enumerable: true, get: function () { return run_1.runSynthesizePolicy; } });
|
|
8
|
-
var schemas_ts_1 = require("./schemas.js");
|
|
9
|
-
Object.defineProperty(exports, "RecordTransactionToolShape", { enumerable: true, get: function () { return schemas_ts_1.RecordTransactionToolShape; } });
|
|
10
|
-
Object.defineProperty(exports, "SynthesizePolicyToolShape", { enumerable: true, get: function () { return schemas_ts_1.SynthesizePolicyToolShape; } });
|
|
11
|
-
var server_ts_1 = require("./server.js");
|
|
12
|
-
Object.defineProperty(exports, "createMcpServer", { enumerable: true, get: function () { return server_ts_1.createMcpServer; } });
|
|
13
|
-
Object.defineProperty(exports, "registerTools", { enumerable: true, get: function () { return server_ts_1.registerTools; } });
|
|
14
|
-
var result_ts_1 = require("./tools/result.js");
|
|
15
|
-
Object.defineProperty(exports, "mcpErrorFromCore", { enumerable: true, get: function () { return result_ts_1.mcpErrorFromCore; } });
|
|
16
|
-
Object.defineProperty(exports, "mcpResultFromCore", { enumerable: true, get: function () { return result_ts_1.mcpResultFromCore; } });
|
|
17
|
-
var http_ts_1 = require("./transports/http.js");
|
|
18
|
-
Object.defineProperty(exports, "startHttpServer", { enumerable: true, get: function () { return http_ts_1.startHttpServer; } });
|
|
19
|
-
var stdio_ts_1 = require("./transports/stdio.js");
|
|
20
|
-
Object.defineProperty(exports, "startStdioServer", { enumerable: true, get: function () { return stdio_ts_1.startStdioServer; } });
|