@sluzen/mcp 0.0.0-stage → 0.1.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 CHANGED
@@ -1,3 +1,87 @@
1
- # Temporary Holding Version
1
+ # @sluzen/mcp
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ MCP (stdio) surface for **HTTP 402 schemes on XRPL (Payment | PayChan | allowlisted RLUSD)**.
4
+
5
+ Wraps live Sluzen APIs and `@sluzen/sdk`. Does **not** invent rails. Private keys / seeds stay on the MCP host only. Never paste a seed into sluzen.network.
6
+
7
+ Rail contract: [`.ops/TERMS_RAIL.md`](../.ops/TERMS_RAIL.md) **§2.3** (PayChan) · **§2.4** (RLUSD). Specs: [`.ops/SPEC_PAYCHAN.md`](../.ops/SPEC_PAYCHAN.md), [`.ops/SPEC_RLUSD.md`](../.ops/SPEC_RLUSD.md).
8
+
9
+ ## Schemes
10
+
11
+ | Loop | Challenge | Sign (local) | Deliver to Sluzen | Latency (honest) |
12
+ |------|-----------|--------------|-------------------|------------------|
13
+ | A Payment XRP | `scheme: exact`, `asset: XRP` | `sign_payment_xrp` | `proxy_pay` or `facilitator_settle` | Ledger `submitAndWait` |
14
+ | B PayChan | `scheme: paychan`, `asset: XRP` | `sign_paychan_claim` | `proxy_pay` (JSON) or `facilitator_authorize` | Claim verify / authorize: sub-second possible |
15
+ | C Payment RLUSD | `scheme: exact`, `asset: RLUSD` + allowlisted `iou` | `sign_payment_rlusd` | same as A | Ledger `submitAndWait` |
16
+
17
+ Open `PaymentChannelCreate` and on-chain `PaymentChannelClaim` stay on the agent or issuer host. Sluzen verifies claims and debits prepaid take; it does not hold session keys or custody funds.
18
+
19
+ `GET /facilitator/supported` lists exact + paychan (XRP). RLUSD appears only on per-route challenges.
20
+
21
+ ## Custody ban
22
+
23
+ - Seeds: `CLIENT_WALLET_SEED` (or tool override) on the **local** MCP host.
24
+ - No MoR, refund, dispute, or Operator-held session keys.
25
+ - Prepaid / 0.8% take: already enforced by the proxy; this package does not reimplement fees.
26
+
27
+ ## Tools
28
+
29
+ | Tool | Role |
30
+ |------|------|
31
+ | `health` | `GET /health` |
32
+ | `facilitator_supported` | `GET /facilitator/supported` |
33
+ | `challenge` | Proxy unpaid path or `POST /facilitator/challenge` |
34
+ | `sign_payment_xrp` | Local exact XRP Payment blob |
35
+ | `sign_payment_rlusd` | Local allowlisted RLUSD IOU Payment blob (fail-closed) |
36
+ | `sign_paychan_claim` | Local off-chain claim + JSON `PAYMENT-SIGNATURE` |
37
+ | `proxy_pay` | Retry with `PAYMENT-SIGNATURE` |
38
+ | `facilitator_verify` | Read-only verify (exact or PayChan triad) |
39
+ | `facilitator_settle` | Exact only (`use_authorize` if PayChan JSON) |
40
+ | `facilitator_authorize` | PayChan debit + cursor |
41
+
42
+ Fail-closed when currency is not allowlisted, PayChan params are invalid, or local seed is missing.
43
+
44
+ ## Env
45
+
46
+ | Variable | Required | Notes |
47
+ |----------|----------|--------|
48
+ | `CLIENT_WALLET_SEED` | For sign tools | Local host only |
49
+ | `XRPL_NODE` | For exact Payment sign (autofill) | WebSocket rippled |
50
+ | `SLUZEN_API_BASE` | No | Default `https://api.sluzen.network` |
51
+
52
+ ## Claude Desktop (stdio)
53
+
54
+ ```json
55
+ {
56
+ "mcpServers": {
57
+ "sluzen": {
58
+ "command": "node",
59
+ "args": ["C:/Pons/sluzen-mcp/dist/bin/sluzen-mcp.js"],
60
+ "env": {
61
+ "CLIENT_WALLET_SEED": "(local seed)",
62
+ "XRPL_NODE": "wss://s.altnet.rippletest.net:51233",
63
+ "SLUZEN_API_BASE": "https://api.sluzen.network"
64
+ }
65
+ }
66
+ }
67
+ }
68
+ ```
69
+
70
+ Build first: `npm --prefix sluzen-mcp install && npm --prefix sluzen-mcp run build` (and `npm --prefix sluzen-sdk run build`).
71
+
72
+ ## AgentKit-style example
73
+
74
+ [`examples/paid-tool.ts`](examples/paid-tool.ts) picks scheme A/B/C from the challenge and pays via proxy or facilitator. No EVM / multi-rail co-branding.
75
+
76
+ ## Develop
77
+
78
+ ```bash
79
+ npm --prefix sluzen-sdk run build
80
+ npm --prefix sluzen-mcp install
81
+ npm --prefix sluzen-mcp run build
82
+ npm --prefix sluzen-mcp test
83
+ ```
84
+
85
+ ## License
86
+
87
+ ISC
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env node
2
+ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
3
+ import { createRuntime } from "../config.js";
4
+ import { createSluzenMcpServer } from "../server.js";
5
+ async function main() {
6
+ const runtime = createRuntime();
7
+ const server = createSluzenMcpServer(runtime);
8
+ const transport = new StdioServerTransport();
9
+ await server.connect(transport);
10
+ }
11
+ main().catch((err) => {
12
+ console.error(err instanceof Error ? err.message : String(err));
13
+ process.exit(1);
14
+ });
@@ -0,0 +1,17 @@
1
+ export declare const DEFAULT_API_BASE = "https://api.sluzen.network";
2
+ export type McpRuntimeConfig = {
3
+ apiBase: string;
4
+ walletSeed: string;
5
+ xrplNode: string | undefined;
6
+ fetchFn: typeof fetch;
7
+ };
8
+ export declare function resolveApiBase(override?: string): string;
9
+ export declare function resolveWalletSeed(override?: string): string;
10
+ export declare function resolveXrplNode(override?: string): string | undefined;
11
+ export declare function createRuntime(options?: {
12
+ apiBase?: string;
13
+ walletSeed?: string;
14
+ xrplNode?: string;
15
+ fetchFn?: typeof fetch;
16
+ }): McpRuntimeConfig;
17
+ export declare function requireSeed(seed: string): string;
package/dist/config.js ADDED
@@ -0,0 +1,30 @@
1
+ export const DEFAULT_API_BASE = "https://api.sluzen.network";
2
+ export function resolveApiBase(override) {
3
+ const raw = override?.trim() ||
4
+ process.env.SLUZEN_API_BASE?.trim() ||
5
+ DEFAULT_API_BASE;
6
+ return raw.replace(/\/$/, "");
7
+ }
8
+ export function resolveWalletSeed(override) {
9
+ return (override?.trim() ||
10
+ process.env.CLIENT_WALLET_SEED?.trim() ||
11
+ "");
12
+ }
13
+ export function resolveXrplNode(override) {
14
+ const node = override?.trim() || process.env.XRPL_NODE?.trim();
15
+ return node && node.length > 0 ? node : undefined;
16
+ }
17
+ export function createRuntime(options) {
18
+ return {
19
+ apiBase: resolveApiBase(options?.apiBase),
20
+ walletSeed: resolveWalletSeed(options?.walletSeed),
21
+ xrplNode: resolveXrplNode(options?.xrplNode),
22
+ fetchFn: options?.fetchFn ?? fetch,
23
+ };
24
+ }
25
+ export function requireSeed(seed) {
26
+ if (!seed || seed.length === 0) {
27
+ throw new Error("CLIENT_WALLET_SEED is required on the MCP host (local only; never send seed to Sluzen)");
28
+ }
29
+ return seed;
30
+ }
@@ -0,0 +1,3 @@
1
+ export { createRuntime, resolveApiBase, DEFAULT_API_BASE } from "./config.js";
2
+ export { createSluzenMcpServer } from "./server.js";
3
+ export * from "./tools.js";
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { createRuntime, resolveApiBase, DEFAULT_API_BASE } from "./config.js";
2
+ export { createSluzenMcpServer } from "./server.js";
3
+ export * from "./tools.js";
@@ -0,0 +1,8 @@
1
+ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2
+ import { type McpRuntimeConfig } from "./config.js";
3
+ /**
4
+ * Build the Sluzen MCP server (stdio transport attached by the bin).
5
+ * HTTP 402 schemes on XRPL (Payment | PayChan | allowlisted RLUSD).
6
+ * Private keys stay on the MCP host only.
7
+ */
8
+ export declare function createSluzenMcpServer(runtime?: McpRuntimeConfig): McpServer;
package/dist/server.js ADDED
@@ -0,0 +1,122 @@
1
+ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
2
+ import { z } from "zod";
3
+ import { createRuntime } from "./config.js";
4
+ import { toolChallenge, toolFacilitatorAuthorize, toolFacilitatorSettle, toolFacilitatorSupported, toolFacilitatorVerify, toolHealth, toolProxyPay, toolSignPaychanClaim, toolSignPaymentRlusd, toolSignPaymentXrp, } from "./tools.js";
5
+ function textResult(result) {
6
+ const payload = JSON.stringify(result, null, 2);
7
+ if (!result.ok) {
8
+ return {
9
+ content: [{ type: "text", text: payload }],
10
+ isError: true,
11
+ };
12
+ }
13
+ return {
14
+ content: [{ type: "text", text: payload }],
15
+ };
16
+ }
17
+ const challengeInput = z.union([z.string(), z.record(z.unknown())]);
18
+ /**
19
+ * Build the Sluzen MCP server (stdio transport attached by the bin).
20
+ * HTTP 402 schemes on XRPL (Payment | PayChan | allowlisted RLUSD).
21
+ * Private keys stay on the MCP host only.
22
+ */
23
+ export function createSluzenMcpServer(runtime = createRuntime()) {
24
+ const server = new McpServer({
25
+ name: "sluzen",
26
+ version: "0.1.0",
27
+ });
28
+ server.registerTool("health", {
29
+ description: "GET /health on the Sluzen API (rail open, network, supportsPayChan).",
30
+ inputSchema: {},
31
+ }, async () => textResult(await toolHealth(runtime)));
32
+ server.registerTool("facilitator_supported", {
33
+ description: "GET /facilitator/supported. Global kinds: exact XRP + paychan XRP. RLUSD is per-route only.",
34
+ inputSchema: {},
35
+ }, async () => textResult(await toolFacilitatorSupported(runtime)));
36
+ server.registerTool("challenge", {
37
+ description: "Issue an HTTP 402 PAYMENT-REQUIRED challenge (proxy path or facilitator resource). Returns decoded challenge + selectAccept scheme.",
38
+ inputSchema: {
39
+ surface: z.enum(["proxy", "facilitator"]),
40
+ path: z
41
+ .string()
42
+ .optional()
43
+ .describe("Proxy path starting with / (surface=proxy)"),
44
+ resource: z
45
+ .string()
46
+ .optional()
47
+ .describe("https facilitator resource URL (surface=facilitator)"),
48
+ channelId: z
49
+ .string()
50
+ .optional()
51
+ .describe("Optional PayChan channel id for facilitator challenge"),
52
+ method: z.string().optional().describe("HTTP method for proxy unpaid call"),
53
+ },
54
+ }, async (args) => textResult(await toolChallenge(args, runtime)));
55
+ server.registerTool("sign_payment_xrp", {
56
+ description: "Sign exact XRP Payment locally (CLIENT_WALLET_SEED on MCP host). Requires XRPL_NODE for autofill. Latency after submit: ledger submitAndWait.",
57
+ inputSchema: {
58
+ challenge: challengeInput.describe("Decoded challenge object or PAYMENT-REQUIRED base64"),
59
+ walletSeed: z
60
+ .string()
61
+ .optional()
62
+ .describe("Override seed; prefer CLIENT_WALLET_SEED env on host"),
63
+ },
64
+ }, async (args) => textResult(await toolSignPaymentXrp({
65
+ challenge: args.challenge,
66
+ walletSeed: args.walletSeed,
67
+ }, runtime)));
68
+ server.registerTool("sign_payment_rlusd", {
69
+ description: "Sign allowlisted RLUSD IOU Payment locally. Fail-closed if currency/issuer not allowlisted. Prepaid take stays XRP drops on the proxy.",
70
+ inputSchema: {
71
+ challenge: challengeInput,
72
+ walletSeed: z.string().optional(),
73
+ },
74
+ }, async (args) => textResult(await toolSignPaymentRlusd({
75
+ challenge: args.challenge,
76
+ walletSeed: args.walletSeed,
77
+ }, runtime)));
78
+ server.registerTool("sign_paychan_claim", {
79
+ description: "Sign off-chain PayChan claim locally (no ledger round-trip). Open channel and on-chain Claim stay on agent/issuer host.",
80
+ inputSchema: {
81
+ channelId: z.string(),
82
+ cumulativeDrops: z.string(),
83
+ walletSeed: z.string().optional(),
84
+ },
85
+ }, async (args) => textResult(toolSignPaychanClaim(args, runtime)));
86
+ server.registerTool("proxy_pay", {
87
+ description: "Retry a proxy path with PAYMENT-SIGNATURE (exact tx blob or PayChan JSON). Exact settle uses submitAndWait server-side; PayChan authorize path is off-chain claim verify.",
88
+ inputSchema: {
89
+ path: z.string(),
90
+ paymentSignature: z.string(),
91
+ method: z.string().optional(),
92
+ body: z.string().optional(),
93
+ },
94
+ }, async (args) => textResult(await toolProxyPay(args, runtime)));
95
+ server.registerTool("facilitator_verify", {
96
+ description: "POST /facilitator/verify (read-only). Exact: paymentSignature. PayChan: channelId + claimSignature + cumulativeDrops.",
97
+ inputSchema: {
98
+ resource: z.string(),
99
+ paymentSignature: z.string().optional(),
100
+ channelId: z.string().optional(),
101
+ claimSignature: z.string().optional(),
102
+ cumulativeDrops: z.string().optional(),
103
+ },
104
+ }, async (args) => textResult(await toolFacilitatorVerify(args, runtime)));
105
+ server.registerTool("facilitator_settle", {
106
+ description: "POST /facilitator/settle for exact Payment only (server submitAndWait). PayChan must use facilitator_authorize.",
107
+ inputSchema: {
108
+ resource: z.string(),
109
+ paymentSignature: z.string(),
110
+ },
111
+ }, async (args) => textResult(await toolFacilitatorSettle(args, runtime)));
112
+ server.registerTool("facilitator_authorize", {
113
+ description: "POST /facilitator/authorize for PayChan claim increment (debit take + advance cursor). Not for exact Payment.",
114
+ inputSchema: {
115
+ resource: z.string(),
116
+ channelId: z.string(),
117
+ claimSignature: z.string(),
118
+ cumulativeDrops: z.string(),
119
+ },
120
+ }, async (args) => textResult(await toolFacilitatorAuthorize(args, runtime)));
121
+ return server;
122
+ }
@@ -0,0 +1,84 @@
1
+ import { selectAccept, type X402Challenge } from "@sluzen/sdk";
2
+ import { type McpRuntimeConfig } from "./config.js";
3
+ export type ToolOk<T> = {
4
+ ok: true;
5
+ data: T;
6
+ };
7
+ export type ToolErr = {
8
+ ok: false;
9
+ error: string;
10
+ cause?: string;
11
+ };
12
+ export type ToolResult<T> = ToolOk<T> | ToolErr;
13
+ export declare function toolHealth(runtime?: McpRuntimeConfig): Promise<ToolResult<unknown>>;
14
+ export declare function toolFacilitatorSupported(runtime?: McpRuntimeConfig): Promise<ToolResult<unknown>>;
15
+ export declare function toolChallenge(args: {
16
+ surface: "proxy" | "facilitator";
17
+ path?: string;
18
+ resource?: string;
19
+ channelId?: string;
20
+ method?: string;
21
+ }, runtime?: McpRuntimeConfig): Promise<ToolResult<{
22
+ paymentRequired: string;
23
+ challenge: X402Challenge;
24
+ accept: ReturnType<typeof selectAccept>;
25
+ callId?: string;
26
+ status: number;
27
+ }>>;
28
+ export declare function toolSignPaymentXrp(args: {
29
+ challenge: X402Challenge | string;
30
+ walletSeed?: string;
31
+ }, runtime?: McpRuntimeConfig): Promise<ToolResult<{
32
+ paymentSignature: string;
33
+ }>>;
34
+ export declare function toolSignPaymentRlusd(args: {
35
+ challenge: X402Challenge | string;
36
+ walletSeed?: string;
37
+ }, runtime?: McpRuntimeConfig): Promise<ToolResult<{
38
+ paymentSignature: string;
39
+ }>>;
40
+ export declare function toolSignPaychanClaim(args: {
41
+ channelId: string;
42
+ cumulativeDrops: string;
43
+ walletSeed?: string;
44
+ }, runtime?: McpRuntimeConfig): ToolResult<{
45
+ channelId: string;
46
+ claimSignature: string;
47
+ cumulativeDrops: string;
48
+ paymentSignature: string;
49
+ publicKey: string;
50
+ account: string;
51
+ }>;
52
+ export declare function toolProxyPay(args: {
53
+ path: string;
54
+ paymentSignature: string;
55
+ method?: string;
56
+ body?: string;
57
+ }, runtime?: McpRuntimeConfig): Promise<ToolResult<{
58
+ status: number;
59
+ data: unknown;
60
+ paymentResponse?: string;
61
+ callId?: string;
62
+ }>>;
63
+ export declare function toolFacilitatorVerify(args: {
64
+ resource: string;
65
+ paymentSignature?: string;
66
+ channelId?: string;
67
+ claimSignature?: string;
68
+ cumulativeDrops?: string;
69
+ }, runtime?: McpRuntimeConfig): Promise<ToolResult<unknown>>;
70
+ export declare function toolFacilitatorSettle(args: {
71
+ resource: string;
72
+ paymentSignature: string;
73
+ }, runtime?: McpRuntimeConfig): Promise<ToolResult<unknown>>;
74
+ export declare function toolFacilitatorAuthorize(args: {
75
+ resource: string;
76
+ channelId: string;
77
+ claimSignature: string;
78
+ cumulativeDrops: string;
79
+ }, runtime?: McpRuntimeConfig): Promise<ToolResult<unknown>>;
80
+ /** Assert accept is PayChan with usable channel params (fail-closed helper). */
81
+ export declare function assertPayChanParams(challenge: X402Challenge): ToolResult<{
82
+ channelId: string;
83
+ minCumulativeDrops?: string;
84
+ }>;
package/dist/tools.js ADDED
@@ -0,0 +1,341 @@
1
+ import { decodeChallenge, encodePayChanPaymentSignature, facilitatorAuthorize, facilitatorChallenge, facilitatorSettle, facilitatorSupported, facilitatorVerify, isAllowlistedIou, isExactAccept, isPayChanAccept, isRlusdProductCurrency, isXrpDropsCurrency, PAYMENT_REQUIRED_HEADER, PAYMENT_RESPONSE_HEADER, PAYMENT_SIGNATURE_HEADER, CALL_ID_HEADER, selectAccept, signExactPayment, signPayChanClaim, } from "@sluzen/sdk";
2
+ import { createRuntime, requireSeed, } from "./config.js";
3
+ function fail(error, cause) {
4
+ return cause ? { ok: false, error, cause } : { ok: false, error };
5
+ }
6
+ function ok(data) {
7
+ return { ok: true, data };
8
+ }
9
+ function catchFail(err) {
10
+ if (err instanceof Error) {
11
+ const cause = "reason" in err && typeof err.reason === "string"
12
+ ? err.reason
13
+ : undefined;
14
+ return fail(err.message, cause);
15
+ }
16
+ return fail(String(err));
17
+ }
18
+ function parseChallengeJson(challenge) {
19
+ if (typeof challenge === "string") {
20
+ const trimmed = challenge.trim();
21
+ if (trimmed.startsWith("{")) {
22
+ return JSON.parse(trimmed);
23
+ }
24
+ return decodeChallenge(trimmed);
25
+ }
26
+ return challenge;
27
+ }
28
+ export async function toolHealth(runtime = createRuntime()) {
29
+ try {
30
+ const response = await runtime.fetchFn(`${runtime.apiBase}/health`);
31
+ const text = await response.text();
32
+ let body = text;
33
+ try {
34
+ body = text.length > 0 ? JSON.parse(text) : {};
35
+ }
36
+ catch {
37
+ /* keep text */
38
+ }
39
+ if (!response.ok) {
40
+ return fail(`health HTTP ${response.status}`);
41
+ }
42
+ return ok(body);
43
+ }
44
+ catch (err) {
45
+ return catchFail(err);
46
+ }
47
+ }
48
+ export async function toolFacilitatorSupported(runtime = createRuntime()) {
49
+ try {
50
+ const data = await facilitatorSupported({
51
+ apiBase: runtime.apiBase,
52
+ fetchFn: runtime.fetchFn,
53
+ });
54
+ return ok(data);
55
+ }
56
+ catch (err) {
57
+ return catchFail(err);
58
+ }
59
+ }
60
+ export async function toolChallenge(args, runtime = createRuntime()) {
61
+ try {
62
+ if (args.surface === "facilitator") {
63
+ const resource = args.resource?.trim();
64
+ if (!resource) {
65
+ return fail("resource is required for facilitator challenge");
66
+ }
67
+ const issued = await facilitatorChallenge({
68
+ apiBase: runtime.apiBase,
69
+ resource,
70
+ channelId: args.channelId,
71
+ fetchFn: runtime.fetchFn,
72
+ });
73
+ const challenge = decodeChallenge(issued.paymentRequired);
74
+ const accept = selectAccept(challenge);
75
+ return ok({
76
+ paymentRequired: issued.paymentRequired,
77
+ challenge,
78
+ accept,
79
+ callId: issued.callId ?? challenge.callId,
80
+ status: 402,
81
+ });
82
+ }
83
+ const path = args.path?.trim();
84
+ if (!path || !path.startsWith("/")) {
85
+ return fail("path starting with / is required for proxy challenge");
86
+ }
87
+ const method = (args.method ?? "GET").toUpperCase();
88
+ const response = await runtime.fetchFn(`${runtime.apiBase}${path}`, {
89
+ method,
90
+ });
91
+ const paymentRequired = response.headers.get(PAYMENT_REQUIRED_HEADER);
92
+ if (response.status !== 402 || !paymentRequired) {
93
+ return fail(response.status === 402
94
+ ? "402 without PAYMENT-REQUIRED (settlement/bind failure, not a quote)"
95
+ : `expected 402, got HTTP ${response.status}`, "challenge_failed");
96
+ }
97
+ const challenge = decodeChallenge(paymentRequired);
98
+ const accept = selectAccept(challenge);
99
+ const callId = response.headers.get(CALL_ID_HEADER) ?? challenge.callId ?? undefined;
100
+ return ok({
101
+ paymentRequired,
102
+ challenge,
103
+ accept,
104
+ callId,
105
+ status: 402,
106
+ });
107
+ }
108
+ catch (err) {
109
+ return catchFail(err);
110
+ }
111
+ }
112
+ export async function toolSignPaymentXrp(args, runtime = createRuntime()) {
113
+ try {
114
+ const seed = requireSeed(args.walletSeed ?? runtime.walletSeed);
115
+ const challenge = parseChallengeJson(args.challenge);
116
+ const accept = selectAccept(challenge);
117
+ if (!isExactAccept(accept) || accept.asset !== "XRP") {
118
+ return fail("challenge is not exact XRP Payment", "scheme_mismatch");
119
+ }
120
+ if (!isXrpDropsCurrency(challenge.currency)) {
121
+ return fail(`currency ${challenge.currency} is not XRP`, "iou_not_supported");
122
+ }
123
+ const paymentSignature = await signExactPayment({
124
+ challenge,
125
+ walletSeed: seed,
126
+ node: runtime.xrplNode,
127
+ });
128
+ return ok({ paymentSignature });
129
+ }
130
+ catch (err) {
131
+ return catchFail(err);
132
+ }
133
+ }
134
+ export async function toolSignPaymentRlusd(args, runtime = createRuntime()) {
135
+ try {
136
+ const seed = requireSeed(args.walletSeed ?? runtime.walletSeed);
137
+ const challenge = parseChallengeJson(args.challenge);
138
+ const accept = selectAccept(challenge);
139
+ if (!isExactAccept(accept) || accept.asset !== "RLUSD") {
140
+ return fail("challenge is not exact allowlisted RLUSD Payment", "scheme_mismatch");
141
+ }
142
+ if (!isRlusdProductCurrency(challenge.currency) || challenge.iou == null) {
143
+ return fail("RLUSD challenge missing iou", "iou_not_supported");
144
+ }
145
+ if (!isAllowlistedIou({
146
+ currency: challenge.iou.currency,
147
+ issuer: challenge.iou.issuer,
148
+ network: challenge.network,
149
+ })) {
150
+ return fail("IOU issuer/currency not on Sluzen allowlist", "iou_not_supported");
151
+ }
152
+ const paymentSignature = await signExactPayment({
153
+ challenge,
154
+ walletSeed: seed,
155
+ node: runtime.xrplNode,
156
+ });
157
+ return ok({ paymentSignature });
158
+ }
159
+ catch (err) {
160
+ return catchFail(err);
161
+ }
162
+ }
163
+ export function toolSignPaychanClaim(args, runtime = createRuntime()) {
164
+ try {
165
+ const seed = requireSeed(args.walletSeed ?? runtime.walletSeed);
166
+ const channelId = args.channelId.trim().toUpperCase();
167
+ if (!/^[0-9A-F]{64}$/.test(channelId)) {
168
+ return fail("channelId must be 64 hex characters", "invalid_channel_id");
169
+ }
170
+ const claim = signPayChanClaim({
171
+ channelId,
172
+ cumulativeDrops: args.cumulativeDrops,
173
+ walletSeed: seed,
174
+ });
175
+ const paymentSignature = encodePayChanPaymentSignature(claim);
176
+ return ok({
177
+ channelId: claim.channelId,
178
+ claimSignature: claim.claimSignature,
179
+ cumulativeDrops: claim.cumulativeDrops,
180
+ paymentSignature,
181
+ publicKey: claim.publicKey,
182
+ account: claim.account,
183
+ });
184
+ }
185
+ catch (err) {
186
+ return catchFail(err);
187
+ }
188
+ }
189
+ export async function toolProxyPay(args, runtime = createRuntime()) {
190
+ try {
191
+ const path = args.path.trim();
192
+ if (!path.startsWith("/")) {
193
+ return fail("path must start with /");
194
+ }
195
+ if (!args.paymentSignature || args.paymentSignature.trim().length === 0) {
196
+ return fail("paymentSignature is required");
197
+ }
198
+ const method = (args.method ?? "GET").toUpperCase();
199
+ const headers = new Headers({
200
+ [PAYMENT_SIGNATURE_HEADER]: args.paymentSignature,
201
+ });
202
+ const init = { method, headers };
203
+ if (args.body != null && method !== "GET" && method !== "HEAD") {
204
+ headers.set("content-type", "application/json");
205
+ init.body = args.body;
206
+ }
207
+ const response = await runtime.fetchFn(`${runtime.apiBase}${path}`, init);
208
+ const text = await response.text();
209
+ let data = text;
210
+ const ct = response.headers.get("content-type") ?? "";
211
+ if (ct.includes("application/json") && text.length > 0) {
212
+ try {
213
+ data = JSON.parse(text);
214
+ }
215
+ catch {
216
+ /* keep text */
217
+ }
218
+ }
219
+ return ok({
220
+ status: response.status,
221
+ data,
222
+ paymentResponse: response.headers.get(PAYMENT_RESPONSE_HEADER) ?? undefined,
223
+ callId: response.headers.get(CALL_ID_HEADER) ?? undefined,
224
+ });
225
+ }
226
+ catch (err) {
227
+ return catchFail(err);
228
+ }
229
+ }
230
+ export async function toolFacilitatorVerify(args, runtime = createRuntime()) {
231
+ try {
232
+ const resource = args.resource.trim();
233
+ if (!resource) {
234
+ return fail("resource is required");
235
+ }
236
+ if (args.paymentSignature != null && args.paymentSignature.length > 0) {
237
+ const verified = await facilitatorVerify({
238
+ apiBase: runtime.apiBase,
239
+ resource,
240
+ paymentSignature: args.paymentSignature,
241
+ fetchFn: runtime.fetchFn,
242
+ });
243
+ return ok(verified);
244
+ }
245
+ if (args.channelId &&
246
+ args.claimSignature &&
247
+ args.cumulativeDrops != null) {
248
+ const verified = await facilitatorVerify({
249
+ apiBase: runtime.apiBase,
250
+ resource,
251
+ channelId: args.channelId,
252
+ claimSignature: args.claimSignature,
253
+ cumulativeDrops: args.cumulativeDrops,
254
+ fetchFn: runtime.fetchFn,
255
+ });
256
+ return ok(verified);
257
+ }
258
+ return fail("provide paymentSignature (exact) or channelId+claimSignature+cumulativeDrops (PayChan)", "missing_payment_signature");
259
+ }
260
+ catch (err) {
261
+ return catchFail(err);
262
+ }
263
+ }
264
+ export async function toolFacilitatorSettle(args, runtime = createRuntime()) {
265
+ try {
266
+ if (!args.paymentSignature?.trim()) {
267
+ return fail("paymentSignature is required");
268
+ }
269
+ // Detect accidental PayChan JSON blob
270
+ const trimmed = args.paymentSignature.trim();
271
+ if (trimmed.startsWith("{")) {
272
+ try {
273
+ const parsed = JSON.parse(trimmed);
274
+ if (typeof parsed.channelId === "string") {
275
+ return fail("PayChan routes use facilitator_authorize, not settle", "use_authorize");
276
+ }
277
+ }
278
+ catch {
279
+ /* not JSON */
280
+ }
281
+ }
282
+ const settled = await facilitatorSettle({
283
+ apiBase: runtime.apiBase,
284
+ resource: args.resource,
285
+ paymentSignature: args.paymentSignature,
286
+ fetchFn: runtime.fetchFn,
287
+ handler: () => null,
288
+ });
289
+ return ok({
290
+ txHash: settled.txHash,
291
+ callId: settled.callId,
292
+ network: settled.network,
293
+ });
294
+ }
295
+ catch (err) {
296
+ return catchFail(err);
297
+ }
298
+ }
299
+ export async function toolFacilitatorAuthorize(args, runtime = createRuntime()) {
300
+ try {
301
+ const channelId = args.channelId.trim().toUpperCase();
302
+ if (!/^[0-9A-F]{64}$/.test(channelId)) {
303
+ return fail("channelId must be 64 hex characters", "invalid_channel_id");
304
+ }
305
+ if (!args.claimSignature?.trim()) {
306
+ return fail("claimSignature is required");
307
+ }
308
+ const authorized = await facilitatorAuthorize({
309
+ apiBase: runtime.apiBase,
310
+ resource: args.resource,
311
+ channelId,
312
+ claimSignature: args.claimSignature,
313
+ cumulativeDrops: args.cumulativeDrops,
314
+ fetchFn: runtime.fetchFn,
315
+ });
316
+ return ok(authorized);
317
+ }
318
+ catch (err) {
319
+ return catchFail(err);
320
+ }
321
+ }
322
+ /** Assert accept is PayChan with usable channel params (fail-closed helper). */
323
+ export function assertPayChanParams(challenge) {
324
+ try {
325
+ const accept = selectAccept(challenge);
326
+ if (!isPayChanAccept(accept)) {
327
+ return fail("challenge is not PayChan", "scheme_mismatch");
328
+ }
329
+ const channelId = accept.extra.channelId?.trim().toUpperCase();
330
+ if (!channelId || !/^[0-9A-F]{64}$/.test(channelId)) {
331
+ return fail("PayChan challenge missing valid channelId (open channel first on agent host)", "invalid_channel_id");
332
+ }
333
+ return ok({
334
+ channelId,
335
+ minCumulativeDrops: accept.extra.minCumulativeDrops,
336
+ });
337
+ }
338
+ catch (err) {
339
+ return catchFail(err);
340
+ }
341
+ }
@@ -0,0 +1,170 @@
1
+ /**
2
+ * AgentKit-style "paid tool" snippet over live Sluzen rails.
3
+ *
4
+ * HTTP 402 schemes on XRPL (Payment | PayChan | allowlisted RLUSD).
5
+ * Contract: TERMS_RAIL §2.3 (PayChan) · §2.4 (RLUSD) — see ../../.ops/TERMS_RAIL.md
6
+ *
7
+ * Run (after npm run build in sluzen-mcp):
8
+ * CLIENT_WALLET_SEED=... XRPL_NODE=wss://... npx tsx examples/paid-tool.ts /api/v1/investor-demo
9
+ *
10
+ * No custody on Sluzen. Seed stays on this host. No MoR / refund / dispute.
11
+ */
12
+ import {
13
+ createRuntime,
14
+ toolChallenge,
15
+ toolFacilitatorAuthorize,
16
+ toolFacilitatorSettle,
17
+ toolProxyPay,
18
+ toolSignPaychanClaim,
19
+ toolSignPaymentRlusd,
20
+ toolSignPaymentXrp,
21
+ } from "../src/index.js";
22
+ import { isExactAccept, isPayChanAccept, selectAccept } from "@sluzen/sdk";
23
+
24
+ async function paidProxyCall(path: string): Promise<unknown> {
25
+ const runtime = createRuntime();
26
+ const challenged = await toolChallenge({ surface: "proxy", path }, runtime);
27
+ if (!challenged.ok) {
28
+ throw new Error(challenged.error);
29
+ }
30
+
31
+ const { challenge, accept } = challenged.data;
32
+ const scheme = selectAccept(challenge);
33
+
34
+ if (isPayChanAccept(scheme) || isPayChanAccept(accept)) {
35
+ // Loop B: open channel on agent host first; then N signed claims.
36
+ const channelId = accept.scheme === "paychan" ? accept.extra.channelId : undefined;
37
+ if (!channelId) {
38
+ throw new Error(
39
+ "PayChan challenge missing channelId (open PaymentChannelCreate on agent host)",
40
+ );
41
+ }
42
+ const prev =
43
+ accept.scheme === "paychan"
44
+ ? BigInt(accept.extra.prevAcceptedCumulative ?? "0")
45
+ : 0n;
46
+ const step = BigInt(
47
+ accept.scheme === "paychan" ? accept.maxAmountRequired : "0",
48
+ );
49
+ const cumulativeDrops = (prev + step).toString();
50
+ const signed = toolSignPaychanClaim(
51
+ { channelId, cumulativeDrops },
52
+ runtime,
53
+ );
54
+ if (!signed.ok) {
55
+ throw new Error(signed.error);
56
+ }
57
+ const paid = await toolProxyPay(
58
+ { path, paymentSignature: signed.data.paymentSignature },
59
+ runtime,
60
+ );
61
+ if (!paid.ok) {
62
+ throw new Error(paid.error);
63
+ }
64
+ return paid.data;
65
+ }
66
+
67
+ if (isExactAccept(scheme) && scheme.asset === "RLUSD") {
68
+ // Loop C: allowlisted RLUSD IOU Payment (ledger submitAndWait).
69
+ const signed = await toolSignPaymentRlusd({ challenge }, runtime);
70
+ if (!signed.ok) {
71
+ throw new Error(signed.error);
72
+ }
73
+ const paid = await toolProxyPay(
74
+ { path, paymentSignature: signed.data.paymentSignature },
75
+ runtime,
76
+ );
77
+ if (!paid.ok) {
78
+ throw new Error(paid.error);
79
+ }
80
+ return paid.data;
81
+ }
82
+
83
+ // Loop A: exact XRP Payment per call (ledger submitAndWait).
84
+ const signed = await toolSignPaymentXrp({ challenge }, runtime);
85
+ if (!signed.ok) {
86
+ throw new Error(signed.error);
87
+ }
88
+ const paid = await toolProxyPay(
89
+ { path, paymentSignature: signed.data.paymentSignature },
90
+ runtime,
91
+ );
92
+ if (!paid.ok) {
93
+ throw new Error(paid.error);
94
+ }
95
+ return paid.data;
96
+ }
97
+
98
+ /** Facilitator surface: settle (exact) or authorize (PayChan). */
99
+ async function paidFacilitatorCall(resource: string): Promise<unknown> {
100
+ const runtime = createRuntime();
101
+ const challenged = await toolChallenge(
102
+ { surface: "facilitator", resource },
103
+ runtime,
104
+ );
105
+ if (!challenged.ok) {
106
+ throw new Error(challenged.error);
107
+ }
108
+ const { challenge, accept } = challenged.data;
109
+
110
+ if (isPayChanAccept(accept)) {
111
+ const channelId = accept.extra.channelId;
112
+ if (!channelId) {
113
+ throw new Error("PayChan challenge missing channelId");
114
+ }
115
+ const prev = BigInt(accept.extra.prevAcceptedCumulative ?? "0");
116
+ const cumulativeDrops = (prev + BigInt(accept.maxAmountRequired)).toString();
117
+ const signed = toolSignPaychanClaim(
118
+ { channelId, cumulativeDrops },
119
+ runtime,
120
+ );
121
+ if (!signed.ok) {
122
+ throw new Error(signed.error);
123
+ }
124
+ return toolFacilitatorAuthorize(
125
+ {
126
+ resource,
127
+ channelId: signed.data.channelId,
128
+ claimSignature: signed.data.claimSignature,
129
+ cumulativeDrops: signed.data.cumulativeDrops,
130
+ },
131
+ runtime,
132
+ );
133
+ }
134
+
135
+ if (isExactAccept(accept) && accept.asset === "RLUSD") {
136
+ const signed = await toolSignPaymentRlusd({ challenge }, runtime);
137
+ if (!signed.ok) {
138
+ throw new Error(signed.error);
139
+ }
140
+ return toolFacilitatorSettle(
141
+ { resource, paymentSignature: signed.data.paymentSignature },
142
+ runtime,
143
+ );
144
+ }
145
+
146
+ const signed = await toolSignPaymentXrp({ challenge }, runtime);
147
+ if (!signed.ok) {
148
+ throw new Error(signed.error);
149
+ }
150
+ return toolFacilitatorSettle(
151
+ { resource, paymentSignature: signed.data.paymentSignature },
152
+ runtime,
153
+ );
154
+ }
155
+
156
+ async function main(): Promise<void> {
157
+ const path = process.argv[2] ?? "/api/v1/investor-demo";
158
+ if (path.startsWith("https://")) {
159
+ const result = await paidFacilitatorCall(path);
160
+ console.log(JSON.stringify(result, null, 2));
161
+ return;
162
+ }
163
+ const result = await paidProxyCall(path);
164
+ console.log(JSON.stringify(result, null, 2));
165
+ }
166
+
167
+ main().catch((err) => {
168
+ console.error(err instanceof Error ? err.message : String(err));
169
+ process.exit(1);
170
+ });
package/package.json CHANGED
@@ -1,6 +1,40 @@
1
1
  {
2
2
  "name": "@sluzen/mcp",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Sluzen MCP server: HTTP 402 schemes on XRPL (Payment | PayChan | allowlisted RLUSD). Local seed only.",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "bin": {
9
+ "sluzen-mcp": "dist/bin/sluzen-mcp.js"
10
+ },
11
+ "files": [
12
+ "dist",
13
+ "README.md",
14
+ "examples"
15
+ ],
16
+ "scripts": {
17
+ "build": "tsc",
18
+ "start": "node dist/bin/sluzen-mcp.js",
19
+ "test": "vitest run",
20
+ "prepublishOnly": "npm run build"
21
+ },
22
+ "engines": {
23
+ "node": ">=20"
24
+ },
25
+ "author": "Vectis",
26
+ "license": "ISC",
27
+ "publishConfig": {
28
+ "access": "public"
29
+ },
30
+ "dependencies": {
31
+ "@modelcontextprotocol/sdk": "^1.32.1",
32
+ "@sluzen/sdk": "^1.7.0",
33
+ "zod": "^3.25.76"
34
+ },
35
+ "devDependencies": {
36
+ "@types/node": "^22.15.0",
37
+ "typescript": "^6.0.3",
38
+ "vitest": "^4.1.9"
39
+ }
40
+ }