-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
90 lines (76 loc) · 2.74 KB
/
main.ts
File metadata and controls
90 lines (76 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { parseArgs, type ParseOptions } from '@std/cli/parse-args';
import { NostrMCPProxy } from '@contextvm/sdk/proxy';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { PrivateKeySigner } from '@contextvm/sdk/signer/private-key-signer';
import { loadConfig } from './src/config.ts';
import { initCommand } from './src/commands/init.ts';
import meta from './deno.json' with { type: 'json' };
import { YAML_CONFIG_PATH } from './src/config.ts';
import { ApplesauceRelayPool } from '@contextvm/sdk/relay';
function printUsage() {
console.log(`
Usage: proxy-cli [command|OPTIONS]
Commands:
init Run the interactive configuration wizard.
Options:
--private-key <hex-key> Your Nostr private key in hex format. (Env: PROXY_PRIVATE_KEY)
--relays <urls...> Comma-separated list of Nostr relay URLs. (Env: PROXY_RELAYS)
--server-pubkey <hex-key> The public key of the server to connect to. (Env: PROXY_SERVER_PUBKEY)
--encryption-mode <mode> The encryption mode to use (optional, required, disabled). (Env: PROXY_ENCRYPTION_MODE)
-h, --help Show this help message.
-v, --version Show the version number.
Configuration can also be provided via a '${YAML_CONFIG_PATH}' file or environment variables.
Priority: CLI flags > ${YAML_CONFIG_PATH} > environment variables.
`);
}
const options: ParseOptions = {
boolean: ['help', 'version', 'init'],
alias: { h: 'help', v: 'version' },
};
const args = parseArgs(Deno.args, options);
if (args.help) {
printUsage();
Deno.exit(0);
}
if (args.version) {
console.log((meta as { version?: string }).version ?? '0.1.0');
Deno.exit(0);
}
async function main() {
if (args.init || Deno.args[0] === 'init') {
await initCommand();
Deno.exit(0);
}
const config = await loadConfig(Deno.args);
const mcpHostTransport = new StdioServerTransport();
const signer = new PrivateKeySigner(config.privateKey);
const relayPool = new ApplesauceRelayPool(config.relays);
const proxy = new NostrMCPProxy({
mcpHostTransport,
nostrTransportOptions: {
signer,
relayHandler: relayPool,
serverPubkey: config.serverPubkey,
encryptionMode: config.encryptionMode,
},
});
await proxy.start();
const shutdown = async () => {
if (proxy) {
try {
await proxy.stop();
} catch (error) {
console.error('Error stopping NostrMCPProxy:', error);
}
}
Deno.exit(0);
};
Deno.addSignalListener('SIGINT', shutdown);
Deno.addSignalListener('SIGTERM', shutdown);
}
if (import.meta.main) {
main().catch((error) => {
console.error('Failed to start gateway:', error);
Deno.exit(1);
});
}