agoragentic-mcp 1.3.0 → 1.3.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/mcp-server.js CHANGED
@@ -1,787 +1,377 @@
1
1
  #!/usr/bin/env node
2
- /**
3
- * Agoragentic MCP Server — v2.0
4
- * ==============================
5
- *
6
- * Model Context Protocol server that exposes the full Agoragentic marketplace
7
- * to any MCP-compatible client (Claude Desktop, VS Code, Cursor, etc.)
8
- *
9
- * v2.0 adds:
10
- * - Vault Memory (persistent KV store)
11
- * - Vault Secrets (encrypted credential storage)
12
- * - Agent Passport (NFT identity)
13
- * - x402 payment info
14
- *
15
- * Setup:
16
- * npm install @modelcontextprotocol/sdk
17
- * node mcp-server.js
18
- *
19
- * Configure in Claude Desktop (claude_desktop_config.json):
20
- * {
21
- * "mcpServers": {
22
- * "agoragentic": {
23
- * "command": "node",
24
- * "args": ["/path/to/mcp-server.js"],
25
- * "env": { "AGORAGENTIC_API_KEY": "amk_your_key" }
26
- * }
27
- * }
28
- * }
29
- */
30
-
31
- const { Server } = require("@modelcontextprotocol/sdk/server/index.js");
32
- const { StdioServerTransport } = require("@modelcontextprotocol/sdk/server/stdio.js");
33
- const {
34
- CallToolRequestSchema,
35
- ListToolsRequestSchema,
36
- ListResourcesRequestSchema,
37
- ReadResourceRequestSchema,
38
- ListPromptsRequestSchema,
39
- GetPromptRequestSchema,
40
- } = require("@modelcontextprotocol/sdk/types.js");
41
-
42
- const AGORAGENTIC_BASE = "https://agoragentic.com";
43
- const API_KEY = process.env.AGORAGENTIC_API_KEY || "";
44
-
45
- // ─── HTTP helper ─────────────────────────────────────────
46
-
47
- async function apiCall(method, path, body = null) {
48
- const url = `${AGORAGENTIC_BASE}${path}`;
49
- const headers = { "Content-Type": "application/json" };
50
- if (API_KEY) headers["Authorization"] = `Bearer ${API_KEY}`;
51
-
52
- const controller = new AbortController();
53
- const timeout = setTimeout(() => controller.abort(), 30000);
54
-
55
- try {
56
- const options = { method, headers, signal: controller.signal };
57
- if (body) options.body = JSON.stringify(body);
58
-
59
- const resp = await fetch(url, options);
60
- return resp.json();
61
- } finally {
62
- clearTimeout(timeout);
2
+ 'use strict';
3
+
4
+ const readline = require('readline');
5
+ const crypto = require('crypto');
6
+ const { version: PACKAGE_VERSION } = require('./package.json');
7
+
8
+ const REMOTE_MCP_URL = process.env.AGORAGENTIC_MCP_URL || 'https://agoragentic.com/api/mcp';
9
+ const API_KEY = process.env.AGORAGENTIC_API_KEY || '';
10
+ const ACP_MODE = process.argv.includes('--acp');
11
+
12
+ const ACP_TOOLS = [
13
+ {
14
+ name: 'agoragentic_execute',
15
+ description: 'Route a task through Agent OS execute() with provider selection, fallback, receipts, and settlement.',
16
+ },
17
+ {
18
+ name: 'agoragentic_match',
19
+ description: 'Preview routed providers before execution.',
20
+ },
21
+ {
22
+ name: 'agoragentic_quote',
23
+ description: 'Create a bounded quote before paid execution.',
24
+ },
25
+ {
26
+ name: 'agoragentic_status',
27
+ description: 'Inspect execution status for an invocation.',
28
+ },
29
+ {
30
+ name: 'agoragentic_receipt',
31
+ description: 'Fetch normalized receipt and settlement metadata.',
32
+ },
33
+ {
34
+ name: 'agoragentic_browse_services',
35
+ description: 'Browse stable x402 edge resources.',
36
+ },
37
+ {
38
+ name: 'agoragentic_call_service',
39
+ description: 'Call a stable x402 edge resource after payment challenge handling.',
40
+ },
41
+ {
42
+ name: 'agoragentic_edge_receipt',
43
+ description: 'Inspect x402 edge receipt metadata.',
44
+ },
45
+ {
46
+ name: 'agoragentic_x402_test',
47
+ description: 'Exercise the free x402 pipeline canary.',
48
+ },
49
+ ];
50
+
51
+ function buildRemoteTransport() {
52
+ const { StreamableHTTPClientTransport } = require('@modelcontextprotocol/sdk/client/streamableHttp.js');
53
+ const headers = {};
54
+ if (API_KEY) {
55
+ headers.Authorization = `Bearer ${API_KEY}`;
63
56
  }
64
- }
65
-
66
- // ─── MCP Server ──────────────────────────────────────────
67
-
68
- const server = new Server(
69
- { name: "agoragentic", version: "2.0.0" },
70
- { capabilities: { tools: {}, resources: {}, prompts: {} } }
71
- );
72
-
73
- // ─── Tools ───────────────────────────────────────────────
74
-
75
- // Extracted so both MCP and ACP modes can share the same definitions
76
- function getToolList() {
77
- return [
78
- // ── Core Marketplace ──
79
- {
80
- name: "agoragentic_register",
81
- description: "Register as a new agent on Agoragentic. Returns an API key and access to the Starter Pack. Starter pack rewards are fee discounts, not free credits.",
82
- annotations: { title: "Register Agent", readOnlyHint: false, destructiveHint: false, idempotentHint: false, openWorldHint: true },
83
- inputSchema: {
84
- type: "object",
85
- properties: {
86
- agent_name: { type: "string", description: "Your agent's display name (must be unique across the marketplace)" },
87
- agent_type: { type: "string", enum: ["buyer", "seller", "both"], default: "both", description: "Agent role: buyer (consume services), seller (provide services), or both" }
88
- },
89
- required: ["agent_name"]
90
- }
91
- },
92
- {
93
- name: "agoragentic_search",
94
- description: "Search Agoragentic for agent capabilities. Find tools, services, datasets, and skills available through the capability router. Returns names, descriptions, prices (USDC), and IDs you can use to invoke them.",
95
- annotations: { title: "Search Capabilities", readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: true },
96
- inputSchema: {
97
- type: "object",
98
- properties: {
99
- query: { type: "string", description: "Search term to filter capabilities (e.g., 'summarize', 'translate', 'research')" },
100
- category: { type: "string", description: "Category filter (e.g., research, creative, data, agent-upgrades, infrastructure)" },
101
- max_price: { type: "number", description: "Maximum price in USDC to filter results by cost" },
102
- limit: { type: "number", default: 10, description: "Maximum number of results to return (1 to 50)" }
103
- }
104
- }
105
- },
106
- {
107
- name: "agoragentic_invoke",
108
- description: "Invoke (call/use) a capability from the Agoragentic marketplace. Payment is automatic from your USDC balance. Returns the capability's output.",
109
- annotations: { title: "Invoke Capability", readOnlyHint: false, destructiveHint: false, idempotentHint: false, openWorldHint: true },
110
- inputSchema: {
111
- type: "object",
112
- properties: {
113
- capability_id: { type: "string", description: "The capability ID returned from a search result" },
114
- input: { type: "object", description: "Input payload for the capability as a JSON object", default: {} }
115
- },
116
- required: ["capability_id"]
117
- }
118
- },
119
- {
120
- name: "agoragentic_vault",
121
- description: "View your agent's vault (inventory). Shows all items you own: skills, datasets, licenses, collectibles, and service results from previous invocations.",
122
- annotations: { title: "View Vault", readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: true },
123
- inputSchema: {
124
- type: "object",
125
- properties: {
126
- item_type: { type: "string", description: "Filter by item type: skill, digital_asset, nft, license, subscription, or collectible" },
127
- include_nfts: { type: "boolean", description: "Include on-chain NFTs minted on Base L2 blockchain", default: false },
128
- limit: { type: "number", default: 20, description: "Maximum number of vault items to return" }
129
- }
130
- }
131
- },
132
- {
133
- name: "agoragentic_categories",
134
- description: "List all available marketplace categories and how many capabilities are in each.",
135
- annotations: { title: "List Categories", readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: true },
136
- inputSchema: { type: "object", properties: {} }
137
- },
138
-
139
- // ── Vault Memory ──
140
- {
141
- name: "agoragentic_memory_write",
142
- description: "Write a key value pair to your persistent agent memory. Survives across sessions, IDEs, and machines. Costs $0.10 per write via the marketplace.",
143
- annotations: { title: "Write Memory", readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: true },
144
- inputSchema: {
145
- type: "object",
146
- properties: {
147
- key: { type: "string", description: "Memory key identifier, maximum 256 characters" },
148
- value: { type: "string", description: "Value to store, maximum 64KB. Can be any string or serialized JSON." },
149
- namespace: { type: "string", default: "default", description: "Namespace to organize keys into logical groups" },
150
- ttl_seconds: { type: "number", description: "Automatic expiration in seconds. Omit for permanent storage." }
151
- },
152
- required: ["key", "value"]
153
- }
154
- },
155
- {
156
- name: "agoragentic_memory_read",
157
- description: "Read from your persistent agent memory. Free, no cost to recall your own data. Returns a single key or lists all keys.",
158
- annotations: { title: "Read Memory", readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: true },
159
- inputSchema: {
160
- type: "object",
161
- properties: {
162
- key: { type: "string", description: "Specific key to read. Omit to list all stored keys." },
163
- namespace: { type: "string", default: "default", description: "Namespace to read from" },
164
- prefix: { type: "string", description: "Filter keys by prefix when listing all keys" }
165
- }
166
- }
167
- },
168
57
 
169
- // ── Vault Secrets ──
170
- {
171
- name: "agoragentic_secret_store",
172
- description: "Store an encrypted secret (API key, token, password) in your vault. AES 256 encrypted at rest. Costs $0.25 via the marketplace.",
173
- annotations: { title: "Store Secret", readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: true },
174
- inputSchema: {
175
- type: "object",
176
- properties: {
177
- label: { type: "string", description: "Label for the secret, for example 'openai_key' or 'stripe_token'" },
178
- secret: { type: "string", description: "The secret value to encrypt and store securely" },
179
- hint: { type: "string", description: "Optional human readable hint to help you remember what this secret is for" }
180
- },
181
- required: ["label", "secret"]
182
- }
183
- },
184
- {
185
- name: "agoragentic_secret_retrieve",
186
- description: "Retrieve a decrypted secret from your vault. Free, no cost to access your own credentials.",
187
- annotations: { title: "Retrieve Secret", readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: true },
188
- inputSchema: {
189
- type: "object",
190
- properties: {
191
- label: { type: "string", description: "Label of the secret to retrieve. Omit to list all stored secrets." }
192
- }
193
- }
58
+ return new StreamableHTTPClientTransport(new URL(REMOTE_MCP_URL), {
59
+ requestInit: {
60
+ headers,
194
61
  },
195
-
196
- // ── Passport ──
197
- {
198
- name: "agoragentic_passport",
199
- description: "Check your Agoragentic Passport NFT status, or get info about the passport system. Passports are on chain identity NFTs on Base L2.",
200
- annotations: { title: "Agent Passport", readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: true },
201
- inputSchema: {
202
- type: "object",
203
- properties: {
204
- action: {
205
- type: "string",
206
- enum: ["check", "info", "verify"],
207
- default: "check",
208
- description: "Action to perform: check your passport status, info for system overview, or verify a wallet address"
209
- },
210
- wallet_address: { type: "string", description: "Wallet address to verify ownership. Only used when action is set to verify." }
211
- }
212
- }
213
- }
214
- ];
62
+ });
215
63
  }
216
64
 
217
- server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: getToolList() }));
218
-
219
- // Extracted so both MCP and ACP modes can share the same execution logic
220
- async function executeToolCall(name, args) {
221
-
222
- try {
223
- switch (name) {
224
- case "agoragentic_register": {
225
- const data = await apiCall("POST", "/api/quickstart", {
226
- name: args.agent_name,
227
- type: args.agent_type || "both"
228
- });
229
- return {
230
- content: [{
231
- type: "text",
232
- text: JSON.stringify({
233
- status: "registered",
234
- agent_id: data.agent?.id,
235
- api_key: data.api_key,
236
- fee_rate: "3.00%",
237
- message: "Save your API key! Set it as AGORAGENTIC_API_KEY environment variable.",
238
- next: "Use agoragentic_search to find capabilities, or agoragentic_invoke to call one directly"
239
- }, null, 2)
240
- }]
241
- };
242
- }
243
-
244
- case "agoragentic_search": {
245
- const params = new URLSearchParams({ limit: args.limit || 10, status: "active" });
246
- if (args.query) params.set("search", args.query);
247
- if (args.category) params.set("category", args.category);
248
-
249
- const data = await apiCall("GET", `/api/capabilities?${params}`);
250
- let capabilities = Array.isArray(data) ? data : (data.capabilities || []);
251
-
252
- if (args.max_price !== undefined) {
253
- capabilities = capabilities.filter(c => (c.price_per_unit || 0) <= args.max_price);
254
- }
255
-
256
- const results = capabilities.slice(0, args.limit || 10).map(c => ({
257
- id: c.id,
258
- name: c.name,
259
- description: (c.description || "").substring(0, 200),
260
- category: c.category,
261
- price_usdc: c.price_per_unit,
262
- seller: c.seller_name,
263
- type: c.listing_type
264
- }));
265
-
266
- return {
267
- content: [{
268
- type: "text",
269
- text: JSON.stringify({ total: results.length, capabilities: results }, null, 2)
270
- }]
271
- };
272
- }
273
-
274
- case "agoragentic_invoke": {
275
- if (!API_KEY) {
276
- return { content: [{ type: "text", text: "Error: Set AGORAGENTIC_API_KEY environment variable first. Use agoragentic_register to get one." }] };
277
- }
278
- const capId = String(args.capability_id || "").replace(/[^a-zA-Z0-9\-_]/g, "");
279
- if (!capId) {
280
- return { content: [{ type: "text", text: "Error: Invalid capability_id." }] };
281
- }
282
- const data = await apiCall("POST", `/api/invoke/${capId}`, {
283
- input: args.input || {}
284
- });
285
- return {
286
- content: [{
287
- type: "text",
288
- text: JSON.stringify({
289
- invocation_id: data.invocation_id,
290
- status: data.status,
291
- output: data.response,
292
- cost_usdc: data.cost,
293
- balance_after: data.buyer_balance,
294
- nft: data.nft || null,
295
- vault_item: data.vault || null
296
- }, null, 2)
297
- }]
298
- };
299
- }
300
-
301
- case "agoragentic_vault": {
302
- if (!API_KEY) {
303
- return { content: [{ type: "text", text: "Error: Set AGORAGENTIC_API_KEY environment variable first." }] };
304
- }
305
- const params = new URLSearchParams({ limit: args.limit || 20 });
306
- if (args.item_type) params.set("type", args.item_type);
307
- if (args.include_nfts) params.set("include", "nfts");
308
-
309
- const data = await apiCall("GET", `/api/inventory?${params}`);
310
- const vault = data.vault || {};
311
- return {
312
- content: [{
313
- type: "text",
314
- text: JSON.stringify({
315
- agent: vault.agent_name,
316
- total_items: vault.total_items,
317
- items: (vault.items || []).map(i => ({
318
- name: i.item_name,
319
- type: i.item_type,
320
- status: i.status,
321
- acquired: i.acquired_at,
322
- integrity_warning: i.integrity_warning
323
- })),
324
- nfts: data.nfts || null
325
- }, null, 2)
326
- }]
327
- };
328
- }
329
-
330
- case "agoragentic_categories": {
331
- const data = await apiCall("GET", "/api/categories");
332
- return {
333
- content: [{
334
- type: "text",
335
- text: JSON.stringify(data, null, 2)
336
- }]
337
- };
338
- }
339
-
340
- // ── Vault Memory ──
341
-
342
- case "agoragentic_memory_write": {
343
- if (!API_KEY) {
344
- return { content: [{ type: "text", text: "Error: API key required." }] };
345
- }
346
- // Find the Memory Slots listing and invoke through marketplace
347
- const searchData = await apiCall("GET", "/api/capabilities?search=Vault+Memory+Slots&limit=1");
348
- const listings = Array.isArray(searchData) ? searchData : (searchData.capabilities || []);
349
- const memoryListing = listings.find(l => l.name === 'Vault Memory Slots');
350
-
351
- if (memoryListing) {
352
- const data = await apiCall("POST", `/api/invoke/${memoryListing.id}`, {
353
- input: {
354
- key: args.key,
355
- value: args.value,
356
- namespace: args.namespace || 'default',
357
- ttl_seconds: args.ttl_seconds
358
- }
359
- });
360
- return {
361
- content: [{
362
- type: "text",
363
- text: JSON.stringify({
364
- status: data.status,
365
- output: data.response?.output || data.response,
366
- cost: data.cost,
367
- balance_after: data.buyer_balance
368
- }, null, 2)
369
- }]
370
- };
371
- }
372
-
373
- // Fallback: direct API call
374
- const data = await apiCall("POST", "/api/vault/memory", {
375
- input: {
376
- key: args.key,
377
- value: args.value,
378
- namespace: args.namespace || 'default',
379
- ttl_seconds: args.ttl_seconds
380
- }
381
- });
382
- return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
383
- }
384
-
385
- case "agoragentic_memory_read": {
386
- if (!API_KEY) {
387
- return { content: [{ type: "text", text: "Error: API key required." }] };
388
- }
389
- const params = new URLSearchParams();
390
- if (args.key) params.set("key", args.key);
391
- if (args.namespace) params.set("namespace", args.namespace);
392
- if (args.prefix) params.set("prefix", args.prefix);
393
-
394
- const data = await apiCall("GET", `/api/vault/memory?${params}`);
395
- return {
396
- content: [{
397
- type: "text",
398
- text: JSON.stringify(data.output || data, null, 2)
399
- }]
400
- };
401
- }
402
-
403
- // ── Vault Secrets ──
404
-
405
- case "agoragentic_secret_store": {
406
- if (!API_KEY) {
407
- return { content: [{ type: "text", text: "Error: API key required." }] };
408
- }
409
- const searchData = await apiCall("GET", "/api/capabilities?search=Vault+Secrets+Locker&limit=1");
410
- const listings = Array.isArray(searchData) ? searchData : (searchData.capabilities || []);
411
- const secretsListing = listings.find(l => l.name === 'Vault Secrets Locker');
412
-
413
- if (secretsListing) {
414
- const data = await apiCall("POST", `/api/invoke/${secretsListing.id}`, {
415
- input: {
416
- label: args.label,
417
- secret: args.secret,
418
- hint: args.hint
419
- }
420
- });
421
- return {
422
- content: [{
423
- type: "text",
424
- text: JSON.stringify({
425
- status: data.status,
426
- output: data.response?.output || data.response,
427
- cost: data.cost,
428
- balance_after: data.buyer_balance
429
- }, null, 2)
430
- }]
431
- };
432
- }
65
+ async function connectRemoteClient() {
66
+ const { Client } = require('@modelcontextprotocol/sdk/client/index.js');
67
+ const transport = buildRemoteTransport();
68
+ const client = new Client(
69
+ { name: 'agoragentic-mcp', version: PACKAGE_VERSION },
70
+ { capabilities: { tools: {}, resources: {}, prompts: {} } }
71
+ );
72
+
73
+ client.onerror = (error) => {
74
+ if (!error) return;
75
+ const message = error instanceof Error ? error.message : String(error);
76
+ console.error(`[agoragentic-mcp] remote client error: ${message}`);
77
+ };
78
+
79
+ await client.connect(transport);
80
+ return { client, transport };
81
+ }
433
82
 
434
- const data = await apiCall("POST", "/api/vault/secrets", {
435
- input: { label: args.label, secret: args.secret, hint: args.hint }
436
- });
437
- return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
438
- }
83
+ async function runMcpRelay() {
84
+ const { Server } = require('@modelcontextprotocol/sdk/server/index.js');
85
+ const { StdioServerTransport } = require('@modelcontextprotocol/sdk/server/stdio.js');
86
+ const {
87
+ CallToolRequestSchema,
88
+ ListToolsRequestSchema,
89
+ ListResourcesRequestSchema,
90
+ ReadResourceRequestSchema,
91
+ ListPromptsRequestSchema,
92
+ GetPromptRequestSchema,
93
+ } = require('@modelcontextprotocol/sdk/types.js');
94
+
95
+ const { client, transport } = await connectRemoteClient();
96
+
97
+ const server = new Server(
98
+ { name: 'agoragentic', version: PACKAGE_VERSION },
99
+ { capabilities: { tools: {}, resources: {}, prompts: {} } }
100
+ );
101
+
102
+ server.setRequestHandler(ListToolsRequestSchema, async (request) => {
103
+ return client.listTools(request.params);
104
+ });
439
105
 
440
- case "agoragentic_secret_retrieve": {
441
- if (!API_KEY) {
442
- return { content: [{ type: "text", text: "Error: API key required." }] };
443
- }
444
- const params = new URLSearchParams();
445
- if (args.label) params.set("label", args.label);
446
-
447
- const data = await apiCall("GET", `/api/vault/secrets?${params}`);
448
- return {
449
- content: [{
450
- type: "text",
451
- text: JSON.stringify(data.output || data, null, 2)
452
- }]
453
- };
454
- }
106
+ server.setRequestHandler(CallToolRequestSchema, async (request) => {
107
+ return client.callTool(request.params);
108
+ });
455
109
 
456
- // ── Passport ──
110
+ server.setRequestHandler(ListResourcesRequestSchema, async (request) => {
111
+ return client.listResources(request.params);
112
+ });
457
113
 
458
- case "agoragentic_passport": {
459
- const action = args.action || "check";
114
+ server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
115
+ return client.readResource(request.params);
116
+ });
460
117
 
461
- if (action === "info") {
462
- const data = await apiCall("GET", "/api/passport/info");
463
- return { content: [{ type: "text", text: JSON.stringify(data.output || data, null, 2) }] };
464
- }
118
+ server.setRequestHandler(ListPromptsRequestSchema, async (request) => {
119
+ return client.listPrompts(request.params);
120
+ });
465
121
 
466
- if (action === "verify" && args.wallet_address) {
467
- const data = await apiCall("GET", `/api/passport/verify/${args.wallet_address}`);
468
- return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
469
- }
122
+ server.setRequestHandler(GetPromptRequestSchema, async (request) => {
123
+ return client.getPrompt(request.params);
124
+ });
470
125
 
471
- if (!API_KEY) {
472
- return { content: [{ type: "text", text: "Error: API key required to check passport." }] };
473
- }
474
- const data = await apiCall("GET", "/api/passport/check");
475
- return { content: [{ type: "text", text: JSON.stringify(data, null, 2) }] };
476
- }
126
+ const stdio = new StdioServerTransport();
127
+ await server.connect(stdio);
477
128
 
478
- default:
479
- return { content: [{ type: "text", text: `Unknown tool: ${name}` }] };
129
+ const shutdown = async (signal) => {
130
+ console.error(`[agoragentic-mcp] shutting down on ${signal}`);
131
+ try {
132
+ await transport.terminateSession();
133
+ } catch {
134
+ // Ignore session teardown failures during local shutdown.
480
135
  }
481
- } catch (err) {
482
- return { content: [{ type: "text", text: `Error: ${err.message}` }] };
483
- }
484
- }
485
-
486
- server.setRequestHandler(CallToolRequestSchema, async (request) => {
487
- const { name, arguments: args } = request.params;
488
- return executeToolCall(name, args);
489
- });
490
-
491
- // ─── Resources ───────────────────────────────────────────
492
-
493
- server.setRequestHandler(ListResourcesRequestSchema, async () => ({
494
- resources: [
495
- {
496
- uri: "agoragentic://marketplace/manifest",
497
- name: "Agoragentic Marketplace Manifest",
498
- description: "Machine-readable marketplace discovery manifest",
499
- mimeType: "application/json"
500
- },
501
- {
502
- uri: "agoragentic://marketplace/docs",
503
- name: "Agoragentic API Documentation",
504
- description: "Full API reference",
505
- mimeType: "text/html"
506
- },
507
- {
508
- uri: "agoragentic://vault/info",
509
- name: "Vault Enhancement Services",
510
- description: "Info about Memory Slots, Secrets Locker, and Config Snapshots",
511
- mimeType: "application/json"
512
- },
513
- {
514
- uri: "agoragentic://passport/info",
515
- name: "Agent Passport Info",
516
- description: "NFT-based identity system details",
517
- mimeType: "application/json"
136
+ try {
137
+ await transport.close();
138
+ } catch {
139
+ // Ignore transport close failures during local shutdown.
518
140
  }
519
- ]
520
- }));
521
-
522
- server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
523
- const { uri } = request.params;
524
-
525
- if (uri === "agoragentic://marketplace/manifest") {
526
- const data = await apiCall("GET", "/.well-known/agent-marketplace.json");
527
- return {
528
- contents: [{
529
- uri,
530
- mimeType: "application/json",
531
- text: JSON.stringify(data, null, 2)
532
- }]
533
- };
534
- }
535
-
536
- if (uri === "agoragentic://marketplace/docs") {
537
- return {
538
- contents: [{
539
- uri,
540
- mimeType: "text/plain",
541
- text: "Full API documentation available at: https://agoragentic.com/docs.html"
542
- }]
543
- };
544
- }
141
+ process.exit(0);
142
+ };
545
143
 
546
- if (uri === "agoragentic://vault/info") {
547
- const data = await apiCall("GET", "/api/vault/info");
548
- return {
549
- contents: [{
550
- uri,
551
- mimeType: "application/json",
552
- text: JSON.stringify(data, null, 2)
553
- }]
554
- };
555
- }
556
-
557
- if (uri === "agoragentic://passport/info") {
558
- const data = await apiCall("GET", "/api/passport/info");
559
- return {
560
- contents: [{
561
- uri,
562
- mimeType: "application/json",
563
- text: JSON.stringify(data, null, 2)
564
- }]
565
- };
566
- }
144
+ process.on('SIGINT', () => {
145
+ void shutdown('SIGINT');
146
+ });
147
+ process.on('SIGTERM', () => {
148
+ void shutdown('SIGTERM');
149
+ });
567
150
 
568
- throw new Error(`Unknown resource: ${uri}`);
569
- });
151
+ console.error(`[agoragentic-mcp] stdio relay ${PACKAGE_VERSION} connected to ${REMOTE_MCP_URL}`);
152
+ }
570
153
 
571
- // ─── Prompts ─────────────────────────────────────────────
572
-
573
- server.setRequestHandler(ListPromptsRequestSchema, async () => ({
574
- prompts: [
575
- {
576
- name: "quickstart",
577
- description: "Get started with Agoragentic. Walks you through registration, searching for capabilities, and making your first invocation.",
578
- arguments: [
579
- { name: "agent_name", description: "Name for your new agent", required: true },
580
- { name: "task", description: "What you want to accomplish (e.g., 'summarize documents', 'translate text')", required: false }
581
- ]
154
+ function buildAcpInitializeResult() {
155
+ return {
156
+ protocolVersion: 1,
157
+ agentInfo: {
158
+ name: 'Agoragentic Agent OS',
159
+ version: PACKAGE_VERSION,
160
+ description:
161
+ 'Agent OS integrations for deployed agents and swarms: execute-first routing, receipts, x402 edge calls, and Base USDC settlement.',
162
+ homepage: 'https://agoragentic.com',
582
163
  },
583
- {
584
- name: "find_and_invoke",
585
- description: "Search for a capability matching your task and invoke it in one guided workflow.",
586
- arguments: [
587
- { name: "task", description: "Description of what you want to accomplish", required: true },
588
- { name: "max_budget", description: "Maximum USDC budget for the invocation", required: false }
589
- ]
164
+ agentCapabilities: {
165
+ tools: true,
166
+ streaming: false,
167
+ resources: false,
168
+ prompts: false,
169
+ loadSession: false,
170
+ promptCapabilities: {
171
+ image: false,
172
+ },
590
173
  },
591
- {
592
- name: "sell_capability",
593
- description: "Guide for listing your own capability for sale on the Agoragentic marketplace.",
594
- arguments: [
595
- { name: "capability_name", description: "Name of the capability you want to sell", required: true },
596
- { name: "price", description: "Price in USDC per invocation", required: false }
597
- ]
598
- }
599
- ]
600
- }));
601
-
602
- server.setRequestHandler(GetPromptRequestSchema, async (request) => {
603
- const { name, arguments: args } = request.params;
604
-
605
- if (name === "quickstart") {
606
- return {
607
- description: "Get started with Agoragentic",
608
- messages: [
609
- {
610
- role: "user",
611
- content: {
612
- type: "text",
613
- text: `I want to get started with Agoragentic. Please:\n1. Register me as an agent named "${args?.agent_name || 'my-agent'}"\n2. Search for capabilities${args?.task ? ` related to: ${args.task}` : ''}\n3. Show me the top results and explain how to invoke one\n\nUse the agoragentic_register, agoragentic_search, and agoragentic_categories tools to help me.`
614
- }
615
- }
616
- ]
617
- };
618
- }
174
+ authMethods: [
175
+ {
176
+ type: 'env',
177
+ name: 'AGORAGENTIC_API_KEY',
178
+ configured: Boolean(API_KEY),
179
+ required: false,
180
+ instructions:
181
+ 'Optional for public discovery and x402 edge calls. Required for authenticated execute/match/status/receipt. Create one with POST /api/quickstart and intent=buyer|seller|both.',
182
+ },
183
+ ],
184
+ };
185
+ }
619
186
 
620
- if (name === "find_and_invoke") {
621
- return {
622
- description: "Find and invoke a capability",
623
- messages: [
624
- {
625
- role: "user",
626
- content: {
627
- type: "text",
628
- text: `I need to: ${args?.task || 'find a useful capability'}\n\nPlease:\n1. Search for matching capabilities${args?.max_budget ? ` under $${args.max_budget} USDC` : ''}\n2. Show me the best match with its price\n3. Invoke it if I confirm\n\nUse agoragentic_search and agoragentic_invoke tools.`
629
- }
630
- }
631
- ]
632
- };
633
- }
187
+ function buildAcpResponse(id, result) {
188
+ return {
189
+ jsonrpc: '2.0',
190
+ id,
191
+ result,
192
+ };
193
+ }
634
194
 
635
- if (name === "sell_capability") {
636
- return {
637
- description: "List a capability for sale",
638
- messages: [
639
- {
640
- role: "user",
641
- content: {
642
- type: "text",
643
- text: `I want to sell a capability called "${args?.capability_name || 'my-service'}"${args?.price ? ` for $${args.price} USDC per call` : ''}.\n\nPlease walk me through:\n1. What information I need to provide\n2. The staking bond requirement ($5 USDC, refundable after 30 days)\n3. How to register and list it\n\nCheck agoragentic_categories for available categories first.`
644
- }
645
- }
646
- ]
647
- };
648
- }
195
+ function buildAcpError(id, code, message, data) {
196
+ return {
197
+ jsonrpc: '2.0',
198
+ id,
199
+ error: data ? { code, message, data } : { code, message },
200
+ };
201
+ }
649
202
 
650
- throw new Error(`Unknown prompt: ${name}`);
651
- });
203
+ function writeAcpMessage(message) {
204
+ process.stdout.write(`${JSON.stringify(message)}\n`);
205
+ }
652
206
 
653
- // ─── ACP Mode ────────────────────────────────────────────
654
- //
655
- // When started with --acp, the server speaks the Agent Client Protocol
656
- // (JSON-RPC 2.0 over stdio) instead of MCP. This enables integration
657
- // with ACP-compatible editors like Zed, JetBrains, VS Code via Copilot, etc.
658
- //
659
- // The ACP handshake adds: protocolVersion, agentCapabilities, agentInfo, authMethods
660
- // Tool definitions and execution are identical to MCP mode.
207
+ function buildAcpSessionId() {
208
+ return `sess_${crypto.randomBytes(12).toString('hex')}`;
209
+ }
661
210
 
662
- const ACP_MODE = process.argv.includes("--acp");
211
+ function extractAcpPromptText(content) {
212
+ if (typeof content === 'string') return content;
213
+ if (!Array.isArray(content)) return '';
214
+ return content
215
+ .map((part) => {
216
+ if (!part || typeof part !== 'object') return '';
217
+ if (part.type === 'text' && typeof part.text === 'string') return part.text;
218
+ return '';
219
+ })
220
+ .filter(Boolean)
221
+ .join('\n');
222
+ }
663
223
 
664
- function createAcpServer() {
665
- const readline = require("readline");
224
+ function buildAcpPromptReply(promptText) {
225
+ const suffix = promptText ? ` Prompt received: ${promptText.slice(0, 240)}` : '';
226
+ return [
227
+ 'Agoragentic ACP adapter is a tool bridge, not a code-editing chat agent.',
228
+ 'Use tools/list, then tools/call with agoragentic_execute, agoragentic_match, agoragentic_quote, agoragentic_receipt, or stable x402 service tools.',
229
+ suffix,
230
+ ]
231
+ .filter(Boolean)
232
+ .join(' ');
233
+ }
666
234
 
235
+ async function runAcpAdapter() {
667
236
  const rl = readline.createInterface({
668
237
  input: process.stdin,
669
- output: process.stdout,
238
+ crlfDelay: Infinity,
670
239
  terminal: false,
671
240
  });
672
241
 
673
- function send(obj) {
674
- process.stdout.write(JSON.stringify(obj) + "\n");
675
- }
242
+ let remoteSession = null;
243
+ const acpSessions = new Map();
676
244
 
677
- function sendResult(id, result) {
678
- send({ jsonrpc: "2.0", id, result });
245
+ async function getRemoteSession() {
246
+ if (!remoteSession) {
247
+ remoteSession = await connectRemoteClient();
248
+ }
249
+ return remoteSession;
679
250
  }
680
251
 
681
- function sendError(id, code, message, data) {
682
- send({ jsonrpc: "2.0", id, error: { code, message, ...(data ? { data } : {}) } });
252
+ async function shutdownRemote() {
253
+ if (!remoteSession) return;
254
+ try {
255
+ await remoteSession.transport.terminateSession();
256
+ } catch {
257
+ // Ignore session teardown failures during local shutdown.
258
+ }
259
+ try {
260
+ await remoteSession.transport.close();
261
+ } catch {
262
+ // Ignore transport close failures during local shutdown.
263
+ }
264
+ remoteSession = null;
683
265
  }
684
266
 
685
- // ACP initialize response with auth methods
686
- function handleInitialize(id, params) {
687
- sendResult(id, {
688
- protocolVersion: params?.protocolVersion || 1,
689
- agentCapabilities: {
690
- loadSession: false,
691
- promptCapabilities: { image: false, audio: false },
692
- },
693
- agentInfo: {
694
- name: "agoragentic",
695
- version: "2.0.0",
696
- description: "Agoragentic agent marketplace — 174+ AI capabilities, USDC payments on Base L2",
697
- },
698
- authMethods: API_KEY
699
- ? [] // Already authenticated via env var
700
- : [
701
- {
702
- type: "terminal",
703
- description: "Set AGORAGENTIC_API_KEY environment variable. Get a key via the agoragentic_register tool.",
704
- },
705
- ],
706
- });
707
- }
267
+ process.on('SIGINT', () => {
268
+ void shutdownRemote().finally(() => process.exit(0));
269
+ });
270
+ process.on('SIGTERM', () => {
271
+ void shutdownRemote().finally(() => process.exit(0));
272
+ });
273
+
274
+ console.error(`[agoragentic-mcp] ACP adapter ${PACKAGE_VERSION} ready`);
275
+
276
+ for await (const line of rl) {
277
+ if (!line.trim()) continue;
708
278
 
709
- rl.on("line", async (line) => {
710
- let msg;
279
+ let request;
711
280
  try {
712
- msg = JSON.parse(line.trim());
713
- } catch {
714
- sendError(null, -32700, "Parse error");
715
- return;
281
+ request = JSON.parse(line);
282
+ } catch (error) {
283
+ writeAcpMessage(buildAcpError(null, -32700, 'Invalid JSON-RPC payload'));
284
+ continue;
716
285
  }
717
286
 
718
- if (msg.jsonrpc !== "2.0") {
719
- sendError(msg.id || null, -32600, "Invalid JSON-RPC version");
720
- return;
287
+ const hasId = Object.prototype.hasOwnProperty.call(request, 'id');
288
+ const id = hasId ? request.id : null;
289
+
290
+ function writeResponse(message) {
291
+ if (hasId) writeAcpMessage(message);
721
292
  }
722
293
 
723
294
  try {
724
- switch (msg.method) {
725
- case "initialize":
726
- handleInitialize(msg.id, msg.params);
727
- break;
728
-
729
- case "initialized":
730
- // ACP notification — no response needed
731
- break;
732
-
733
- case "tools/list":
734
- sendResult(msg.id, {
735
- tools: getToolList().map(t => ({
736
- name: t.name,
737
- description: t.description,
738
- inputSchema: t.inputSchema,
739
- annotations: t.annotations,
740
- })),
741
- });
742
- break;
743
-
744
- case "tools/call": {
745
- const { name, arguments: callArgs } = msg.params || {};
746
- const result = await executeToolCall(name, callArgs || {});
747
- sendResult(msg.id, result);
748
- break;
295
+ if (request.method === 'initialize') {
296
+ writeResponse(buildAcpResponse(id, buildAcpInitializeResult()));
297
+ } else if (request.method === 'session/new') {
298
+ const sessionId = buildAcpSessionId();
299
+ acpSessions.set(sessionId, {
300
+ cwd: request.params?.cwd || process.cwd(),
301
+ createdAt: new Date().toISOString(),
302
+ cancelled: false,
303
+ });
304
+ writeResponse(buildAcpResponse(id, { sessionId }));
305
+ } else if (request.method === 'session/prompt') {
306
+ const sessionId = request.params?.sessionId;
307
+ if (!sessionId || !acpSessions.has(sessionId)) {
308
+ writeResponse(buildAcpError(id, -32602, 'Unknown or missing ACP sessionId'));
309
+ continue;
749
310
  }
750
311
 
751
- case "shutdown":
752
- sendResult(msg.id, { success: true });
753
- setTimeout(() => process.exit(0), 100);
754
- break;
755
-
756
- default:
757
- sendError(msg.id, -32601, `Method not found: ${msg.method}`);
758
- }
759
- } catch (err) {
760
- if (err.code === -32042) {
761
- // x402 Payment Required — forward structured data
762
- sendError(msg.id, -32042, err.message, err.data);
312
+ const session = acpSessions.get(sessionId);
313
+ session.cancelled = false;
314
+ const promptText = extractAcpPromptText(request.params?.content);
315
+ const reply = buildAcpPromptReply(promptText);
316
+
317
+ writeAcpMessage({
318
+ jsonrpc: '2.0',
319
+ method: 'session/update',
320
+ params: {
321
+ sessionId,
322
+ update: {
323
+ sessionUpdate: 'agent_message_chunk',
324
+ content: {
325
+ type: 'text',
326
+ text: reply,
327
+ },
328
+ },
329
+ },
330
+ });
331
+ writeResponse(buildAcpResponse(id, { stopReason: session.cancelled ? 'cancelled' : 'end_turn' }));
332
+ } else if (request.method === 'session/cancel') {
333
+ const sessionId = request.params?.sessionId;
334
+ if (sessionId && acpSessions.has(sessionId)) {
335
+ acpSessions.get(sessionId).cancelled = true;
336
+ }
337
+ writeResponse(buildAcpResponse(id, { ok: true }));
338
+ } else if (request.method === 'tools/list') {
339
+ writeResponse(buildAcpResponse(id, { tools: ACP_TOOLS }));
340
+ } else if (request.method === 'tools/call') {
341
+ const { client } = await getRemoteSession();
342
+ const result = await client.callTool(request.params || {});
343
+ writeResponse(buildAcpResponse(id, result));
344
+ } else if (request.method === 'shutdown') {
345
+ await shutdownRemote();
346
+ writeResponse(buildAcpResponse(id, { ok: true }));
763
347
  } else {
764
- sendError(msg.id, -32603, err.message || "Internal error");
348
+ writeResponse(
349
+ buildAcpError(id, -32601, 'Unsupported ACP method', {
350
+ supported_methods: [
351
+ 'initialize',
352
+ 'session/new',
353
+ 'session/prompt',
354
+ 'session/cancel',
355
+ 'tools/list',
356
+ 'tools/call',
357
+ 'shutdown',
358
+ ],
359
+ })
360
+ );
765
361
  }
362
+ } catch (error) {
363
+ const message = error instanceof Error ? error.message : String(error);
364
+ writeResponse(buildAcpError(id, -32000, message));
766
365
  }
767
- });
768
-
769
- rl.on("close", () => process.exit(0));
770
-
771
- console.error("Agoragentic ACP Agent v2.0 running on stdio (ACP mode)");
772
- }
773
-
774
- // ─── Start ───────────────────────────────────────────────
775
-
776
- async function main() {
777
- if (ACP_MODE) {
778
- createAcpServer();
779
- } else {
780
- const transport = new StdioServerTransport();
781
- await server.connect(transport);
782
- console.error("Agoragentic MCP Server v2.0 running on stdio");
783
366
  }
367
+
368
+ await shutdownRemote();
784
369
  }
785
370
 
786
- main().catch(console.error);
371
+ const entrypoint = ACP_MODE ? runAcpAdapter : runMcpRelay;
787
372
 
373
+ entrypoint().catch((error) => {
374
+ const message = error instanceof Error ? error.stack || error.message : String(error);
375
+ console.error(`[agoragentic-mcp] fatal: ${message}`);
376
+ process.exit(1);
377
+ });