@contextvm/sdk 0.1.1 → 0.1.3

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.
Files changed (72) hide show
  1. package/COPYING.LESSER +165 -0
  2. package/README.md +178 -2
  3. package/dist/esm/core/constants.d.ts +65 -0
  4. package/dist/esm/core/constants.d.ts.map +1 -0
  5. package/dist/esm/core/constants.js +70 -0
  6. package/dist/esm/core/constants.js.map +1 -0
  7. package/dist/esm/core/encryption.d.ts +18 -0
  8. package/dist/esm/core/encryption.d.ts.map +1 -0
  9. package/dist/esm/core/encryption.js +40 -0
  10. package/dist/esm/core/encryption.js.map +1 -0
  11. package/dist/esm/core/index.d.ts +5 -0
  12. package/dist/esm/core/index.d.ts.map +1 -0
  13. package/dist/esm/core/index.js +5 -0
  14. package/dist/esm/core/index.js.map +1 -0
  15. package/dist/esm/core/interfaces.d.ts +39 -0
  16. package/dist/esm/core/interfaces.d.ts.map +1 -0
  17. package/dist/esm/core/interfaces.js +13 -0
  18. package/dist/esm/core/interfaces.js.map +1 -0
  19. package/dist/esm/core/utils/serializers.d.ts +29 -0
  20. package/dist/esm/core/utils/serializers.d.ts.map +1 -0
  21. package/dist/esm/core/utils/serializers.js +46 -0
  22. package/dist/esm/core/utils/serializers.js.map +1 -0
  23. package/dist/esm/gateway/index.d.ts +60 -0
  24. package/dist/esm/gateway/index.d.ts.map +1 -0
  25. package/dist/esm/gateway/index.js +110 -0
  26. package/dist/esm/gateway/index.js.map +1 -0
  27. package/dist/esm/index.d.ts +7 -0
  28. package/dist/esm/index.d.ts.map +1 -0
  29. package/dist/esm/index.js +7 -0
  30. package/dist/esm/index.js.map +1 -0
  31. package/dist/esm/package.json +1 -0
  32. package/dist/esm/proxy/index.d.ts +42 -0
  33. package/dist/esm/proxy/index.d.ts.map +1 -0
  34. package/dist/esm/proxy/index.js +59 -0
  35. package/dist/esm/proxy/index.js.map +1 -0
  36. package/dist/esm/proxy/proxy-server.d.ts +3 -0
  37. package/dist/esm/proxy/proxy-server.d.ts.map +1 -0
  38. package/dist/esm/proxy/proxy-server.js +55 -0
  39. package/dist/esm/proxy/proxy-server.js.map +1 -0
  40. package/dist/esm/relay/index.d.ts +2 -0
  41. package/dist/esm/relay/index.d.ts.map +1 -0
  42. package/dist/esm/relay/index.js +2 -0
  43. package/dist/esm/relay/index.js.map +1 -0
  44. package/dist/esm/relay/simple-relay-pool.d.ts +19 -0
  45. package/dist/esm/relay/simple-relay-pool.d.ts.map +1 -0
  46. package/dist/esm/relay/simple-relay-pool.js +44 -0
  47. package/dist/esm/relay/simple-relay-pool.js.map +1 -0
  48. package/dist/esm/signer/index.d.ts +2 -0
  49. package/dist/esm/signer/index.d.ts.map +1 -0
  50. package/dist/esm/signer/index.js +2 -0
  51. package/dist/esm/signer/index.js.map +1 -0
  52. package/dist/esm/signer/private-key-signer.d.ts +16 -0
  53. package/dist/esm/signer/private-key-signer.d.ts.map +1 -0
  54. package/dist/esm/signer/private-key-signer.js +23 -0
  55. package/dist/esm/signer/private-key-signer.js.map +1 -0
  56. package/dist/esm/transport/base-nostr-transport.d.ts +67 -0
  57. package/dist/esm/transport/base-nostr-transport.d.ts.map +1 -0
  58. package/dist/esm/transport/base-nostr-transport.js +122 -0
  59. package/dist/esm/transport/base-nostr-transport.js.map +1 -0
  60. package/dist/esm/transport/index.d.ts +4 -0
  61. package/dist/esm/transport/index.d.ts.map +1 -0
  62. package/dist/esm/transport/index.js +4 -0
  63. package/dist/esm/transport/index.js.map +1 -0
  64. package/dist/esm/transport/nostr-client-transport.d.ts +60 -0
  65. package/dist/esm/transport/nostr-client-transport.d.ts.map +1 -0
  66. package/dist/esm/transport/nostr-client-transport.js +116 -0
  67. package/dist/esm/transport/nostr-client-transport.js.map +1 -0
  68. package/dist/esm/transport/nostr-server-transport.d.ts +128 -0
  69. package/dist/esm/transport/nostr-server-transport.d.ts.map +1 -0
  70. package/dist/esm/transport/nostr-server-transport.js +383 -0
  71. package/dist/esm/transport/nostr-server-transport.js.map +1 -0
  72. package/package.json +2 -2
package/COPYING.LESSER ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
package/README.md CHANGED
@@ -1,3 +1,179 @@
1
- # ctxvm-sdk
1
+ # @ctxvm/sdk: CTXVM Protocol SDK
2
2
 
3
- `@ctxvm/sdk` is a TypeScript SDK designed to implement the `ctxvm` protocol, providing modular access to core functionalities, relay interactions, and signing operations.
3
+ A JavaScript/TypeScript SDK that implements the Context Vending Machine (CTXVM) Protocol, bridging Nostr and Model Context Protocol (MCP) to enable decentralized access and exposure of computational services.
4
+
5
+ ## Overview
6
+
7
+ The CTXVM Protocol defines how Nostr and Model Context Machines can be used to expose MCP server capabilities, enabling standardized usage of these resources through a decentralized, cryptographically secure messaging system.
8
+
9
+ This SDK provides the necessary components to interact with the CTXVM Protocol:
10
+
11
+ - **Core Module**: Contains fundamental definitions, constants, interfaces, and utilities (e.g., encryption, serialization).
12
+ - **Transports**: Critical for communication, providing `NostrClientTransport` and `NostrServerTransport` implementations for enabling MCP over Nostr.
13
+ - **Proxy**: A client-side MCP server that connects to other servers through Nostr, exposing server capabilities locally. Particularly useful for clients that don't natively support Nostr transport.
14
+ - **Gateway**: Implements Nostr server transport, binding to another MCP server and exposing its capabilities through the Nostr network.
15
+ - **Relay**: Functionality for managing Nostr relays, abstracting relay interactions.
16
+ - **Signer**: Provides cryptographic signing capabilities required for Nostr events.
17
+
18
+ Both the Proxy and Gateway leverage Nostr transports, allowing existing MCP servers to maintain their conventional transports while gaining Nostr interoperability.
19
+
20
+ ## Installation
21
+
22
+ This project requires [Bun](https://bun.sh/) (version 1.2.0 or higher).
23
+
24
+ 1. **Clone the repository:**
25
+
26
+ ```bash
27
+ git clone https://github.com/ContextVM/ts-sdk.git
28
+ cd ts-sdk
29
+ ```
30
+
31
+ 2. **Install dependencies:**
32
+
33
+ ```bash
34
+ bun install
35
+ ```
36
+
37
+ 3. **Build the project:**
38
+ ```bash
39
+ bun run build
40
+ ```
41
+
42
+ ## Usage
43
+
44
+ The SDK provides methods for bridging MCP and Nostr. At its core, the SDK relies on Nostr transports and utilities to facilitate communication.
45
+
46
+ ### Core Building Blocks: Signers and Relay Pools
47
+
48
+ The SDK provides default implementations: `PrivateKeySigner` implements the `NostrSigner` interface, and `SimpleRelayPool` implements the `RelayHandler` interface. You can extend the SDK's capabilities by creating custom implementations that satisfy these interfaces, allowing you to integrate with different signing mechanisms or relay management logic.ª
49
+
50
+ ### Nostr Transports
51
+
52
+ The SDK provides specialized transports to send and receive MCP messages over the Nostr network.
53
+
54
+ #### `NostrClientTransport`
55
+
56
+ Used by MCP clients to connect to remote MCP servers exposed via Nostr.
57
+
58
+ ```typescript
59
+ import { Client } from '@modelcontextprotocol/sdk/client';
60
+ import { NostrClientTransport } from '@ctxvm/sdk/transport';
61
+ import { EncryptionMode } from '@ctxvm/sdk/core';
62
+
63
+ // Public key of the target MCP server on Nostr
64
+ const REMOTE_SERVER_PUBKEY = 'remote_server_public_key_here';
65
+
66
+ const clientNostrTransport = new NostrClientTransport({
67
+ signer: signer, // Your NostrSigner instance
68
+ relayHandler: relayPool, // Your RelayHandler instance
69
+ serverPubkey: REMOTE_SERVER_PUBKEY, // Public key of the target MCP server
70
+ encryptionMode: EncryptionMode.OPTIONAL, // Optional: REQUIRED, OPTIONAL (default), or DISABLED
71
+ });
72
+
73
+ const mcpClient = new Client();
74
+
75
+ // To connect the MCP client:
76
+ await mcpClient.connect(clientNostrTransport);
77
+ // Subsequent MCP calls (e.g., listTools, callTool) would use this transport.
78
+ await mcpClient.listTools();
79
+ // await mcpClient.close();
80
+ ```
81
+
82
+ #### `NostrServerTransport`
83
+
84
+ Used by MCP servers to expose their capabilities to Nostr clients.
85
+
86
+ ```typescript
87
+ import { McpServer } from '@modelcontextprotocol/sdk/server';
88
+ import { NostrServerTransport } from '@ctxvm/sdk/transport';
89
+ import { EncryptionMode } from '@ctxvm/sdk/core';
90
+ // Assume signer and relayPool are configured as shown above.
91
+
92
+ const server = new McpServer({
93
+ name: 'demo-server',
94
+ version: '1.0.0',
95
+ });
96
+
97
+ // Before connecting, register your MCP server's capabilities.
98
+
99
+ const serverNostrTransport = new NostrServerTransport({
100
+ signer: signer, // Server's NostrSigner instance
101
+ relayHandler: relayPool, // Server's RelayHandler instance
102
+ isPublicServer: true, // Optional: set to true to announce server capabilities via Nostr events
103
+ encryptionMode: EncryptionMode.OPTIONAL, // Optional: REQUIRED, OPTIONAL (default), or DISABLED
104
+ serverInfo: {
105
+ // Optional: Information for public announcements
106
+ name: 'Nostr MCP Example Server',
107
+ website: 'https://example.com/mcp-server',
108
+ },
109
+ });
110
+
111
+ // To connect the MCP server and make it available via Nostr:
112
+ await mcpServer.connect(serverNostrTransport);
113
+ // This will keep the server running and listening for Nostr events.
114
+ // To gracefully shut down: await mcpServer.close();
115
+ ```
116
+
117
+ ### Bridging Components: Proxy and Gateway
118
+
119
+ The SDK also provides higher-level components that leverage these transports to bridge conventional MCP setups with Nostr.
120
+
121
+ #### `NostrMCPProxy` (Client-Side Bridge)
122
+
123
+ Allows an MCP client (e.g., a local application communicating via a mcp transport) to communicate with a remote MCP server over Nostr. The proxy acts as a translation layer, using `NostrClientTransport` internally.
124
+
125
+ ```typescript
126
+ import { StdioServerTransport } from '@modelcontextprotocol/sdk/stdio';
127
+ import { NostrMCPProxy } from '@ctxvm/sdk/proxy';
128
+
129
+ // This transport represents the connection between your local MCP client and the proxy.
130
+ const hostTransport = new StdioServerTransport();
131
+
132
+ const proxy = new NostrMCPProxy({
133
+ mcpHostTransport: hostTransport, // Your local client's transport
134
+ nostrTransportOptions: {
135
+ signer: signer,
136
+ relayHandler: relayPool,
137
+ serverPubkey: REMOTE_SERVER_PUBKEY, // Target the remote MCP server public key via Nostr
138
+ },
139
+ });
140
+
141
+ // To start the proxy:
142
+ await proxy.start();
143
+ ```
144
+
145
+ #### `NostrMCPGateway` (Server-Side Bridge)
146
+
147
+ Allows an existing MCP server (e.g., communicating via a mcp transport) to expose its capabilities through Nostr. The gateway uses `NostrServerTransport` internally.
148
+
149
+ ```typescript
150
+ import { StdioClientTransport } from '@modelcontextprotocol/sdk/stdio';
151
+ import { NostrMCPGateway } from '@ctxvm/sdk/gateway';
152
+
153
+ // This transport defines how the Gateway connects to your traditional MCP server.
154
+ const serverTransport = new StdioClientTransport({
155
+ command: 'bun',
156
+ args: ['path/to/your/mcp-server.ts'],
157
+ });
158
+
159
+ const gateway = new NostrMCPGateway({
160
+ mcpServerTransport: serverTransport, // Your existing MCP server's transport
161
+ nostrTransportOptions: {
162
+ signer: signer,
163
+ relayHandler: relayPool,
164
+ isPublicServer: true, // Set to true to announce server capabilities to Nostr clients for discovery
165
+ },
166
+ });
167
+
168
+ // To start the gateway and expose your MCP server to Nostr clients:
169
+ await gateway.start();
170
+ // The gateway will run indefinitely, relaying messages between Nostr and your MCP server.
171
+ ```
172
+
173
+ ## Running Tests
174
+
175
+ To run the test suite, use Bun:
176
+
177
+ ```bash
178
+ bun tests
179
+ ```
@@ -0,0 +1,65 @@
1
+ import { AnnouncementMethods } from './interfaces.js';
2
+ /**
3
+ * CTXVM-specific event kinds.
4
+ *
5
+ * All CTXVM messages are ephemeral events.
6
+ * @see https://github.com/nostr-protocol/nips/blob/master/01.md#kinds
7
+ */
8
+ export declare const CTXVM_MESSAGES_KIND = 25910;
9
+ /**
10
+ * Encrypted CTXVM messages using NIP-59 Gift Wrap.
11
+ * @see https://github.com/nostr-protocol/nips/blob/master/59.md
12
+ */
13
+ export declare const GIFT_WRAP_KIND = 1059;
14
+ /**
15
+ * Addressable event for server announcements.
16
+ */
17
+ export declare const SERVER_ANNOUNCEMENT_KIND = 11316;
18
+ /**
19
+ * Addressable event for listing available tools.
20
+ */
21
+ export declare const TOOLS_LIST_KIND = 11317;
22
+ /**
23
+ * Addressable event for listing available resources.
24
+ */
25
+ export declare const RESOURCES_LIST_KIND = 11318;
26
+ /**
27
+ * Addressable event for listing available resources.
28
+ */
29
+ export declare const RESOURCETEMPLATES_LIST_KIND = 11319;
30
+ /**
31
+ * Addressable event for listing available prompts.
32
+ */
33
+ export declare const PROMPTS_LIST_KIND = 11320;
34
+ /**
35
+ * CTXVM-specific Nostr event tags.
36
+ */
37
+ export declare const NOSTR_TAGS: {
38
+ readonly PUBKEY: "p";
39
+ /**
40
+ * Event ID for correlating requests and responses.
41
+ */
42
+ readonly EVENT_ID: "e";
43
+ /**
44
+ * Capability tag for tools, resources, and prompts to provide pricing metadata.
45
+ */
46
+ readonly CAPABILITY: "cap";
47
+ /**
48
+ * Name tag for server announcements.
49
+ */
50
+ readonly NAME: "name";
51
+ /**
52
+ * Website tag for server announcements.
53
+ */
54
+ readonly WEBSITE: "website";
55
+ /**
56
+ * Picture tag for server announcements.
57
+ */
58
+ readonly PICTURE: "picture";
59
+ /**
60
+ * Support encryption tag for server announcements.
61
+ */
62
+ readonly SUPPORT_ENCRYPTION: "support_encryption";
63
+ };
64
+ export declare const announcementMethods: AnnouncementMethods;
65
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../src/core/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEtD;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,QAAQ,CAAC;AAEzC;;;GAGG;AACH,eAAO,MAAM,cAAc,OAAO,CAAC;AAEnC;;GAEG;AACH,eAAO,MAAM,wBAAwB,QAAQ,CAAC;AAE9C;;GAEG;AACH,eAAO,MAAM,eAAe,QAAQ,CAAC;AAErC;;GAEG;AACH,eAAO,MAAM,mBAAmB,QAAQ,CAAC;AAEzC;;GAEG;AACH,eAAO,MAAM,2BAA2B,QAAQ,CAAC;AAEjD;;GAEG;AACH,eAAO,MAAM,iBAAiB,QAAQ,CAAC;AAEvC;;GAEG;AACH,eAAO,MAAM,UAAU;;IAErB;;OAEG;;IAEH;;OAEG;;IAEH;;OAEG;;IAEH;;OAEG;;IAEH;;OAEG;;IAEH;;OAEG;;CAEK,CAAC;AAEX,eAAO,MAAM,mBAAmB,EAAE,mBAMxB,CAAC"}
@@ -0,0 +1,70 @@
1
+ /**
2
+ * CTXVM-specific event kinds.
3
+ *
4
+ * All CTXVM messages are ephemeral events.
5
+ * @see https://github.com/nostr-protocol/nips/blob/master/01.md#kinds
6
+ */
7
+ export const CTXVM_MESSAGES_KIND = 25910;
8
+ /**
9
+ * Encrypted CTXVM messages using NIP-59 Gift Wrap.
10
+ * @see https://github.com/nostr-protocol/nips/blob/master/59.md
11
+ */
12
+ export const GIFT_WRAP_KIND = 1059;
13
+ /**
14
+ * Addressable event for server announcements.
15
+ */
16
+ export const SERVER_ANNOUNCEMENT_KIND = 11316;
17
+ /**
18
+ * Addressable event for listing available tools.
19
+ */
20
+ export const TOOLS_LIST_KIND = 11317;
21
+ /**
22
+ * Addressable event for listing available resources.
23
+ */
24
+ export const RESOURCES_LIST_KIND = 11318;
25
+ /**
26
+ * Addressable event for listing available resources.
27
+ */
28
+ export const RESOURCETEMPLATES_LIST_KIND = 11319;
29
+ /**
30
+ * Addressable event for listing available prompts.
31
+ */
32
+ export const PROMPTS_LIST_KIND = 11320;
33
+ /**
34
+ * CTXVM-specific Nostr event tags.
35
+ */
36
+ export const NOSTR_TAGS = {
37
+ PUBKEY: 'p',
38
+ /**
39
+ * Event ID for correlating requests and responses.
40
+ */
41
+ EVENT_ID: 'e',
42
+ /**
43
+ * Capability tag for tools, resources, and prompts to provide pricing metadata.
44
+ */
45
+ CAPABILITY: 'cap',
46
+ /**
47
+ * Name tag for server announcements.
48
+ */
49
+ NAME: 'name',
50
+ /**
51
+ * Website tag for server announcements.
52
+ */
53
+ WEBSITE: 'website',
54
+ /**
55
+ * Picture tag for server announcements.
56
+ */
57
+ PICTURE: 'picture',
58
+ /**
59
+ * Support encryption tag for server announcements.
60
+ */
61
+ SUPPORT_ENCRYPTION: 'support_encryption',
62
+ };
63
+ export const announcementMethods = {
64
+ server: 'initialize',
65
+ tools: 'tools/list',
66
+ resources: 'resources/list',
67
+ resourceTemplates: 'resources/templates/list',
68
+ prompts: 'prompts/list',
69
+ };
70
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../../../src/core/constants.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,KAAK,CAAC;AAEzC;;;GAGG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,IAAI,CAAC;AAEnC;;GAEG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,KAAK,CAAC;AAE9C;;GAEG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,KAAK,CAAC;AAErC;;GAEG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,KAAK,CAAC;AAEzC;;GAEG;AACH,MAAM,CAAC,MAAM,2BAA2B,GAAG,KAAK,CAAC;AAEjD;;GAEG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,KAAK,CAAC;AAEvC;;GAEG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG;IACxB,MAAM,EAAE,GAAG;IACX;;OAEG;IACH,QAAQ,EAAE,GAAG;IACb;;OAEG;IACH,UAAU,EAAE,KAAK;IACjB;;OAEG;IACH,IAAI,EAAE,MAAM;IACZ;;OAEG;IACH,OAAO,EAAE,SAAS;IAClB;;OAEG;IACH,OAAO,EAAE,SAAS;IAClB;;OAEG;IACH,kBAAkB,EAAE,oBAAoB;CAChC,CAAC;AAEX,MAAM,CAAC,MAAM,mBAAmB,GAAwB;IACtD,MAAM,EAAE,YAAY;IACpB,KAAK,EAAE,YAAY;IACnB,SAAS,EAAE,gBAAgB;IAC3B,iBAAiB,EAAE,0BAA0B;IAC7C,OAAO,EAAE,cAAc;CACf,CAAC"}
@@ -0,0 +1,18 @@
1
+ import { NostrEvent } from 'nostr-tools/pure';
2
+ /**
3
+ * Encrypts a JSON-RPC message using a simplified NIP-17/NIP-59 gift wrap scheme.
4
+ * The message is encrypted with NIP-44 and wrapped in a kind 1059 event.
5
+ * @param message The JSON-RPC message to encrypt.
6
+ * @param recipientPublicKey The public key of the recipient.
7
+ * @returns The encrypted gift wrap event.
8
+ */
9
+ export declare function encryptMessage(message: string, recipientPublicKey: string): NostrEvent;
10
+ /**
11
+ * Decrypts a gift-wrapped Nostr event.
12
+ * @param event The gift wrap event (kind 1059).
13
+ * @param recipientPrivateKey The private key of the recipient.
14
+ * @returns The decrypted message content.
15
+ * @throws If the event is not a valid gift wrap or decryption fails.
16
+ */
17
+ export declare function decryptMessage(event: NostrEvent, recipientPrivateKey: Uint8Array): string;
18
+ //# sourceMappingURL=encryption.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"encryption.d.ts","sourceRoot":"","sources":["../../../src/core/encryption.ts"],"names":[],"mappings":"AACA,OAAO,EAIL,UAAU,EACX,MAAM,kBAAkB,CAAC;AAG1B;;;;;;GAMG;AACH,wBAAgB,cAAc,CAC5B,OAAO,EAAE,MAAM,EACf,kBAAkB,EAAE,MAAM,GACzB,UAAU,CAiBZ;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAC5B,KAAK,EAAE,UAAU,EACjB,mBAAmB,EAAE,UAAU,GAC9B,MAAM,CAWR"}
@@ -0,0 +1,40 @@
1
+ import { nip44 } from 'nostr-tools';
2
+ import { finalizeEvent, generateSecretKey, getPublicKey, } from 'nostr-tools/pure';
3
+ import { GIFT_WRAP_KIND, NOSTR_TAGS } from './constants.js';
4
+ /**
5
+ * Encrypts a JSON-RPC message using a simplified NIP-17/NIP-59 gift wrap scheme.
6
+ * The message is encrypted with NIP-44 and wrapped in a kind 1059 event.
7
+ * @param message The JSON-RPC message to encrypt.
8
+ * @param recipientPublicKey The public key of the recipient.
9
+ * @returns The encrypted gift wrap event.
10
+ */
11
+ export function encryptMessage(message, recipientPublicKey) {
12
+ const giftWrapPrivateKey = generateSecretKey();
13
+ const giftWrapPublicKey = getPublicKey(giftWrapPrivateKey);
14
+ const conversationKey = nip44.v2.utils.getConversationKey(giftWrapPrivateKey, recipientPublicKey);
15
+ const encryptedContent = nip44.v2.encrypt(message, conversationKey);
16
+ const giftWrap = {
17
+ kind: GIFT_WRAP_KIND,
18
+ content: encryptedContent,
19
+ tags: [[NOSTR_TAGS.PUBKEY, recipientPublicKey]],
20
+ created_at: Math.floor(Date.now() / 1000),
21
+ pubkey: giftWrapPublicKey,
22
+ };
23
+ return finalizeEvent(giftWrap, giftWrapPrivateKey);
24
+ }
25
+ /**
26
+ * Decrypts a gift-wrapped Nostr event.
27
+ * @param event The gift wrap event (kind 1059).
28
+ * @param recipientPrivateKey The private key of the recipient.
29
+ * @returns The decrypted message content.
30
+ * @throws If the event is not a valid gift wrap or decryption fails.
31
+ */
32
+ export function decryptMessage(event, recipientPrivateKey) {
33
+ if (event.kind !== GIFT_WRAP_KIND) {
34
+ throw new Error('Event is not a gift wrap.');
35
+ }
36
+ const conversationKey = nip44.v2.utils.getConversationKey(recipientPrivateKey, event.pubkey);
37
+ const decryptedMsg = nip44.v2.decrypt(event.content, conversationKey);
38
+ return decryptedMsg;
39
+ }
40
+ //# sourceMappingURL=encryption.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"encryption.js","sourceRoot":"","sources":["../../../src/core/encryption.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACpC,OAAO,EACL,aAAa,EACb,iBAAiB,EACjB,YAAY,GAEb,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAE5D;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAC5B,OAAe,EACf,kBAA0B;IAE1B,MAAM,kBAAkB,GAAG,iBAAiB,EAAE,CAAC;IAC/C,MAAM,iBAAiB,GAAG,YAAY,CAAC,kBAAkB,CAAC,CAAC;IAC3D,MAAM,eAAe,GAAG,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,kBAAkB,CACvD,kBAAkB,EAClB,kBAAkB,CACnB,CAAC;IACF,MAAM,gBAAgB,GAAG,KAAK,CAAC,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC;IACpE,MAAM,QAAQ,GAAG;QACf,IAAI,EAAE,cAAc;QACpB,OAAO,EAAE,gBAAgB;QACzB,IAAI,EAAE,CAAC,CAAC,UAAU,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAAC;QAC/C,UAAU,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC;QACzC,MAAM,EAAE,iBAAiB;KAC1B,CAAC;IAEF,OAAO,aAAa,CAAC,QAAQ,EAAE,kBAAkB,CAAC,CAAC;AACrD,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAC5B,KAAiB,EACjB,mBAA+B;IAE/B,IAAI,KAAK,CAAC,IAAI,KAAK,cAAc,EAAE,CAAC;QAClC,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;IAC/C,CAAC;IAED,MAAM,eAAe,GAAG,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,kBAAkB,CACvD,mBAAmB,EACnB,KAAK,CAAC,MAAM,CACb,CAAC;IACF,MAAM,YAAY,GAAG,KAAK,CAAC,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC;IACtE,OAAO,YAAY,CAAC;AACtB,CAAC"}
@@ -0,0 +1,5 @@
1
+ export * from './constants.js';
2
+ export * from './interfaces.js';
3
+ export * from './utils/serializers.js';
4
+ export * from './encryption.js';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/core/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,wBAAwB,CAAC;AACvC,cAAc,iBAAiB,CAAC"}
@@ -0,0 +1,5 @@
1
+ export * from './constants.js';
2
+ export * from './interfaces.js';
3
+ export * from './utils/serializers.js';
4
+ export * from './encryption.js';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/core/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,wBAAwB,CAAC;AACvC,cAAc,iBAAiB,CAAC"}
@@ -0,0 +1,39 @@
1
+ import { InitializeRequest, ListPromptsRequest, ListResourcesRequest, ListResourceTemplatesRequest, ListToolsRequest } from '@modelcontextprotocol/sdk/types.js';
2
+ import type { Filter, NostrEvent } from 'nostr-tools';
3
+ /**
4
+ * Defines the encryption mode for the transport.
5
+ * - `optional`: Encrypts messages if the incoming message was encrypted.
6
+ * - `required`: Enforces encryption for all messages.
7
+ * - `disabled`: Disables encryption entirely.
8
+ */
9
+ export declare enum EncryptionMode {
10
+ OPTIONAL = "optional",
11
+ REQUIRED = "required",
12
+ DISABLED = "disabled"
13
+ }
14
+ /**
15
+ * A generic interface for Nostr signers.
16
+ */
17
+ export interface NostrSigner {
18
+ getPublicKey(): Promise<string>;
19
+ signEvent(event: Omit<NostrEvent, 'sig' | 'id'>): Promise<NostrEvent>;
20
+ getSecretKey(): Promise<Uint8Array>;
21
+ }
22
+ /**
23
+ * A generic interface for Nostr relays.
24
+ */
25
+ export interface RelayHandler {
26
+ connect(): Promise<void>;
27
+ disconnect(relayUrls?: string[]): Promise<void>;
28
+ publish(event: NostrEvent): Promise<void>;
29
+ subscribe(filters: Filter[], onEvent: (event: NostrEvent) => void, onEose?: () => void): Promise<void>;
30
+ unsubscribe(): void;
31
+ }
32
+ export interface AnnouncementMethods {
33
+ server: InitializeRequest['method'];
34
+ tools: ListToolsRequest['method'];
35
+ resources: ListResourcesRequest['method'];
36
+ resourceTemplates: ListResourceTemplatesRequest['method'];
37
+ prompts: ListPromptsRequest['method'];
38
+ }
39
+ //# sourceMappingURL=interfaces.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interfaces.d.ts","sourceRoot":"","sources":["../../../src/core/interfaces.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,oBAAoB,EACpB,4BAA4B,EAC5B,gBAAgB,EACjB,MAAM,oCAAoC,CAAC;AAC5C,OAAO,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEtD;;;;;GAKG;AACH,oBAAY,cAAc;IACxB,QAAQ,aAAa;IACrB,QAAQ,aAAa;IACrB,QAAQ,aAAa;CACtB;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,YAAY,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAChC,SAAS,CAAC,KAAK,EAAE,IAAI,CAAC,UAAU,EAAE,KAAK,GAAG,IAAI,CAAC,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IACtE,YAAY,IAAI,OAAO,CAAC,UAAU,CAAC,CAAC;CACrC;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACzB,UAAU,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAChD,OAAO,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1C,SAAS,CACP,OAAO,EAAE,MAAM,EAAE,EACjB,OAAO,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,IAAI,EACpC,MAAM,CAAC,EAAE,MAAM,IAAI,GAClB,OAAO,CAAC,IAAI,CAAC,CAAC;IACjB,WAAW,IAAI,IAAI,CAAC;CACrB;AAED,MAAM,WAAW,mBAAmB;IAClC,MAAM,EAAE,iBAAiB,CAAC,QAAQ,CAAC,CAAC;IACpC,KAAK,EAAE,gBAAgB,CAAC,QAAQ,CAAC,CAAC;IAClC,SAAS,EAAE,oBAAoB,CAAC,QAAQ,CAAC,CAAC;IAC1C,iBAAiB,EAAE,4BAA4B,CAAC,QAAQ,CAAC,CAAC;IAC1D,OAAO,EAAE,kBAAkB,CAAC,QAAQ,CAAC,CAAC;CACvC"}
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Defines the encryption mode for the transport.
3
+ * - `optional`: Encrypts messages if the incoming message was encrypted.
4
+ * - `required`: Enforces encryption for all messages.
5
+ * - `disabled`: Disables encryption entirely.
6
+ */
7
+ export var EncryptionMode;
8
+ (function (EncryptionMode) {
9
+ EncryptionMode["OPTIONAL"] = "optional";
10
+ EncryptionMode["REQUIRED"] = "required";
11
+ EncryptionMode["DISABLED"] = "disabled";
12
+ })(EncryptionMode || (EncryptionMode = {}));
13
+ //# sourceMappingURL=interfaces.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../../../src/core/interfaces.ts"],"names":[],"mappings":"AASA;;;;;GAKG;AACH,MAAM,CAAN,IAAY,cAIX;AAJD,WAAY,cAAc;IACxB,uCAAqB,CAAA;IACrB,uCAAqB,CAAA;IACrB,uCAAqB,CAAA;AACvB,CAAC,EAJW,cAAc,KAAd,cAAc,QAIzB"}