@gvnrdao/dh-mcp 0.0.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/.env.example +22 -0
- package/README.md +39 -0
- package/dist/index.js +1575 -0
- package/package.json +26 -0
- package/src/actions/index.ts +13 -0
- package/src/actions/loan-create.action.ts +28 -0
- package/src/actions/loan-extend.action.ts +17 -0
- package/src/actions/loan-mint.action.ts +17 -0
- package/src/actions/loan-pay.action.ts +17 -0
- package/src/actions/loan-withdraw.action.ts +17 -0
- package/src/actions/types.ts +33 -0
- package/src/config.ts +43 -0
- package/src/context.ts +87 -0
- package/src/index.ts +23 -0
- package/src/resources/position.ts +39 -0
- package/src/resources/protocol.ts +57 -0
- package/src/server.ts +58 -0
- package/src/signer/eoa-dh.ts +35 -0
- package/src/signer/index.ts +12 -0
- package/src/signer/safe-module-signer-adapter.ts +61 -0
- package/src/signer/safe-module.ts +110 -0
- package/src/signer/types.ts +12 -0
- package/src/tools/_helpers.ts +47 -0
- package/src/tools/btc.ts +38 -0
- package/src/tools/loan.ts +197 -0
- package/src/tools/position.ts +93 -0
- package/src/tools/protocol.ts +35 -0
- package/src/util/btc-address.ts +58 -0
- package/src/util/eip712-login.ts +65 -0
- package/src/util/errors.ts +24 -0
- package/src/util/extend-fee-bound.ts +51 -0
- package/src/util/format.ts +9 -0
- package/src/util/network.ts +34 -0
- package/src/util/server-fetch.ts +27 -0
- package/src/util/server-mint.ts +108 -0
- package/src/util/server-renew.ts +107 -0
- package/src/util/server-repay.ts +118 -0
- package/src/util/server-session.ts +108 -0
- package/src/util/server-withdraw.ts +34 -0
- package/src/util/tx-validator.ts +231 -0
- package/src/util/validate.ts +41 -0
- package/tsconfig.json +20 -0
- package/tsup.config.ts +11 -0
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gvnrdao/dh-mcp",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "MCP server for Diamond Hands Protocol",
|
|
5
|
+
"bin": {
|
|
6
|
+
"dh-mcp": "./dist/index.js"
|
|
7
|
+
},
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsup",
|
|
10
|
+
"dev": "tsup --watch",
|
|
11
|
+
"typecheck": "tsc --noEmit",
|
|
12
|
+
"prepublishOnly": "npm run typecheck && npm run build"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"@gvnrdao/dh-sdk": "^0.0.284",
|
|
16
|
+
"@modelcontextprotocol/sdk": "^1.29.0",
|
|
17
|
+
"bitcoinjs-lib": "^6.1.7",
|
|
18
|
+
"ethers": "^6.13.0",
|
|
19
|
+
"zod": "^3.23.0"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@types/node": "^20.0.0",
|
|
23
|
+
"tsup": "^8.4.0",
|
|
24
|
+
"typescript": "^5.9.3"
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { createPositionAction } from "./loan-create.action.js";
|
|
2
|
+
export { mintAction } from "./loan-mint.action.js";
|
|
3
|
+
export { payAction } from "./loan-pay.action.js";
|
|
4
|
+
export { extendAction } from "./loan-extend.action.js";
|
|
5
|
+
export { withdrawAction } from "./loan-withdraw.action.js";
|
|
6
|
+
export type {
|
|
7
|
+
CreatePositionParams,
|
|
8
|
+
CreatePositionResult,
|
|
9
|
+
MintParams,
|
|
10
|
+
PayParams,
|
|
11
|
+
ExtendParams,
|
|
12
|
+
WithdrawBtcParams,
|
|
13
|
+
} from "./types.js";
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { Ctx } from "../context.js";
|
|
2
|
+
import { isSuccess } from "@gvnrdao/dh-sdk";
|
|
3
|
+
import { assertSelectedTerm } from "../util/validate.js";
|
|
4
|
+
import type { CreatePositionParams, CreatePositionResult } from "./types.js";
|
|
5
|
+
|
|
6
|
+
export async function createPositionAction(
|
|
7
|
+
ctx: Ctx,
|
|
8
|
+
params: CreatePositionParams,
|
|
9
|
+
): Promise<CreatePositionResult> {
|
|
10
|
+
assertSelectedTerm(params.selectedTerm);
|
|
11
|
+
|
|
12
|
+
const result = await ctx.sdk.createLoan({ selectedTerm: params.selectedTerm });
|
|
13
|
+
if (!isSuccess(result)) {
|
|
14
|
+
return { success: false, error: result.error.message };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const loan = result.value;
|
|
18
|
+
if (!loan.success) {
|
|
19
|
+
return { success: false, error: loan.error ?? "Loan creation failed." };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
success: true,
|
|
24
|
+
positionId: loan.positionId,
|
|
25
|
+
vaultAddress: loan.vaultAddress,
|
|
26
|
+
transactionHash: loan.transactionHash,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Ctx } from "../context.js";
|
|
2
|
+
import { networkToChainId } from "../config.js";
|
|
3
|
+
import { renewViaServer, type ServerRenewResult } from "../util/server-renew.js";
|
|
4
|
+
import { assertPositionIdHex, assertSelectedTerm } from "../util/validate.js";
|
|
5
|
+
import type { ExtendParams } from "./types.js";
|
|
6
|
+
|
|
7
|
+
export async function extendAction(ctx: Ctx, params: ExtendParams): Promise<ServerRenewResult> {
|
|
8
|
+
assertPositionIdHex(params.positionId);
|
|
9
|
+
assertSelectedTerm(params.selectedTerm);
|
|
10
|
+
|
|
11
|
+
const chainId = networkToChainId(ctx.config.network);
|
|
12
|
+
if (!ctx.dhSigner) {
|
|
13
|
+
return { success: false, error: "extendAction requires a signer (set DH_PRIVATE_KEY)" };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return renewViaServer(ctx.dhSigner, ctx.config, chainId, params.positionId, params.selectedTerm);
|
|
17
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Ctx } from "../context.js";
|
|
2
|
+
import { networkToChainId } from "../config.js";
|
|
3
|
+
import { mintViaServer, type ServerMintResult } from "../util/server-mint.js";
|
|
4
|
+
import { assertPositionIdHex, assertUcdAmount } from "../util/validate.js";
|
|
5
|
+
import type { MintParams } from "./types.js";
|
|
6
|
+
|
|
7
|
+
export async function mintAction(ctx: Ctx, params: MintParams): Promise<ServerMintResult> {
|
|
8
|
+
assertPositionIdHex(params.positionId);
|
|
9
|
+
assertUcdAmount(params.amountUcd);
|
|
10
|
+
|
|
11
|
+
const chainId = networkToChainId(ctx.config.network);
|
|
12
|
+
if (!ctx.dhSigner) {
|
|
13
|
+
return { success: false, error: "mintAction requires a signer (set DH_PRIVATE_KEY)" };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return mintViaServer(ctx.dhSigner, ctx.config, chainId, params.positionId, params.amountUcd);
|
|
17
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Ctx } from "../context.js";
|
|
2
|
+
import { networkToChainId } from "../config.js";
|
|
3
|
+
import { repayViaServer, type ServerRepayResult } from "../util/server-repay.js";
|
|
4
|
+
import { assertPositionIdHex, assertUcdAmount } from "../util/validate.js";
|
|
5
|
+
import type { PayParams } from "./types.js";
|
|
6
|
+
|
|
7
|
+
export async function payAction(ctx: Ctx, params: PayParams): Promise<ServerRepayResult> {
|
|
8
|
+
assertPositionIdHex(params.positionId);
|
|
9
|
+
assertUcdAmount(params.amountUcd, "Payment amount");
|
|
10
|
+
|
|
11
|
+
const chainId = networkToChainId(ctx.config.network);
|
|
12
|
+
if (!ctx.dhSigner) {
|
|
13
|
+
return { success: false, error: "payAction requires a signer (set DH_PRIVATE_KEY)" };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return repayViaServer(ctx.dhSigner, ctx.config, chainId, params.positionId, params.amountUcd);
|
|
17
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Ctx } from "../context.js";
|
|
2
|
+
import { networkToChainId } from "../config.js";
|
|
3
|
+
import { withdrawViaServer, type ServerWithdrawResult } from "../util/server-withdraw.js";
|
|
4
|
+
import { assertPositionIdHex, assertSatsAmount } from "../util/validate.js";
|
|
5
|
+
import { validateBtcAddress, btcNetworkFor } from "../util/btc-address.js";
|
|
6
|
+
import type { WithdrawBtcParams } from "./types.js";
|
|
7
|
+
|
|
8
|
+
export async function withdrawAction(ctx: Ctx, params: WithdrawBtcParams): Promise<ServerWithdrawResult> {
|
|
9
|
+
assertPositionIdHex(params.positionId);
|
|
10
|
+
assertSatsAmount(params.amountSats);
|
|
11
|
+
|
|
12
|
+
const chainId = networkToChainId(ctx.config.network);
|
|
13
|
+
const expectedBtcNet = btcNetworkFor(ctx.config.network, chainId);
|
|
14
|
+
validateBtcAddress(params.btcAddress, expectedBtcNet);
|
|
15
|
+
|
|
16
|
+
return withdrawViaServer(ctx.sdk, params.positionId, params.btcAddress, params.amountSats, params.networkFeeSats);
|
|
17
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export interface MintParams {
|
|
2
|
+
positionId: string;
|
|
3
|
+
amountUcd: number;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface PayParams {
|
|
7
|
+
positionId: string;
|
|
8
|
+
amountUcd: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface ExtendParams {
|
|
12
|
+
positionId: string;
|
|
13
|
+
selectedTerm: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface WithdrawBtcParams {
|
|
17
|
+
positionId: string;
|
|
18
|
+
btcAddress: string;
|
|
19
|
+
amountSats: number;
|
|
20
|
+
networkFeeSats?: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface CreatePositionParams {
|
|
24
|
+
selectedTerm: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface CreatePositionResult {
|
|
28
|
+
success: boolean;
|
|
29
|
+
positionId?: string;
|
|
30
|
+
vaultAddress?: string;
|
|
31
|
+
transactionHash?: string;
|
|
32
|
+
error?: string;
|
|
33
|
+
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export type SafeMode = "none" | "module";
|
|
2
|
+
|
|
3
|
+
export interface McpConfig {
|
|
4
|
+
network: string;
|
|
5
|
+
rpcUrl?: string;
|
|
6
|
+
serviceEndpoint: string;
|
|
7
|
+
litNetwork: string;
|
|
8
|
+
validatorVersion?: number;
|
|
9
|
+
safeMode: SafeMode;
|
|
10
|
+
safeAddress?: string;
|
|
11
|
+
agentModuleAddress?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function resolveConfig(): McpConfig {
|
|
15
|
+
return {
|
|
16
|
+
network: process.env["DH_NETWORK"] ?? "sepolia",
|
|
17
|
+
rpcUrl: process.env["ETH_RPC_URL"] ?? process.env["RPC_URL"],
|
|
18
|
+
serviceEndpoint:
|
|
19
|
+
process.env["DH_SERVICE_ENDPOINT"] ??
|
|
20
|
+
process.env["LIT_OPS_ENDPOINT"] ??
|
|
21
|
+
"http://localhost:3005",
|
|
22
|
+
litNetwork: process.env["DH_LIT_NETWORK"] ?? "chipotle",
|
|
23
|
+
validatorVersion: process.env["DH_VALIDATOR_VERSION"]
|
|
24
|
+
? parseInt(process.env["DH_VALIDATOR_VERSION"], 10)
|
|
25
|
+
: undefined,
|
|
26
|
+
safeMode: (process.env["DH_SAFE_MODE"] as SafeMode | undefined) ?? "none",
|
|
27
|
+
safeAddress: process.env["DH_SAFE_ADDRESS"],
|
|
28
|
+
agentModuleAddress: process.env["DH_AGENT_MODULE"],
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function networkToChainId(network: string): number {
|
|
33
|
+
const map: Record<string, number> = {
|
|
34
|
+
mainnet: 1,
|
|
35
|
+
ethereum: 1,
|
|
36
|
+
sepolia: 11155111,
|
|
37
|
+
hardhat: 31337,
|
|
38
|
+
local: 31337,
|
|
39
|
+
};
|
|
40
|
+
const id = map[network.toLowerCase()];
|
|
41
|
+
if (!id) throw new Error(`Unknown network: ${network}`);
|
|
42
|
+
return id;
|
|
43
|
+
}
|
package/src/context.ts
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { ethers } from "ethers";
|
|
2
|
+
import { DiamondHandsSDK } from "@gvnrdao/dh-sdk";
|
|
3
|
+
import type { DiamondHandsSDKConfig } from "@gvnrdao/dh-sdk";
|
|
4
|
+
import { resolveConfig, networkToChainId } from "./config.js";
|
|
5
|
+
import type { McpConfig } from "./config.js";
|
|
6
|
+
import { loadEnvSigner } from "./signer/index.js";
|
|
7
|
+
import { EoaDHSigner } from "./signer/eoa-dh.js";
|
|
8
|
+
import { SafeModuleDHSigner } from "./signer/safe-module.js";
|
|
9
|
+
import { SafeModuleSignerAdapter } from "./signer/safe-module-signer-adapter.js";
|
|
10
|
+
import type { DHSigner } from "./signer/types.js";
|
|
11
|
+
import { assertSafeServiceEndpoint } from "./util/network.js";
|
|
12
|
+
|
|
13
|
+
export interface Ctx {
|
|
14
|
+
sdk: DiamondHandsSDK;
|
|
15
|
+
config: McpConfig;
|
|
16
|
+
chainId: number;
|
|
17
|
+
signer?: ethers.Signer;
|
|
18
|
+
dhSigner?: DHSigner;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function buildContext(requireSigner: boolean): Promise<Ctx> {
|
|
22
|
+
const config = resolveConfig();
|
|
23
|
+
assertSafeServiceEndpoint(config.serviceEndpoint);
|
|
24
|
+
|
|
25
|
+
const chainId = networkToChainId(config.network);
|
|
26
|
+
|
|
27
|
+
const provider = config.rpcUrl
|
|
28
|
+
? new ethers.JsonRpcProvider(config.rpcUrl, chainId)
|
|
29
|
+
: ethers.getDefaultProvider(chainId === 1 ? "homestead" : config.network);
|
|
30
|
+
|
|
31
|
+
let contractSigner: ethers.Signer | undefined;
|
|
32
|
+
if (requireSigner) {
|
|
33
|
+
contractSigner = loadEnvSigner(provider as ethers.Provider);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let dhSigner: DHSigner | undefined;
|
|
37
|
+
let sdkContractSigner: ethers.Signer | undefined = contractSigner;
|
|
38
|
+
|
|
39
|
+
if (contractSigner) {
|
|
40
|
+
if (config.safeMode === "module") {
|
|
41
|
+
if (!config.safeAddress || !config.agentModuleAddress) {
|
|
42
|
+
throw new Error(
|
|
43
|
+
"DH_SAFE_MODE=module but DH_SAFE_ADDRESS / DH_AGENT_MODULE are not set.",
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
const safeModuleDh = new SafeModuleDHSigner(
|
|
47
|
+
contractSigner,
|
|
48
|
+
config.safeAddress,
|
|
49
|
+
config.agentModuleAddress,
|
|
50
|
+
);
|
|
51
|
+
dhSigner = safeModuleDh;
|
|
52
|
+
sdkContractSigner = new SafeModuleSignerAdapter(
|
|
53
|
+
safeModuleDh,
|
|
54
|
+
config.safeAddress,
|
|
55
|
+
provider as ethers.Provider,
|
|
56
|
+
);
|
|
57
|
+
} else {
|
|
58
|
+
dhSigner = new EoaDHSigner(contractSigner);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const sdkConfig: DiamondHandsSDKConfig = {
|
|
63
|
+
mode: "service",
|
|
64
|
+
serviceEndpoint: config.serviceEndpoint,
|
|
65
|
+
contractSigner: sdkContractSigner,
|
|
66
|
+
authSigner: contractSigner,
|
|
67
|
+
provider,
|
|
68
|
+
ethRpcUrl: config.rpcUrl,
|
|
69
|
+
chainId,
|
|
70
|
+
litNetwork: config.litNetwork as DiamondHandsSDKConfig["litNetwork"],
|
|
71
|
+
...(config.validatorVersion !== undefined && {
|
|
72
|
+
validators: {
|
|
73
|
+
loanCreation: config.validatorVersion,
|
|
74
|
+
minting: config.validatorVersion,
|
|
75
|
+
payment: config.validatorVersion,
|
|
76
|
+
extension: config.validatorVersion,
|
|
77
|
+
},
|
|
78
|
+
}),
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const result = await DiamondHandsSDK.create(sdkConfig);
|
|
82
|
+
if (!result.success) {
|
|
83
|
+
throw new Error(`SDK initialization failed: ${result.error.message}`);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return { sdk: result.value, config, chainId, signer: contractSigner, dhSigner };
|
|
87
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
2
|
+
import { createServer, disposeCachedContext } from "./server.js";
|
|
3
|
+
|
|
4
|
+
async function main(): Promise<void> {
|
|
5
|
+
const server = createServer();
|
|
6
|
+
const transport = new StdioServerTransport();
|
|
7
|
+
|
|
8
|
+
// Stdin/stdout are owned by MCP. Everything human-readable goes to stderr.
|
|
9
|
+
const cleanup = (): void => {
|
|
10
|
+
disposeCachedContext();
|
|
11
|
+
process.exit(0);
|
|
12
|
+
};
|
|
13
|
+
process.on("SIGINT", cleanup);
|
|
14
|
+
process.on("SIGTERM", cleanup);
|
|
15
|
+
|
|
16
|
+
await server.connect(transport);
|
|
17
|
+
process.stderr.write("[dh-mcp] Diamond Hands MCP server connected over stdio.\n");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
main().catch((err) => {
|
|
21
|
+
process.stderr.write(`[dh-mcp] fatal: ${(err as Error).stack ?? (err as Error).message}\n`);
|
|
22
|
+
process.exit(1);
|
|
23
|
+
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import { ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
3
|
+
import type { Ctx } from "../context.js";
|
|
4
|
+
import { serialize } from "../util/format.js";
|
|
5
|
+
|
|
6
|
+
export function registerPositionResource(server: McpServer, getCtx: () => Promise<Ctx>): void {
|
|
7
|
+
server.registerResource(
|
|
8
|
+
"position",
|
|
9
|
+
new ResourceTemplate("position://{id}", { list: undefined }),
|
|
10
|
+
{
|
|
11
|
+
title: "Lending position",
|
|
12
|
+
description: "Full position state by ID (debt, term, expiry, vault, borrower).",
|
|
13
|
+
mimeType: "application/json",
|
|
14
|
+
},
|
|
15
|
+
async (uri, { id }) => {
|
|
16
|
+
const ctx = await getCtx();
|
|
17
|
+
const positionId = Array.isArray(id) ? id[0] : id;
|
|
18
|
+
if (!positionId) {
|
|
19
|
+
throw new Error("position resource requires an `id` segment");
|
|
20
|
+
}
|
|
21
|
+
const result = await ctx.sdk.getLoanById(positionId, true);
|
|
22
|
+
if (!result.success) {
|
|
23
|
+
throw new Error(`Failed to fetch position ${positionId}: ${result.error.message}`);
|
|
24
|
+
}
|
|
25
|
+
if (!result.value) {
|
|
26
|
+
throw new Error(`Position ${positionId} not found`);
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
contents: [
|
|
30
|
+
{
|
|
31
|
+
uri: uri.href,
|
|
32
|
+
mimeType: "application/json",
|
|
33
|
+
text: serialize(result.value),
|
|
34
|
+
},
|
|
35
|
+
],
|
|
36
|
+
};
|
|
37
|
+
},
|
|
38
|
+
);
|
|
39
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import type { Ctx } from "../context.js";
|
|
3
|
+
import { serialize } from "../util/format.js";
|
|
4
|
+
|
|
5
|
+
export function registerProtocolResources(server: McpServer, getCtx: () => Promise<Ctx>): void {
|
|
6
|
+
server.registerResource(
|
|
7
|
+
"protocol-config",
|
|
8
|
+
"protocol://config",
|
|
9
|
+
{
|
|
10
|
+
title: "Protocol configuration",
|
|
11
|
+
description: "Diamond Hands protocol config: contract addresses, pause status, circuit breaker state.",
|
|
12
|
+
mimeType: "application/json",
|
|
13
|
+
},
|
|
14
|
+
async (uri) => {
|
|
15
|
+
const ctx = await getCtx();
|
|
16
|
+
const result = await ctx.sdk.getProtocolConfig();
|
|
17
|
+
if (!result.success) {
|
|
18
|
+
throw new Error(`Failed to fetch protocol config: ${result.error.message}`);
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
contents: [
|
|
22
|
+
{
|
|
23
|
+
uri: uri.href,
|
|
24
|
+
mimeType: "application/json",
|
|
25
|
+
text: serialize(result.value),
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
};
|
|
29
|
+
},
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
server.registerResource(
|
|
33
|
+
"protocol-terms",
|
|
34
|
+
"protocol://terms",
|
|
35
|
+
{
|
|
36
|
+
title: "Available loan terms",
|
|
37
|
+
description: "All configured loan terms (months) with origination and extension fees.",
|
|
38
|
+
mimeType: "application/json",
|
|
39
|
+
},
|
|
40
|
+
async (uri) => {
|
|
41
|
+
const ctx = await getCtx();
|
|
42
|
+
const result = await ctx.sdk.getTermsWithFees();
|
|
43
|
+
if (!result.success) {
|
|
44
|
+
throw new Error(`Failed to fetch terms: ${result.error.message}`);
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
contents: [
|
|
48
|
+
{
|
|
49
|
+
uri: uri.href,
|
|
50
|
+
mimeType: "application/json",
|
|
51
|
+
text: serialize(result.value),
|
|
52
|
+
},
|
|
53
|
+
],
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
);
|
|
57
|
+
}
|
package/src/server.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import { buildContext, type Ctx } from "./context.js";
|
|
3
|
+
import { registerLoanTools } from "./tools/loan.js";
|
|
4
|
+
import { registerPositionTools } from "./tools/position.js";
|
|
5
|
+
import { registerBtcTools } from "./tools/btc.js";
|
|
6
|
+
import { registerProtocolTools } from "./tools/protocol.js";
|
|
7
|
+
import { registerPositionResource } from "./resources/position.js";
|
|
8
|
+
import { registerProtocolResources } from "./resources/protocol.js";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Lazy SDK context — initialized on first tool/resource call rather than at
|
|
12
|
+
* server start. This lets the server boot even without DH_PRIVATE_KEY so that
|
|
13
|
+
* read-only tools (btc_price, protocol_*, position_*) work; only signer-
|
|
14
|
+
* dependent tools (loan_create, loan_mint, etc.) require the env var.
|
|
15
|
+
*/
|
|
16
|
+
let cached: Ctx | null = null;
|
|
17
|
+
let cachedSigned: Ctx | null = null;
|
|
18
|
+
|
|
19
|
+
async function getReadCtx(): Promise<Ctx> {
|
|
20
|
+
if (cached) return cached;
|
|
21
|
+
// Try signer-equipped first if private key is present; falls back to read-only.
|
|
22
|
+
const hasKey = !!(process.env["DH_PRIVATE_KEY"] ?? process.env["PRIVATE_KEY"]);
|
|
23
|
+
cached = await buildContext(hasKey);
|
|
24
|
+
if (hasKey) cachedSigned = cached;
|
|
25
|
+
return cached;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function getSignedCtx(): Promise<Ctx> {
|
|
29
|
+
if (cachedSigned) return cachedSigned;
|
|
30
|
+
cachedSigned = await buildContext(true);
|
|
31
|
+
cached = cachedSigned;
|
|
32
|
+
return cachedSigned;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function createServer(): McpServer {
|
|
36
|
+
const server = new McpServer({
|
|
37
|
+
name: "diamond-hands",
|
|
38
|
+
version: "0.0.1",
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
registerLoanTools(server, getSignedCtx);
|
|
42
|
+
registerPositionTools(server, getReadCtx);
|
|
43
|
+
registerBtcTools(server, getReadCtx);
|
|
44
|
+
registerProtocolTools(server, getReadCtx);
|
|
45
|
+
|
|
46
|
+
registerPositionResource(server, getReadCtx);
|
|
47
|
+
registerProtocolResources(server, getReadCtx);
|
|
48
|
+
|
|
49
|
+
return server;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function disposeCachedContext(): void {
|
|
53
|
+
if (cached) {
|
|
54
|
+
cached.sdk.dispose();
|
|
55
|
+
cached = null;
|
|
56
|
+
cachedSigned = null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ethers } from "ethers";
|
|
2
|
+
import type { DHSigner } from "./types.js";
|
|
3
|
+
|
|
4
|
+
export class EoaDHSigner implements DHSigner {
|
|
5
|
+
readonly kind = "eoa-env" as const;
|
|
6
|
+
private readonly wallet: ethers.Signer;
|
|
7
|
+
|
|
8
|
+
constructor(wallet: ethers.Signer) {
|
|
9
|
+
this.wallet = wallet;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async getBorrowerAddress(): Promise<string> {
|
|
13
|
+
return this.wallet.getAddress();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async getAuthSignerAddress(): Promise<string> {
|
|
17
|
+
return this.wallet.getAddress();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async signAuthMessage(messageHash: string): Promise<string> {
|
|
21
|
+
return this.wallet.signMessage(ethers.getBytes(messageHash));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async wrapInnerTx(innerTx: ethers.TransactionRequest): Promise<ethers.TransactionRequest> {
|
|
25
|
+
return innerTx;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async signTransaction(outerTx: ethers.TransactionRequest): Promise<string> {
|
|
29
|
+
return this.wallet.signTransaction(outerTx);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
getEthersSigner(): ethers.Signer {
|
|
33
|
+
return this.wallet;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ethers } from "ethers";
|
|
2
|
+
|
|
3
|
+
export function loadEnvSigner(provider: ethers.Provider): ethers.Wallet {
|
|
4
|
+
const key = process.env["DH_PRIVATE_KEY"] ?? process.env["PRIVATE_KEY"];
|
|
5
|
+
if (!key || key.trim().length === 0) {
|
|
6
|
+
throw new Error(
|
|
7
|
+
"No private key found. Set DH_PRIVATE_KEY environment variable.",
|
|
8
|
+
);
|
|
9
|
+
}
|
|
10
|
+
const normalized = key.startsWith("0x") ? key : `0x${key}`;
|
|
11
|
+
return new ethers.Wallet(normalized, provider);
|
|
12
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { ethers } from "ethers";
|
|
2
|
+
import type { SafeModuleDHSigner } from "./safe-module.js";
|
|
3
|
+
|
|
4
|
+
export class SafeModuleSignerAdapter extends ethers.AbstractSigner<ethers.Provider> {
|
|
5
|
+
constructor(
|
|
6
|
+
private readonly dhSigner: SafeModuleDHSigner,
|
|
7
|
+
private readonly safeAddress: string,
|
|
8
|
+
provider: ethers.Provider,
|
|
9
|
+
) {
|
|
10
|
+
super(provider);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
async getAddress(): Promise<string> {
|
|
14
|
+
return this.safeAddress;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async signMessage(message: string | Uint8Array): Promise<string> {
|
|
18
|
+
const bytes = message instanceof Uint8Array ? message : ethers.getBytes(message);
|
|
19
|
+
return this.dhSigner.getEthersSigner().signMessage(bytes);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async sendTransaction(tx: ethers.TransactionRequest): Promise<ethers.TransactionResponse> {
|
|
23
|
+
if (!this.provider) {
|
|
24
|
+
throw new Error("SafeModuleSignerAdapter: no provider attached — call connect(provider) first");
|
|
25
|
+
}
|
|
26
|
+
if (!tx.to) {
|
|
27
|
+
throw new Error("SafeModuleSignerAdapter.sendTransaction: tx.to is required");
|
|
28
|
+
}
|
|
29
|
+
if (typeof tx.data !== "string" || !tx.data.startsWith("0x")) {
|
|
30
|
+
throw new Error("SafeModuleSignerAdapter.sendTransaction: tx.data is required and must be hex calldata");
|
|
31
|
+
}
|
|
32
|
+
const network = await this.provider.getNetwork();
|
|
33
|
+
const chainId = tx.chainId ?? network.chainId;
|
|
34
|
+
const outer = await this.dhSigner.wrapInnerTx({
|
|
35
|
+
to: tx.to as string,
|
|
36
|
+
data: tx.data,
|
|
37
|
+
value: 0,
|
|
38
|
+
chainId,
|
|
39
|
+
});
|
|
40
|
+
const signed = await this.dhSigner.signTransaction(outer);
|
|
41
|
+
return await this.provider.broadcastTransaction(signed);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async signTransaction(_tx: ethers.TransactionRequest): Promise<string> {
|
|
45
|
+
throw new Error(
|
|
46
|
+
"SafeModuleSignerAdapter: signTransaction is not supported — use sendTransaction.",
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async signTypedData(
|
|
51
|
+
domain: ethers.TypedDataDomain,
|
|
52
|
+
types: Record<string, ethers.TypedDataField[]>,
|
|
53
|
+
value: Record<string, unknown>,
|
|
54
|
+
): Promise<string> {
|
|
55
|
+
return this.dhSigner.getEthersSigner().signTypedData(domain, types, value);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
connect(provider: ethers.Provider): this {
|
|
59
|
+
return new SafeModuleSignerAdapter(this.dhSigner, this.safeAddress, provider) as this;
|
|
60
|
+
}
|
|
61
|
+
}
|