@x207/mcp 0.0.0-stage → 0.0.2
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/LICENSE +202 -0
- package/README.md +258 -2
- package/dist/backend.d.ts +25 -0
- package/dist/backend.js +83 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.js +50 -0
- package/dist/config.d.ts +20 -0
- package/dist/config.js +31 -0
- package/dist/guards.d.ts +25 -0
- package/dist/guards.js +67 -0
- package/dist/http.d.ts +19 -0
- package/dist/http.js +94 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +11 -0
- package/dist/net-guard.d.ts +20 -0
- package/dist/net-guard.js +182 -0
- package/dist/server.d.ts +24 -0
- package/dist/server.js +47 -0
- package/dist/tools.d.ts +27 -0
- package/dist/tools.js +147 -0
- package/dist/write-config.d.ts +17 -0
- package/dist/write-config.js +72 -0
- package/dist/write-tools.d.ts +43 -0
- package/dist/write-tools.js +443 -0
- package/package.json +59 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,259 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @x207/mcp
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
An [MCP](https://modelcontextprotocol.io) server for x207, the mutual-credit settlement layer for x402 agent payments on NEAR. It lets a model such as Claude read a circle: its rules and members, one member's position and limit, the result of an epoch, a clearing proposal checked by the contract, the marketplace, and one order.
|
|
4
|
+
|
|
5
|
+
Status: version 0.0.1, pre-release. By default the server has **read tools only**: it signs nothing, sends nothing and holds no key. The eight **write tools** exist only on stdio, only when you turn them on, and only behind the guards below. Node 22.22.2 or newer.
|
|
6
|
+
|
|
7
|
+
## The six read tools
|
|
8
|
+
|
|
9
|
+
| Tool | What it answers | Reads |
|
|
10
|
+
| --- | --- | --- |
|
|
11
|
+
| `x207_circle_overview` | The circle and its rules, and its members | contract views `get_circle`, `get_params`; read API `GET /members` (the view `list_members` when there is no read API) |
|
|
12
|
+
| `x207_member_inspect` | One member: status, stake, position, credit limit | read API `GET /members/{account}` and `GET /members/{account}/limits` (the views `member` and `limit` when there is no read API) |
|
|
13
|
+
| `x207_epoch_report` | One clearing epoch, the current one by default: phase, deadlines, cleared ratio and settlements | contract view `get_epoch`; read API `GET /epochs/{n}`, `GET /epochs/{n}/metrics`, `GET /epochs/{n}/settlements` (the view `list_positions` when there is no read API) |
|
|
14
|
+
| `x207_simulate_clearing` | Whether a proposed clearing is valid, and what it would clear | contract view `simulate_clearing` |
|
|
15
|
+
| `x207_search_listings` | Marketplace listings, filtered by account, asset type and class | read API `GET /listings` (needs the read API) |
|
|
16
|
+
| `x207_order_status` | One service order, or a list of orders by party and status | read API `GET /orders/{hash}` and `GET /orders` (needs the read API) |
|
|
17
|
+
|
|
18
|
+
All six are marked `readOnlyHint: true`. Credit amounts are decimal strings in the smallest unit (6 decimals). A tool that fails returns an error result with a short reason, and a part of an answer that could not be read (for example a read API that is down) is reported in `warnings` while the rest is returned. A result longer than 100,000 characters is cut and says so.
|
|
19
|
+
|
|
20
|
+
## Configuration
|
|
21
|
+
|
|
22
|
+
Everything comes from the environment. No variable holds a secret.
|
|
23
|
+
|
|
24
|
+
| Variable | Meaning |
|
|
25
|
+
| --- | --- |
|
|
26
|
+
| `X207_CIRCLE_CONTRACT` | **Required.** The circle contract account, for example `x207-v013.testnet`. |
|
|
27
|
+
| `X207_CIRCLE_ID` | The circle id in the read API paths. Default `agents-main`. |
|
|
28
|
+
| `X207_RPC_URL` | NEAR JSON-RPC endpoints, comma separated, tried in order. Default: `https://rpc.testnet.fastnear.com` then `https://rpc.testnet.near.org` for a `.testnet` contract, otherwise the mainnet endpoints of `@x207/core`. An endpoint that fails or answers 429 hands over to the next. |
|
|
29
|
+
| `X207_BACKEND_URL` | Base URL of the x207 read API, for example `http://127.0.0.1:8080`. Optional; without it `x207_search_listings` and `x207_order_status` explain what is missing and the other tools answer from the contract. |
|
|
30
|
+
| `X207_TIMEOUT_MS` | Timeout of one RPC or read API call. Default 10000. |
|
|
31
|
+
|
|
32
|
+
## Use with Claude Desktop (stdio)
|
|
33
|
+
|
|
34
|
+
Install the package, then add the server to `claude_desktop_config.json`:
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
npm install -g @x207/mcp
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
```json
|
|
41
|
+
{
|
|
42
|
+
"mcpServers": {
|
|
43
|
+
"x207": {
|
|
44
|
+
"command": "x207-mcp",
|
|
45
|
+
"env": {
|
|
46
|
+
"X207_CIRCLE_CONTRACT": "x207-v013.testnet",
|
|
47
|
+
"X207_BACKEND_URL": "http://127.0.0.1:8080"
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
From a checkout of the repository, build once with `npm run build` and point `command` at `node` with `"args": ["/path/to/sdk/packages/mcp/dist/cli.js"]`. Restart Claude Desktop, then ask for example: "Give me the overview of the x207 circle", "Inspect the member alice.x207-v013.testnet", or "Report epoch 1".
|
|
55
|
+
|
|
56
|
+
## Write tools (stdio only, off by default)
|
|
57
|
+
|
|
58
|
+
Set `X207_MCP_WRITE=1` and give the agent's account and keys in the environment. The HTTP server never has these tools, whatever the environment says.
|
|
59
|
+
|
|
60
|
+
| Tool | What it does | Signs with | Off unless |
|
|
61
|
+
| --- | --- | --- | --- |
|
|
62
|
+
| `x207_pay_request` | Fetches a URL; on an x207 402 it signs a credit voucher and repeats the request. The facilitator of the seller registers the voucher; this tool sends nothing to the chain. | voucher key | a voucher key |
|
|
63
|
+
| `x207_list_offer` | Signs a listing (what the agent offers, what it seeks) and posts it to the marketplace book. | voucher key | a voucher key and `X207_BACKEND_URL` |
|
|
64
|
+
| `x207_make_offer` | Signs an offer or a counter-offer (`in_reply_to`) on a listing and posts it. | voucher key | a voucher key and `X207_BACKEND_URL` |
|
|
65
|
+
| `x207_accept_offer` | Previews the acceptance at the backend, checks what the agent would pay, signs the bundle and posts the acceptance. | voucher key | a voucher key and `X207_BACKEND_URL` |
|
|
66
|
+
| `x207_confirm_milestone` | Signs a notice for a milestone of an order (`confirmed`, `disputed`, `delivered`, `cancelled`) and posts it. | voucher key | a voucher key and `X207_BACKEND_URL` |
|
|
67
|
+
| `x207_join` | Joins the circle and stakes USDT: `join` with the 0.35 NEAR storage deposit, then `ft_transfer_call`. | NEAR key | `X207_ALLOW_OUTFLOWS=1` and `X207_ALLOW_FULL_ACCESS_KEY=1` |
|
|
68
|
+
| `x207_settle` | Pays the residual the agent owes for an epoch (at most the amount due): `ft_transfer_call` with the `settle` message. | NEAR key | `X207_ALLOW_OUTFLOWS=1` and `X207_ALLOW_FULL_ACCESS_KEY=1` |
|
|
69
|
+
| `x207_claim` | Claims what the agent is owed for an epoch, to its own account or its Intents balance: `claim` with 1 yoctoNEAR. | NEAR key | `X207_ALLOW_FULL_ACCESS_KEY=1` |
|
|
70
|
+
|
|
71
|
+
A tool whose key or switch is missing is not listed at all. The **voucher key** is an off-chain ed25519 key of the agent (`ed25519:<base58>`); it is not a NEAR access key. The **NEAR key** is an access key of the agent's account.
|
|
72
|
+
|
|
73
|
+
| Variable | Meaning |
|
|
74
|
+
| --- | --- |
|
|
75
|
+
| `X207_MCP_WRITE` | `1` turns the write tools on. Default: off. |
|
|
76
|
+
| `X207_AGENT_ACCOUNT` | The NEAR account of the agent, a member of the circle. Required with `X207_MCP_WRITE=1`. |
|
|
77
|
+
| `X207_AGENT_VOUCHER_KEY` | Voucher secret key. Signs vouchers, listings, offers, acceptances and notices. |
|
|
78
|
+
| `X207_AGENT_NEAR_KEY` | NEAR secret key of the agent account. Signs the transactions of `x207_join`, `x207_settle` and `x207_claim` only. |
|
|
79
|
+
| `X207_MAX_SPEND_SESSION` | Most the agent may commit in one run of the server, in credit units (6 decimals). Default `1000000` (1 credit). |
|
|
80
|
+
| `X207_MAX_PRICE_PER_CALL` | Most one payment, offer or milestone may be. Default `100000` (0.1 credit). |
|
|
81
|
+
| `X207_ALLOWED_COUNTERPARTIES` | Comma separated accounts the agent may deal with. Empty: any active member of the circle. |
|
|
82
|
+
| `X207_ALLOW_OUTFLOWS` | `1` allows `x207_join` and `x207_settle`, which move USDT from the agent into the circle. Default off. |
|
|
83
|
+
| `X207_ALLOW_FULL_ACCESS_KEY` | `1` allows the NEAR key to be a full-access key. Default off. |
|
|
84
|
+
| `X207_ALLOW_PRIVATE_URLS` | `1` lets `x207_pay_request` fetch a URL on this machine or in a private network, for a seller you run yourself. Default off. |
|
|
85
|
+
|
|
86
|
+
### The guards
|
|
87
|
+
|
|
88
|
+
1. **Budget of the session.** Every voucher signed, every payment an acceptance would make, every milestone confirmed and every stake or residual paid counts against `X207_MAX_SPEND_SESSION`. A commitment that does not fit is refused before it is signed; one that fails afterwards is given back.
|
|
89
|
+
2. **Price per call.** An amount above `X207_MAX_PRICE_PER_CALL` is refused. The price in a 402 comes from the seller, so the server does not trust it: the check runs on the requirement before the voucher is signed.
|
|
90
|
+
3. **Risky tools are off.** `x207_join` and `x207_settle` stay off until the operator sets `X207_ALLOW_OUTFLOWS=1`.
|
|
91
|
+
4. **Counterparties.** The other party of a payment, offer, acceptance or milestone must be an **active member** of the circle (a contract view), must be in `X207_ALLOWED_COUNTERPARTIES` when that list is set, and is never the agent itself.
|
|
92
|
+
5. **Network.** The URL given to `x207_pay_request` comes from a model, so it can come from a prompt injection. The tool refuses a URL whose host is, or resolves to, a loopback, private, link-local (the metadata service of a cloud machine), carrier-grade NAT, multicast or unspecified address, and names such as `localhost`, `*.local` and `*.internal`. It does not follow redirects (the answer says where a redirect points), gives each call 30 seconds, and reads at most 1 MiB of an answer. `X207_ALLOW_PRIVATE_URLS=1` turns the address check off for a seller on your own machine.
|
|
93
|
+
6. **Keys.** Keys come from the environment only. No result, error or log line contains a key: every text that leaves the server is scrubbed of the configured keys and of anything shaped like an ed25519 secret key. Before a tool sends a transaction the server reads the permission of the NEAR key (`view_access_key`). **A function-call key cannot attach a deposit, and join, stake, settle and claim all attach one** (NEAR protocol rule), so with a function-call key those tools refuse and send nothing. With a full-access key they run only if `X207_ALLOW_FULL_ACCESS_KEY=1`; use a dedicated agent account with a small balance. The tools that sign with the voucher key send no transaction, so they do not need a NEAR key at all.
|
|
94
|
+
|
|
95
|
+
Also: the backend returns the terms of a bundle together with their hash, and `x207_accept_offer` refuses when the hash does not match the terms; `x207_make_offer` refuses when the listing the book returns does not hash to the hash that was asked for.
|
|
96
|
+
|
|
97
|
+
### Security limit: the balance of the agent account is the largest possible loss
|
|
98
|
+
|
|
99
|
+
A NEAR deposit cannot be attached by a function-call key, so the tools that send one (`x207_join`, `x207_settle`, `x207_claim`) need a **full-access key** of the agent account. A full-access key can do anything the account can do, and none of the guards above limits what a stolen or misused key does outside this server. So use a **dedicated agent account** that holds only what the agent may lose: whatever NEAR and USDT sit on that account is the most that can be lost. The session budget and the price cap protect against a model that misbehaves; they do not protect against someone who has the key. Leave `X207_ALLOW_FULL_ACCESS_KEY` unset, and the three tools off, if the agent only needs to pay, list, offer, accept and confirm: those tools sign with the voucher key and never need a NEAR key.
|
|
100
|
+
|
|
101
|
+
### Claude Desktop with the write tools
|
|
102
|
+
|
|
103
|
+
```json
|
|
104
|
+
{
|
|
105
|
+
"mcpServers": {
|
|
106
|
+
"x207-agent": {
|
|
107
|
+
"command": "x207-mcp",
|
|
108
|
+
"env": {
|
|
109
|
+
"X207_CIRCLE_CONTRACT": "x207-v013.testnet",
|
|
110
|
+
"X207_BACKEND_URL": "http://127.0.0.1:8080",
|
|
111
|
+
"X207_MCP_WRITE": "1",
|
|
112
|
+
"X207_AGENT_ACCOUNT": "agent.x207-v013.testnet",
|
|
113
|
+
"X207_AGENT_VOUCHER_KEY": "ed25519:…",
|
|
114
|
+
"X207_MAX_SPEND_SESSION": "500000",
|
|
115
|
+
"X207_MAX_PRICE_PER_CALL": "50000"
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Keep the keys out of any file you commit. Then ask for example: "List a translation service for 0.05 credit", "Pay https://seller.example/api/summarize", or "Confirm milestone 1 of order …".
|
|
123
|
+
|
|
124
|
+
## Use over Streamable HTTP
|
|
125
|
+
|
|
126
|
+
```bash
|
|
127
|
+
X207_CIRCLE_CONTRACT=x207-v013.testnet X207_BACKEND_URL=http://127.0.0.1:8080 x207-mcp --http --port 3207
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
The endpoint is `POST http://127.0.0.1:3207/mcp`. It is stateless (no session) and serves **only the read tools**, so it can be shown to anybody who can already read the contract and the read API. `GET /healthz` answers 200. It listens on `127.0.0.1` unless `--host` says otherwise; put a reverse proxy with TLS and a rate limit in front of it before you expose it. On a loopback address it serves only the `Host` names of the loopback (`127.0.0.1`, `localhost`, `[::1]`), so a web page cannot reach it through DNS rebinding; behind a proxy set `X207_ALLOWED_HOSTS=mcp.example.org` (comma separated) to serve exactly those names.
|
|
131
|
+
|
|
132
|
+
To try it with the [MCP Inspector](https://github.com/modelcontextprotocol/inspector):
|
|
133
|
+
|
|
134
|
+
```bash
|
|
135
|
+
npx @modelcontextprotocol/inspector --cli http://127.0.0.1:3207/mcp --transport http --method tools/list
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## Use as a library
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
import { configFromEnv, createX207McpServer } from "@x207/mcp";
|
|
142
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
143
|
+
|
|
144
|
+
const server = createX207McpServer(configFromEnv(), "stdio"); // "http" gives the read tools only
|
|
145
|
+
await server.connect(new StdioServerTransport());
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## Example output (real data)
|
|
149
|
+
|
|
150
|
+
Run against the circle `x207-v013.testnet` (contract views from the testnet RPC) and a local read API (`backend`) that served the read model of that circle with its three demo accounts. The tool results below are real; long fields (signatures, hashes of the order spec) are left out and marked by the keys that remain.
|
|
151
|
+
|
|
152
|
+
`x207_member_inspect` with `{"account": "x207-demo-seller.testnet"}`:
|
|
153
|
+
|
|
154
|
+
```json
|
|
155
|
+
{
|
|
156
|
+
"account": "x207-demo-seller.testnet",
|
|
157
|
+
"member": {
|
|
158
|
+
"account": "x207-demo-seller.testnet",
|
|
159
|
+
"status": "active",
|
|
160
|
+
"stake": "100000000",
|
|
161
|
+
"limit": "80000000",
|
|
162
|
+
"outstandingDebt": "0",
|
|
163
|
+
"headroom": "80000000",
|
|
164
|
+
"joinedAt": "2026-11-04T04:25:29Z"
|
|
165
|
+
},
|
|
166
|
+
"limits": {
|
|
167
|
+
"items": [
|
|
168
|
+
{
|
|
169
|
+
"epoch": null,
|
|
170
|
+
"oldLimit": "0",
|
|
171
|
+
"newLimit": "80000000",
|
|
172
|
+
"status": "active",
|
|
173
|
+
"reason": "stake",
|
|
174
|
+
"blockHeight": 276145725
|
|
175
|
+
}
|
|
176
|
+
]
|
|
177
|
+
},
|
|
178
|
+
"source": "read API",
|
|
179
|
+
"warnings": []
|
|
180
|
+
}
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
`x207_search_listings` with `{"account": "x207-demo-seller.testnet", "limit": 1}`:
|
|
184
|
+
|
|
185
|
+
```json
|
|
186
|
+
{
|
|
187
|
+
"items": [
|
|
188
|
+
{
|
|
189
|
+
"listingHash": "6eb11d3068ba7e5f84503d9d454242e891fedd676ffd1e03324d4889a304fa41",
|
|
190
|
+
"account": "x207-demo-seller.testnet",
|
|
191
|
+
"offer": {
|
|
192
|
+
"assetType": "near-rpc:access",
|
|
193
|
+
"assetClass": "B",
|
|
194
|
+
"quantity": 1,
|
|
195
|
+
"value": "2000000"
|
|
196
|
+
},
|
|
197
|
+
"want": {
|
|
198
|
+
"assetType": "usdc",
|
|
199
|
+
"assetClass": "A",
|
|
200
|
+
"quantity": 1,
|
|
201
|
+
"value": "2000000"
|
|
202
|
+
},
|
|
203
|
+
"expiresAt": "2026-12-31T00:00:00Z",
|
|
204
|
+
"status": "open"
|
|
205
|
+
}
|
|
206
|
+
],
|
|
207
|
+
"nextCursor": "eyJhIjoxfQ"
|
|
208
|
+
}
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
`x207_order_status` with the `hash` of the order of `x207-demo-buyer.testnet`:
|
|
212
|
+
|
|
213
|
+
```json
|
|
214
|
+
{
|
|
215
|
+
"orderHash": "332b9bddcc9e17578ba2e55b1fd7d8c0323f7a8225f4ebc03a83b7c0668840a3",
|
|
216
|
+
"buyer": "x207-demo-buyer.testnet",
|
|
217
|
+
"seller": "x207-demo-seller.testnet",
|
|
218
|
+
"status": "open",
|
|
219
|
+
"budget": "30000000",
|
|
220
|
+
"unit": "credit",
|
|
221
|
+
"decimals": 6,
|
|
222
|
+
"milestones": [
|
|
223
|
+
{
|
|
224
|
+
"index": 1,
|
|
225
|
+
"amount": "10000000",
|
|
226
|
+
"dueAt": 1796947200,
|
|
227
|
+
"status": "open"
|
|
228
|
+
},
|
|
229
|
+
{
|
|
230
|
+
"index": 2,
|
|
231
|
+
"amount": "20000000",
|
|
232
|
+
"dueAt": 1797811200,
|
|
233
|
+
"status": "open"
|
|
234
|
+
}
|
|
235
|
+
]
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
Without a read API the same server still answers `x207_circle_overview`, `x207_member_inspect`, `x207_epoch_report` and `x207_simulate_clearing` from the contract alone: on `x207-v013.testnet` `x207_circle_overview` returns `member_count` 24 and the rules (`epoch_length` 600, fees 10 and 5 bps, `prune_delay_epochs` 2), and `x207_epoch_report` for epoch 1 shows the phase `finalized` with 210 edges and 15 participants.
|
|
240
|
+
|
|
241
|
+
## Safety
|
|
242
|
+
|
|
243
|
+
- The read tools call contract views and `GET` requests only; there is no code path in them that signs, sends or changes anything. The write tools are described above and are off by default.
|
|
244
|
+
- Inputs are checked before any call: account ids, hashes, amounts as integer strings, at most 300 edges for a simulation, at most 200 items per page.
|
|
245
|
+
- Path segments are encoded, so an input cannot reach another endpoint of the read API.
|
|
246
|
+
- A circle on testnet reads from a testnet endpoint by default.
|
|
247
|
+
|
|
248
|
+
## Develop
|
|
249
|
+
|
|
250
|
+
```bash
|
|
251
|
+
npm run build # from the repository root of sdk
|
|
252
|
+
npx vitest run packages/mcp
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
The tests play the NEAR RPC and the read API with a mock `fetch`, run the HTTP transport on a local port, and drive the built `x207-mcp` over stdio (that test skips itself when `dist` is not built).
|
|
256
|
+
|
|
257
|
+
## License
|
|
258
|
+
|
|
259
|
+
Apache-2.0
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** The most of one read API answer that the server reads (security review batch 8): a broken or hostile backend cannot fill the memory of the server. */
|
|
2
|
+
export declare const MAX_BACKEND_BYTES = 4194304;
|
|
3
|
+
export type BackendGet = (path: string, query?: Record<string, string | number | boolean | undefined>) => Promise<unknown>;
|
|
4
|
+
export declare class BackendError extends Error {
|
|
5
|
+
readonly status: number;
|
|
6
|
+
constructor(status: number, message: string);
|
|
7
|
+
}
|
|
8
|
+
/** `path` is relative to `/x207/circles/{circle}` and made of fixed words and `seg(...)` parts. */
|
|
9
|
+
export declare function createBackendGet(options: {
|
|
10
|
+
baseUrl: string;
|
|
11
|
+
circleId: string;
|
|
12
|
+
timeoutMs: number;
|
|
13
|
+
fetch?: typeof fetch;
|
|
14
|
+
}): BackendGet;
|
|
15
|
+
/** One path segment, encoded. */
|
|
16
|
+
export declare const seg: (value: string | number) => string;
|
|
17
|
+
/** A POST with a JSON body against the read API / marketplace book. Used only by the write tools (SD-082), which sign the body first. */
|
|
18
|
+
export type BackendPost = (path: string, body: unknown) => Promise<unknown>;
|
|
19
|
+
export declare function createBackendPost(options: {
|
|
20
|
+
baseUrl: string;
|
|
21
|
+
circleId: string;
|
|
22
|
+
timeoutMs: number;
|
|
23
|
+
fetch?: typeof fetch;
|
|
24
|
+
}): BackendPost;
|
|
25
|
+
//# sourceMappingURL=backend.d.ts.map
|
package/dist/backend.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { readCapped } from "./net-guard.js";
|
|
2
|
+
/** The most of one read API answer that the server reads (security review batch 8): a broken or hostile backend cannot fill the memory of the server. */
|
|
3
|
+
export const MAX_BACKEND_BYTES = 4_194_304;
|
|
4
|
+
export class BackendError extends Error {
|
|
5
|
+
status;
|
|
6
|
+
constructor(status, message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.status = status;
|
|
9
|
+
this.name = "BackendError";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/** `path` is relative to `/x207/circles/{circle}` and made of fixed words and `seg(...)` parts. */
|
|
13
|
+
export function createBackendGet(options) {
|
|
14
|
+
const fetchFn = options.fetch ?? fetch;
|
|
15
|
+
return async (path, query = {}) => {
|
|
16
|
+
const qs = new URLSearchParams();
|
|
17
|
+
for (const [k, v] of Object.entries(query))
|
|
18
|
+
if (v !== undefined)
|
|
19
|
+
qs.set(k, String(v));
|
|
20
|
+
const url = `${options.baseUrl}/x207/circles/${encodeURIComponent(options.circleId)}${path}${qs.size > 0 ? `?${qs}` : ""}`;
|
|
21
|
+
let response;
|
|
22
|
+
try {
|
|
23
|
+
response = await fetchFn(url, { method: "GET", headers: { accept: "application/json" }, signal: AbortSignal.timeout(options.timeoutMs) });
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
throw new BackendError(0, `read API unreachable (${path.split("?")[0]}): ${error.message}`);
|
|
27
|
+
}
|
|
28
|
+
const capped = await readCapped(response, MAX_BACKEND_BYTES);
|
|
29
|
+
if (capped.cut)
|
|
30
|
+
throw new BackendError(response.status, `read API ${path.split("?")[0]} answered more than ${MAX_BACKEND_BYTES} bytes`);
|
|
31
|
+
const text = capped.text;
|
|
32
|
+
if (!response.ok) {
|
|
33
|
+
let detail = text.replace(/\s+/g, " ").slice(0, 200);
|
|
34
|
+
try {
|
|
35
|
+
const body = JSON.parse(text);
|
|
36
|
+
const e = typeof body.error === "object" ? body.error : undefined;
|
|
37
|
+
detail = [e?.code ?? (typeof body.error === "string" ? body.error : undefined), e?.message ?? body.message].filter(Boolean).join(": ") || detail;
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
// not JSON: the cut text stays
|
|
41
|
+
}
|
|
42
|
+
throw new BackendError(response.status, `read API ${path.split("?")[0]} answered ${response.status}: ${detail}`);
|
|
43
|
+
}
|
|
44
|
+
try {
|
|
45
|
+
return JSON.parse(text);
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
throw new BackendError(response.status, `read API ${path.split("?")[0]} did not answer JSON`);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/** One path segment, encoded. */
|
|
53
|
+
export const seg = (value) => encodeURIComponent(String(value));
|
|
54
|
+
export function createBackendPost(options) {
|
|
55
|
+
const fetchFn = options.fetch ?? fetch;
|
|
56
|
+
return async (path, body) => {
|
|
57
|
+
const url = `${options.baseUrl}/x207/circles/${encodeURIComponent(options.circleId)}${path}`;
|
|
58
|
+
let response;
|
|
59
|
+
try {
|
|
60
|
+
response = await fetchFn(url, { method: "POST", headers: { accept: "application/json", "content-type": "application/json" }, body: JSON.stringify(body), signal: AbortSignal.timeout(options.timeoutMs) });
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
throw new BackendError(0, `read API unreachable (POST ${path}): ${error.message}`);
|
|
64
|
+
}
|
|
65
|
+
const capped = await readCapped(response, MAX_BACKEND_BYTES);
|
|
66
|
+
if (capped.cut)
|
|
67
|
+
throw new BackendError(response.status, `read API POST ${path} answered more than ${MAX_BACKEND_BYTES} bytes`);
|
|
68
|
+
const text = capped.text;
|
|
69
|
+
let parsed = null;
|
|
70
|
+
try {
|
|
71
|
+
parsed = JSON.parse(text);
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
// not JSON
|
|
75
|
+
}
|
|
76
|
+
if (!response.ok) {
|
|
77
|
+
const e = parsed?.error;
|
|
78
|
+
throw new BackendError(response.status, `POST ${path} answered ${response.status}: ${[e?.code, e?.message].filter(Boolean).join(": ") || text.replace(/\s+/g, " ").slice(0, 200)}`);
|
|
79
|
+
}
|
|
80
|
+
return parsed;
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=backend.js.map
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// x207-mcp: the MCP server of x207. stdio by default (for Claude Desktop and other local clients); `--http` for Streamable HTTP (read tools only).
|
|
3
|
+
// X207_CIRCLE_CONTRACT=x207-v013.testnet X207_BACKEND_URL=http://127.0.0.1:8080 x207-mcp [--http [--port 3207] [--host 127.0.0.1]]
|
|
4
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
5
|
+
import { ConfigError, configFromEnv } from "./config.js";
|
|
6
|
+
import { startHttpServer } from "./http.js";
|
|
7
|
+
import { createX207McpServer } from "./server.js";
|
|
8
|
+
import { writeConfigFromEnv } from "./write-config.js";
|
|
9
|
+
const USAGE = "usage: x207-mcp [--http [--port <n>] [--host <address>]] (environment: X207_CIRCLE_CONTRACT, X207_CIRCLE_ID, X207_RPC_URL, X207_BACKEND_URL, X207_TIMEOUT_MS, X207_ALLOWED_HOSTS)";
|
|
10
|
+
async function main(argv) {
|
|
11
|
+
const args = new Map();
|
|
12
|
+
for (let i = 0; i < argv.length; i += 1) {
|
|
13
|
+
const a = argv[i];
|
|
14
|
+
if (a === "--help" || a === "-h") {
|
|
15
|
+
console.error(USAGE);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
if (a === "--http")
|
|
19
|
+
args.set("http", true);
|
|
20
|
+
else if (a === "--port" || a === "--host") {
|
|
21
|
+
const v = argv[(i += 1)];
|
|
22
|
+
if (v === undefined || v.startsWith("--"))
|
|
23
|
+
throw new ConfigError(`${a} needs a value\n${USAGE}`);
|
|
24
|
+
args.set(a.slice(2), v);
|
|
25
|
+
}
|
|
26
|
+
else
|
|
27
|
+
throw new ConfigError(`unknown argument ${a}\n${USAGE}`);
|
|
28
|
+
}
|
|
29
|
+
const config = configFromEnv();
|
|
30
|
+
if (args.has("http")) {
|
|
31
|
+
if (process.env.X207_MCP_WRITE === "1")
|
|
32
|
+
console.error("x207-mcp: X207_MCP_WRITE is ignored on HTTP: the HTTP server only has the read tools");
|
|
33
|
+
const port = args.has("port") ? Number(args.get("port")) : 3207;
|
|
34
|
+
if (!Number.isInteger(port) || port < 0 || port > 65535)
|
|
35
|
+
throw new ConfigError("--port must be an integer from 0 to 65535");
|
|
36
|
+
const allowedHosts = (process.env.X207_ALLOWED_HOSTS ?? "").split(",").map((h) => h.trim()).filter((h) => h !== "");
|
|
37
|
+
const { url } = await startHttpServer(config, { port, host: args.get("host") ?? "127.0.0.1", ...(allowedHosts.length > 0 ? { allowedHosts } : {}) });
|
|
38
|
+
console.error(`x207-mcp: Streamable HTTP (read tools only) on ${url}`);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
// stdout belongs to the protocol: every message of ours goes to stderr
|
|
42
|
+
const write = writeConfigFromEnv();
|
|
43
|
+
await createX207McpServer(config, "stdio", { write }).connect(new StdioServerTransport());
|
|
44
|
+
console.error(`x207-mcp: stdio, circle ${config.circleContract}, read API ${config.backendUrl ?? "not configured (contract views only)"}, ${write ? `WRITE tools on for ${write.account}` : "read tools only"}`);
|
|
45
|
+
}
|
|
46
|
+
main(process.argv.slice(2)).catch((error) => {
|
|
47
|
+
console.error(`x207-mcp: ${error instanceof ConfigError ? error.message : error}`);
|
|
48
|
+
process.exit(1);
|
|
49
|
+
});
|
|
50
|
+
//# sourceMappingURL=cli.js.map
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface McpConfig {
|
|
2
|
+
/** The circle contract account, e.g. `x207-v013.testnet`. */
|
|
3
|
+
circleContract: string;
|
|
4
|
+
/** The circle id in the paths of the read API, e.g. `agents-main`. */
|
|
5
|
+
circleId: string;
|
|
6
|
+
/** NEAR JSON-RPC endpoints, tried in order. */
|
|
7
|
+
rpcUrls: readonly string[];
|
|
8
|
+
/** Base URL of the x207 read API (`/x207/circles/{circle}/...`). Without it the tools that need it say so, and the tools that can use the contract views fall back to them. */
|
|
9
|
+
backendUrl: string | null;
|
|
10
|
+
/** Timeout of one RPC or read API call in ms. */
|
|
11
|
+
timeoutMs: number;
|
|
12
|
+
fetch?: typeof fetch;
|
|
13
|
+
}
|
|
14
|
+
/** Tried in order: a public endpoint that answers 429 hands over to the next. */
|
|
15
|
+
export declare const TESTNET_RPC_URLS: readonly ["https://rpc.testnet.fastnear.com", "https://rpc.testnet.near.org"];
|
|
16
|
+
export declare class ConfigError extends Error {
|
|
17
|
+
}
|
|
18
|
+
/** `X207_CIRCLE_CONTRACT` (required), `X207_CIRCLE_ID` (default `agents-main`), `X207_RPC_URL` (comma separated; default by network), `X207_BACKEND_URL` (optional), `X207_TIMEOUT_MS`. */
|
|
19
|
+
export declare function configFromEnv(env?: Record<string, string | undefined>): McpConfig;
|
|
20
|
+
//# sourceMappingURL=config.d.ts.map
|