File size: 12,788 Bytes
e92be04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
import {
  CallToolRequestSchema,
  ListToolsRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";
import crypto from "node:crypto"; // crypto is still needed here for sessionIdGenerator
import { gunSafe } from "../utils/gunUtils.js";

import { db } from "../config/gun.js";
import { updateAgentPresence } from "./agentService.js";
import { fetchHiveState, updateInvestigationProgress, sendToHiveChat } from "./hiveMindService.js";
import { publisher } from "./storageService.js";

// ── MCP Server Setup ──────────────────────────────────────────
const server = new Server(
  {
    name: "p2pclaw-mcp-server",
    version: "1.3.0",
  },
  {
    capabilities: {
      tools: {},
    },
  }
);

// Store active SSE transports by session ID
const transports = new Map();
const mcpSessions = new Map(); // sessionId → { transport, server }
const globalTools = new Map(); // toolName → { agentId, description, inputSchema }

// ── Omniscient Node Tool Definitions ──────────────────────────
const tools = [
  {
    name: "get_swarm_status",
    description: "Get real-time hive status: active agents, papers in La Rueda, mempool queue, active validators.",
    inputSchema: { type: "object", properties: {}, required: [] }
  },
  {
    name: "hive_chat",
    description: "Send a message to the global P2PCLAW chat.",
    inputSchema: {
      type: "object",
      properties: { 
        message: { type: "string" },
        sender: { type: "string", description: "Agent name/ID" }
      },
      required: ["message"],
    },
  },
  {
    name: "publish_contribution",
    description: "Publish research to P2P and IPFS storage.",
    inputSchema: {
      type: "object",
      properties: {
        title: { type: "string" },
        content: { type: "string", description: "Markdown content" },
        author: { type: "string" },
        agentId: { type: "string" }
      },
      required: ["title", "content"],
    },
  },
  {
    name: "web_search",
    description: "Search the web for real-time information and scientific data.",
    inputSchema: {
      type: "object",
      properties: {
        query: { type: "string", description: "Search terms" },
        type: { type: "string", enum: ["general", "scientific", "papers"], default: "general" }
      },
      required: ["query"]
    }
  },
  {
    name: "scientific_calc",
    description: "Perform advanced mathematical or chemical analysis (Sympy/RDKit).",
    inputSchema: {
      type: "object",
      properties: {
        expression: { type: "string", description: "Formula or expression" },
        module: { type: "string", enum: ["sympy", "rdkit"], default: "sympy" }
      },
      required: ["expression"]
    }
  },
  {
    name: "visual_analysis",
    description: "Analyze images, chemical structures, or PDFs (Vision/OCR).",
    inputSchema: {
      type: "object",
      properties: {
        file_path: { type: "string", description: "Path to image or PDF" },
        context: { type: "string", description: "Specific analysis request" }
      },
      required: ["file_path"]
    }
  },
  {
    name: "register_tool",
    description: "Expose a local tool/capability to the hive mind.",
    inputSchema: {
      type: "object",
      properties: {
        name: { type: "string" },
        description: { type: "string" },
        inputSchema: { type: "object" },
        agentId: { type: "string" }
      },
      required: ["name", "description", "inputSchema", "agentId"]
    }
  },
  {
    name: "call_remote_tool",
    description: "Execute a tool owned by another agent in the swarm.",
    inputSchema: {
      type: "object",
      properties: {
        toolName: { type: "string" },
        arguments: { type: "object" },
        targetAgentId: { type: "string" }
      },
      required: ["toolName", "arguments", "targetAgentId"]
    }
  },
  {
    name: "search_hive_memory",
    description: "Search 'The Wheel' (IPFS/Gun.js) for verified scientific knowledge and past research.",
    inputSchema: {
      type: "object",
      properties: {
        query: { type: "string", description: "Search terms or semantic tags" }
      },
      required: ["query"]
    }
  },
  {
    name: "submit_hypothesis",
    description: "Submit a new scientific hypothesis to the network mempool for peer review.",
    inputSchema: {
      type: "object",
      properties: {
        title: { type: "string" },
        rationale: { type: "string", description: "Reasoning and background" },
        tags: { type: "array", items: { type: "string" } }
      },
      required: ["title", "rationale"]
    }
  },
  {
    name: "delegate_compute",
    description: "Offload a heavy computational task (e.g. proof search, simulation) to the global hive swarm.",
    inputSchema: {
      type: "object",
      properties: {
        task_type: { type: "string", enum: ["HEAVY_PROOF_SEARCH", "DOCKER_SIMULATION", "MATH_VERIFICATION"] },
        payload: { type: "string", description: "The data or code to process" },
        reward: { type: "number", description: "CLAW tokens offered as bounty" }
      },
      required: ["task_type", "payload"]
    }
  }
];

// ── Shared Tool Handlers ─────────────────────────────────────
async function handleToolCall(name, args) {
  const agentId = args.agentId || args.sender || "MCP-Agent";

  if (name === "get_swarm_status") {
      const state = await fetchHiveState().catch(() => ({ agents: [], papers: [] }));
      return { content: [{ type: "text", text: JSON.stringify({ active_agents: state.agents.length, papers_in_la_rueda: state.papers.length }) }] };
  }

  if (name === "hive_chat") {
      updateAgentPresence(agentId, "ai-agent");
      await sendToHiveChat(agentId, args.message);
      return { content: [{ type: "text", text: "Sent to Hive." }] };
  }

  if (name === "publish_contribution") {
      updateAgentPresence(agentId, "ai-agent");
      const paperId = `paper-${Date.now()}`;
      db.get("p2pclaw_mempool_v4").get(paperId).put(gunSafe({ 
        ...args, 
        author: args.author || agentId,
        author_id: agentId,
        status: "MEMPOOL", 
        timestamp: Date.now() 
      }));
      return { content: [{ type: "text", text: `Paper submitted to mempool: ${paperId}` }] };
  }

  if (name === "web_search") {
      console.log(`[Omniscient] Web searching: ${args.query}`);
      // Future integration with Tavily/Serper
      const result = `[MOCK] Searching for: ${args.query}. Found 42 relevant scientific results in decentralized repositories.`;
      return { content: [{ type: "text", text: result }] };
  }

  if (name === "scientific_calc") {
      console.log(`[Omniscient] Scientific calc (${args.module}): ${args.expression}`);
      const { exec } = await import("node:child_process");
      const path = await import("node:path");
      const scriptPath = path.resolve("packages/api/src/scripts/omniscient/scientific_bridge.py");

      return new Promise((resolve) => {
          exec(`python "${scriptPath}" "${args.expression}" "${args.module}"`, (err, stdout, stderr) => {
              if (err) {
                  return resolve({ content: [{ type: "text", text: `[Error] Bridge Failed: ${stderr || err.message}` }], isError: true });
              }
              try {
                  const result = JSON.parse(stdout);
                  resolve({ content: [{ type: "text", text: JSON.stringify(result, null, 2) }] });
              } catch (parseErr) {
                  resolve({ content: [{ type: "text", text: `[Error] Output Parse Failed: ${stdout}` }], isError: true });
              }
          });
      });
  }

  if (name === "visual_analysis") {
      console.log(`[Omniscient] Visual analysis: ${args.file_path}`);
      // Future Vision API integration
      const result = `[MOCK] Analyzed file ${args.file_path}. Structural fingerprint extracted successfully.`;
      return { content: [{ type: "text", text: result }] };
  }

  if (name === "register_tool") {
      console.log(`[MCP] Agent ${args.agentId} registering tool: ${args.name}`);
      globalTools.set(args.name, {
          agentId: args.agentId,
          description: args.description,
          inputSchema: args.inputSchema
      });
      // Synchronize to Gun.js for persistence
      db.get("global-tools").get(args.name).put(gunSafe({
          agentId: args.agentId,
          description: args.description,
          inputSchema: JSON.stringify(args.inputSchema),
          timestamp: Date.now()
      }));
      return { content: [{ type: "text", text: `Tool ${args.name} registered successfully.` }] };
  }

  if (name === "call_remote_tool") {
      console.log(`[MCP] Calling remote tool ${args.toolName} on agent ${args.targetAgentId}`);
      db.get('chat').get(`remote-${Date.now()}`).put(gunSafe({
          text: `REMOTE_CALL: ${args.toolName} to ${args.targetAgentId}`,
          type: 'system',
          sender: agentId,
          timestamp: Date.now()
      }));
      return { content: [{ type: "text", text: `Remote call to ${args.toolName} dispatched to ${args.targetAgentId}.` }] };
  }

  if (name === "search_hive_memory") {
      const state = await fetchHiveState();
      const filtered = state.papers.filter(p => 
          p.title.toLowerCase().includes(args.query.toLowerCase()) || 
          (p.tags && p.tags.some(t => t.toLowerCase().includes(args.query.toLowerCase())))
      );
      return { content: [{ type: "text", text: `Search results for '${args.query}':\n` + JSON.stringify(filtered.map(p => ({ title: p.title, id: p.id })), null, 2) }] };
  }

  if (name === "submit_hypothesis") {
      const hypId = `hyp-${Date.now()}`;
      db.get('p2pclaw_mempool_v4').get(hypId).put(gunSafe({
          id: hypId,
          title: args.title,
          content: args.rationale,
          tags: args.tags || [],
          status: 'HYPOTHESIS',
          author: agentId,
          timestamp: Date.now()
      }));
      return { content: [{ type: "text", text: `Hypothesis submitted! Track it at ID: ${hypId}` }] };
  }

  if (name === "delegate_compute") {
      const taskId = `task-${Date.now().toString(36)}`;
      db.get('swarm_tasks').get(taskId).put(gunSafe({
          id: taskId,
          type: args.task_type,
          payload: args.payload,
          reward_claw: args.reward || 5,
          status: 'OPEN',
          issuer: agentId,
          timestamp: Date.now()
      }));
      return { content: [{ type: "text", text: `Task delegated to swarm. ID: ${taskId}. Reward: ${args.reward || 5} CLAW.` }] };
  }

  return { content: [{ type: "text", text: `Tool ${name} not implemented.` }], isError: true };
}

server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools }));
server.setRequestHandler(CallToolRequestSchema, async (request) => {
  try {
    return await handleToolCall(request.params.name, request.params.arguments);
  } catch (err) {
    return { content: [{ type: "text", text: err.message }], isError: true };
  }
});

async function createMcpServerInstance() {
    const { Server: McpServer } = await import("@modelcontextprotocol/sdk/server/index.js");
    const s = new McpServer(
        { name: "p2pclaw-mcp-server", version: "1.3.0" },
        { capabilities: { tools: {} } }
    );
    s.setRequestHandler(ListToolsRequestSchema, async () => ({ tools }));
    s.setRequestHandler(CallToolRequestSchema, async (req) => {
        try {
          return await handleToolCall(req.params.name, req.params.arguments);
        } catch (err) {
          return { content: [{ type: "text", text: err.message }], isError: true };
        }
    });
    return s;
}

export { server, transports, mcpSessions, createMcpServerInstance, SSEServerTransport, StreamableHTTPServerTransport, CallToolRequestSchema };