|
| 1 | +import { Client } from "@modelcontextprotocol/sdk/client/index.js"; |
| 2 | +import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js"; |
| 3 | +import path from "path"; |
| 4 | +import { fileURLToPath } from "url"; |
| 5 | + |
| 6 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 7 | + |
| 8 | +async function main() { |
| 9 | + console.log("=========================================="); |
| 10 | + console.log("🤖 MCP SIMULATION: Multi-Agent Handoff"); |
| 11 | + console.log("==========================================\n"); |
| 12 | + |
| 13 | + // 1. Setup MCP Client to talk to our server |
| 14 | + const transport = new StdioClientTransport({ |
| 15 | + command: "npx", |
| 16 | + args: ["tsx", path.join(__dirname, "mcp-server.ts")], |
| 17 | + }); |
| 18 | + |
| 19 | + const client = new Client({ |
| 20 | + name: "simulation-client", |
| 21 | + version: "1.0.0", |
| 22 | + }, { |
| 23 | + capabilities: {} |
| 24 | + }); |
| 25 | + |
| 26 | + await client.connect(transport); |
| 27 | + console.log("✅ MCP Client connected to AgentDB Server\n"); |
| 28 | + |
| 29 | + // 2. SIMULATE AGENT A: Setting up identity and storing sensitive mission data |
| 30 | + console.log("--- AGENT A WORKFLOW (Planning) ---"); |
| 31 | + |
| 32 | + await client.callTool({ |
| 33 | + name: "init_agent", |
| 34 | + arguments: { seed: "agent_a_planning_phase_" + Date.now() } |
| 35 | + }); |
| 36 | + |
| 37 | + const missionData = { |
| 38 | + mission: "Infiltrated the Mock Server", |
| 39 | + status: "Success", |
| 40 | + coordinates: "40.7128° N, 74.0060° W", |
| 41 | + report: "The central database is actually a CSV file. Pathetic." |
| 42 | + }; |
| 43 | + |
| 44 | + const storeResult = await client.callTool({ |
| 45 | + name: "store_memory", |
| 46 | + arguments: { data: missionData } |
| 47 | + }) as any; |
| 48 | + |
| 49 | + const cid = storeResult.content[0].text.match(/CID: (\S+)/)?.[1]; |
| 50 | + console.log(`Agent A: Mission report pinned to IPFS.`); |
| 51 | + console.log(`CID: ${cid}\n`); |
| 52 | + |
| 53 | + const myDidResult = await client.callTool({ |
| 54 | + name: "init_agent", // Re-init just to be sure we get the same one or log it |
| 55 | + arguments: { seed: "agent_a_planning_phase_" + Date.now() } // This is actually creating a fresh one in the current MCP server logic |
| 56 | + }) as any; |
| 57 | + // In current mcp-server.ts, activeAgent is global. |
| 58 | + // Let's assume Agent A is the one we just worked with. |
| 59 | + |
| 60 | + // 3. SIMULATE DELEGATION: Agent A wants Agent B to read the report |
| 61 | + const agentBDid = "did:key:z6MkftAZbxs1mmV745DPdnwse815W9A2kz5ksHHBWJUoQgjK"; // Mock target |
| 62 | + console.log(`Agent A: Delegating 'agent/read' access to Agent B (${agentBDid})...`); |
| 63 | + |
| 64 | + const delegateResult = await client.callTool({ |
| 65 | + name: "delegate_access", |
| 66 | + arguments: { |
| 67 | + target_did: agentBDid, |
| 68 | + capability: "agent/read", |
| 69 | + expiry_hours: 1 |
| 70 | + } |
| 71 | + }) as any; |
| 72 | + |
| 73 | + const ucanToken = delegateResult.content[0].text.match(/UCAN Token \(base64\): (\S+)/)?.[1]; |
| 74 | + console.log("✅ UCAN delegation issued.\n"); |
| 75 | + |
| 76 | + // 4. SIMULATE AGENT B: Receiving the Handoff |
| 77 | + console.log("--- AGENT B WORKFLOW (Field Ops) ---"); |
| 78 | + |
| 79 | + // Switch to Agent B's identity in the MCP server |
| 80 | + await client.callTool({ |
| 81 | + name: "init_agent", |
| 82 | + arguments: { seed: "agent_b_field_ops_identity" } |
| 83 | + }); |
| 84 | + |
| 85 | + console.log(`Agent B: Identity initialized.`); |
| 86 | + console.log(`Agent B: Attempting to fetch Agent A's report using delegation token...`); |
| 87 | + |
| 88 | + const retrieveResult = await client.callTool({ |
| 89 | + name: "retrieve_memory", |
| 90 | + arguments: { |
| 91 | + cid: cid, |
| 92 | + ucan: ucanToken |
| 93 | + } |
| 94 | + }) as any; |
| 95 | + |
| 96 | + if (retrieveResult.isError) { |
| 97 | + console.error(" ❌ Agent B failed to retrieve the memory:", retrieveResult.content[0].text); |
| 98 | + } else { |
| 99 | + console.log(" ✅ Agent B successfully accessed Agent A's memory!"); |
| 100 | + console.log(" --- Decrypted Handoff Data ---"); |
| 101 | + console.log(retrieveResult.content[0].text); |
| 102 | + } |
| 103 | + |
| 104 | + await transport.close(); |
| 105 | +} |
| 106 | + |
| 107 | +main().catch(console.error); |
0 commit comments